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.

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