Mirror of Quill
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.

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