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.

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