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.

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