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.

580 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. $(".reply-context .author .name").text(data.entry.author.name);
  318. $(".reply-context .author .url").text(data.entry.author.url);
  319. $(".reply-context img.author-img").attr('src', data.entry.author.photo);
  320. if(data.entry.photo) {
  321. $(".reply-context img.post-img").attr('src', data.entry.photo[0]).removeClass('hidden');
  322. } else {
  323. $(".reply-context img.post-img").addClass('hidden');
  324. }
  325. $(".reply-section .loading").addClass("hidden");
  326. $(".reply-context").removeClass("hidden");
  327. });
  328. });
  329. $("#note_category").on('keydown keyup', function(){
  330. userHasSetCategory = true;
  331. });
  332. $("#note_category").on('change', function(){
  333. if($("#note_category").val() == "") {
  334. userHasSetCategory = false;
  335. }
  336. });
  337. // When the reply URL is in the query string, or loads from localstorage, make sure
  338. // to run the event handlers to expand the reply section
  339. if($("#note_in_reply_to").val() != "") {
  340. expandReplySection();
  341. }
  342. $("#btn_post").click(function(){
  343. // Collect all the syndication buttons that are pressed
  344. var syndications = [];
  345. $("#syndication-container button.btn-info").each(function(i,btn){
  346. syndications.push($(btn).data('syndicate-to'));
  347. });
  348. var category = csv_to_array($("#note_category").val());
  349. var formData = new FormData();
  350. if(v=$("#note_content").val()) {
  351. formData.append("content", v);
  352. }
  353. if(v=$("#note_in_reply_to").val()) {
  354. formData.append("in-reply-to", v);
  355. }
  356. if(v=$("#note_location").val()) {
  357. formData.append("location", v);
  358. }
  359. if(category.length > 0) {
  360. for(var i in category) {
  361. formData.append("category[]", category[i]);
  362. }
  363. }
  364. if(syndications.length > 0) {
  365. for(var i in syndications) {
  366. formData.append("syndicate-to[]", syndications[i]);
  367. }
  368. }
  369. if(v=$("#note_slug").val()) {
  370. formData.append("slug", v);
  371. }
  372. // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
  373. if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
  374. formData.append("photo", document.getElementById("note_photo").files[0]);
  375. } else if($(".note_photo_url").val()) {
  376. $(".note_photo_url").each(function(){
  377. if($(this).val()) {
  378. formData.append("photo[]", $(this).val());
  379. }
  380. });
  381. }
  382. // Need to append a placeholder field because if the file size max is hit, $_POST will
  383. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  384. // This will be stripped by Quill before it's sent to the Micropub endpoint
  385. formData.append("null","null");
  386. var request = new XMLHttpRequest();
  387. request.open("POST", "/micropub/multipart");
  388. request.onreadystatechange = function() {
  389. if(request.readyState == XMLHttpRequest.DONE) {
  390. // console.log(request.responseText);
  391. try {
  392. var response = JSON.parse(request.responseText);
  393. localforage.removeItem('current-note');
  394. if(response.location) {
  395. window.location = response.location;
  396. // console.log(response.location);
  397. } else {
  398. $("#test_response").html(response.response).removeClass('hidden');
  399. $("#test_success").addClass('hidden');
  400. $("#test_error").removeClass('hidden');
  401. }
  402. } catch(e) {
  403. $("#test_success").addClass('hidden');
  404. $("#test_error").removeClass('hidden');
  405. }
  406. $("#btn_post").removeClass("loading disabled").text("Post");
  407. }
  408. }
  409. $("#btn_post").addClass("loading disabled").text("Working...");
  410. request.send(formData);
  411. /*
  412. $.post("/micropub/multipart", {
  413. content: $("#note_content").val(),
  414. 'in-reply-to': $("#note_in_reply_to").val(),
  415. location: $("#note_location").val(),
  416. category: category,
  417. slug: $("#note_slug").val(),
  418. 'syndicate-to': syndications
  419. }, function(data){
  420. var response = JSON.parse(data);
  421. if(response.location != false) {
  422. $("#note_form").slideUp(200, function(){
  423. $(window).scrollTop($("#test_success").position().top);
  424. });
  425. $("#test_success").removeClass('hidden');
  426. $("#test_error").addClass('hidden');
  427. $("#post_href").attr("href", response.location);
  428. $("#note_content").val("");
  429. $("#note_in_reply_to").val("");
  430. $("#note_category").val("");
  431. $("#note_slug").val("");
  432. } else {
  433. $("#test_success").addClass('hidden');
  434. $("#test_error").removeClass('hidden');
  435. }
  436. $("#last_response_date").html("(just now)");
  437. $("#test_request").html(response.request);
  438. $("#last_request_container").show();
  439. $("#test_response").html(response.response);
  440. });
  441. */
  442. return false;
  443. });
  444. function location_error(msg) {
  445. $("#note_location_msg").val(msg);
  446. $("#note_location_chk").removeAttr("checked");
  447. $("#note_location_loading").hide();
  448. $("#note_location_img").hide();
  449. $("#note_location_msg").removeClass("img-visible");
  450. }
  451. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  452. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  453. function fetch_location() {
  454. $("#note_location_loading").show();
  455. navigator.geolocation.getCurrentPosition(function(position){
  456. $("#note_location_loading").hide();
  457. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  458. $("#note_location_msg").val(geo);
  459. $("#note_location").val(geo);
  460. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  461. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  462. $("#note_location_img").show();
  463. $("#note_location_msg").addClass("img-visible");
  464. }, function(err){
  465. if(err.code == 1) {
  466. location_error("The website was not able to get permission");
  467. } else if(err.code == 2) {
  468. location_error("Location information was unavailable");
  469. } else if(err.code == 3) {
  470. location_error("Timed out getting location");
  471. }
  472. });
  473. }
  474. $("#note_location_chk").click(function(){
  475. if($(this).attr("checked") == "checked") {
  476. if(navigator.geolocation) {
  477. $.post("/prefs", {
  478. enabled: 1
  479. });
  480. fetch_location();
  481. } else {
  482. location_error("Browser location is not supported");
  483. }
  484. } else {
  485. $("#note_location_img").hide();
  486. $("#note_location_msg").removeClass("img-visible");
  487. $("#note_location_msg").val('');
  488. $("#note_location").val('');
  489. $.post("/prefs", {
  490. enabled: 0
  491. });
  492. }
  493. });
  494. if($("#location_enabled").val() == 1) {
  495. $("#note_location_chk").attr("checked","checked");
  496. fetch_location();
  497. }
  498. bind_syndication_buttons();
  499. if($("#note_in_reply_to").val() != "") {
  500. $("#note_in_reply_to").change();
  501. } else {
  502. restoreNoteState();
  503. }
  504. });
  505. <?= partial('partials/syndication-js') ?>
  506. </script>