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.

917 lines
30 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 class="reply-author">
  13. <img src="" width="48" class="author-img">
  14. </div>
  15. <div class="reply-content">
  16. <img src="" class="post-img hidden">
  17. <div class="author"><div class="syndications"></div><span class="name"></span> <span class="url"></span></div>
  18. <h4 class="post-name hidden"></h4>
  19. <span class="content"></span>
  20. </div>
  21. </div>
  22. </div>
  23. <a href="" id="expand-reply" class="btn btn-xs btn-info">Reply</a>
  24. </div>
  25. <div class="form-group hidden" id="form_rsvp">
  26. <label for="note_rsvp">RSVP</label>
  27. <select id="note_rsvp" class="form-control">
  28. <option value="yes">Yes</option>
  29. <option value="no">No</option>
  30. <option value="maybe">Maybe</option>
  31. <option value="interested">Interested</option>
  32. <option value=""></option>
  33. </select>
  34. </div>
  35. <div class="form-group hidden" id="note-name">
  36. <label for="note_name">Issue Title</label>
  37. <input type="text" id="note_name" value="" class="form-control" placeholder="">
  38. </div>
  39. <div class="form-group">
  40. <div id="note_content_remaining" class="pcheck206"><img src="/images/twitter.ico"> <span>280</span></div>
  41. <label for="note_content">Content</label>
  42. <textarea id="note_content" value="" class="form-control" style="height: 4em;"></textarea>
  43. </div>
  44. <div class="form-group hidden" id="content-type-selection">
  45. <label for="note_content_type">Content Type</label>
  46. <select class="form-control" id="note_content_type">
  47. <option value="text/plain">Text</option>
  48. <option value="text/markdown">Markdown</option>
  49. </select>
  50. </div>
  51. <div class="form-group" id="form_tags">
  52. <label for="note_category">Tags</label>
  53. <input type="text" id="note_category" value="" class="form-control" placeholder="e.g. web, personal">
  54. </div>
  55. <div class="form-group" id="form_slug">
  56. <label for="note_slug">Slug</label>
  57. <input type="text" id="note_slug" value="" class="form-control">
  58. </div>
  59. <?php if(supports_post_type($this->user, 'photo')): ?>
  60. <div class="form-group hidden" id="photo-previews">
  61. </div>
  62. <a href="javascript:addNewPhoto();" id="expand-photo-section"><i class="glyphicon glyphicon-camera" style="color: #aaa; font-size: 36px;"></i></a>
  63. <?php endif ?>
  64. <div class="form-group" style="margin-top: 1em;">
  65. <label for="note_syndicate-to">Syndicate <a href="javascript:reload_syndications()">(refresh list)</a></label>
  66. <div id="syndication-container">
  67. <?php
  68. if($this->syndication_targets) {
  69. echo '<ul>';
  70. foreach($this->syndication_targets as $syn) {
  71. echo '<li>'
  72. . '<button data-syndicate-to="'.(isset($syn['uid']) ? htmlspecialchars($syn['uid']) : htmlspecialchars($syn['target'])).'" class="btn btn-default btn-block">'
  73. . ($syn['favicon'] ? '<img src="'.htmlspecialchars($syn['favicon']).'" width="16" height="16"> ' : '')
  74. . htmlspecialchars($syn['target'])
  75. . '</button>'
  76. . '</li>';
  77. }
  78. echo '</ul>';
  79. } else {
  80. ?><div class="bs-callout bs-callout-warning">No syndication targets were found on your site.
  81. You can provide a <a href="/docs#syndication">list of supported syndication targets</a> that will appear as checkboxes here.</div><?php
  82. }
  83. ?>
  84. </div>
  85. </div>
  86. <div class="form-group">
  87. <label for="note_location">Location</label>
  88. <input type="checkbox" id="note_location_chk" value="">
  89. <img src="/images/spinner.gif" id="note_location_loading" style="display: none;">
  90. <input type="text" name="note_location_msg" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  91. <input type="hidden" id="note_location">
  92. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  93. <div id="note_location_img" style="display: none;">
  94. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  95. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  96. </div>
  97. </div>
  98. <button class="btn btn-success" id="btn_post">Post</button>
  99. </form>
  100. <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>
  101. <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>
  102. <div id="last_request_container" style="display: none;">
  103. <h4>Request made to your Micropub endpoint</h4>
  104. <pre id="test_request" style="width: 100%; min-height: 140px;"></pre>
  105. </div>
  106. <?php if($this->test_response): ?>
  107. <h4>Last response from your Micropub endpoint <span id="last_response_date">(<?= relative_time($this->response_date) ?>)</span></h4>
  108. <?php endif; ?>
  109. <pre id="test_response" class="<?= $this->test_response ? '' : 'hidden' ?>" style="width: 100%; min-height: 240px;"><?= htmlspecialchars($this->test_response) ?></pre>
  110. <hr>
  111. <div style="text-align: right;">
  112. <a href="/add-to-home?start">Add to Home Screen</a>
  113. </div>
  114. </div>
  115. <!-- Add Photo -->
  116. <div class="modal fade" id="photo-modal" tabindex="-1" role="dialog" aria-labelledby="photo-modal-title" aria-hidden="true">
  117. <div class="modal-dialog">
  118. <div class="modal-content">
  119. <div class="modal-header">
  120. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  121. <h4 class="modal-title" id="photo-modal-title">Add Photo</h4>
  122. </div>
  123. <div class="modal-body">
  124. <div id="modal_photo_preview" class="hidden">
  125. <img>
  126. </div>
  127. <label id="note_photo_button" class="btn btn-default btn-file" style="margin-bottom: 1em;">
  128. Choose File <input type="file" name="note_photo" id="note_photo" accept="image/*">
  129. </label>
  130. <div style="margin-bottom: 1em;">
  131. <input type="url" id="note_photo_url" class="form-control" placeholder="Paste image URL">
  132. </div>
  133. <?php if($this->media_endpoint): ?>
  134. <input type="text" id="note_photo_alt" class="form-control" placeholder="Image alt text">
  135. <?php endif ?>
  136. </div>
  137. <div class="modal-footer">
  138. <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  139. <button type="button" class="btn btn-primary save-btn">Add</button>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. <!-- Edit Photo -->
  145. <div class="modal fade" id="edit-photo-modal" tabindex="-1" role="dialog" aria-labelledby="edit-photo-modal-title" aria-hidden="true">
  146. <div class="modal-dialog">
  147. <div class="modal-content">
  148. <div class="modal-header">
  149. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
  150. <h4 class="modal-title" id="edit-photo-modal-title">Edit Photo</h4>
  151. </div>
  152. <div class="modal-body">
  153. <div id="modal_edit_photo_preview" style="margin-bottom: 4px;">
  154. <img>
  155. </div>
  156. <input type="text" id="note_edit_photo_alt" class="form-control" placeholder="Image alt text">
  157. <input type="hidden" id="modal_edit_photo_index">
  158. </div>
  159. <div class="modal-footer">
  160. <!-- <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> -->
  161. <button type="button" class="btn btn-danger remove-btn" data-dismiss="modal">Remove</button>
  162. <button type="button" class="btn btn-primary save-btn">Save</button>
  163. </div>
  164. </div>
  165. </div>
  166. </div>
  167. <style type="text/css">
  168. #reply {
  169. margin-bottom: 1em;
  170. }
  171. #note_content_remaining {
  172. float: right;
  173. font-size: 0.8em;
  174. font-weight: bold;
  175. }
  176. #modal_photo_preview {
  177. text-align: center;
  178. }
  179. #modal_photo_preview img {
  180. max-height: 40vh;
  181. }
  182. #modal_photo_preview img, #modal_edit_photo_preview img {
  183. max-width: 100%;
  184. border-radius: 4px;
  185. margin-bottom: 4px;
  186. }
  187. #photo-previews span {
  188. width: 24%;
  189. height: 180px;
  190. margin-right: 1px;
  191. position: relative;
  192. overflow: hidden;
  193. display: inline-block;
  194. }
  195. #photo-previews img {
  196. position: absolute;
  197. left: 50%;
  198. top: 50%;
  199. height: 100%;
  200. width: auto;
  201. -webkit-transform: translate(-50%,-50%);
  202. -ms-transform: translate(-50%,-50%);
  203. transform: translate(-50%,-50%);
  204. cursor: pointer;
  205. }
  206. .pcheck206 { color: #6ba15c; } /* tweet fits within the limit even after adding RT @username */
  207. .pcheck207 { color: #c4b404; } /* danger zone, tweet will overflow when RT @username is added */
  208. .pcheck200,.pcheck208 { color: #59cb3a; } /* exactly fits 280 chars, both with or without RT */
  209. .pcheck413 { color: #a73b3b; } /* over max tweet length */
  210. .reply-context {
  211. display: flex;
  212. flex-direction: row;
  213. padding: 4px;
  214. margin: 0 3em;
  215. border: 1px #ccc solid;
  216. border-radius: 4px;
  217. background: #f4f4f4;
  218. max-height: 140px;
  219. overflow-y: hidden;
  220. }
  221. .reply-context .reply-content {
  222. flex: 1 0;
  223. }
  224. .reply-context img.author-img {
  225. border-radius: 4px;
  226. width: 48px;
  227. margin-right: 4px;
  228. }
  229. .reply-context .syndications {
  230. float: right;
  231. padding-right: 4px;
  232. }
  233. .reply-context .syndications img {
  234. width: 16px;
  235. }
  236. .reply-context .author {
  237. color: #777;
  238. font-weight: bold;
  239. font-size: 0.9em;
  240. }
  241. .reply-context .author .url {
  242. color: #aaa;
  243. }
  244. .reply-context img.post-img {
  245. float: right;
  246. width: 200px;
  247. }
  248. </style>
  249. <script>
  250. function saveNoteState() {
  251. var state = {
  252. content: $("#note_content").val(),
  253. inReplyTo: $("#note_in_reply_to").val(),
  254. category: $("#note_category").val(),
  255. slug: $("#note_slug").val(),
  256. photos: photos
  257. };
  258. state.syndications = [];
  259. $("#syndication-container button.btn-info").each(function(i,btn){
  260. state.syndications[$(btn).data('syndicate-to')] = 'selected';
  261. });
  262. // console.log("saving",state);
  263. localforage.setItem('current-note', state);
  264. }
  265. function restoreNoteState() {
  266. localforage.getItem('current-note', function(err,note){
  267. if(note) {
  268. $("#note_content").val(note.content);
  269. $("#note_in_reply_to").val(note.inReplyTo);
  270. $("#note_category").val(note.category);
  271. $("#note_slug").val(note.slug);
  272. if(note.photos) {
  273. photos = note.photos;
  274. refreshPhotoPreviews();
  275. }
  276. if(note.inReplyTo) {
  277. expandReplySection();
  278. }
  279. $("#syndication-container button").each(function(i,btn){
  280. if($(btn).data('syndicate-to') in note.syndications) {
  281. $(btn).addClass('btn-info');
  282. }
  283. });
  284. $("#note_content").change();
  285. if($("#note_content").val().match(/`/)) {
  286. switchToMarkdown();
  287. }
  288. activateTokenField();
  289. } else {
  290. activateTokenField();
  291. }
  292. });
  293. }
  294. function expandReplySection() {
  295. $("#expand-reply").click();
  296. $("#note_in_reply_to").change();
  297. }
  298. function activateTokenField() {
  299. $("#note_category").tokenfield({
  300. createTokensOnBlur: true,
  301. beautify: true,
  302. });
  303. }
  304. var hasMediaEndpoint = <?= $this->media_endpoint ? 'true' : 'false' ?>;
  305. var photos = [];
  306. function addNewPhoto() {
  307. // Reset modal
  308. $("#note_photo").val("");
  309. $("#note_photo_url").val("");
  310. $("#note_photo_alt").val("");
  311. $("#modal_photo_preview").addClass("hidden");
  312. $("#note_photo_button").removeClass("hidden");
  313. $("#note_photo_url").removeClass("hidden");
  314. // Show the modal
  315. $("#photo-modal").modal();
  316. }
  317. $(function(){
  318. // Check if there's a pending file at the media endpoint
  319. $.getJSON("/new/last-photo.json", function(response){
  320. if(response.url) {
  321. photos.push({
  322. url: response.url,
  323. alt: null,
  324. external: true
  325. });
  326. refreshPhotoPreviews();
  327. }
  328. });
  329. $("#note_photo").on("change", function(e){
  330. // If the user has a media endpoint, upload the photo to it right now
  331. if(hasMediaEndpoint) {
  332. var formData = new FormData();
  333. formData.append("null","null");
  334. formData.append("photo", e.target.files[0]);
  335. var request = new XMLHttpRequest();
  336. request.open("POST", "/micropub/media");
  337. request.onreadystatechange = function() {
  338. if(request.readyState == XMLHttpRequest.DONE) {
  339. try {
  340. var response = JSON.parse(request.responseText);
  341. if(response.location) {
  342. $("#modal_photo_preview img").attr("src", response.location);
  343. $("#note_photo_url").removeClass("hidden").val(response.location);
  344. $("#note_photo_button").addClass("hidden");
  345. } else {
  346. console.log("Endpoint did not return a location header", response);
  347. }
  348. } catch(e) {
  349. console.log(e);
  350. }
  351. }
  352. }
  353. request.send(formData);
  354. } else {
  355. $("#modal_photo_preview img").attr("src", URL.createObjectURL(e.target.files[0]));
  356. }
  357. $("#modal_photo_preview").removeClass("hidden");
  358. $("#note_photo_button").addClass("hidden");
  359. $("#note_photo_url").addClass("hidden");
  360. });
  361. $("#note_photo_url").on("change", function(){
  362. $("#modal_photo_preview img").attr("src", $(this).val());
  363. $("#modal_photo_preview").removeClass("hidden");
  364. $("#note_photo_button").addClass("hidden");
  365. });
  366. $("#photo-modal .save-btn").click(function(){
  367. if($("#note_photo_url").val()) {
  368. photos.push({
  369. url: $("#note_photo_url").val(),
  370. alt: $("#note_photo_alt").val(),
  371. external: true
  372. });
  373. } else {
  374. photos.push({
  375. url: URL.createObjectURL(document.getElementById("note_photo").files[0]),
  376. alt: $("#note_photo_alt").val(),
  377. file: document.getElementById("note_photo").files[0],
  378. external: false
  379. });
  380. }
  381. $("#photo-modal").modal('hide');
  382. refreshPhotoPreviews();
  383. saveNoteState();
  384. });
  385. $("#edit-photo-modal .save-btn").click(function(){
  386. var index = $("#modal_edit_photo_index").val();
  387. photos[index].alt = $("#note_edit_photo_alt").val();
  388. refreshPhotoPreviews();
  389. saveNoteState();
  390. $("#edit-photo-modal").modal('hide');
  391. });
  392. $("#edit-photo-modal .remove-btn").click(function(){
  393. var new_photos = [];
  394. for(i=0; i<photos.length; i++) {
  395. if(i != $("#modal_edit_photo_index").val()) {
  396. new_photos.push(photos[i]);
  397. }
  398. }
  399. photos = new_photos;
  400. refreshPhotoPreviews();
  401. saveNoteState();
  402. });
  403. $(document).bind('keydown', function(e){
  404. // Easter egg: press ctrl+shift+c to reveal a content type selection
  405. if(e.keyCode == 67 && e.ctrlKey && e.shiftKey) {
  406. $("#content-type-selection").removeClass("hidden");
  407. }
  408. // Easter egg: press ctrl+shift+m to switch to markdown
  409. if(e.keyCode == 77 && e.ctrlKey && e.shiftKey) {
  410. switchToMarkdown();
  411. }
  412. });
  413. });
  414. function switchToMarkdown() {
  415. $("#content-type-selection select").val("text/markdown");
  416. $("#content-type-selection").removeClass("hidden");
  417. }
  418. function refreshPhotoPreviews() {
  419. $("#photo-previews").html("");
  420. for(i=0; i<photos.length; i++) {
  421. var img = document.createElement('img');
  422. img.setAttribute('src', photos[i].url);
  423. img.setAttribute('alt', photos[i].alt);
  424. img.setAttribute('title', photos[i].alt);
  425. var span = document.createElement('span');
  426. span.appendChild(img);
  427. $("#photo-previews").append(span);
  428. }
  429. if(photos.length == 0) {
  430. $("#photo-previews").addClass("hidden");
  431. } else {
  432. $("#photo-previews").removeClass("hidden");
  433. }
  434. $("#photo-previews img").unbind("click").bind("click", function(){
  435. $("#modal_edit_photo_preview img").attr("src", $(this).attr("src"));
  436. var index = false;
  437. for(i=0; i<photos.length; i++) {
  438. if(photos[i].url == $(this).attr("src")) {
  439. index = i;
  440. }
  441. }
  442. $("#note_edit_photo_alt").val(photos[index].alt);
  443. $("#modal_edit_photo_index").val(index);
  444. $("#edit-photo-modal").modal();
  445. });
  446. }
  447. /*
  448. $("#note_photo").on("change", function(e){
  449. // If the user has a media endpoint, upload the photo to it right now
  450. if(hasMediaEndpoint) {
  451. // TODO: add loading state indicator here
  452. console.log("Uploading file to media endpoint...");
  453. var formData = new FormData();
  454. formData.append("null","null");
  455. formData.append("photo", e.target.files[0]);
  456. var request = new XMLHttpRequest();
  457. request.open("POST", "/micropub/media");
  458. request.onreadystatechange = function() {
  459. if(request.readyState == XMLHttpRequest.DONE) {
  460. try {
  461. var response = JSON.parse(request.responseText);
  462. if(response.location) {
  463. // Replace the file upload form with the URL
  464. replacePhotoWithPhotoURL(response.location);
  465. saveNoteState();
  466. } else {
  467. console.log("Endpoint did not return a location header", response);
  468. }
  469. } catch(e) {
  470. console.log(e);
  471. }
  472. }
  473. }
  474. request.send(formData);
  475. } else {
  476. $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
  477. $("#photo_preview_container").removeClass("hidden");
  478. }
  479. });
  480. */
  481. $(function(){
  482. var userHasSetCategory = false;
  483. $("#note_content, #note_category, #note_in_reply_to, #note_slug").on('keyup change', function(e){
  484. saveNoteState();
  485. });
  486. $("#note_content").on('keyup', function(e){
  487. var scrollHeight = document.getElementById("note_content").scrollHeight;
  488. var currentHeight = parseInt($("#note_content").css("height"));
  489. if(Math.abs(scrollHeight - currentHeight) > 20) {
  490. $("#note_content").css("height", (scrollHeight+30)+"px");
  491. }
  492. // If you type a backtick in the content, and are replying to a github issue, switch to markdown
  493. if(e.key == '`') {
  494. if($("#note_in_reply_to").val().match(/github\.com/)) {
  495. switchToMarkdown();
  496. }
  497. }
  498. });
  499. $("#expand-reply").click(function(){
  500. $('.reply-section').removeClass('hidden');
  501. $(this).addClass('hidden');
  502. return false;
  503. });
  504. // Preview the photo when one is chosen
  505. $("#photo_preview_container").addClass("hidden");
  506. $("#note_content").on('change keyup', function(e){
  507. var text = $("#note_content").val();
  508. var tweet_length = tw_text_proxy(text).length;
  509. var tweet_check = tw_length_check(text, 280, "<?= $this->user->twitter_username ?>");
  510. var remaining = 280 - tweet_length;
  511. $("#note_content_remaining span").html(remaining);
  512. $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
  513. $("#note_content_remaining").addClass("pcheck"+tweet_check);
  514. // If the user didn't enter any categories, add them from the post
  515. // if(!userHasSetCategory) {
  516. // var tags = $("#note_content").val().match(/#[a-z][a-z0-9]+/ig);
  517. // if(tags) {
  518. // $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
  519. // }
  520. // }
  521. });
  522. $("#note_in_reply_to").on('change', function(){
  523. var reply_to = $("#note_in_reply_to").val();
  524. if(reply_to == "") {
  525. $(".reply-section").addClass("hidden");
  526. $(".reply-context").addClass("hidden");
  527. $("#note-name").addClass("hidden");
  528. $("#note_content").siblings("label").text("Content");
  529. $("#expand-reply").removeClass("hidden");
  530. return;
  531. }
  532. if($("#note_in_reply_to").val().match(/^https:\/\/github\.com\/([^\/]+)\/([^\/]+)$/)) {
  533. // Add the "name" field for issues
  534. $("#note-name").removeClass("hidden");
  535. $("#note_content").siblings("label").text("Description");
  536. }
  537. $(".reply-section .loading").removeClass("hidden");
  538. $.get("/reply/preview", {url:reply_to}, function(data){
  539. if(data.canonical_reply_url != reply_to) {
  540. $("#note_in_reply_to").val(data.canonical_reply_url);
  541. }
  542. // var category = csv_to_array($("#note_category").val());
  543. // for(var i in data.entry.category) {
  544. // if($.inArray(data.entry.category[i], category) == -1) {
  545. // category.push(data.entry.category[i]);
  546. // }
  547. // }
  548. // $("#note_category").val(category.join(", "));
  549. /*
  550. // stop auto-populating usernames in replies, since Twitter no longer requires it
  551. if($("#note_content").val() == "" && data.mentions) {
  552. var mentions = '';
  553. for(var i in data.mentions) {
  554. mentions += '@'+data.mentions[i]+' ';
  555. }
  556. $("#note_content").val(mentions);
  557. }
  558. */
  559. if(data.entry) {
  560. $(".reply-context .content").text(data.entry.content.text);
  561. if(data.entry.name) {
  562. $(".reply-context .post-name").text(data.entry.name).removeClass('hidden');
  563. } else {
  564. $(".reply-context .post-name").addClass('hidden');
  565. }
  566. if(data.entry.author) {
  567. $(".reply-context .author .name").text(data.entry.author.name);
  568. $(".reply-context .author .url").text(data.entry.author.url);
  569. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  570. $(".reply-context .reply-author").removeClass("hidden");
  571. } else {
  572. $(".reply-context .reply-author").addClass("hidden");
  573. }
  574. if(data.entry.photo) {
  575. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  576. } else {
  577. $(".reply-context img.post-img").addClass('hidden');
  578. }
  579. if(data.entry.type == "event") {
  580. $("#form_rsvp").removeClass("hidden");
  581. } else {
  582. $("#form_rsvp").addClass("hidden");
  583. }
  584. if(data.syndications) {
  585. $(".reply-context .syndications").html('');
  586. for(var i in data.syndications) {
  587. var syn = data.syndications[i];
  588. $(".reply-context .syndications").append('<a href="'+syn.url+'"><img src="/images/services/'+syn.icon+'"></a>');
  589. }
  590. }
  591. $(".reply-context").removeClass("hidden");
  592. }
  593. $(".reply-section .loading").addClass("hidden");
  594. });
  595. });
  596. $("#note_category").on('keydown keyup', function(){
  597. userHasSetCategory = true;
  598. });
  599. $("#note_category").on('change', function(){
  600. if($("#note_category").val() == "") {
  601. userHasSetCategory = false;
  602. }
  603. });
  604. // When the reply URL is in the query string, or loads from localstorage, make sure
  605. // to run the event handlers to expand the reply section
  606. if($("#note_in_reply_to").val() != "") {
  607. expandReplySection();
  608. }
  609. $("#btn_post").click(function(){
  610. // Collect all the syndication buttons that are pressed
  611. var syndications = [];
  612. $("#syndication-container button.btn-info").each(function(i,btn){
  613. syndications.push($(btn).data('syndicate-to'));
  614. });
  615. var category = tokenfieldToArray("#note_category");
  616. var formData = new FormData();
  617. var entry = {};
  618. var doMultipart = false;
  619. var hasAltText = false;
  620. if(v=$("#note_name").val()) {
  621. formData.append("name", v);
  622. entry['name'] = [v];
  623. }
  624. if(v=$("#note_content").val()) {
  625. formData.append("content", v);
  626. entry['content'] = [v];
  627. }
  628. if(v=$("#note_in_reply_to").val()) {
  629. formData.append("in-reply-to", v);
  630. entry['in-reply-to'] = [v];
  631. }
  632. if(v=$("#note_location").val()) {
  633. formData.append("location", v);
  634. entry['location'] = [v];
  635. }
  636. if(category.length > 0) {
  637. for(var i in category) {
  638. formData.append("category[]", category[i]);
  639. }
  640. entry['category'] = category;
  641. }
  642. if(syndications.length > 0) {
  643. for(var i in syndications) {
  644. formData.append("<?= $this->user->micropub_syndicate_field ?>[]", syndications[i]);
  645. }
  646. entry["<?= $this->user->micropub_syndicate_field ?>"] = syndications;
  647. }
  648. if(v=$("#note_slug").val()) {
  649. formData.append("<?= $this->user->micropub_slug_field ?>", v);
  650. entry["<?= $this->user->micropub_slug_field ?>"] = v;
  651. }
  652. if(!$("#form_rsvp").hasClass("hidden") && $("#note_rsvp").val()) {
  653. formData.append("rsvp", $("#note_rsvp").val());
  654. entry["rsvp"] = $("#note_rsvp").val();
  655. }
  656. function appendPhotoToFormData(photo, prop) {
  657. if(photo.external) {
  658. if(photo.alt) {
  659. hasAltText = true;
  660. formData.append(prop+"[value]", photo.url);
  661. formData.append(prop+"[alt]", photo.alt);
  662. entry['photo'].push({
  663. value: photo.url,
  664. alt: photo.alt
  665. })
  666. } else {
  667. formData.append(prop, photo.url);
  668. entry['photo'].push(photo.url);
  669. }
  670. } else {
  671. formData.append(prop, photo.file);
  672. doMultipart = true;
  673. }
  674. }
  675. if(photos.length == 1) {
  676. entry['photo'] = [];
  677. appendPhotoToFormData(photos[0], "photo");
  678. } else if(photos.length > 1) {
  679. entry['photo'] = [];
  680. for(i=0; i<photos.length; i++) {
  681. appendPhotoToFormData(photos[i], "photo[]");
  682. }
  683. }
  684. if(!$("#content-type-selection").hasClass("hidden")) {
  685. entry['p3k-content-type'] = $("#note_content_type").val();
  686. formData.append('p3k-content-type', $("#note_content_type").val());
  687. }
  688. // Need to append a placeholder field because if the file size max is hit, $_POST will
  689. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  690. // This will be stripped by Quill before it's sent to the Micropub endpoint
  691. formData.append("null","null");
  692. $("#btn_post").addClass("loading disabled").text("Working...");
  693. if(doMultipart || !hasAltText) {
  694. var request = new XMLHttpRequest();
  695. request.open("POST", "/micropub/multipart");
  696. request.onreadystatechange = function() {
  697. if(request.readyState == XMLHttpRequest.DONE) {
  698. handle_post_response(request.responseText);
  699. }
  700. }
  701. request.send(formData);
  702. } else {
  703. // Convert all single-value properties to arrays
  704. for(var k in entry) {
  705. if(typeof entry[k] == "string") {
  706. entry[k] = [entry[k]];
  707. }
  708. }
  709. $.post("/micropub/postjson", {
  710. data: JSON.stringify({
  711. "type": ["h-entry"],
  712. "properties": entry
  713. })
  714. }, function(response) {
  715. handle_post_response(response);
  716. });
  717. }
  718. return false;
  719. });
  720. function handle_post_response(response) {
  721. try {
  722. if(typeof response == "string") {
  723. response = JSON.parse(response);
  724. }
  725. localforage.removeItem('current-note');
  726. if(response.location) {
  727. window.location = response.location;
  728. // console.log(response.location);
  729. } else {
  730. $("#test_response").html(response.response).removeClass('hidden');
  731. $("#test_success").addClass('hidden');
  732. $("#test_error").removeClass('hidden');
  733. }
  734. } catch(e) {
  735. $("#test_success").addClass('hidden');
  736. $("#test_error").removeClass('hidden');
  737. }
  738. $("#btn_post").removeClass("loading disabled").text("Post");
  739. }
  740. function location_error(msg) {
  741. $("#note_location_msg").val(msg);
  742. $("#note_location_chk").removeAttr("checked");
  743. $("#note_location_loading").hide();
  744. $("#note_location_img").hide();
  745. $("#note_location_msg").removeClass("img-visible");
  746. }
  747. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  748. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  749. function fetch_location() {
  750. $("#note_location_loading").show();
  751. navigator.geolocation.getCurrentPosition(function(position){
  752. $("#note_location_loading").hide();
  753. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  754. $("#note_location_msg").val(geo);
  755. $("#note_location").val(geo);
  756. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  757. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  758. $("#note_location_img").show();
  759. $("#note_location_msg").addClass("img-visible");
  760. $.post("/prefs/timezone", {
  761. latitude: position.coords.latitude,
  762. longitude: position.coords.longitude
  763. });
  764. }, function(err){
  765. if(err.code == 1) {
  766. location_error("The website was not able to get permission");
  767. } else if(err.code == 2) {
  768. location_error("Location information was unavailable");
  769. } else if(err.code == 3) {
  770. location_error("Timed out getting location");
  771. }
  772. });
  773. }
  774. $("#note_location_chk").click(function(){
  775. if($(this).attr("checked") == "checked") {
  776. if(navigator.geolocation) {
  777. $.post("/prefs", {
  778. enabled: 1
  779. });
  780. fetch_location();
  781. } else {
  782. location_error("Browser location is not supported");
  783. }
  784. } else {
  785. $("#note_location_img").hide();
  786. $("#note_location_msg").removeClass("img-visible");
  787. $("#note_location_msg").val('');
  788. $("#note_location").val('');
  789. $.post("/prefs", {
  790. enabled: 0
  791. });
  792. }
  793. });
  794. if($("#location_enabled").val() == 1) {
  795. $("#note_location_chk").attr("checked","checked");
  796. fetch_location();
  797. }
  798. bind_syndication_buttons();
  799. if($("#note_in_reply_to").val() != "") {
  800. $("#note_in_reply_to").change();
  801. } else {
  802. restoreNoteState();
  803. }
  804. });
  805. <?= partial('partials/syndication-js') ?>
  806. </script>