Mirror of Quill
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.

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