Mirror of Quill
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

565 lines
20 KiB

  1. <div class="narrow">
  2. <?= partial('partials/header') ?>
  3. <form role="form" style="margin-top: 20px;" id="note_form">
  4. <div id="reply">
  5. <div class="reply-section hidden">
  6. <div class="form-group has-feedback">
  7. <label for="note_in_reply_to">Reply To (a URL you are replying to)</label>
  8. <input type="text" id="note_in_reply_to" value="<?= $this->in_reply_to ?>" class="form-control">
  9. <span class="loading hidden glyphicon glyphicon-refresh glyphicon-spin form-control-feedback"></span>
  10. </div>
  11. <div class="reply-context hidden">
  12. <div>
  13. <img src="" width="48" class="author-img">
  14. </div>
  15. <div>
  16. <img src="" class="post-img hidden">
  17. <div class="author"><span class="name"></span> <span class="url"></span></div>
  18. <span class="content"></span>
  19. </div>
  20. </div>
  21. </div>
  22. <a href="" id="expand-reply" class="btn btn-xs btn-info">Reply</a>
  23. </div>
  24. <div class="form-group">
  25. <div id="note_content_remaining" class="pcheck206"><img src="/images/twitter.ico"> <span>140</span></div>
  26. <label for="note_content">Content</label>
  27. <textarea id="note_content" value="" class="form-control" style="height: 4em;"></textarea>
  28. </div>
  29. <div class="form-group" id="form_tags">
  30. <label for="note_category">Tags (comma-separated list)</label>
  31. <input type="text" id="note_category" value="" class="form-control" placeholder="e.g. web, personal">
  32. </div>
  33. <div class="form-group" id="form_slug">
  34. <label for="note_slug">Slug</label>
  35. <input type="text" id="note_slug" value="" class="form-control">
  36. </div>
  37. <div class="form-group">
  38. <label for="note_photo">Photo</label>
  39. <input type="file" name="note_photo" id="note_photo" accept="image/*">
  40. <a href="javascript:switchToManualPhotoURL();" id="note_manual_photo">enter photo url</a>
  41. <a href="javascript:addPhotoURL();" class="hidden" id="add_photo">add photo</a>
  42. <br>
  43. <div id="photo_preview_container" class="hidden">
  44. <img src="" id="photo_preview" style="max-width: 300px; max-height: 300px;">
  45. <div>
  46. <button type="button" class="btn btn-danger btn-sm" id="remove_photo"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Remove image</button>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="form-group">
  51. <label for="note_syndicate-to">Syndicate <a href="javascript:reload_syndications()">(refresh list)</a></label>
  52. <div id="syndication-container">
  53. <?php
  54. if($this->syndication_targets) {
  55. echo '<ul>';
  56. foreach($this->syndication_targets as $syn) {
  57. echo '<li>'
  58. . '<button data-syndicate-to="'.(isset($syn['uid']) ? htmlspecialchars($syn['uid']) : htmlspecialchars($syn['target'])).'" class="btn btn-default btn-block">'
  59. . ($syn['favicon'] ? '<img src="'.htmlspecialchars($syn['favicon']).'" width="16" height="16"> ' : '')
  60. . htmlspecialchars($syn['target'])
  61. . '</button>'
  62. . '</li>';
  63. }
  64. echo '</ul>';
  65. } else {
  66. ?><div class="bs-callout bs-callout-warning">No syndication targets were found on your site.
  67. You can provide a <a href="/docs#syndication">list of supported syndication targets</a> that will appear as checkboxes here.</div><?php
  68. }
  69. ?>
  70. </div>
  71. </div>
  72. <div class="form-group">
  73. <label for="note_location">Location</label>
  74. <input type="checkbox" id="note_location_chk" value="">
  75. <img src="/images/spinner.gif" id="note_location_loading" style="display: none;">
  76. <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  77. <input type="hidden" id="note_location">
  78. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  79. <div id="note_location_img" style="display: none;">
  80. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  81. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  82. </div>
  83. </div>
  84. <button class="btn btn-success" id="btn_post">Post</button>
  85. </form>
  86. <div class="alert alert-success hidden" id="test_success"><strong>Success! We found a Location header in the response!</strong><br>Your post should be on your website now!<br><a href="" id="post_href">View your post</a></div>
  87. <div class="alert alert-danger hidden" id="test_error"><strong>Your endpoint did not return a Location header.</strong><br>See <a href="/creating-a-micropub-endpoint">Creating a Micropub Endpoint</a> for more information.</div>
  88. <div id="last_request_container" style="display: none;">
  89. <h4>Request made to your Micropub endpoint</h4>
  90. <pre id="test_request" style="width: 100%; min-height: 140px;"></pre>
  91. </div>
  92. <?php if($this->test_response): ?>
  93. <h4>Last response from your Micropub endpoint <span id="last_response_date">(<?= relative_time($this->response_date) ?>)</span></h4>
  94. <?php endif; ?>
  95. <pre id="test_response" class="<?= $this->test_response ? '' : 'hidden' ?>" style="width: 100%; min-height: 240px;"><?= htmlspecialchars($this->test_response) ?></pre>
  96. <hr>
  97. <div style="text-align: right;">
  98. <a href="/add-to-home?start">Add to Home Screen</a>
  99. </div>
  100. </div>
  101. <style type="text/css">
  102. #reply {
  103. margin-bottom: 1em;
  104. }
  105. #note_content_remaining {
  106. float: right;
  107. font-size: 0.8em;
  108. font-weight: bold;
  109. }
  110. .pcheck206 { color: #6ba15c; } /* tweet fits within the limit even after adding RT @username */
  111. .pcheck207 { color: #c4b404; } /* danger zone, tweet will overflow when RT @username is added */
  112. .pcheck200,.pcheck208 { color: #59cb3a; } /* exactly fits 140 chars, both with or without RT */
  113. .pcheck413 { color: #a73b3b; } /* over max tweet length */
  114. .reply-context {
  115. display: flex;
  116. flex-direction: row;
  117. padding: 4px;
  118. margin: 0 3em;
  119. border: 1px #ccc solid;
  120. border-radius: 4px;
  121. background: #f4f4f4;
  122. }
  123. .reply-context img.author-img {
  124. border-radius: 4px;
  125. width: 48px;
  126. margin-right: 4px;
  127. }
  128. .reply-context .author {
  129. color: #777;
  130. font-weight: bold;
  131. font-size: 0.9em;
  132. }
  133. .reply-context .author .url {
  134. color: #aaa;
  135. }
  136. .reply-context img.post-img {
  137. float: right;
  138. width: 200px;
  139. }
  140. </style>
  141. <script>
  142. function saveNoteState() {
  143. var state = {
  144. content: $("#note_content").val(),
  145. inReplyTo: $("#note_in_reply_to").val(),
  146. category: $("#note_category").val(),
  147. slug: $("#note_slug").val(),
  148. photo: $("#note_photo_url").val()
  149. };
  150. state.syndications = [];
  151. $("#syndication-container button.btn-info").each(function(i,btn){
  152. state.syndications[$(btn).data('syndicate-to')] = 'selected';
  153. });
  154. localforage.setItem('current-note', state);
  155. }
  156. function restoreNoteState() {
  157. localforage.getItem('current-note', function(err,note){
  158. if(note) {
  159. $("#note_content").val(note.content);
  160. $("#note_in_reply_to").val(note.inReplyTo);
  161. $("#note_category").val(note.category);
  162. $("#note_slug").val(note.slug);
  163. if(note.photo) {
  164. replacePhotoWithPhotoURL(note.photo);
  165. }
  166. if(note.inReplyTo) {
  167. expandReplySection();
  168. }
  169. $("#syndication-container button").each(function(i,btn){
  170. if($(btn).data('syndicate-to') in note.syndications) {
  171. $(btn).addClass('btn-info');
  172. }
  173. });
  174. $("#note_content").change();
  175. }
  176. });
  177. }
  178. function replacePhotoWithPhotoURL(url) {
  179. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  180. $(".note_photo_url").val(url);
  181. $("#note_photo").remove();
  182. $("#photo_preview").attr("src", url);
  183. $("#photo_preview_container").removeClass("hidden");
  184. $("#note_manual_photo").addClass("hidden");
  185. }
  186. function switchToManualPhotoURL() {
  187. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  188. $("#note_photo").remove();
  189. $("#note_photo_url").change(function(){
  190. $("#photo_preview").attr("src", $(this).val());
  191. $("#photo_preview_container").removeClass("hidden");
  192. });
  193. $("#note_manual_photo").addClass("hidden");
  194. $("#add_photo").removeClass("hidden");
  195. }
  196. function addPhotoURL() {
  197. $(".note_photo_url:last").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control" style="margin-top:2px;">');
  198. if($(".note_photo_url").length == 4) {
  199. $("#add_photo").remove();
  200. }
  201. }
  202. function expandReplySection() {
  203. $("#expand-reply").click();
  204. $("#note_in_reply_to").change();
  205. }
  206. $(function(){
  207. var userHasSetCategory = false;
  208. var hasMediaEndpoint = <?= $this->media_endpoint ? 'true' : 'false' ?>;
  209. $("#note_content, #note_category, #note_in_reply_to, #note_slug, #note_photo_url").on('keyup change', function(e){
  210. saveNoteState();
  211. })
  212. $("#expand-reply").click(function(){
  213. $('.reply-section').removeClass('hidden');
  214. $(this).addClass('hidden');
  215. return false;
  216. });
  217. // Preview the photo when one is chosen
  218. $("#photo_preview_container").addClass("hidden");
  219. $("#note_photo").on("change", function(e){
  220. // If the user has a media endpoint, upload the photo to it right now
  221. if(hasMediaEndpoint) {
  222. // TODO: add loading state indicator here
  223. console.log("Uploading file to media endpoint...");
  224. var formData = new FormData();
  225. formData.append("null","null");
  226. formData.append("photo", e.target.files[0]);
  227. var request = new XMLHttpRequest();
  228. request.open("POST", "/micropub/media");
  229. request.onreadystatechange = function() {
  230. if(request.readyState == XMLHttpRequest.DONE) {
  231. try {
  232. var response = JSON.parse(request.responseText);
  233. if(response.location) {
  234. // Replace the file upload form with the URL
  235. replacePhotoWithPhotoURL(response.location);
  236. saveNoteState();
  237. } else {
  238. console.log("Endpoint did not return a location header", response);
  239. }
  240. } catch(e) {
  241. console.log(e);
  242. }
  243. }
  244. }
  245. request.send(formData);
  246. } else {
  247. $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
  248. $("#photo_preview_container").removeClass("hidden");
  249. }
  250. });
  251. $("#remove_photo").on("click", function(){
  252. $("#note_photo").val("");
  253. $(".note_photo_url").val("");
  254. $("#photo_preview").attr("src", "" );
  255. $("#photo_preview_container").addClass("hidden");
  256. saveNoteState();
  257. });
  258. $("#note_content").on('change keyup', function(e){
  259. var text = $("#note_content").val();
  260. var tweet_length = tw_text_proxy(text).length;
  261. var tweet_check = tw_length_check(text, 140, "<?= $this->user->twitter_username ?>");
  262. var remaining = 140 - tweet_length;
  263. $("#note_content_remaining span").html(remaining);
  264. $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
  265. $("#note_content_remaining").addClass("pcheck"+tweet_check);
  266. // If the user didn't enter any categories, add them from the post
  267. if(!userHasSetCategory) {
  268. var tags = $("#note_content").val().match(/#[a-z][a-z0-9]+/ig);
  269. if(tags) {
  270. $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
  271. }
  272. }
  273. });
  274. $("#note_in_reply_to").on('change', function(){
  275. var reply_to = $("#note_in_reply_to").val();
  276. if(reply_to == "") {
  277. $(".reply-section").addClass("hidden");
  278. $(".reply-context").addClass("hidden");
  279. $("#expand-reply").removeClass("hidden");
  280. return;
  281. }
  282. $(".reply-section .loading").removeClass("hidden");
  283. $.get("/reply/preview", {url:reply_to}, function(data){
  284. if(data.canonical_reply_url != reply_to) {
  285. $("#note_in_reply_to").val(data.canonical_reply_url);
  286. }
  287. var category = csv_to_array($("#note_category").val());
  288. for(var i in data.entry.category) {
  289. if($.inArray(data.entry.category[i], category) == -1) {
  290. category.push(data.entry.category[i]);
  291. }
  292. }
  293. $("#note_category").val(category.join(", "));
  294. if($("#note_content").val() == "" && data.mentions) {
  295. var mentions = '';
  296. for(var i in data.mentions) {
  297. mentions += '@'+data.mentions[i]+' ';
  298. }
  299. console.log(mentions);
  300. $("#note_content").val(mentions);
  301. }
  302. $(".reply-context .content").text(data.entry.content.text);
  303. $(".reply-context .name").text(data.entry.author.name);
  304. $(".reply-context .url").text(data.entry.author.url);
  305. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  306. if(data.entry.photo) {
  307. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  308. } else {
  309. $(".reply-context img.post-img").addClass('hidden');
  310. }
  311. $(".reply-section .loading").addClass("hidden");
  312. $(".reply-context").removeClass("hidden");
  313. });
  314. });
  315. $("#note_category").on('keydown keyup', function(){
  316. userHasSetCategory = true;
  317. });
  318. $("#note_category").on('change', function(){
  319. if($("#note_category").val() == "") {
  320. userHasSetCategory = false;
  321. }
  322. });
  323. // When the reply URL is in the query string, or loads from localstorage, make sure
  324. // to run the event handlers to expand the reply section
  325. if($("#note_in_reply_to").val() != "") {
  326. expandReplySection();
  327. }
  328. $("#btn_post").click(function(){
  329. // Collect all the syndication buttons that are pressed
  330. var syndications = [];
  331. $("#syndication-container button.btn-info").each(function(i,btn){
  332. syndications.push($(btn).data('syndicate-to'));
  333. });
  334. var category = csv_to_array($("#note_category").val());
  335. var formData = new FormData();
  336. if(v=$("#note_content").val()) {
  337. formData.append("content", v);
  338. }
  339. if(v=$("#note_in_reply_to").val()) {
  340. formData.append("in-reply-to", v);
  341. }
  342. if(v=$("#note_location").val()) {
  343. formData.append("location", v);
  344. }
  345. if(category.length > 0) {
  346. for(var i in category) {
  347. formData.append("category[]", category[i]);
  348. }
  349. }
  350. if(syndications.length > 0) {
  351. for(var i in syndications) {
  352. formData.append("syndicate-to[]", syndications[i]);
  353. }
  354. }
  355. if(v=$("#note_slug").val()) {
  356. formData.append("slug", v);
  357. }
  358. // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
  359. if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
  360. formData.append("photo", document.getElementById("note_photo").files[0]);
  361. } else if($(".note_photo_url").val()) {
  362. $(".note_photo_url").each(function(){
  363. if($(this).val()) {
  364. formData.append("photo[]", $(this).val());
  365. }
  366. });
  367. }
  368. // Need to append a placeholder field because if the file size max is hit, $_POST will
  369. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  370. // This will be stripped by Quill before it's sent to the Micropub endpoint
  371. formData.append("null","null");
  372. var request = new XMLHttpRequest();
  373. request.open("POST", "/micropub/multipart");
  374. request.onreadystatechange = function() {
  375. if(request.readyState == XMLHttpRequest.DONE) {
  376. // console.log(request.responseText);
  377. try {
  378. var response = JSON.parse(request.responseText);
  379. localforage.removeItem('current-note');
  380. if(response.location) {
  381. window.location = response.location;
  382. // console.log(response.location);
  383. } else {
  384. $("#test_response").html(response.response).removeClass('hidden');
  385. $("#test_success").addClass('hidden');
  386. $("#test_error").removeClass('hidden');
  387. }
  388. } catch(e) {
  389. $("#test_success").addClass('hidden');
  390. $("#test_error").removeClass('hidden');
  391. }
  392. $("#btn_post").removeClass("loading disabled").text("Post");
  393. }
  394. }
  395. $("#btn_post").addClass("loading disabled").text("Working...");
  396. request.send(formData);
  397. /*
  398. $.post("/micropub/multipart", {
  399. content: $("#note_content").val(),
  400. 'in-reply-to': $("#note_in_reply_to").val(),
  401. location: $("#note_location").val(),
  402. category: category,
  403. slug: $("#note_slug").val(),
  404. 'syndicate-to': syndications
  405. }, function(data){
  406. var response = JSON.parse(data);
  407. if(response.location != false) {
  408. $("#note_form").slideUp(200, function(){
  409. $(window).scrollTop($("#test_success").position().top);
  410. });
  411. $("#test_success").removeClass('hidden');
  412. $("#test_error").addClass('hidden');
  413. $("#post_href").attr("href", response.location);
  414. $("#note_content").val("");
  415. $("#note_in_reply_to").val("");
  416. $("#note_category").val("");
  417. $("#note_slug").val("");
  418. } else {
  419. $("#test_success").addClass('hidden');
  420. $("#test_error").removeClass('hidden');
  421. }
  422. $("#last_response_date").html("(just now)");
  423. $("#test_request").html(response.request);
  424. $("#last_request_container").show();
  425. $("#test_response").html(response.response);
  426. });
  427. */
  428. return false;
  429. });
  430. function location_error(msg) {
  431. $("#note_location_msg").val(msg);
  432. $("#note_location_chk").removeAttr("checked");
  433. $("#note_location_loading").hide();
  434. $("#note_location_img").hide();
  435. $("#note_location_msg").removeClass("img-visible");
  436. }
  437. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  438. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  439. function fetch_location() {
  440. $("#note_location_loading").show();
  441. navigator.geolocation.getCurrentPosition(function(position){
  442. $("#note_location_loading").hide();
  443. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  444. $("#note_location_msg").val(geo);
  445. $("#note_location").val(geo);
  446. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  447. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  448. $("#note_location_img").show();
  449. $("#note_location_msg").addClass("img-visible");
  450. }, function(err){
  451. if(err.code == 1) {
  452. location_error("The website was not able to get permission");
  453. } else if(err.code == 2) {
  454. location_error("Location information was unavailable");
  455. } else if(err.code == 3) {
  456. location_error("Timed out getting location");
  457. }
  458. });
  459. }
  460. $("#note_location_chk").click(function(){
  461. if($(this).attr("checked") == "checked") {
  462. if(navigator.geolocation) {
  463. $.post("/prefs", {
  464. enabled: 1
  465. });
  466. fetch_location();
  467. } else {
  468. location_error("Browser location is not supported");
  469. }
  470. } else {
  471. $("#note_location_img").hide();
  472. $("#note_location_msg").removeClass("img-visible");
  473. $("#note_location_msg").val('');
  474. $("#note_location").val('');
  475. $.post("/prefs", {
  476. enabled: 0
  477. });
  478. }
  479. });
  480. if($("#location_enabled").val() == 1) {
  481. $("#note_location_chk").attr("checked","checked");
  482. fetch_location();
  483. }
  484. bind_syndication_buttons();
  485. if($("#note_in_reply_to").val() != "") {
  486. $("#note_in_reply_to").change();
  487. } else {
  488. restoreNoteState();
  489. }
  490. });
  491. <?= partial('partials/syndication-js') ?>
  492. </script>