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.

238 lines
8.3 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. <tr>
  45. <td>p3k-food</td>
  46. <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>
  47. </tr>
  48. <tr>
  49. <td>p3k-type</td>
  50. <td class="break">Will be either <code>drink</code> or <code>eat</code> depending on the type of entry</td>
  51. </tr>
  52. </table>
  53. </div>
  54. </div>
  55. <?php endif; ?>
  56. </div>
  57. <script>
  58. $(function(){
  59. function tz_seconds_to_offset(seconds) {
  60. var tz_offset = '';
  61. var hours = zero_pad(Math.abs(seconds / 60 / 60));
  62. var minutes = zero_pad(Math.floor(seconds / 60) % 60);
  63. return (seconds < 0 ? '-' : '+') + hours + ":" + minutes;
  64. }
  65. function zero_pad(num) {
  66. num = "" + num;
  67. if(num.length == 1) {
  68. num = "0" + num;
  69. }
  70. return num;
  71. }
  72. function bind_keyboard_shortcuts() {
  73. $(".text-custom-eat").keydown(function(e){
  74. if(e.keyCode == 13) {
  75. $(".btn-custom-eat").click();
  76. return false;
  77. }
  78. });
  79. $(".text-custom-drink").keydown(function(e){
  80. if(e.keyCode == 13) {
  81. $(".btn-custom-drink").click();
  82. return false;
  83. }
  84. });
  85. }
  86. function location_error(msg) {
  87. $("#note_location_msg").val(msg);
  88. // $("#note_location_chk").removeAttr("checked");
  89. $("#note_location_loading").hide();
  90. $("#note_location_img").hide();
  91. $("#note_location_msg").removeClass("img-visible");
  92. }
  93. var map_template_wide = "<?= build_static_map_url('{lat}', '{lng}', 180, 700, 15) ?>";
  94. var map_template_small = "<?= build_static_map_url('{lat}', '{lng}', 320, 480, 15) ?>";
  95. function fetch_location() {
  96. $("#note_location_loading").show();
  97. navigator.geolocation.getCurrentPosition(function(position){
  98. load_entry_buttons(position.coords);
  99. $("#note_location_loading").hide();
  100. var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
  101. $("#note_location_msg").val(geo);
  102. $("#note_location").val(geo);
  103. $("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  104. $("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
  105. $("#note_location_img").show();
  106. $("#note_location_msg").addClass("img-visible");
  107. }, function(err){
  108. if(err.code == 1) {
  109. location_error("The website was not able to get permission");
  110. } else if(err.code == 2) {
  111. location_error("Location information was unavailable");
  112. } else if(err.code == 3) {
  113. location_error("Timed out getting location");
  114. }
  115. // Load the entry buttons with no location context
  116. load_entry_buttons();
  117. });
  118. }
  119. function set_location_enabled(enabled) {
  120. localforage.setItem('location-enabled', {enabled: enabled});
  121. }
  122. function get_location_enabled(callback) {
  123. localforage.getItem('location-enabled', function(err,val){
  124. if(val) {
  125. callback(val.enabled);
  126. } else {
  127. callback(false);
  128. }
  129. });
  130. }
  131. $("#note_location_chk").click(function(){
  132. if($(this).attr("checked") == "checked") {
  133. if(navigator.geolocation) {
  134. set_location_enabled(true);
  135. fetch_location(); // will load the entry buttons even if location fails
  136. } else {
  137. set_location_enabled(false);
  138. location_error("Browser location is not supported");
  139. }
  140. } else {
  141. $("#note_location_img").hide();
  142. $("#note_location_msg").removeClass("img-visible");
  143. $("#note_location_msg").val('');
  144. $("#note_location").val('');
  145. set_location_enabled(false);
  146. // Load the buttons now
  147. // This is for when the browser is taking too long to find location,
  148. // the user might un-check the box
  149. load_entry_buttons();
  150. }
  151. });
  152. // This loads the buttons with or without location
  153. function load_entry_buttons(coords) {
  154. var latitude = coords ? coords.latitude : '';
  155. var longitude = coords ? coords.longitude : '';
  156. $.getJSON('/options.json', {
  157. latitude: latitude,
  158. longitude: longitude
  159. }, function(response) {
  160. $("#entry-buttons").html(response.buttons);
  161. bind_keyboard_shortcuts();
  162. });
  163. }
  164. ///////////////////////////////////////////////////////////////
  165. // App Start
  166. // Set the date from JS
  167. var d = new Date();
  168. $("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate()));
  169. $("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
  170. $("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1));
  171. // Check if location is enabled in the localstorage prefs
  172. get_location_enabled(function(enabled){
  173. if(enabled) {
  174. // If location is enabled, fetch location and load the entry buttons
  175. fetch_location(); // will load the buttons even if location fails
  176. $("#note_location_chk").attr("checked","checked");
  177. } else {
  178. // If location is not enabled, fetch prefs immediately
  179. $("#note_location_chk").removeAttr("checked");
  180. load_entry_buttons();
  181. }
  182. });
  183. function onUpdateReady() {
  184. // Show the notice that says there is a new version of the app
  185. $("#new_version_available").show();
  186. }
  187. window.applicationCache.addEventListener('updateready', onUpdateReady);
  188. if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
  189. onUpdateReady();
  190. }
  191. });
  192. </script>
  193. <style type="text/css">
  194. .scroll-container {
  195. width: 100%;
  196. overflow-x: scroll;
  197. }
  198. .scroll-container .break {
  199. word-break: break-all;
  200. }
  201. </style>