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.

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