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.

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