Mirror of Quill
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.

369 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']) ? 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. <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. $("#btn_post").click(function(){
  161. // Collect all the syndication buttons that are pressed
  162. var syndications = [];
  163. $("#syndication-container button.btn-info").each(function(i,btn){
  164. syndications.push($(btn).data('syndicate-to'));
  165. });
  166. var category = csv_to_array($("#note_category").val());
  167. var formData = new FormData();
  168. if(v=$("#note_content").val()) {
  169. formData.append("content", v);
  170. }
  171. if(v=$("#note_in_reply_to").val()) {
  172. formData.append("in-reply-to", v);
  173. }
  174. if(v=$("#note_location").val()) {
  175. formData.append("location", v);
  176. }
  177. if(category.length > 0) {
  178. for(var i in category) {
  179. formData.append("category[]", category[i]);
  180. }
  181. }
  182. if(syndications.length > 0) {
  183. for(var i in syndications) {
  184. formData.append("syndicate-to[]", syndications[i]);
  185. }
  186. }
  187. if(v=$("#note_slug").val()) {
  188. formData.append("slug", v);
  189. }
  190. if(document.getElementById("note_photo").files[0]) {
  191. formData.append("photo", document.getElementById("note_photo").files[0]);
  192. }
  193. // Need to append a placeholder field because if the file size max is hit, $_POST will
  194. // be empty, so the server needs to be able to recognize a post with only a file vs a failed one.
  195. // This will be stripped by Quill before it's sent to the Micropub endpoint
  196. formData.append("null","null");
  197. var request = new XMLHttpRequest();
  198. request.open("POST", "/micropub/multipart");
  199. request.onreadystatechange = function() {
  200. if(request.readyState == XMLHttpRequest.DONE) {
  201. console.log(request.responseText);
  202. try {
  203. var response = JSON.parse(request.responseText);
  204. if(response.location) {
  205. window.location = response.location;
  206. // console.log(response.location);
  207. } else {
  208. $("#test_response").html(response.response).removeClass('hidden');
  209. $("#test_success").addClass('hidden');
  210. $("#test_error").removeClass('hidden');
  211. }
  212. } catch(e) {
  213. $("#test_success").addClass('hidden');
  214. $("#test_error").removeClass('hidden');
  215. }
  216. $("#btn_post").removeClass("loading disabled").text("Post");
  217. }
  218. }
  219. $("#btn_post").addClass("loading disabled").text("Working...");
  220. request.send(formData);
  221. /*
  222. $.post("/micropub/multipart", {
  223. content: $("#note_content").val(),
  224. 'in-reply-to': $("#note_in_reply_to").val(),
  225. location: $("#note_location").val(),
  226. category: category,
  227. slug: $("#note_slug").val(),
  228. 'syndicate-to': syndications
  229. }, function(data){
  230. var response = JSON.parse(data);
  231. if(response.location != false) {
  232. $("#note_form").slideUp(200, function(){
  233. $(window).scrollTop($("#test_success").position().top);
  234. });
  235. $("#test_success").removeClass('hidden');
  236. $("#test_error").addClass('hidden');
  237. $("#post_href").attr("href", response.location);
  238. $("#note_content").val("");
  239. $("#note_in_reply_to").val("");
  240. $("#note_category").val("");
  241. $("#note_slug").val("");
  242. } else {
  243. $("#test_success").addClass('hidden');
  244. $("#test_error").removeClass('hidden');
  245. }
  246. $("#last_response_date").html("(just now)");
  247. $("#test_request").html(response.request);
  248. $("#last_request_container").show();
  249. $("#test_response").html(response.response);
  250. });
  251. */
  252. return false;
  253. });
  254. function location_error(msg) {
  255. $("#note_location_msg").val(msg);
  256. $("#note_location_chk").removeAttr("checked");
  257. $("#note_location_loading").hide();
  258. $("#note_location_img").hide();
  259. $("#note_location_msg").removeClass("img-visible");
  260. }
  261. var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
  262. var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
  263. function fetch_location() {
  264. $("#note_location_loading").show();
  265. navigator.geolocation.getCurrentPosition(function(position){
  266. $("#note_location_loading").hide();
  267. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  268. $("#note_location_msg").val(geo);
  269. $("#note_location").val(geo);
  270. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  271. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  272. $("#note_location_img").show();
  273. $("#note_location_msg").addClass("img-visible");
  274. }, function(err){
  275. if(err.code == 1) {
  276. location_error("The website was not able to get permission");
  277. } else if(err.code == 2) {
  278. location_error("Location information was unavailable");
  279. } else if(err.code == 3) {
  280. location_error("Timed out getting location");
  281. }
  282. });
  283. }
  284. $("#note_location_chk").click(function(){
  285. if($(this).attr("checked") == "checked") {
  286. if(navigator.geolocation) {
  287. $.post("/prefs", {
  288. enabled: 1
  289. });
  290. fetch_location();
  291. } else {
  292. location_error("Browser location is not supported");
  293. }
  294. } else {
  295. $("#note_location_img").hide();
  296. $("#note_location_msg").removeClass("img-visible");
  297. $("#note_location_msg").val('');
  298. $("#note_location").val('');
  299. $.post("/prefs", {
  300. enabled: 0
  301. });
  302. }
  303. });
  304. if($("#location_enabled").val() == 1) {
  305. $("#note_location_chk").attr("checked","checked");
  306. fetch_location();
  307. }
  308. bind_syndication_buttons();
  309. });
  310. <?= partial('partials/syndication-js') ?>
  311. </script>