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.

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