basic.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. addEvent("onload", init);
  2. addEvent("onresize", checkOrientation);
  3. var channelmonitoractive = false;
  4. var screenmode = 0;
  5. var label_start_monitor_label = "START";
  6. var label_stop_monitor_label = "STOP";
  7. var initial_value = "";
  8. function init(){
  9. try {
  10. document.getElementById("MonitorToggle").innerHTML = label_start_monitor_label;
  11. var myStyleTweaks = new StyleTweaker();
  12. myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
  13. myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
  14. myStyleTweaks.tweak();
  15. initSystemServices();
  16. }
  17. catch (e) {
  18. var error = e.toString();
  19. alert(error);
  20. }
  21. }
  22. function checkOrientation(){
  23. try {
  24. var width = parseInt(screen.width);
  25. var height = parseInt(screen.height);
  26. //var landscapeTweaker = new StyleTweaker();
  27. // Landscape tweaks for template styles not available in templates?
  28. //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
  29. if (width > height) {
  30. // if already in landscape mode, return
  31. if (screenmode == 1)
  32. return;
  33. screenmode = 1;
  34. // landscapeTweaker.tweak();
  35. }
  36. else {
  37. // if already in portrait mode, return
  38. if (screenmode == 0)
  39. return;
  40. screenmode = 0;
  41. // landscapeTweaker.untweak();
  42. }
  43. }
  44. catch (e) {
  45. var error = e.toString();
  46. alert(error);
  47. }
  48. }
  49. /*
  50. Onclick handler for start/stop monitoring button
  51. Starts and stops position monitoring
  52. */
  53. function MonitorToggle_onclick(actionobj){
  54. try {
  55. if (channelmonitoractive) {
  56. actionobj.innerHTML = label_start_monitor_label;
  57. stopMonitoring();
  58. }
  59. else {
  60. actionobj.innerHTML = label_stop_monitor_label;
  61. startMonitoring();
  62. }
  63. channelmonitoractive = !channelmonitoractive;
  64. }
  65. catch (e) {
  66. var error = e.toString();
  67. alert(error);
  68. }
  69. }
  70. /*
  71. Displays position data in widget ui.
  72. result object is passed to display function in
  73. service platform asyncronous callback handlers. Schema:
  74. Position
  75. {
  76. "coords": Coordinates ,
  77. "timestamp": Date
  78. }
  79. Coordinates
  80. {
  81. "latitude": number,
  82. "longitude": number,
  83. "altitude": number,
  84. "accuracy": number, //currently Not Supported
  85. "altitudeAccuracy": number, //currently Not Supported
  86. "heading": number,
  87. "speed": number,
  88. }
  89. */
  90. function displayPositionData(result){
  91. try {
  92. document.getElementById("lat").innerHTML = round(result.coords.latitude) + " \u00B0";
  93. document.getElementById("lon").innerHTML = round(result.coords.longitude) + " \u00B0";
  94. if (result.coords.altitude != null && !isNaN(result.coords.altitude))
  95. document.getElementById("alt").innerHTML = Math.round(result.coords.altitude) + " m";
  96. if (result.coords.heading != null)
  97. document.getElementById("heading").innerHTML = result.coords.heading + " \u00B0";
  98. if (result.coords.speed != null)
  99. document.getElementById("speed").innerHTML = result.coords.speed + " m/s";
  100. if (result.timestamp != null)
  101. document.getElementById("time").innerHTML = formatDateTime(result.timestamp);
  102. }
  103. catch (e) {
  104. var error = e.toString();
  105. alert(error);
  106. }
  107. }
  108. /*
  109. This function puts initial label for all elements in UI
  110. */
  111. function UIReset(){
  112. try {
  113. document.getElementById("lat").innerHTML = initial_value;
  114. document.getElementById("lon").innerHTML = initial_value;
  115. document.getElementById("alt").innerHTML = initial_value;
  116. document.getElementById("heading").innerHTML = initial_value;
  117. document.getElementById("speed").innerHTML = initial_value;
  118. document.getElementById("time").innerHTML = initial_value;
  119. }
  120. catch (e) {
  121. var error = e.toString();
  122. alert(error);
  123. }
  124. }
  125. /*
  126. * Rounds number
  127. */
  128. function round(v){
  129. var a = 100000;
  130. return Math.round(v * a) / a;
  131. }
  132. /*
  133. * Formats JS Date object in form: 'dd/MM/yyyy hh:mm'
  134. * date - date object to be formated
  135. */
  136. function formatDateTime(date){
  137. if (typeof date == "string") {
  138. return date;
  139. }
  140. else {
  141. var separator = "/";
  142. return preformatNumber(date.getDate()) + separator + preformatNumber((date.getMonth() + 1)) + separator + date.getFullYear() + " " + preformatNumber(date.getHours()) + ":" + preformatNumber(date.getMinutes()) + ":" + preformatNumber(date.getSeconds());
  143. }
  144. }
  145. /*
  146. * Adds extra '0' in front of number if it is < 10
  147. */
  148. function preformatNumber(n){
  149. return n < 10 ? "0" + n : n;
  150. }