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.

915 lines
30 KiB

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