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.

852 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>280</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 280 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, 280, "<?= $this->user->twitter_username ?>");
  468. var remaining = 280 - 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. /*
  501. // stop auto-populating usernames in replies, since Twitter no longer requires it
  502. if($("#note_content").val() == "" && data.mentions) {
  503. var mentions = '';
  504. for(var i in data.mentions) {
  505. mentions += '@'+data.mentions[i]+' ';
  506. }
  507. $("#note_content").val(mentions);
  508. }
  509. */
  510. if(data.entry) {
  511. $(".reply-context .content").text(data.entry.content.text);
  512. if(data.entry.name) {
  513. $(".reply-context .post-name").text(data.entry.name).removeClass('hidden');
  514. } else {
  515. $(".reply-context .post-name").addClass('hidden');
  516. }
  517. if(data.entry.author) {
  518. $(".reply-context .author .name").text(data.entry.author.name);
  519. $(".reply-context .author .url").text(data.entry.author.url);
  520. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  521. $(".reply-context .reply-author").removeClass("hidden");
  522. } else {
  523. $(".reply-context .reply-author").addClass("hidden");
  524. }
  525. if(data.entry.photo) {
  526. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  527. } else {
  528. $(".reply-context img.post-img").addClass('hidden');
  529. }
  530. if(data.entry.type == "event") {
  531. $("#form_rsvp").removeClass("hidden");
  532. } else {
  533. $("#form_rsvp").addClass("hidden");
  534. }
  535. if(data.syndications) {
  536. $(".reply-context .syndications").html('');
  537. for(var i in data.syndications) {
  538. var syn = data.syndications[i];
  539. $(".reply-context .syndications").append('<a href="'+syn.url+'"><img src="/images/services/'+syn.icon+'"></a>');
  540. }
  541. }
  542. $(".reply-context").removeClass("hidden");
  543. }
  544. $(".reply-section .loading").addClass("hidden");
  545. });
  546. });
  547. $("#note_category").on('keydown keyup', function(){
  548. userHasSetCategory = true;
  549. });
  550. $("#note_category").on('change', function(){
  551. if($("#note_category").val() == "") {
  552. userHasSetCategory = false;
  553. }
  554. });
  555. // When the reply URL is in the query string, or loads from localstorage, make sure
  556. // to run the event handlers to expand the reply section
  557. if($("#note_in_reply_to").val() != "") {
  558. expandReplySection();
  559. }
  560. $("#btn_post").click(function(){
  561. // Collect all the syndication buttons that are pressed
  562. var syndications = [];
  563. $("#syndication-container button.btn-info").each(function(i,btn){
  564. syndications.push($(btn).data('syndicate-to'));
  565. });
  566. var category = csv_to_array($("#note_category").val());
  567. var formData = new FormData();
  568. var entry = {};
  569. var doMultipart = false;
  570. var hasAltText = false;
  571. if(v=$("#note_content").val()) {
  572. formData.append("content", v);
  573. entry['content'] = [v];
  574. }
  575. if(v=$("#note_in_reply_to").val()) {
  576. formData.append("in-reply-to", v);
  577. entry['in-reply-to'] = [v];
  578. }
  579. if(v=$("#note_location").val()) {
  580. formData.append("location", v);
  581. entry['location'] = [v];
  582. }
  583. if(category.length > 0) {
  584. for(var i in category) {
  585. formData.append("category[]", category[i]);
  586. }
  587. entry['category'] = category;
  588. }
  589. if(syndications.length > 0) {
  590. for(var i in syndications) {
  591. formData.append("<?= $this->user->micropub_syndicate_field ?>[]", syndications[i]);
  592. }
  593. entry["<?= $this->user->micropub_syndicate_field ?>"] = syndications;
  594. }
  595. if(v=$("#note_slug").val()) {
  596. formData.append("<?= $this->user->micropub_slug_field ?>", v);
  597. entry["<?= $this->user->micropub_slug_field ?>"] = v;
  598. }
  599. if(!$("#form_rsvp").hasClass("hidden") && $("#note_rsvp").val()) {
  600. formData.append("rsvp", $("#note_rsvp").val());
  601. entry["rsvp"] = $("#note_rsvp").val();
  602. }
  603. function appendPhotoToFormData(photo, prop) {
  604. if(photo.external) {
  605. if(photo.alt) {
  606. hasAltText = true;
  607. formData.append(prop+"[value]", photo.url);
  608. formData.append(prop+"[alt]", photo.alt);
  609. entry['photo'].push({
  610. value: photo.url,
  611. alt: photo.alt
  612. })
  613. } else {
  614. formData.append(prop, photo.url);
  615. entry['photo'].push(photo.url);
  616. }
  617. } else {
  618. formData.append(prop, photo.file);
  619. doMultipart = true;
  620. }
  621. }
  622. if(photos.length == 1) {
  623. entry['photo'] = [];
  624. appendPhotoToFormData(photos[0], "photo");
  625. } else if(photos.length > 1) {
  626. entry['photo'] = [];
  627. for(i=0; i<photos.length; i++) {
  628. appendPhotoToFormData(photos[i], "photo[]");
  629. }
  630. }
  631. // Need to append a placeholder field because if the file size max is hit, $_POST will
  632. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  633. // This will be stripped by Quill before it's sent to the Micropub endpoint
  634. formData.append("null","null");
  635. $("#btn_post").addClass("loading disabled").text("Working...");
  636. if(doMultipart || !hasAltText) {
  637. var request = new XMLHttpRequest();
  638. request.open("POST", "/micropub/multipart");
  639. request.onreadystatechange = function() {
  640. if(request.readyState == XMLHttpRequest.DONE) {
  641. handle_post_response(request.responseText);
  642. }
  643. }
  644. request.send(formData);
  645. } else {
  646. // Convert all single-value properties to arrays
  647. for(var k in entry) {
  648. if(typeof entry[k] == "string") {
  649. entry[k] = [entry[k]];
  650. }
  651. }
  652. $.post("/micropub/postjson", {
  653. data: JSON.stringify({
  654. "type": ["h-entry"],
  655. "properties": entry
  656. })
  657. }, function(response) {
  658. handle_post_response(response);
  659. });
  660. }
  661. return false;
  662. });
  663. function handle_post_response(response) {
  664. try {
  665. if(typeof response == "string") {
  666. response = JSON.parse(response);
  667. }
  668. localforage.removeItem('current-note');
  669. if(response.location) {
  670. window.location = response.location;
  671. // console.log(response.location);
  672. } else {
  673. $("#test_response").html(response.response).removeClass('hidden');
  674. $("#test_success").addClass('hidden');
  675. $("#test_error").removeClass('hidden');
  676. }
  677. } catch(e) {
  678. $("#test_success").addClass('hidden');
  679. $("#test_error").removeClass('hidden');
  680. }
  681. $("#btn_post").removeClass("loading disabled").text("Post");
  682. }
  683. function location_error(msg) {
  684. $("#note_location_msg").val(msg);
  685. $("#note_location_chk").removeAttr("checked");
  686. $("#note_location_loading").hide();
  687. $("#note_location_img").hide();
  688. $("#note_location_msg").removeClass("img-visible");
  689. }
  690. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  691. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  692. function fetch_location() {
  693. $("#note_location_loading").show();
  694. navigator.geolocation.getCurrentPosition(function(position){
  695. $("#note_location_loading").hide();
  696. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  697. $("#note_location_msg").val(geo);
  698. $("#note_location").val(geo);
  699. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  700. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  701. $("#note_location_img").show();
  702. $("#note_location_msg").addClass("img-visible");
  703. $.post("/prefs/timezone", {
  704. latitude: position.coords.latitude,
  705. longitude: position.coords.longitude
  706. });
  707. }, function(err){
  708. if(err.code == 1) {
  709. location_error("The website was not able to get permission");
  710. } else if(err.code == 2) {
  711. location_error("Location information was unavailable");
  712. } else if(err.code == 3) {
  713. location_error("Timed out getting location");
  714. }
  715. });
  716. }
  717. $("#note_location_chk").click(function(){
  718. if($(this).attr("checked") == "checked") {
  719. if(navigator.geolocation) {
  720. $.post("/prefs", {
  721. enabled: 1
  722. });
  723. fetch_location();
  724. } else {
  725. location_error("Browser location is not supported");
  726. }
  727. } else {
  728. $("#note_location_img").hide();
  729. $("#note_location_msg").removeClass("img-visible");
  730. $("#note_location_msg").val('');
  731. $("#note_location").val('');
  732. $.post("/prefs", {
  733. enabled: 0
  734. });
  735. }
  736. });
  737. if($("#location_enabled").val() == 1) {
  738. $("#note_location_chk").attr("checked","checked");
  739. fetch_location();
  740. }
  741. bind_syndication_buttons();
  742. if($("#note_in_reply_to").val() != "") {
  743. $("#note_in_reply_to").change();
  744. } else {
  745. restoreNoteState();
  746. }
  747. });
  748. <?= partial('partials/syndication-js') ?>
  749. </script>