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.

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