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.

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