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.

889 lines
29 KiB

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