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.

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