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.

376 lines
14 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']) ? $syn['uid'] : $syn['target']).'" class="btn btn-default btn-block">'
  41. . ($syn['favicon'] ? '<img src="'.$syn['favicon'].'" width="16" height="16"> ' : '')
  42. . $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. <tr>
  94. <td>access token</td>
  95. <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>
  96. </tr>
  97. </table>
  98. </div>
  99. <hr>
  100. <div style="text-align: right;">
  101. <a href="/add-to-home?start">Add to Home Screen</a>
  102. </div>
  103. </div>
  104. <style type="text/css">
  105. #note_content_remaining {
  106. float: right;
  107. font-size: 0.8em;
  108. font-weight: bold;
  109. }
  110. .pcheck206 { color: #6ba15c; } /* tweet fits within the limit even after adding RT @username */
  111. .pcheck207 { color: #c4b404; } /* danger zone, tweet will overflow when RT @username is added */
  112. .pcheck200,.pcheck208 { color: #59cb3a; } /* exactly fits 140 chars, both with or without RT */
  113. .pcheck413 { color: #a73b3b; } /* over max tweet length */
  114. </style>
  115. <script>
  116. $(function(){
  117. var userHasSetCategory = false;
  118. $("#photo_preview_container").addClass("hidden");
  119. $("#note_photo").on("change", function(e){
  120. $("#photo_preview_container").removeClass("hidden");
  121. $("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
  122. });
  123. $("#remove_photo").on("click", function(){
  124. $("#note_photo").val("");
  125. $("#photo_preview").attr("src", "" );
  126. $("#photo_preview_container").addClass("hidden");
  127. });
  128. $("#note_content").on('change keyup', function(e){
  129. var text = $("#note_content").val();
  130. var tweet_length = tw_text_proxy(text).length;
  131. var tweet_check = tw_length_check(text, 140, "<?= $this->user->twitter_username ?>");
  132. var remaining = 140 - tweet_length;
  133. $("#note_content_remaining span").html(remaining);
  134. $("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
  135. $("#note_content_remaining").addClass("pcheck"+tweet_check);
  136. // If the user didn't enter any categories, add them from the post
  137. if(!userHasSetCategory) {
  138. var tags = $("#note_content").val().match(/#[a-z0-9]+/g);
  139. if(tags) {
  140. $("#note_category").val(tags.map(function(tag){ return tag.replace('#',''); }).join(", "));
  141. }
  142. }
  143. });
  144. $("#note_in_reply_to").on('change', function(){
  145. if(match=$("#note_in_reply_to").val().match(/twitter\.com\/([^\/]+)\/status/)) {
  146. $("#note_content").val( "@"+match[1]+" "+$("#note_content").val() );
  147. }
  148. });
  149. $("#note_category").on('keydown keyup', function(){
  150. userHasSetCategory = true;
  151. });
  152. $("#note_category").on('change', function(){
  153. if($("#note_category").val() == "") {
  154. userHasSetCategory = false;
  155. }
  156. });
  157. if($("#note_in_reply_to").val() != "") {
  158. $("#note_in_reply_to").change();
  159. }
  160. // ctrl-s to save
  161. $(window).on('keydown', function(e){
  162. if(e.keyCode == 83 && e.ctrlKey){
  163. $("#btn_post").click();
  164. }
  165. });
  166. $("#btn_post").click(function(){
  167. // Collect all the syndication buttons that are pressed
  168. var syndications = [];
  169. $("#syndication-container button.btn-info").each(function(i,btn){
  170. syndications.push($(btn).data('syndicate-to'));
  171. });
  172. var category = csv_to_array($("#note_category").val());
  173. var formData = new FormData();
  174. if(v=$("#note_content").val()) {
  175. formData.append("content", v);
  176. }
  177. if(v=$("#note_in_reply_to").val()) {
  178. formData.append("in-reply-to", v);
  179. }
  180. if(v=$("#note_location").val()) {
  181. formData.append("location", v);
  182. }
  183. if(category.length > 0) {
  184. for(var i in category) {
  185. formData.append("category[]", category[i]);
  186. }
  187. }
  188. if(syndications.length > 0) {
  189. for(var i in syndications) {
  190. formData.append("syndicate-to[]", syndications[i]);
  191. }
  192. }
  193. if(v=$("#note_slug").val()) {
  194. formData.append("slug", v);
  195. }
  196. if(document.getElementById("note_photo").files[0]) {
  197. formData.append("photo", document.getElementById("note_photo").files[0]);
  198. }
  199. // Need to append a placeholder field because if the file size max is hit, $_POST will
  200. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  201. // This will be stripped by Quill before it's sent to the Micropub endpoint
  202. formData.append("null","null");
  203. var request = new XMLHttpRequest();
  204. request.open("POST", "/micropub/multipart");
  205. request.onreadystatechange = function() {
  206. if(request.readyState == XMLHttpRequest.DONE) {
  207. console.log(request.responseText);
  208. try {
  209. var response = JSON.parse(request.responseText);
  210. if(response.location) {
  211. window.location = response.location;
  212. // console.log(response.location);
  213. } else {
  214. $("#test_response").html(response.response).removeClass('hidden');
  215. $("#test_success").addClass('hidden');
  216. $("#test_error").removeClass('hidden');
  217. }
  218. } catch(e) {
  219. $("#test_success").addClass('hidden');
  220. $("#test_error").removeClass('hidden');
  221. }
  222. $("#btn_post").removeClass("loading disabled").text("Post");
  223. }
  224. }
  225. $("#btn_post").addClass("loading disabled").text("Working...");
  226. request.send(formData);
  227. /*
  228. $.post("/micropub/multipart", {
  229. content: $("#note_content").val(),
  230. 'in-reply-to': $("#note_in_reply_to").val(),
  231. location: $("#note_location").val(),
  232. category: category,
  233. slug: $("#note_slug").val(),
  234. 'syndicate-to': syndications
  235. }, function(data){
  236. var response = JSON.parse(data);
  237. if(response.location != false) {
  238. $("#note_form").slideUp(200, function(){
  239. $(window).scrollTop($("#test_success").position().top);
  240. });
  241. $("#test_success").removeClass('hidden');
  242. $("#test_error").addClass('hidden');
  243. $("#post_href").attr("href", response.location);
  244. $("#note_content").val("");
  245. $("#note_in_reply_to").val("");
  246. $("#note_category").val("");
  247. $("#note_slug").val("");
  248. } else {
  249. $("#test_success").addClass('hidden');
  250. $("#test_error").removeClass('hidden');
  251. }
  252. $("#last_response_date").html("(just now)");
  253. $("#test_request").html(response.request);
  254. $("#last_request_container").show();
  255. $("#test_response").html(response.response);
  256. });
  257. */
  258. return false;
  259. });
  260. function location_error(msg) {
  261. $("#note_location_msg").val(msg);
  262. $("#note_location_chk").removeAttr("checked");
  263. $("#note_location_loading").hide();
  264. $("#note_location_img").hide();
  265. $("#note_location_msg").removeClass("img-visible");
  266. }
  267. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  268. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  269. function fetch_location() {
  270. $("#note_location_loading").show();
  271. navigator.geolocation.getCurrentPosition(function(position){
  272. $("#note_location_loading").hide();
  273. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  274. $("#note_location_msg").val(geo);
  275. $("#note_location").val(geo);
  276. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  277. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  278. $("#note_location_img").show();
  279. $("#note_location_msg").addClass("img-visible");
  280. }, function(err){
  281. if(err.code == 1) {
  282. location_error("The website was not able to get permission");
  283. } else if(err.code == 2) {
  284. location_error("Location information was unavailable");
  285. } else if(err.code == 3) {
  286. location_error("Timed out getting location");
  287. }
  288. });
  289. }
  290. $("#note_location_chk").click(function(){
  291. if($(this).attr("checked") == "checked") {
  292. if(navigator.geolocation) {
  293. $.post("/prefs", {
  294. enabled: 1
  295. });
  296. fetch_location();
  297. } else {
  298. location_error("Browser location is not supported");
  299. }
  300. } else {
  301. $("#note_location_img").hide();
  302. $("#note_location_msg").removeClass("img-visible");
  303. $("#note_location_msg").val('');
  304. $("#note_location").val('');
  305. $.post("/prefs", {
  306. enabled: 0
  307. });
  308. }
  309. });
  310. if($("#location_enabled").val() == 1) {
  311. $("#note_location_chk").attr("checked","checked");
  312. fetch_location();
  313. }
  314. bind_syndication_buttons();
  315. });
  316. <?= partial('partials/syndication-js') ?>
  317. </script>