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.

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