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.

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