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.

590 lines
21 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 (comma-separated list)</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">
  39. <label for="note_photo">Photo</label>
  40. <input type="file" name="note_photo" id="note_photo" accept="image/*">
  41. <a href="javascript:switchToManualPhotoURL();" id="note_manual_photo">enter photo url</a>
  42. <a href="javascript:addPhotoURL();" class="hidden" id="add_photo">add photo</a>
  43. <br>
  44. <div id="photo_preview_container" class="hidden">
  45. <img src="" id="photo_preview" style="max-width: 300px; max-height: 300px;">
  46. <div>
  47. <button type="button" class="btn btn-danger btn-sm" id="remove_photo"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Remove image</button>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <label for="note_syndicate-to">Syndicate <a href="javascript:reload_syndications()">(refresh list)</a></label>
  53. <div id="syndication-container">
  54. <?php
  55. if($this->syndication_targets) {
  56. echo '<ul>';
  57. foreach($this->syndication_targets as $syn) {
  58. echo '<li>'
  59. . '<button data-syndicate-to="'.(isset($syn['uid']) ? htmlspecialchars($syn['uid']) : htmlspecialchars($syn['target'])).'" class="btn btn-default btn-block">'
  60. . ($syn['favicon'] ? '<img src="'.htmlspecialchars($syn['favicon']).'" width="16" height="16"> ' : '')
  61. . htmlspecialchars($syn['target'])
  62. . '</button>'
  63. . '</li>';
  64. }
  65. echo '</ul>';
  66. } else {
  67. ?><div class="bs-callout bs-callout-warning">No syndication targets were found on your site.
  68. You can provide a <a href="/docs#syndication">list of supported syndication targets</a> that will appear as checkboxes here.</div><?php
  69. }
  70. ?>
  71. </div>
  72. </div>
  73. <div class="form-group">
  74. <label for="note_location">Location</label>
  75. <input type="checkbox" id="note_location_chk" value="">
  76. <img src="/images/spinner.gif" id="note_location_loading" style="display: none;">
  77. <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  78. <input type="hidden" id="note_location">
  79. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  80. <div id="note_location_img" style="display: none;">
  81. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  82. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  83. </div>
  84. </div>
  85. <button class="btn btn-success" id="btn_post">Post</button>
  86. </form>
  87. <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>
  88. <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>
  89. <div id="last_request_container" style="display: none;">
  90. <h4>Request made to your Micropub endpoint</h4>
  91. <pre id="test_request" style="width: 100%; min-height: 140px;"></pre>
  92. </div>
  93. <?php if($this->test_response): ?>
  94. <h4>Last response from your Micropub endpoint <span id="last_response_date">(<?= relative_time($this->response_date) ?>)</span></h4>
  95. <?php endif; ?>
  96. <pre id="test_response" class="<?= $this->test_response ? '' : 'hidden' ?>" style="width: 100%; min-height: 240px;"><?= htmlspecialchars($this->test_response) ?></pre>
  97. <hr>
  98. <div style="text-align: right;">
  99. <a href="/add-to-home?start">Add to Home Screen</a>
  100. </div>
  101. </div>
  102. <style type="text/css">
  103. #reply {
  104. margin-bottom: 1em;
  105. }
  106. #note_content_remaining {
  107. float: right;
  108. font-size: 0.8em;
  109. font-weight: bold;
  110. }
  111. .pcheck206 { color: #6ba15c; } /* tweet fits within the limit even after adding RT @username */
  112. .pcheck207 { color: #c4b404; } /* danger zone, tweet will overflow when RT @username is added */
  113. .pcheck200,.pcheck208 { color: #59cb3a; } /* exactly fits 140 chars, both with or without RT */
  114. .pcheck413 { color: #a73b3b; } /* over max tweet length */
  115. .reply-context {
  116. display: flex;
  117. flex-direction: row;
  118. padding: 4px;
  119. margin: 0 3em;
  120. border: 1px #ccc solid;
  121. border-radius: 4px;
  122. background: #f4f4f4;
  123. max-height: 140px;
  124. overflow-y: hidden;
  125. }
  126. .reply-context .reply-content {
  127. flex: 1 0;
  128. }
  129. .reply-context img.author-img {
  130. border-radius: 4px;
  131. width: 48px;
  132. margin-right: 4px;
  133. }
  134. .reply-context .author {
  135. color: #777;
  136. font-weight: bold;
  137. font-size: 0.9em;
  138. }
  139. .reply-context .author .url {
  140. color: #aaa;
  141. }
  142. .reply-context img.post-img {
  143. float: right;
  144. width: 200px;
  145. }
  146. </style>
  147. <script>
  148. function saveNoteState() {
  149. var state = {
  150. content: $("#note_content").val(),
  151. inReplyTo: $("#note_in_reply_to").val(),
  152. category: $("#note_category").val(),
  153. slug: $("#note_slug").val(),
  154. photo: $("#note_photo_url").val()
  155. };
  156. state.syndications = [];
  157. $("#syndication-container button.btn-info").each(function(i,btn){
  158. state.syndications[$(btn).data('syndicate-to')] = 'selected';
  159. });
  160. localforage.setItem('current-note', state);
  161. }
  162. function restoreNoteState() {
  163. localforage.getItem('current-note', function(err,note){
  164. if(note) {
  165. $("#note_content").val(note.content);
  166. $("#note_in_reply_to").val(note.inReplyTo);
  167. $("#note_category").val(note.category);
  168. $("#note_slug").val(note.slug);
  169. if(note.photo) {
  170. replacePhotoWithPhotoURL(note.photo);
  171. }
  172. if(note.inReplyTo) {
  173. expandReplySection();
  174. }
  175. $("#syndication-container button").each(function(i,btn){
  176. if($(btn).data('syndicate-to') in note.syndications) {
  177. $(btn).addClass('btn-info');
  178. }
  179. });
  180. $("#note_content").change();
  181. }
  182. });
  183. }
  184. function replacePhotoWithPhotoURL(url) {
  185. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  186. $(".note_photo_url").val(url);
  187. $("#note_photo").remove();
  188. $("#photo_preview").attr("src", url);
  189. $("#photo_preview_container").removeClass("hidden");
  190. $("#note_manual_photo").addClass("hidden");
  191. }
  192. function switchToManualPhotoURL() {
  193. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  194. $("#note_photo").remove();
  195. $("#note_photo_url").change(function(){
  196. $("#photo_preview").attr("src", $(this).val());
  197. $("#photo_preview_container").removeClass("hidden");
  198. });
  199. $("#note_manual_photo").addClass("hidden");
  200. $("#add_photo").removeClass("hidden");
  201. }
  202. function addPhotoURL() {
  203. $(".note_photo_url:last").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control" style="margin-top:2px;">');
  204. if($(".note_photo_url").length == 4) {
  205. $("#add_photo").remove();
  206. }
  207. }
  208. function expandReplySection() {
  209. $("#expand-reply").click();
  210. $("#note_in_reply_to").change();
  211. }
  212. $(function(){
  213. var userHasSetCategory = false;
  214. var hasMediaEndpoint = <?= $this->media_endpoint ? 'true' : 'false' ?>;
  215. $("#note_content, #note_category, #note_in_reply_to, #note_slug, #note_photo_url").on('keyup change', function(e){
  216. saveNoteState();
  217. });
  218. $("#note_content").on('keyup', function(){
  219. var scrollHeight = document.getElementById("note_content").scrollHeight;
  220. var currentHeight = parseInt($("#note_content").css("height"));
  221. if(Math.abs(scrollHeight - currentHeight) > 20) {
  222. $("#note_content").css("height", (scrollHeight+30)+"px");
  223. }
  224. });
  225. $("#expand-reply").click(function(){
  226. $('.reply-section').removeClass('hidden');
  227. $(this).addClass('hidden');
  228. return false;
  229. });
  230. // Preview the photo when one is chosen
  231. $("#photo_preview_container").addClass("hidden");
  232. $("#note_photo").on("change", function(e){
  233. // If the user has a media endpoint, upload the photo to it right now
  234. if(hasMediaEndpoint) {
  235. // TODO: add loading state indicator here
  236. console.log("Uploading file to media endpoint...");
  237. var formData = new FormData();
  238. formData.append("null","null");
  239. formData.append("photo", e.target.files[0]);
  240. var request = new XMLHttpRequest();
  241. request.open("POST", "/micropub/media");
  242. request.onreadystatechange = function() {
  243. if(request.readyState == XMLHttpRequest.DONE) {
  244. try {
  245. var response = JSON.parse(request.responseText);
  246. if(response.location) {
  247. // Replace the file upload form with the URL
  248. replacePhotoWithPhotoURL(response.location);
  249. saveNoteState();
  250. } else {
  251. console.log("Endpoint did not return a location header", response);
  252. }
  253. } catch(e) {
  254. console.log(e);
  255. }
  256. }
  257. }
  258. request.send(formData);
  259. } else {
  260. $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
  261. $("#photo_preview_container").removeClass("hidden");
  262. }
  263. });
  264. $("#remove_photo").on("click", function(){
  265. $("#note_photo").val("");
  266. $(".note_photo_url").val("");
  267. $("#photo_preview").attr("src", "" );
  268. $("#photo_preview_container").addClass("hidden");
  269. saveNoteState();
  270. });
  271. $("#note_content").on('change keyup', function(e){
  272. var text = $("#note_content").val();
  273. var tweet_length = tw_text_proxy(text).length;
  274. var tweet_check = tw_length_check(text, 140, "<?= $this->user->twitter_username ?>");
  275. var remaining = 140 - tweet_length;
  276. $("#note_content_remaining span").html(remaining);
  277. $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
  278. $("#note_content_remaining").addClass("pcheck"+tweet_check);
  279. // If the user didn't enter any categories, add them from the post
  280. if(!userHasSetCategory) {
  281. var tags = $("#note_content").val().match(/#[a-z][a-z0-9]+/ig);
  282. if(tags) {
  283. $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
  284. }
  285. }
  286. });
  287. $("#note_in_reply_to").on('change', function(){
  288. var reply_to = $("#note_in_reply_to").val();
  289. if(reply_to == "") {
  290. $(".reply-section").addClass("hidden");
  291. $(".reply-context").addClass("hidden");
  292. $("#expand-reply").removeClass("hidden");
  293. return;
  294. }
  295. $(".reply-section .loading").removeClass("hidden");
  296. $.get("/reply/preview", {url:reply_to}, function(data){
  297. if(data.canonical_reply_url != reply_to) {
  298. $("#note_in_reply_to").val(data.canonical_reply_url);
  299. }
  300. var category = csv_to_array($("#note_category").val());
  301. for(var i in data.entry.category) {
  302. if($.inArray(data.entry.category[i], category) == -1) {
  303. category.push(data.entry.category[i]);
  304. }
  305. }
  306. $("#note_category").val(category.join(", "));
  307. if($("#note_content").val() == "" && data.mentions) {
  308. var mentions = '';
  309. for(var i in data.mentions) {
  310. mentions += '@'+data.mentions[i]+' ';
  311. }
  312. $("#note_content").val(mentions);
  313. }
  314. $(".reply-context .content").text(data.entry.content.text);
  315. if(data.entry.name) {
  316. $(".reply-context .post-name").text(data.entry.name).removeClass('hidden');
  317. } else {
  318. $(".reply-context .post-name").addClass('hidden');
  319. }
  320. if(data.entry.author) {
  321. $(".reply-context .author .name").text(data.entry.author.name);
  322. $(".reply-context .author .url").text(data.entry.author.url);
  323. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  324. }
  325. if(data.entry.photo) {
  326. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  327. } else {
  328. $(".reply-context img.post-img").addClass('hidden');
  329. }
  330. $(".reply-section .loading").addClass("hidden");
  331. $(".reply-context").removeClass("hidden");
  332. });
  333. });
  334. $("#note_category").on('keydown keyup', function(){
  335. userHasSetCategory = true;
  336. });
  337. $("#note_category").on('change', function(){
  338. if($("#note_category").val() == "") {
  339. userHasSetCategory = false;
  340. }
  341. });
  342. // When the reply URL is in the query string, or loads from localstorage, make sure
  343. // to run the event handlers to expand the reply section
  344. if($("#note_in_reply_to").val() != "") {
  345. expandReplySection();
  346. }
  347. $("#btn_post").click(function(){
  348. // Collect all the syndication buttons that are pressed
  349. var syndications = [];
  350. $("#syndication-container button.btn-info").each(function(i,btn){
  351. syndications.push($(btn).data('syndicate-to'));
  352. });
  353. var category = csv_to_array($("#note_category").val());
  354. var formData = new FormData();
  355. if(v=$("#note_content").val()) {
  356. formData.append("content", v);
  357. }
  358. if(v=$("#note_in_reply_to").val()) {
  359. formData.append("in-reply-to", v);
  360. }
  361. if(v=$("#note_location").val()) {
  362. formData.append("location", v);
  363. }
  364. if(category.length > 0) {
  365. for(var i in category) {
  366. formData.append("category[]", category[i]);
  367. }
  368. }
  369. if(syndications.length > 0) {
  370. for(var i in syndications) {
  371. formData.append("syndicate-to[]", syndications[i]);
  372. }
  373. }
  374. if(v=$("#note_slug").val()) {
  375. formData.append("<?= $this->user->micropub_slug_field ?>", v);
  376. }
  377. // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
  378. if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
  379. formData.append("photo", document.getElementById("note_photo").files[0]);
  380. } else if($(".note_photo_url").val()) {
  381. $(".note_photo_url").each(function(){
  382. if($(this).val()) {
  383. formData.append("photo[]", $(this).val());
  384. }
  385. });
  386. }
  387. // Need to append a placeholder field because if the file size max is hit, $_POST will
  388. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  389. // This will be stripped by Quill before it's sent to the Micropub endpoint
  390. formData.append("null","null");
  391. var request = new XMLHttpRequest();
  392. request.open("POST", "/micropub/multipart");
  393. request.onreadystatechange = function() {
  394. if(request.readyState == XMLHttpRequest.DONE) {
  395. // console.log(request.responseText);
  396. try {
  397. var response = JSON.parse(request.responseText);
  398. localforage.removeItem('current-note');
  399. if(response.location) {
  400. window.location = response.location;
  401. // console.log(response.location);
  402. } else {
  403. $("#test_response").html(response.response).removeClass('hidden');
  404. $("#test_success").addClass('hidden');
  405. $("#test_error").removeClass('hidden');
  406. }
  407. } catch(e) {
  408. $("#test_success").addClass('hidden');
  409. $("#test_error").removeClass('hidden');
  410. }
  411. $("#btn_post").removeClass("loading disabled").text("Post");
  412. }
  413. }
  414. $("#btn_post").addClass("loading disabled").text("Working...");
  415. request.send(formData);
  416. /*
  417. $.post("/micropub/multipart", {
  418. content: $("#note_content").val(),
  419. 'in-reply-to': $("#note_in_reply_to").val(),
  420. location: $("#note_location").val(),
  421. category: category,
  422. slug: $("#note_slug").val(),
  423. 'syndicate-to': syndications
  424. }, function(data){
  425. var response = JSON.parse(data);
  426. if(response.location != false) {
  427. $("#note_form").slideUp(200, function(){
  428. $(window).scrollTop($("#test_success").position().top);
  429. });
  430. $("#test_success").removeClass('hidden');
  431. $("#test_error").addClass('hidden');
  432. $("#post_href").attr("href", response.location);
  433. $("#note_content").val("");
  434. $("#note_in_reply_to").val("");
  435. $("#note_category").val("");
  436. $("#note_slug").val("");
  437. } else {
  438. $("#test_success").addClass('hidden');
  439. $("#test_error").removeClass('hidden');
  440. }
  441. $("#last_response_date").html("(just now)");
  442. $("#test_request").html(response.request);
  443. $("#last_request_container").show();
  444. $("#test_response").html(response.response);
  445. });
  446. */
  447. return false;
  448. });
  449. function location_error(msg) {
  450. $("#note_location_msg").val(msg);
  451. $("#note_location_chk").removeAttr("checked");
  452. $("#note_location_loading").hide();
  453. $("#note_location_img").hide();
  454. $("#note_location_msg").removeClass("img-visible");
  455. }
  456. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  457. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  458. function fetch_location() {
  459. $("#note_location_loading").show();
  460. navigator.geolocation.getCurrentPosition(function(position){
  461. $("#note_location_loading").hide();
  462. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  463. $("#note_location_msg").val(geo);
  464. $("#note_location").val(geo);
  465. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  466. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  467. $("#note_location_img").show();
  468. $("#note_location_msg").addClass("img-visible");
  469. $.post("/prefs/timezone", {
  470. latitude: position.coords.latitude,
  471. longitude: position.coords.longitude
  472. });
  473. }, function(err){
  474. if(err.code == 1) {
  475. location_error("The website was not able to get permission");
  476. } else if(err.code == 2) {
  477. location_error("Location information was unavailable");
  478. } else if(err.code == 3) {
  479. location_error("Timed out getting location");
  480. }
  481. });
  482. }
  483. $("#note_location_chk").click(function(){
  484. if($(this).attr("checked") == "checked") {
  485. if(navigator.geolocation) {
  486. $.post("/prefs", {
  487. enabled: 1
  488. });
  489. fetch_location();
  490. } else {
  491. location_error("Browser location is not supported");
  492. }
  493. } else {
  494. $("#note_location_img").hide();
  495. $("#note_location_msg").removeClass("img-visible");
  496. $("#note_location_msg").val('');
  497. $("#note_location").val('');
  498. $.post("/prefs", {
  499. enabled: 0
  500. });
  501. }
  502. });
  503. if($("#location_enabled").val() == 1) {
  504. $("#note_location_chk").attr("checked","checked");
  505. fetch_location();
  506. }
  507. bind_syndication_buttons();
  508. if($("#note_in_reply_to").val() != "") {
  509. $("#note_in_reply_to").change();
  510. } else {
  511. restoreNoteState();
  512. }
  513. });
  514. <?= partial('partials/syndication-js') ?>
  515. </script>