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.

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