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.

310 lines
11 KiB

9 years ago
  1. <div class="narrow">
  2. <?= partial('partials/header') ?>
  3. <form role="form" style="margin-top: 20px;" id="note_form" action="/post" method="post">
  4. <div class="form-group">
  5. <h3>Date</h3>
  6. <input type="date" class="form-control" style="max-width:160px; float:left; margin-right: 4px;" id="note_date" name="note_date" value="">
  7. <input type="text" class="form-control" style="max-width:85px; float:left; margin-right: 4px;" id="note_time" name="note_time" value="">
  8. <input type="text" class="form-control" style="max-width:75px;" id="note_tzoffset" name="note_tzoffset" value="">
  9. </div>
  10. <?php if($this->micropub_media_endpoint): ?>
  11. <div class="form-group">
  12. <h3>Photo</h3>
  13. <input type="file" name="note_photo_upload" id="note_photo_upload" accept="image/*">
  14. <div id="note_photo_loading" class="hidden">
  15. <img src="/images/spinner.gif"> Uploading...
  16. </div>
  17. <div id="photo_preview_container" class="hidden">
  18. <input type="text" name="note_photo" id="note_photo" class="form-control" readonly="readonly"><br>
  19. <img src="" id="photo_preview" style="max-width: 300px; max-height: 300px;">
  20. <div>
  21. <button type="button" class="btn btn-danger btn-sm" id="remove_photo"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Remove image</button>
  22. </div>
  23. </div>
  24. </div>
  25. <?php endif; ?>
  26. <div id="entry-buttons">
  27. </div>
  28. <div class="form-group">
  29. <h3>Location <input type="checkbox" id="note_location_chk" value=""><img src="/images/spinner.gif" id="note_location_loading" style="display: none;"></h3>
  30. <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  31. <input type="hidden" id="note_location" name="location">
  32. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  33. <div id="note_location_img" style="display: none;">
  34. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  35. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  36. </div>
  37. </div>
  38. </form>
  39. <?php if($this->micropub_endpoint): ?>
  40. <div class="scroll-container">
  41. <div class="callout">
  42. <p>Clicking an item will post this note to your Micropub endpoint. Below is some information about the request that will be made.</p>
  43. <table class="table table-condensed">
  44. <tr>
  45. <td>me</td>
  46. <td class="break"><code><?= session('me') ?></code> (should be your website)</td>
  47. </tr>
  48. <tr>
  49. <td>scope</td>
  50. <td class="break"><code><?= $this->token_scope ?></code></td>
  51. </tr>
  52. <tr>
  53. <td>micropub endpoint</td>
  54. <td class="break"><code><?= $this->micropub_endpoint ?></code></td>
  55. </tr>
  56. <tr>
  57. <td>access token</td>
  58. <td class="break">String of length <b><?= strlen($this->access_token) ?></b><?= (strlen($this->access_token) > 0) ? (', ending in <code>' . substr($this->access_token, -7) . '</code>') : '' ?> (should be greater than length 0)</td>
  59. </tr>
  60. <?php if($this->enable_array_micropub): ?>
  61. <tr>
  62. <td>ate or drank</td>
  63. <td class="break">
  64. <p>A property named <code>ate</code> or <code>drank</code> will include an <code>h-food</code> object corresponding to the item you tapped.</p>
  65. <pre>{"ate":[{"type":["h-food"],"name":["Coffee"]}]}</pre>
  66. </td>
  67. </tr>
  68. <?php else: ?>
  69. <tr>
  70. <td>h-food</td>
  71. <td class="break"><form action="/prefs/enable-h-food" method="post"><input type="submit" class="btn btn-default" value="Switch to new h-food format"></form></td>
  72. </tr>
  73. <tr>
  74. <td>p3k-food</td>
  75. <td class="break">The button you tap (or your custom text) will be sent to your Micropub endpoint in a field named <code>p3k-food</code></td>
  76. </tr>
  77. <tr>
  78. <td>p3k-type</td>
  79. <td class="break">Will be either <code>drink</code> or <code>eat</code> depending on the type of entry</td>
  80. </tr>
  81. <?php endif; ?>
  82. </table>
  83. </div>
  84. </div>
  85. <?php endif; ?>
  86. </div>
  87. <script>
  88. $(function(){
  89. function tz_seconds_to_offset(seconds) {
  90. var tz_offset = '';
  91. var hours = zero_pad(Math.floor(Math.abs(seconds / 60 / 60)));
  92. var minutes = zero_pad(Math.floor(seconds / 60) % 60);
  93. return (seconds < 0 ? '-' : '+') + hours + ":" + minutes;
  94. }
  95. function zero_pad(num) {
  96. num = "" + num;
  97. if(num.length == 1) {
  98. num = "0" + num;
  99. }
  100. return num;
  101. }
  102. function replacePhotoWithPhotoURL(url) {
  103. $("#note_photo").val(url);
  104. $("#note_photo_upload").addClass("hidden");
  105. $("#note_photo_loading").addClass("hidden");
  106. $("#photo_preview").attr("src", url);
  107. $("#photo_preview_container").removeClass("hidden");
  108. }
  109. function bind_keyboard_shortcuts() {
  110. $(".text-custom-eat").keydown(function(e){
  111. if(e.keyCode == 13) {
  112. $(".btn-custom-eat").click();
  113. return false;
  114. }
  115. });
  116. $(".text-custom-drink").keydown(function(e){
  117. if(e.keyCode == 13) {
  118. $(".btn-custom-drink").click();
  119. return false;
  120. }
  121. });
  122. }
  123. function location_error(msg) {
  124. $("#note_location_msg").val(msg);
  125. // $("#note_location_chk").removeAttr("checked");
  126. $("#note_location_loading").hide();
  127. $("#note_location_img").hide();
  128. $("#note_location_msg").removeClass("img-visible");
  129. }
  130. var map_template_wide = "<?= build_static_map_url('{lat}', '{lng}', 180, 700, 15) ?>";
  131. var map_template_small = "<?= build_static_map_url('{lat}', '{lng}', 320, 480, 15) ?>";
  132. function fetch_location() {
  133. $("#note_location_loading").show();
  134. navigator.geolocation.getCurrentPosition(function(position){
  135. load_entry_buttons(position.coords);
  136. $("#note_location_loading").hide();
  137. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  138. $("#note_location_msg").val(geo);
  139. $("#note_location").val(geo);
  140. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  141. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  142. $("#note_location_img").show();
  143. $("#note_location_msg").addClass("img-visible");
  144. }, function(err){
  145. if(err.code == 1) {
  146. location_error("The website was not able to get permission");
  147. } else if(err.code == 2) {
  148. location_error("Location information was unavailable");
  149. } else if(err.code == 3) {
  150. location_error("Timed out getting location");
  151. }
  152. // Load the entry buttons with no location context
  153. load_entry_buttons();
  154. });
  155. }
  156. function set_location_enabled(enabled) {
  157. localforage.setItem('location-enabled', {enabled: enabled});
  158. }
  159. function get_location_enabled(callback) {
  160. localforage.getItem('location-enabled', function(err,val){
  161. if(val) {
  162. callback(val.enabled);
  163. } else {
  164. callback(false);
  165. }
  166. });
  167. }
  168. $("#note_location_chk").click(function(){
  169. if($(this).attr("checked") == "checked") {
  170. if(navigator.geolocation) {
  171. set_location_enabled(true);
  172. fetch_location(); // will load the entry buttons even if location fails
  173. } else {
  174. set_location_enabled(false);
  175. location_error("Browser location is not supported");
  176. }
  177. } else {
  178. $("#note_location_img").hide();
  179. $("#note_location_msg").removeClass("img-visible");
  180. $("#note_location_msg").val('');
  181. $("#note_location").val('');
  182. set_location_enabled(false);
  183. // Load the buttons now
  184. // This is for when the browser is taking too long to find location,
  185. // the user might un-check the box
  186. load_entry_buttons();
  187. }
  188. });
  189. // This loads the buttons with or without location
  190. function load_entry_buttons(coords) {
  191. var latitude = coords ? coords.latitude : '';
  192. var longitude = coords ? coords.longitude : '';
  193. $.getJSON('/options.json', {
  194. latitude: latitude,
  195. longitude: longitude
  196. }, function(response) {
  197. $("#entry-buttons").html(response.buttons);
  198. bind_keyboard_shortcuts();
  199. });
  200. }
  201. $("#photo_preview_container").addClass("hidden");
  202. $("#note_photo_upload").on("change", function(e){
  203. $("#note_photo_upload").addClass("hidden");
  204. $("#note_photo_loading").removeClass("hidden");
  205. var formData = new FormData();
  206. formData.append("null","null");
  207. formData.append("file", e.target.files[0]);
  208. var request = new XMLHttpRequest();
  209. request.open("POST", "/micropub/media");
  210. request.onreadystatechange = function() {
  211. if(request.readyState == XMLHttpRequest.DONE) {
  212. try {
  213. var response = JSON.parse(request.responseText);
  214. console.log(response);
  215. if(response.location) {
  216. // Replace the file upload form with the URL
  217. replacePhotoWithPhotoURL(response.location);
  218. } else {
  219. console.log("Endpoint did not return a location header", response);
  220. $("#remove_photo").click();
  221. $("#note_photo_loading").addClass("hidden");
  222. }
  223. } catch(e) {
  224. $("#remove_photo").click();
  225. $("#note_photo_loading").addClass("hidden");
  226. console.log(e);
  227. }
  228. }
  229. }
  230. request.send(formData);
  231. });
  232. $("#remove_photo").on("click", function(){
  233. $("#note_photo").val("");
  234. $("#note_photo_upload").removeClass("hidden").val("");
  235. $("#photo_preview").attr("src", "" );
  236. $("#photo_preview_container").addClass("hidden");
  237. });
  238. ///////////////////////////////////////////////////////////////
  239. // App Start
  240. // Set the date from JS
  241. var d = new Date();
  242. $("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
  243. $("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
  244. $("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));
  245. // Check if location is enabled in the localstorage prefs
  246. get_location_enabled(function(enabled){
  247. if(enabled) {
  248. // If location is enabled, fetch location and load the entry buttons
  249. fetch_location(); // will load the buttons even if location fails
  250. $("#note_location_chk").attr("checked","checked");
  251. } else {
  252. // If location is not enabled, fetch prefs immediately
  253. $("#note_location_chk").removeAttr("checked");
  254. load_entry_buttons();
  255. }
  256. });
  257. });
  258. </script>
  259. <style type="text/css">
  260. .scroll-container {
  261. width: 100%;
  262. overflow-x: scroll;
  263. }
  264. .scroll-container .break {
  265. word-break: break-all;
  266. }
  267. </style>