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.

582 lines
20 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>
  13. <img src="" width="48" class="author-img">
  14. </div>
  15. <div>
  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 img.author-img {
  127. border-radius: 4px;
  128. width: 48px;
  129. margin-right: 4px;
  130. }
  131. .reply-context .author {
  132. color: #777;
  133. font-weight: bold;
  134. font-size: 0.9em;
  135. }
  136. .reply-context .author .url {
  137. color: #aaa;
  138. }
  139. .reply-context img.post-img {
  140. float: right;
  141. width: 200px;
  142. }
  143. </style>
  144. <script>
  145. function saveNoteState() {
  146. var state = {
  147. content: $("#note_content").val(),
  148. inReplyTo: $("#note_in_reply_to").val(),
  149. category: $("#note_category").val(),
  150. slug: $("#note_slug").val(),
  151. photo: $("#note_photo_url").val()
  152. };
  153. state.syndications = [];
  154. $("#syndication-container button.btn-info").each(function(i,btn){
  155. state.syndications[$(btn).data('syndicate-to')] = 'selected';
  156. });
  157. localforage.setItem('current-note', state);
  158. }
  159. function restoreNoteState() {
  160. localforage.getItem('current-note', function(err,note){
  161. if(note) {
  162. $("#note_content").val(note.content);
  163. $("#note_in_reply_to").val(note.inReplyTo);
  164. $("#note_category").val(note.category);
  165. $("#note_slug").val(note.slug);
  166. if(note.photo) {
  167. replacePhotoWithPhotoURL(note.photo);
  168. }
  169. if(note.inReplyTo) {
  170. expandReplySection();
  171. }
  172. $("#syndication-container button").each(function(i,btn){
  173. if($(btn).data('syndicate-to') in note.syndications) {
  174. $(btn).addClass('btn-info');
  175. }
  176. });
  177. $("#note_content").change();
  178. }
  179. });
  180. }
  181. function replacePhotoWithPhotoURL(url) {
  182. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  183. $(".note_photo_url").val(url);
  184. $("#note_photo").remove();
  185. $("#photo_preview").attr("src", url);
  186. $("#photo_preview_container").removeClass("hidden");
  187. $("#note_manual_photo").addClass("hidden");
  188. }
  189. function switchToManualPhotoURL() {
  190. $("#note_photo").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control">');
  191. $("#note_photo").remove();
  192. $("#note_photo_url").change(function(){
  193. $("#photo_preview").attr("src", $(this).val());
  194. $("#photo_preview_container").removeClass("hidden");
  195. });
  196. $("#note_manual_photo").addClass("hidden");
  197. $("#add_photo").removeClass("hidden");
  198. }
  199. function addPhotoURL() {
  200. $(".note_photo_url:last").after('<input type="url" name="note_photo_url[]" value="" class="note_photo_url form-control" style="margin-top:2px;">');
  201. if($(".note_photo_url").length == 4) {
  202. $("#add_photo").remove();
  203. }
  204. }
  205. function expandReplySection() {
  206. $("#expand-reply").click();
  207. $("#note_in_reply_to").change();
  208. }
  209. $(function(){
  210. var userHasSetCategory = false;
  211. var hasMediaEndpoint = <?= $this->media_endpoint ? 'true' : 'false' ?>;
  212. $("#note_content, #note_category, #note_in_reply_to, #note_slug, #note_photo_url").on('keyup change', function(e){
  213. saveNoteState();
  214. });
  215. $("#note_content").on('keyup', function(){
  216. var scrollHeight = document.getElementById("note_content").scrollHeight;
  217. var currentHeight = parseInt($("#note_content").css("height"));
  218. if(Math.abs(scrollHeight - currentHeight) > 20) {
  219. $("#note_content").css("height", (scrollHeight+30)+"px");
  220. }
  221. });
  222. $("#expand-reply").click(function(){
  223. $('.reply-section').removeClass('hidden');
  224. $(this).addClass('hidden');
  225. return false;
  226. });
  227. // Preview the photo when one is chosen
  228. $("#photo_preview_container").addClass("hidden");
  229. $("#note_photo").on("change", function(e){
  230. // If the user has a media endpoint, upload the photo to it right now
  231. if(hasMediaEndpoint) {
  232. // TODO: add loading state indicator here
  233. console.log("Uploading file to media endpoint...");
  234. var formData = new FormData();
  235. formData.append("null","null");
  236. formData.append("photo", e.target.files[0]);
  237. var request = new XMLHttpRequest();
  238. request.open("POST", "/micropub/media");
  239. request.onreadystatechange = function() {
  240. if(request.readyState == XMLHttpRequest.DONE) {
  241. try {
  242. var response = JSON.parse(request.responseText);
  243. if(response.location) {
  244. // Replace the file upload form with the URL
  245. replacePhotoWithPhotoURL(response.location);
  246. saveNoteState();
  247. } else {
  248. console.log("Endpoint did not return a location header", response);
  249. }
  250. } catch(e) {
  251. console.log(e);
  252. }
  253. }
  254. }
  255. request.send(formData);
  256. } else {
  257. $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
  258. $("#photo_preview_container").removeClass("hidden");
  259. }
  260. });
  261. $("#remove_photo").on("click", function(){
  262. $("#note_photo").val("");
  263. $(".note_photo_url").val("");
  264. $("#photo_preview").attr("src", "" );
  265. $("#photo_preview_container").addClass("hidden");
  266. saveNoteState();
  267. });
  268. $("#note_content").on('change keyup', function(e){
  269. var text = $("#note_content").val();
  270. var tweet_length = tw_text_proxy(text).length;
  271. var tweet_check = tw_length_check(text, 140, "<?= $this->user->twitter_username ?>");
  272. var remaining = 140 - tweet_length;
  273. $("#note_content_remaining span").html(remaining);
  274. $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
  275. $("#note_content_remaining").addClass("pcheck"+tweet_check);
  276. // If the user didn't enter any categories, add them from the post
  277. if(!userHasSetCategory) {
  278. var tags = $("#note_content").val().match(/#[a-z][a-z0-9]+/ig);
  279. if(tags) {
  280. $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
  281. }
  282. }
  283. });
  284. $("#note_in_reply_to").on('change', function(){
  285. var reply_to = $("#note_in_reply_to").val();
  286. if(reply_to == "") {
  287. $(".reply-section").addClass("hidden");
  288. $(".reply-context").addClass("hidden");
  289. $("#expand-reply").removeClass("hidden");
  290. return;
  291. }
  292. $(".reply-section .loading").removeClass("hidden");
  293. $.get("/reply/preview", {url:reply_to}, function(data){
  294. if(data.canonical_reply_url != reply_to) {
  295. $("#note_in_reply_to").val(data.canonical_reply_url);
  296. }
  297. var category = csv_to_array($("#note_category").val());
  298. for(var i in data.entry.category) {
  299. if($.inArray(data.entry.category[i], category) == -1) {
  300. category.push(data.entry.category[i]);
  301. }
  302. }
  303. $("#note_category").val(category.join(", "));
  304. if($("#note_content").val() == "" && data.mentions) {
  305. var mentions = '';
  306. for(var i in data.mentions) {
  307. mentions += '@'+data.mentions[i]+' ';
  308. }
  309. $("#note_content").val(mentions);
  310. }
  311. $(".reply-context .content").text(data.entry.content.text);
  312. if(data.entry.name) {
  313. $(".reply-context .post-name").text(data.entry.name).removeClass('hidden');
  314. } else {
  315. $(".reply-context .post-name").addClass('hidden');
  316. }
  317. if(data.entry.author) {
  318. $(".reply-context .author .name").text(data.entry.author.name);
  319. $(".reply-context .author .url").text(data.entry.author.url);
  320. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  321. }
  322. if(data.entry.photo) {
  323. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  324. } else {
  325. $(".reply-context img.post-img").addClass('hidden');
  326. }
  327. $(".reply-section .loading").addClass("hidden");
  328. $(".reply-context").removeClass("hidden");
  329. });
  330. });
  331. $("#note_category").on('keydown keyup', function(){
  332. userHasSetCategory = true;
  333. });
  334. $("#note_category").on('change', function(){
  335. if($("#note_category").val() == "") {
  336. userHasSetCategory = false;
  337. }
  338. });
  339. // When the reply URL is in the query string, or loads from localstorage, make sure
  340. // to run the event handlers to expand the reply section
  341. if($("#note_in_reply_to").val() != "") {
  342. expandReplySection();
  343. }
  344. $("#btn_post").click(function(){
  345. // Collect all the syndication buttons that are pressed
  346. var syndications = [];
  347. $("#syndication-container button.btn-info").each(function(i,btn){
  348. syndications.push($(btn).data('syndicate-to'));
  349. });
  350. var category = csv_to_array($("#note_category").val());
  351. var formData = new FormData();
  352. if(v=$("#note_content").val()) {
  353. formData.append("content", v);
  354. }
  355. if(v=$("#note_in_reply_to").val()) {
  356. formData.append("in-reply-to", v);
  357. }
  358. if(v=$("#note_location").val()) {
  359. formData.append("location", v);
  360. }
  361. if(category.length > 0) {
  362. for(var i in category) {
  363. formData.append("category[]", category[i]);
  364. }
  365. }
  366. if(syndications.length > 0) {
  367. for(var i in syndications) {
  368. formData.append("syndicate-to[]", syndications[i]);
  369. }
  370. }
  371. if(v=$("#note_slug").val()) {
  372. formData.append("<?= $this->user->micropub_slug_field ?>", v);
  373. }
  374. // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
  375. if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
  376. formData.append("photo", document.getElementById("note_photo").files[0]);
  377. } else if($(".note_photo_url").val()) {
  378. $(".note_photo_url").each(function(){
  379. if($(this).val()) {
  380. formData.append("photo[]", $(this).val());
  381. }
  382. });
  383. }
  384. // Need to append a placeholder field because if the file size max is hit, $_POST will
  385. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  386. // This will be stripped by Quill before it's sent to the Micropub endpoint
  387. formData.append("null","null");
  388. var request = new XMLHttpRequest();
  389. request.open("POST", "/micropub/multipart");
  390. request.onreadystatechange = function() {
  391. if(request.readyState == XMLHttpRequest.DONE) {
  392. // console.log(request.responseText);
  393. try {
  394. var response = JSON.parse(request.responseText);
  395. localforage.removeItem('current-note');
  396. if(response.location) {
  397. window.location = response.location;
  398. // console.log(response.location);
  399. } else {
  400. $("#test_response").html(response.response).removeClass('hidden');
  401. $("#test_success").addClass('hidden');
  402. $("#test_error").removeClass('hidden');
  403. }
  404. } catch(e) {
  405. $("#test_success").addClass('hidden');
  406. $("#test_error").removeClass('hidden');
  407. }
  408. $("#btn_post").removeClass("loading disabled").text("Post");
  409. }
  410. }
  411. $("#btn_post").addClass("loading disabled").text("Working...");
  412. request.send(formData);
  413. /*
  414. $.post("/micropub/multipart", {
  415. content: $("#note_content").val(),
  416. 'in-reply-to': $("#note_in_reply_to").val(),
  417. location: $("#note_location").val(),
  418. category: category,
  419. slug: $("#note_slug").val(),
  420. 'syndicate-to': syndications
  421. }, function(data){
  422. var response = JSON.parse(data);
  423. if(response.location != false) {
  424. $("#note_form").slideUp(200, function(){
  425. $(window).scrollTop($("#test_success").position().top);
  426. });
  427. $("#test_success").removeClass('hidden');
  428. $("#test_error").addClass('hidden');
  429. $("#post_href").attr("href", response.location);
  430. $("#note_content").val("");
  431. $("#note_in_reply_to").val("");
  432. $("#note_category").val("");
  433. $("#note_slug").val("");
  434. } else {
  435. $("#test_success").addClass('hidden');
  436. $("#test_error").removeClass('hidden');
  437. }
  438. $("#last_response_date").html("(just now)");
  439. $("#test_request").html(response.request);
  440. $("#last_request_container").show();
  441. $("#test_response").html(response.response);
  442. });
  443. */
  444. return false;
  445. });
  446. function location_error(msg) {
  447. $("#note_location_msg").val(msg);
  448. $("#note_location_chk").removeAttr("checked");
  449. $("#note_location_loading").hide();
  450. $("#note_location_img").hide();
  451. $("#note_location_msg").removeClass("img-visible");
  452. }
  453. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  454. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  455. function fetch_location() {
  456. $("#note_location_loading").show();
  457. navigator.geolocation.getCurrentPosition(function(position){
  458. $("#note_location_loading").hide();
  459. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  460. $("#note_location_msg").val(geo);
  461. $("#note_location").val(geo);
  462. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  463. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  464. $("#note_location_img").show();
  465. $("#note_location_msg").addClass("img-visible");
  466. }, function(err){
  467. if(err.code == 1) {
  468. location_error("The website was not able to get permission");
  469. } else if(err.code == 2) {
  470. location_error("Location information was unavailable");
  471. } else if(err.code == 3) {
  472. location_error("Timed out getting location");
  473. }
  474. });
  475. }
  476. $("#note_location_chk").click(function(){
  477. if($(this).attr("checked") == "checked") {
  478. if(navigator.geolocation) {
  479. $.post("/prefs", {
  480. enabled: 1
  481. });
  482. fetch_location();
  483. } else {
  484. location_error("Browser location is not supported");
  485. }
  486. } else {
  487. $("#note_location_img").hide();
  488. $("#note_location_msg").removeClass("img-visible");
  489. $("#note_location_msg").val('');
  490. $("#note_location").val('');
  491. $.post("/prefs", {
  492. enabled: 0
  493. });
  494. }
  495. });
  496. if($("#location_enabled").val() == 1) {
  497. $("#note_location_chk").attr("checked","checked");
  498. fetch_location();
  499. }
  500. bind_syndication_buttons();
  501. if($("#note_in_reply_to").val() != "") {
  502. $("#note_in_reply_to").change();
  503. } else {
  504. restoreNoteState();
  505. }
  506. });
  507. <?= partial('partials/syndication-js') ?>
  508. </script>