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.

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