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.

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