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.

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