not really known
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.

329 lines
8.4 KiB

  1. define(['activity/data-model', 'activity/draw', 'webL10n', 'sugar-web/env', 'sugar-web/datastore', 'moment-with-locales.min'], function(DataModel, Draw, l10n, env, datastore, moment) {
  2. 'use strict';
  3. var toggleGridBtn = document.querySelector('#toggle-grid-button'),
  4. toggleHemisphereBtn = document.querySelector('#toggle-hemisphere-button');
  5. var canvas = document.querySelector('canvas'),
  6. ctx = canvas.getContext('2d');
  7. var _ = l10n.get;
  8. var first = true;
  9. l10n.ready(function() {
  10. if (first) {
  11. first = false;
  12. env.getEnvironment(function(err, environment) {
  13. var defaultLanguage = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18n.getUILanguage() : navigator.language;
  14. var language = environment.user ? environment.user.language : defaultLanguage;
  15. l10n.language.code = language;
  16. moment.locale(language);
  17. var refreshTime = setTimeout(function() {
  18. clearTimeout(refreshTime);
  19. updateView();
  20. }, 50);
  21. });
  22. }
  23. });
  24. var IMAGE_SIZE, HALF_SIZE, updateTimeout;
  25. var showGrid, showSouth;
  26. function setup() {
  27. /*
  28. Exposed function - calls all other functions
  29. */
  30. initEventListeners();
  31. updateSizes();
  32. }
  33. function initPrefs(pref) {
  34. /*
  35. Read user preferences from datastore
  36. */
  37. showGrid = pref.showGrid;
  38. if (showGrid) {
  39. showGrid = true;
  40. toggleGridBtn.classList.add('active');
  41. } else {
  42. showGrid = false;
  43. toggleGridBtn.classList.remove('active');
  44. }
  45. showSouth = pref.showSouth;
  46. if (showSouth) {
  47. showSouth = true;
  48. toggleHemisphereBtn.classList.add('active');
  49. } else {
  50. showSouth = false;
  51. toggleHemisphereBtn.classList.remove('active');
  52. }
  53. }
  54. function getPrefs() {
  55. return {
  56. showGrid: showGrid,
  57. showSouth: showSouth
  58. };
  59. }
  60. function updateSizes() {
  61. /*
  62. Dynamically resize elements as and when the window resizes.
  63. */
  64. var navbarOffset = document.querySelector('#main-toolbar').clientHeight;
  65. document.querySelector('#panel-container').style.height = (window.innerHeight - navbarOffset) + 'px';
  66. var canvasPanelHeight = document.querySelector('#panel-right').clientHeight;
  67. var canvasPanelWidth = document.querySelector('#panel-right').clientWidth;
  68. var paddingPercent = 0.05;
  69. IMAGE_SIZE = (1 - paddingPercent) * Math.min(canvasPanelWidth, canvasPanelHeight);
  70. HALF_SIZE = 0.5 * IMAGE_SIZE;
  71. canvas.width = IMAGE_SIZE;
  72. canvas.height = IMAGE_SIZE;
  73. canvas.style.top = 0.5 * (canvasPanelHeight - canvas.height) + 'px';
  74. canvas.style.left = 0.5 * (canvasPanelWidth - canvas.width) + 'px';
  75. }
  76. function updateView() {
  77. /*
  78. Update moon data and
  79. draw moon, repeteadly, after a fixed interval.
  80. Also, depending on user's preferences:
  81. * Toggle hemisphere
  82. * Draw grid
  83. */
  84. clearTimeout(updateTimeout);
  85. updateInfo();
  86. Draw.setImageSize(IMAGE_SIZE);
  87. Draw.moon();
  88. if (showSouth) {
  89. ctx.save();
  90. ctx.rotate(Math.PI);
  91. ctx.drawImage(canvas, -IMAGE_SIZE, -IMAGE_SIZE);
  92. ctx.restore();
  93. if (showGrid) {
  94. Draw.grid(_('SNWE'));
  95. }
  96. } else if (showGrid) {
  97. Draw.grid(_('NSEW'));
  98. }
  99. updateTimeout = setTimeout(updateView, 5000);
  100. }
  101. function updateInfo() {
  102. /*
  103. Update moon data and
  104. render updated information as HTML
  105. */
  106. DataModel.update_moon_calculations();
  107. var infoParts = {};
  108. var keys = [
  109. 'moonInfo',
  110. 'phase',
  111. 'julian',
  112. 'age',
  113. 'lunation',
  114. 'visibility',
  115. 'seleno',
  116. 'full',
  117. 'new',
  118. 'lunar',
  119. 'solar'
  120. ];
  121. infoParts[_(keys[0])] = [
  122. formatDate()
  123. ];
  124. infoParts[_(keys[1])] = [
  125. _(DataModel.moon_phase_name())
  126. ];
  127. infoParts[_(keys[2])] = [
  128. DataModel.julian_date.toFixed(2),
  129. _('astro')
  130. ];
  131. infoParts[_(keys[3])] = [DataModel.days_old,
  132. _('days') + ',',
  133. DataModel.hours_old,
  134. _('hours') + ',',
  135. DataModel.minutes_old,
  136. _('minutes')
  137. ];
  138. infoParts[_(keys[4])] = [
  139. (100 * DataModel.phase_of_moon).toFixed(4) + '%',
  140. _('thru'),
  141. DataModel.lunation
  142. ];
  143. infoParts[_(keys[5])] = [
  144. (100 * DataModel.percent_of_full_moon).toFixed(4) + '%',
  145. _('estimated')
  146. ];
  147. infoParts[_(keys[6])] = [
  148. DataModel.selenographic_deg.toFixed(2) + '\u00b0',
  149. _(DataModel.west_or_east),
  150. '(' + _(DataModel.rise_or_set) + ')'
  151. ];
  152. infoParts[_(keys[7])] = [
  153. formatDate(DataModel.next_full_moon_date),
  154. _('in'),
  155. DataModel.days_until_full_moon.toFixed(),
  156. _('days')
  157. ];
  158. infoParts[_(keys[8])] = [
  159. formatDate(DataModel.next_new_moon_date),
  160. _('in'),
  161. DataModel.days_until_new_moon.toFixed(),
  162. _('days')
  163. ];
  164. infoParts[_(keys[9])] = [
  165. formatDate(DataModel.next_lunar_eclipse_date),
  166. _('in'),
  167. DataModel.days_until_lunar_eclipse.toFixed(),
  168. _('days')
  169. ];
  170. infoParts[_(keys[10])] = [
  171. formatDate(DataModel.next_solar_eclipse_date),
  172. _('in'),
  173. DataModel.days_until_solar_eclipse.toFixed(),
  174. _('days')
  175. ];
  176. var infoHTML = [];
  177. for (var k in infoParts) {
  178. var html = '<p>' + k + ':<br>' + infoParts[k].join(' ') + '</p>';
  179. infoHTML.push(html);
  180. }
  181. infoHTML = infoHTML.join('');
  182. document.querySelector('#panel-left').innerHTML = infoHTML;
  183. document.getElementById("toggle-grid-button").title = _('ToggleGrid');
  184. document.getElementById("toggle-hemisphere-button").title = _('ToggleHemisphere');
  185. document.getElementById("save-image-button").title = _('SaveImage');
  186. }
  187. function initEventListeners() {
  188. /*
  189. Bind event-listeners.
  190. */
  191. window.addEventListener('resize', function() {
  192. updateSizes();
  193. updateView();
  194. });
  195. toggleGridBtn.addEventListener('click', toggleGrid);
  196. toggleHemisphereBtn.addEventListener('click', toggleHemisphere);
  197. document.querySelector('#save-image-button').addEventListener('click', saveImage);
  198. }
  199. function toggleGrid() {
  200. /*
  201. Show/hide grid
  202. */
  203. showGrid = !showGrid;
  204. if (showGrid) {
  205. toggleGridBtn.classList.add('active');
  206. } else {
  207. toggleGridBtn.classList.remove('active');
  208. }
  209. updateView();
  210. }
  211. function toggleHemisphere() {
  212. /*
  213. Rotate moon image to represent southern-hemisphere view.
  214. */
  215. showSouth = !showSouth;
  216. if (showSouth) {
  217. toggleHemisphereBtn.classList.add('active');
  218. } else {
  219. toggleHemisphereBtn.classList.remove('active');
  220. }
  221. updateView();
  222. }
  223. function saveImage() {
  224. /*
  225. Read canvas data as base64 string and
  226. store image in datastore
  227. */
  228. var mimetype = 'image/jpeg';
  229. var inputData = canvas.toDataURL(mimetype, 1);
  230. var metadata = {
  231. mimetype: mimetype,
  232. title: "Image Moon",
  233. activity: "org.olpcfrance.MediaViewerActivity",
  234. timestamp: new Date().getTime(),
  235. creation_time: new Date().getTime(),
  236. file_size: 0
  237. };
  238. datastore.create(metadata, function() {
  239. console.log("export done.")
  240. }, inputData);
  241. }
  242. function formatDate(date) {
  243. /*
  244. Modify rendered dates to match Sugar Moon format
  245. */
  246. if (!date) {
  247. date = new Date();
  248. } else {
  249. date = new Date(1000 * date);
  250. }
  251. var momentDate = moment(date);
  252. return momentDate.format('LLLL').replace(momentDate.format('LT'), momentDate.format('LTS'));
  253. }
  254. return {
  255. setup: setup,
  256. initPrefs: initPrefs,
  257. getPrefs: getPrefs,
  258. updateInfo: updateInfo
  259. };
  260. });