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.

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