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.

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