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.

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