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.

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