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.

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