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.

249 lines
9.8 KiB

  1. define(["sugar-web/activity/activity","sugar-web/env", "worldpalette", "viewpalette", "webL10n"], function (activity, env, worldpalette, viewpalette, webL10n) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. // Initialize the activity.
  5. activity.setup();
  6. //Set background color to user color
  7. var canvas = document.getElementById("canvas");
  8. var canvasColor;
  9. activity.getXOColor(function(error, retcolors){
  10. canvasColor = retcolors;
  11. });
  12. canvas.style.backgroundColor = canvasColor["fill"];
  13. var datastoreObject = activity.getDatastoreObject();
  14. var worldButton = document.getElementById("world-button");
  15. var worldPalette = new worldpalette.ActivityPalette(
  16. worldButton, datastoreObject);
  17. var viewButton = document.getElementById("view-button");
  18. var viewPalette = new viewpalette.ActivityPalette(
  19. viewButton, datastoreObject);
  20. $(document).ready(function() {
  21. var planetarium = $.virtualsky({
  22. id: 'starmap', //Div tag id where you want to place star chart
  23. projection:'stereo', //Type of map projection used
  24. keyboard: false, //Disable/Enable keyboard
  25. showposition: false, //Show/Hide coordinates
  26. showstars: true, //Show/Hide stars
  27. constellations: true, //Show/Hide constellation lines
  28. constellationlabels: true, //Show/Hide Constellation names
  29. showplanets: true, //Show/Hide planets
  30. showplanetlabels: true, //Show/Hide planet names
  31. live: true, //Disabe/Enable real time clock
  32. clock: new Date() //Set clock
  33. });
  34. // Load from datastore
  35. env.getEnvironment(function(err, environment) {
  36. currentenv = environment;
  37. //Set current language
  38. var currentLang = (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) ? chrome.i18.getUILanguage() : navigator.language;
  39. var language = environment.user ? environment.user.language : currentLang;
  40. webL10n.language.code = language;
  41. planetarium.loadLanguage(language); //Set the star chart's language to Sugarizer's language
  42. document.getElementById("locale-date").innerHTML = language; //Localize date
  43. //Process localize event
  44. window.addEventListener("localized", function(){
  45. toLocalize();
  46. })
  47. // Load from datastore
  48. if (!environment.objectId) {
  49. console.log("New instance");
  50. document.getElementById(chartJournal[2]).style.backgroundColor = 'grey';
  51. document.getElementById(chartJournal[3]).style.backgroundColor = 'grey';
  52. } else {
  53. activity.getDatastoreObject().loadAsText(function(error, metadata, data) {
  54. if (error==null && data!=null) {
  55. chartJournal = JSON.parse(data);
  56. customLatitude = planetarium.setLatitude(parseFloat(chartJournal[2].split(',')[0]));
  57. customLongitude = planetarium.setLongitude(parseFloat(chartJournal[2].split(',')[1]));
  58. planetarium.setGeo($(customLatitude,customLongitude)).setClock(0).draw();
  59. try {
  60. document.getElementById(chartJournal[2]).style.backgroundColor = 'grey';
  61. }
  62. catch(e){
  63. console.log("User location used");
  64. }
  65. planetarium.selectProjection(chartJournal[3]);
  66. document.getElementById(chartJournal[3]).style.backgroundColor = 'grey';
  67. if (chartJournal[0] == false){
  68. document.getElementById("const-button").classList.remove("active");
  69. planetarium.toggleConstellationLines();
  70. planetarium.toggleConstellationLabels();
  71. } else if (chartJournal[0] == true){
  72. document.getElementById("const-button").classList.add("active");
  73. }
  74. if (chartJournal[1] == true){
  75. document.getElementById("star-button").classList.add("active");
  76. planetarium.toggleStarLabels();
  77. }
  78. }
  79. });
  80. }
  81. });
  82. //Things to localize
  83. function toLocalize() {
  84. document.getElementById("add-button").title = webL10n.get("AddDay");
  85. document.getElementById("minus-button").title = webL10n.get("MinusDay");
  86. document.getElementById("const-button").title = webL10n.get("ToggleConst");
  87. document.getElementById("star-button").title = webL10n.get("ToggleStar");
  88. document.getElementById("location-button").title = webL10n.get("Location");
  89. document.getElementById("world-button").title = webL10n.get("WorldList");
  90. document.getElementById("view-button").title = webL10n.get("View");
  91. document.getElementById("55.3781,-3.4360").innerHTML = webL10n.get("Britain");
  92. document.getElementById("23.6345,-102.5528").innerHTML = webL10n.get("Mexico");
  93. document.getElementById("40.4637,-3.7492").innerHTML = webL10n.get("Spain");
  94. document.getElementById("51.1657,10.4515").innerHTML = webL10n.get("Germany");
  95. document.getElementById("40.3399,127.5101").innerHTML = webL10n.get("NorthKorea");
  96. document.getElementById("23.8859,45.0792").innerHTML = webL10n.get("SaudiArabia");
  97. document.getElementById("35.9078,127.7669").innerHTML = webL10n.get("SouthKorea");
  98. document.getElementById("46.2276,2.2137").innerHTML = webL10n.get("France");
  99. document.getElementById("51.9194,19.1451").innerHTML = webL10n.get("Poland");
  100. document.getElementById("41.8719,12.5674").innerHTML = webL10n.get("Italy");
  101. document.getElementById("20.5937,78.9629").innerHTML = webL10n.get("India");
  102. document.getElementById("9.0820,8.6753").innerHTML = webL10n.get("Nigeria");
  103. document.getElementById("0.7893,113.9213").innerHTML = webL10n.get("Indonesia");
  104. document.getElementById("-14.2350,-51.9253").innerHTML = webL10n.get("Brazil");
  105. document.getElementById("54.5260,-105.2551").innerHTML = webL10n.get("NorthAmerica");
  106. document.getElementById("-8.7832,-55.4915").innerHTML = webL10n.get("SouthAmerica");
  107. document.getElementById("-8.7832,34.5085").innerHTML = webL10n.get("Africa");
  108. document.getElementById("-25.2744,133.7751").innerHTML = webL10n.get("Australia");
  109. document.getElementById("12.8797,121.7740").innerHTML = webL10n.get("Philippines");
  110. document.getElementById("4.2105,101.9758").innerHTML = webL10n.get("Malaysia");
  111. document.getElementById("36.2048,138.2529").innerHTML = webL10n.get("Japan");
  112. document.getElementById("39.9042,116.4074").innerHTML = webL10n.get("China");
  113. }
  114. //Necessary variables
  115. var chartJournal = [true,false,"55.3781,-3.4360","stereo"];
  116. var longlat = document.getElementById('worldConst').innerHTML;
  117. var customLatitude = planetarium.setLatitude(parseFloat(longlat.split(',')[0]));
  118. var customLongitude = planetarium.setLongitude(parseFloat(longlat.split(',')[1]));
  119. document.getElementById("const-button").classList.add("active");
  120. //Get Location of user and set the star chart to the position
  121. //when Location button is pressed
  122. function getUserLocation(){
  123. if (navigator.geolocation){
  124. navigator.geolocation.getCurrentPosition(userPosition);
  125. }
  126. }
  127. function userPosition(position) {
  128. $("button#location-button").on('click', function(){
  129. console.log(longlat);
  130. try{
  131. document.getElementById(longlat).style.backgroundColor = 'black';
  132. }
  133. catch(e){}
  134. longlat = position.coords.latitude + "," + position.coords.longitude;
  135. customLatitude = planetarium.setLatitude(parseFloat(longlat.split(',')[0]));
  136. customLongitude = planetarium.setLongitude(parseFloat(longlat.split(',')[1]));
  137. planetarium.setGeo($(customLatitude,customLongitude)).setClock(0).draw();
  138. chartJournal[2] = longlat;
  139. console.log(chartJournal);
  140. })
  141. }
  142. getUserLocation();
  143. //Add 1 day to date
  144. $("button#add-button").on('click', function (){
  145. planetarium.clock.setDate(planetarium.clock.getDate() + 1);
  146. planetarium.updateClock(planetarium.clock);
  147. planetarium.draw();
  148. })
  149. //Minus 1 day to date
  150. $("button#minus-button").on('click', function (){
  151. planetarium.clock.setDate(planetarium.clock.getDate() - 1);
  152. planetarium.updateClock(planetarium.clock);
  153. planetarium.draw();
  154. })
  155. //Toggle Constellation Lines and Name
  156. $("button#const-button").on('click', function (){
  157. planetarium.toggleConstellationLines();
  158. planetarium.toggleConstellationLabels();
  159. if (chartJournal[0] == true){
  160. document.getElementById("const-button").classList.remove("active");
  161. chartJournal[0] = false;
  162. } else{
  163. document.getElementById("const-button").classList.add("active");
  164. chartJournal[0] = true;
  165. }
  166. console.log(chartJournal[0]);
  167. })
  168. //Toggle Star names
  169. $("button#star-button").on('click', function (){
  170. planetarium.toggleStarLabels();
  171. if (chartJournal[1] == true){
  172. document.getElementById("star-button").classList.remove("active");
  173. chartJournal[1] = false;
  174. } else{
  175. document.getElementById("star-button").classList.add("active");
  176. chartJournal[1] = true;
  177. }
  178. console.log(chartJournal[1]);
  179. })
  180. //Set long and lat to specific country
  181. $("button.country").on('click', function (){
  182. longlat = document.getElementById('worldConst').innerHTML;
  183. customLatitude = planetarium.setLatitude(parseFloat(longlat.split(',')[0]));
  184. customLongitude = planetarium.setLongitude(parseFloat(longlat.split(',')[1]));
  185. planetarium.setGeo($(customLatitude,customLongitude)).setClock(0).draw();
  186. chartJournal[2] = longlat;
  187. console.log(chartJournal);
  188. })
  189. //Change projection view
  190. $("button.view").on('click', function (){
  191. var pv = document.getElementById('projection-view').innerHTML;
  192. planetarium.selectProjection(pv);
  193. chartJournal[3] = pv;
  194. planetarium.draw();
  195. console.log(chartJournal);
  196. })
  197. //Save in Journal on stop
  198. document.getElementById("stop-button").addEventListener('click', function (event) {
  199. console.log("writing...");
  200. var jsonData = JSON.stringify(chartJournal);
  201. activity.getDatastoreObject().setDataAsText(jsonData);
  202. activity.getDatastoreObject().save(function (error) {
  203. if (error === null) {
  204. console.log("write done.");
  205. console.log(jsonData);
  206. } else {
  207. console.log("write failed.");
  208. }
  209. });
  210. });
  211. });
  212. });
  213. });