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.

252 lines
9.0 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. <div id="entry-buttons">
  11. </div>
  12. <div class="form-group">
  13. <h3>Location <input type="checkbox" id="note_location_chk" value=""><img src="/images/spinner.gif" id="note_location_loading" style="display: none;"></h3>
  14. <input type="text" id="note_location_msg" value="" class="form-control" placeholder="" readonly="readonly">
  15. <input type="hidden" id="note_location" name="location">
  16. <input type="hidden" id="location_enabled" value="<?= $this->location_enabled ?>">
  17. <div id="note_location_img" style="display: none;">
  18. <img src="" height="180" id="note_location_img_wide" class="img-responsive">
  19. <img src="" height="320" id="note_location_img_small" class="img-responsive">
  20. </div>
  21. </div>
  22. </form>
  23. <?php if($this->micropub_endpoint): ?>
  24. <div class="scroll-container">
  25. <div class="callout">
  26. <p>Clicking an item will post this note to your Micropub endpoint. Below is some information about the request that will be made.</p>
  27. <table class="table table-condensed">
  28. <tr>
  29. <td>me</td>
  30. <td class="break"><code><?= session('me') ?></code> (should be your URL)</td>
  31. </tr>
  32. <tr>
  33. <td>scope</td>
  34. <td class="break"><code><?= $this->token_scope ?></code> (should be a space-separated list of permissions including "post")</td>
  35. </tr>
  36. <tr>
  37. <td>micropub endpoint</td>
  38. <td class="break"><code><?= $this->micropub_endpoint ?></code> (should be a URL)</td>
  39. </tr>
  40. <tr>
  41. <td>access token</td>
  42. <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>
  43. </tr>
  44. <?php if($this->enable_array_micropub): ?>
  45. <tr>
  46. <td>ate or drank</td>
  47. <td class="break">
  48. <p>The parameter named <code>ate</code> or <code>drank</code> will include an <code>h-food</code> object corresponding to the item you tapped.</p>
  49. <pre>ate[type]=h-food&amp;ate[properties][name]=Coffee</pre>
  50. </td>
  51. </tr>
  52. <?php else: ?>
  53. <tr>
  54. <td>h-food</td>
  55. <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>
  56. </tr>
  57. <tr>
  58. <td>p3k-food</td>
  59. <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>
  60. </tr>
  61. <tr>
  62. <td>p3k-type</td>
  63. <td class="break">Will be either <code>drink</code> or <code>eat</code> depending on the type of entry</td>
  64. </tr>
  65. <?php endif; ?>
  66. </table>
  67. </div>
  68. </div>
  69. <?php endif; ?>
  70. </div>
  71. <script>
  72. $(function(){
  73. function tz_seconds_to_offset(seconds) {
  74. var tz_offset = '';
  75. var hours = zero_pad(Math.abs(seconds / 60 / 60));
  76. var minutes = zero_pad(Math.floor(seconds / 60) % 60);
  77. return (seconds < 0 ? '-' : '+') + hours + ":" + minutes;
  78. }
  79. function zero_pad(num) {
  80. num = "" + num;
  81. if(num.length == 1) {
  82. num = "0" + num;
  83. }
  84. return num;
  85. }
  86. function bind_keyboard_shortcuts() {
  87. $(".text-custom-eat").keydown(function(e){
  88. if(e.keyCode == 13) {
  89. $(".btn-custom-eat").click();
  90. return false;
  91. }
  92. });
  93. $(".text-custom-drink").keydown(function(e){
  94. if(e.keyCode == 13) {
  95. $(".btn-custom-drink").click();
  96. return false;
  97. }
  98. });
  99. }
  100. function location_error(msg) {
  101. $("#note_location_msg").val(msg);
  102. // $("#note_location_chk").removeAttr("checked");
  103. $("#note_location_loading").hide();
  104. $("#note_location_img").hide();
  105. $("#note_location_msg").removeClass("img-visible");
  106. }
  107. var map_template_wide = "<?= build_static_map_url('{lat}', '{lng}', 180, 700, 15) ?>";
  108. var map_template_small = "<?= build_static_map_url('{lat}', '{lng}', 320, 480, 15) ?>";
  109. function fetch_location() {
  110. $("#note_location_loading").show();
  111. navigator.geolocation.getCurrentPosition(function(position){
  112. load_entry_buttons(position.coords);
  113. $("#note_location_loading").hide();
  114. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  115. $("#note_location_msg").val(geo);
  116. $("#note_location").val(geo);
  117. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  118. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  119. $("#note_location_img").show();
  120. $("#note_location_msg").addClass("img-visible");
  121. }, function(err){
  122. if(err.code == 1) {
  123. location_error("The website was not able to get permission");
  124. } else if(err.code == 2) {
  125. location_error("Location information was unavailable");
  126. } else if(err.code == 3) {
  127. location_error("Timed out getting location");
  128. }
  129. // Load the entry buttons with no location context
  130. load_entry_buttons();
  131. });
  132. }
  133. function set_location_enabled(enabled) {
  134. localforage.setItem('location-enabled', {enabled: enabled});
  135. }
  136. function get_location_enabled(callback) {
  137. localforage.getItem('location-enabled', function(err,val){
  138. if(val) {
  139. callback(val.enabled);
  140. } else {
  141. callback(false);
  142. }
  143. });
  144. }
  145. $("#note_location_chk").click(function(){
  146. if($(this).attr("checked") == "checked") {
  147. if(navigator.geolocation) {
  148. set_location_enabled(true);
  149. fetch_location(); // will load the entry buttons even if location fails
  150. } else {
  151. set_location_enabled(false);
  152. location_error("Browser location is not supported");
  153. }
  154. } else {
  155. $("#note_location_img").hide();
  156. $("#note_location_msg").removeClass("img-visible");
  157. $("#note_location_msg").val('');
  158. $("#note_location").val('');
  159. set_location_enabled(false);
  160. // Load the buttons now
  161. // This is for when the browser is taking too long to find location,
  162. // the user might un-check the box
  163. load_entry_buttons();
  164. }
  165. });
  166. // This loads the buttons with or without location
  167. function load_entry_buttons(coords) {
  168. var latitude = coords ? coords.latitude : '';
  169. var longitude = coords ? coords.longitude : '';
  170. $.getJSON('/options.json', {
  171. latitude: latitude,
  172. longitude: longitude
  173. }, function(response) {
  174. $("#entry-buttons").html(response.buttons);
  175. bind_keyboard_shortcuts();
  176. });
  177. }
  178. ///////////////////////////////////////////////////////////////
  179. // App Start
  180. // Set the date from JS
  181. var d = new Date();
  182. $("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
  183. $("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
  184. $("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));
  185. // Check if location is enabled in the localstorage prefs
  186. get_location_enabled(function(enabled){
  187. if(enabled) {
  188. // If location is enabled, fetch location and load the entry buttons
  189. fetch_location(); // will load the buttons even if location fails
  190. $("#note_location_chk").attr("checked","checked");
  191. } else {
  192. // If location is not enabled, fetch prefs immediately
  193. $("#note_location_chk").removeAttr("checked");
  194. load_entry_buttons();
  195. }
  196. });
  197. function onUpdateReady() {
  198. // Show the notice that says there is a new version of the app
  199. $("#new_version_available").show();
  200. }
  201. window.applicationCache.addEventListener('updateready', onUpdateReady);
  202. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  203. onUpdateReady();
  204. }
  205. });
  206. </script>
  207. <style type="text/css">
  208. .scroll-container {
  209. width: 100%;
  210. overflow-x: scroll;
  211. }
  212. .scroll-container .break {
  213. word-break: break-all;
  214. }
  215. </style>