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.

487 lines
18 KiB

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