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.

944 lines
30 KiB

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