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.

354 lines
13 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. <div class="hidden" id="note_venue">
  31. <input type="url" id="note_venue_url" name="venue_url" class="hidden">
  32. <ul class="entry-buttons">
  33. <li><input type="button" class="btn btn-default name"></li>
  34. </ul>
  35. </div>
  36. <div id="note_location_fields">
  37. <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  38. <input type="hidden" id="note_location" name="location">
  39. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  40. <div id="note_location_img" style="display: none;">
  41. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  42. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  43. </div>
  44. </div>
  45. </div>
  46. </form>
  47. <?php if($this->micropub_endpoint): ?>
  48. <div class="scroll-container">
  49. <div class="callout">
  50. <p>Clicking an item will post this note to your Micropub endpoint. Below is some information about the request that will be made.</p>
  51. <table class="table table-condensed">
  52. <tr>
  53. <td>me</td>
  54. <td class="break"><code><?= session('me') ?></code> (should be your website)</td>
  55. </tr>
  56. <tr>
  57. <td>scope</td>
  58. <td class="break"><code><?= $this->token_scope ?></code></td>
  59. </tr>
  60. <tr>
  61. <td>micropub endpoint</td>
  62. <td class="break"><code><?= $this->micropub_endpoint ?></code></td>
  63. </tr>
  64. <tr>
  65. <td>access token</td>
  66. <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>
  67. </tr>
  68. <?php if($this->enable_array_micropub): ?>
  69. <tr>
  70. <td>ate or drank</td>
  71. <td class="break">
  72. <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>
  73. <pre>{"ate":[{"type":["h-food"],"name":["Coffee"]}]}</pre>
  74. </td>
  75. </tr>
  76. <?php else: ?>
  77. <tr>
  78. <td>h-food</td>
  79. <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>
  80. </tr>
  81. <tr>
  82. <td>p3k-food</td>
  83. <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>
  84. </tr>
  85. <tr>
  86. <td>p3k-type</td>
  87. <td class="break">Will be either <code>drink</code> or <code>eat</code> depending on the type of entry</td>
  88. </tr>
  89. <?php endif; ?>
  90. </table>
  91. </div>
  92. </div>
  93. <?php endif; ?>
  94. </div>
  95. <script>
  96. $(function(){
  97. function tz_seconds_to_offset(seconds) {
  98. var tz_offset = '';
  99. var hours = zero_pad(Math.floor(Math.abs(seconds / 60 / 60)));
  100. var minutes = zero_pad(Math.floor(seconds / 60) % 60);
  101. return (seconds < 0 ? '-' : '+') + hours + ":" + minutes;
  102. }
  103. function zero_pad(num) {
  104. num = "" + num;
  105. if(num.length == 1) {
  106. num = "0" + num;
  107. }
  108. return num;
  109. }
  110. function replacePhotoWithPhotoURL(url) {
  111. $("#note_photo").val(url);
  112. $("#note_photo_upload").addClass("hidden");
  113. $("#note_photo_loading").addClass("hidden");
  114. $("#photo_preview").attr("src", url);
  115. $("#photo_preview_container").removeClass("hidden");
  116. }
  117. function bind_keyboard_shortcuts() {
  118. $(".text-custom-eat").keydown(function(e){
  119. if(e.keyCode == 13) {
  120. $(".btn-custom-eat").click();
  121. return false;
  122. }
  123. });
  124. $(".text-custom-drink").keydown(function(e){
  125. if(e.keyCode == 13) {
  126. $(".btn-custom-drink").click();
  127. return false;
  128. }
  129. });
  130. }
  131. function location_error(msg) {
  132. $("#note_location_msg").val(msg);
  133. // $("#note_location_chk").removeAttr("checked");
  134. $("#note_location_loading").hide();
  135. $("#note_location_img").hide();
  136. $("#note_location_msg").removeClass("img-visible");
  137. }
  138. var map_template_wide = "<?= build_static_map_url('{lat}', '{lng}', 180, 700, 15) ?>";
  139. var map_template_small = "<?= build_static_map_url('{lat}', '{lng}', 320, 480, 15) ?>";
  140. function fetch_location() {
  141. $("#note_location_loading").show();
  142. navigator.geolocation.getCurrentPosition(function(position){
  143. load_entry_buttons(position.coords);
  144. $("#note_location_loading").hide();
  145. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  146. $("#note_location_msg").val(geo);
  147. $("#note_location").val(geo);
  148. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  149. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  150. $("#note_location_img").show();
  151. $("#note_location_msg").addClass("img-visible");
  152. }, function(err){
  153. if(err.code == 1) {
  154. location_error("The website was not able to get permission");
  155. } else if(err.code == 2) {
  156. location_error("Location information was unavailable");
  157. } else if(err.code == 3) {
  158. location_error("Timed out getting location");
  159. }
  160. // Load the entry buttons with no location context
  161. load_entry_buttons();
  162. });
  163. }
  164. function set_location_enabled(enabled) {
  165. localforage.setItem('location-enabled', {enabled: enabled});
  166. }
  167. function get_location_enabled(callback) {
  168. localforage.getItem('location-enabled', function(err,val){
  169. if(val) {
  170. callback(val.enabled);
  171. } else {
  172. callback(false);
  173. }
  174. });
  175. }
  176. $("#note_location_chk").click(function(){
  177. if($(this).attr("checked") == "checked") {
  178. if(navigator.geolocation) {
  179. set_location_enabled(true);
  180. fetch_location(); // will load the entry buttons even if location fails
  181. } else {
  182. set_location_enabled(false);
  183. location_error("Browser location is not supported");
  184. }
  185. } else {
  186. $("#note_location_img").hide();
  187. $("#note_location_msg").removeClass("img-visible");
  188. $("#note_location_msg").val('');
  189. $("#note_location").val('');
  190. set_location_enabled(false);
  191. // Load the buttons now
  192. // This is for when the browser is taking too long to find location,
  193. // the user might un-check the box
  194. load_entry_buttons();
  195. }
  196. });
  197. // This loads the buttons with or without location
  198. function load_entry_buttons(coords) {
  199. var latitude = coords ? coords.latitude : '';
  200. var longitude = coords ? coords.longitude : '';
  201. $.getJSON('/options.json', {
  202. latitude: latitude,
  203. longitude: longitude
  204. }, function(response) {
  205. $("#entry-buttons").html(response.buttons);
  206. bind_keyboard_shortcuts();
  207. });
  208. }
  209. $("#photo_preview_container").addClass("hidden");
  210. $("#note_photo_upload").on("change", function(e){
  211. $("#note_photo_upload").addClass("hidden");
  212. $("#note_photo_loading").removeClass("hidden");
  213. var formData = new FormData();
  214. formData.append("null","null");
  215. formData.append("file", e.target.files[0]);
  216. var request = new XMLHttpRequest();
  217. request.open("POST", "/micropub/media");
  218. request.onreadystatechange = function() {
  219. if(request.readyState == XMLHttpRequest.DONE) {
  220. try {
  221. var response = JSON.parse(request.responseText);
  222. console.log(response);
  223. if(response.location) {
  224. // Replace the file upload form with the URL
  225. replacePhotoWithPhotoURL(response.location);
  226. } else {
  227. console.log("Endpoint did not return a location header", response);
  228. $("#remove_photo").click();
  229. $("#note_photo_loading").addClass("hidden");
  230. }
  231. } catch(e) {
  232. $("#remove_photo").click();
  233. $("#note_photo_loading").addClass("hidden");
  234. console.log(e);
  235. }
  236. }
  237. }
  238. request.send(formData);
  239. });
  240. $("#remove_photo").on("click", function(){
  241. $("#note_photo").val("");
  242. $("#note_photo_upload").removeClass("hidden").val("");
  243. $("#photo_preview").attr("src", "" );
  244. $("#photo_preview_container").addClass("hidden");
  245. });
  246. ///////////////////////////////////////////////////////////////
  247. // App Start
  248. // Set the date from JS
  249. var d = new Date();
  250. $("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
  251. $("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
  252. $("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));
  253. // Check if location is enabled in the localstorage prefs
  254. get_location_enabled(function(enabled){
  255. if(enabled) {
  256. // If location is enabled, fetch location and load the entry buttons
  257. fetch_location(); // will load the buttons even if location fails
  258. $("#note_location_chk").attr("checked","checked");
  259. } else {
  260. // If location is not enabled, fetch prefs immediately
  261. $("#note_location_chk").removeAttr("checked");
  262. load_entry_buttons();
  263. }
  264. });
  265. var last_checkin = false;
  266. function fetch_last_checkin() {
  267. $.get("/micropub/last-checkin", function(data){
  268. if(data.url) {
  269. $("#note_venue").removeClass("hidden");
  270. $("#note_venue .name").val(data.name);
  271. last_checkin = data;
  272. bind_venue_buttons();
  273. }
  274. });
  275. }
  276. function bind_venue_buttons() {
  277. $("#note_venue .entry-buttons li input").click(function(){
  278. if($(this).hasClass("btn-primary")) {
  279. // Disable venue tag
  280. $(this).addClass("btn-default").removeClass("btn-primary");
  281. $("#note_venue_url").val("");
  282. fetch_location();
  283. } else {
  284. // Enable venue tag
  285. $(this).addClass("btn-primary").removeClass("btn-default");
  286. $("#note_venue_url").val(last_checkin.url);
  287. $("#note_location_msg").val(last_checkin.url);
  288. $("#note_location_img_small").attr("src", "");
  289. $("#note_location_img_wide").attr("src", "");
  290. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', last_checkin.lat).replace('{lng}', last_checkin.lng));
  291. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', last_checkin.lat).replace('{lng}', last_checkin.lng));
  292. }
  293. });
  294. }
  295. fetch_last_checkin();
  296. });
  297. </script>
  298. <style type="text/css">
  299. .scroll-container {
  300. width: 100%;
  301. overflow-x: scroll;
  302. }
  303. .scroll-container .break {
  304. word-break: break-all;
  305. }
  306. </style>