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.

460 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. if($("#note_in_reply_to").val() != "") {
  238. $("#note_in_reply_to").change();
  239. }
  240. $("#btn_post").click(function(){
  241. // Collect all the syndication buttons that are pressed
  242. var syndications = [];
  243. $("#syndication-container button.btn-info").each(function(i,btn){
  244. syndications.push($(btn).data('syndicate-to'));
  245. });
  246. var category = csv_to_array($("#note_category").val());
  247. var formData = new FormData();
  248. if(v=$("#note_content").val()) {
  249. formData.append("content", v);
  250. }
  251. if(v=$("#note_in_reply_to").val()) {
  252. formData.append("in-reply-to", v);
  253. }
  254. if(v=$("#note_location").val()) {
  255. formData.append("location", v);
  256. }
  257. if(category.length > 0) {
  258. for(var i in category) {
  259. formData.append("category[]", category[i]);
  260. }
  261. }
  262. if(syndications.length > 0) {
  263. for(var i in syndications) {
  264. formData.append("syndicate-to[]", syndications[i]);
  265. }
  266. }
  267. if(v=$("#note_slug").val()) {
  268. formData.append("slug", v);
  269. }
  270. // Add either the photo as a file, or the photo URL depending on whether the user has a media endpoint
  271. if(document.getElementById("note_photo") && document.getElementById("note_photo").files[0]) {
  272. formData.append("photo", document.getElementById("note_photo").files[0]);
  273. } else if($("#note_photo_url").val()) {
  274. formData.append("photo", $("#note_photo_url").val());
  275. }
  276. // Need to append a placeholder field because if the file size max is hit, $_POST will
  277. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  278. // This will be stripped by Quill before it's sent to the Micropub endpoint
  279. formData.append("null","null");
  280. var request = new XMLHttpRequest();
  281. request.open("POST", "/micropub/multipart");
  282. request.onreadystatechange = function() {
  283. if(request.readyState == XMLHttpRequest.DONE) {
  284. console.log(request.responseText);
  285. try {
  286. var response = JSON.parse(request.responseText);
  287. localforage.removeItem('current-note');
  288. if(response.location) {
  289. window.location = response.location;
  290. // console.log(response.location);
  291. } else {
  292. $("#test_response").html(response.response).removeClass('hidden');
  293. $("#test_success").addClass('hidden');
  294. $("#test_error").removeClass('hidden');
  295. }
  296. } catch(e) {
  297. $("#test_success").addClass('hidden');
  298. $("#test_error").removeClass('hidden');
  299. }
  300. $("#btn_post").removeClass("loading disabled").text("Post");
  301. }
  302. }
  303. $("#btn_post").addClass("loading disabled").text("Working...");
  304. request.send(formData);
  305. /*
  306. $.post("/micropub/multipart", {
  307. content: $("#note_content").val(),
  308. 'in-reply-to': $("#note_in_reply_to").val(),
  309. location: $("#note_location").val(),
  310. category: category,
  311. slug: $("#note_slug").val(),
  312. 'syndicate-to': syndications
  313. }, function(data){
  314. var response = JSON.parse(data);
  315. if(response.location != false) {
  316. $("#note_form").slideUp(200, function(){
  317. $(window).scrollTop($("#test_success").position().top);
  318. });
  319. $("#test_success").removeClass('hidden');
  320. $("#test_error").addClass('hidden');
  321. $("#post_href").attr("href", response.location);
  322. $("#note_content").val("");
  323. $("#note_in_reply_to").val("");
  324. $("#note_category").val("");
  325. $("#note_slug").val("");
  326. } else {
  327. $("#test_success").addClass('hidden');
  328. $("#test_error").removeClass('hidden');
  329. }
  330. $("#last_response_date").html("(just now)");
  331. $("#test_request").html(response.request);
  332. $("#last_request_container").show();
  333. $("#test_response").html(response.response);
  334. });
  335. */
  336. return false;
  337. });
  338. function location_error(msg) {
  339. $("#note_location_msg").val(msg);
  340. $("#note_location_chk").removeAttr("checked");
  341. $("#note_location_loading").hide();
  342. $("#note_location_img").hide();
  343. $("#note_location_msg").removeClass("img-visible");
  344. }
  345. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  346. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  347. function fetch_location() {
  348. $("#note_location_loading").show();
  349. navigator.geolocation.getCurrentPosition(function(position){
  350. $("#note_location_loading").hide();
  351. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  352. $("#note_location_msg").val(geo);
  353. $("#note_location").val(geo);
  354. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  355. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  356. $("#note_location_img").show();
  357. $("#note_location_msg").addClass("img-visible");
  358. }, function(err){
  359. if(err.code == 1) {
  360. location_error("The website was not able to get permission");
  361. } else if(err.code == 2) {
  362. location_error("Location information was unavailable");
  363. } else if(err.code == 3) {
  364. location_error("Timed out getting location");
  365. }
  366. });
  367. }
  368. $("#note_location_chk").click(function(){
  369. if($(this).attr("checked") == "checked") {
  370. if(navigator.geolocation) {
  371. $.post("/prefs", {
  372. enabled: 1
  373. });
  374. fetch_location();
  375. } else {
  376. location_error("Browser location is not supported");
  377. }
  378. } else {
  379. $("#note_location_img").hide();
  380. $("#note_location_msg").removeClass("img-visible");
  381. $("#note_location_msg").val('');
  382. $("#note_location").val('');
  383. $.post("/prefs", {
  384. enabled: 0
  385. });
  386. }
  387. });
  388. if($("#location_enabled").val() == 1) {
  389. $("#note_location_chk").attr("checked","checked");
  390. fetch_location();
  391. }
  392. bind_syndication_buttons();
  393. restoreNoteState();
  394. });
  395. <?= partial('partials/syndication-js') ?>
  396. </script>