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.

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