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.

247 lines
8.7 KiB

  1. define(["sugar-web/activity/activity","mustache", "sugar-web/env"], function (activity,mustache,env) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. // Initialize the activity.
  5. activity.setup();
  6. var requestAnimationFrame = window.requestAnimationFrame ||
  7. window.mozRequestAnimationFrame ||
  8. window.webkitRequestAnimationFrame ||
  9. window.msRequestAnimationFrame;
  10. // Utility to fill a string number with zeros.
  11. function pad(n, width, z) {
  12. z = z || '0';
  13. width = width || 2;
  14. n = n + '';
  15. if (n.length >= width) {
  16. return n;
  17. }
  18. else {
  19. return new Array(width - n.length + 1).join(z) + n;
  20. }
  21. }
  22. function Stopwatch(counter, marks) {
  23. this.elem = document.createElement('li');
  24. var stopwatchList = document.getElementById('stopwatch-list');
  25. stopwatchList.appendChild(this.elem);
  26. this.template =
  27. '<div class="panel-body"></div>' +
  28. '<div class="row">' +
  29. '<div class="col-xs-3 col-sm-3 col-md-2 col-lg-2">' +
  30. '<div class="counter">00:00:00</div>' +
  31. '</div>' +
  32. '<div class="col-xs-5 col-sm-5 col-md-4 col-lg-3">' +
  33. '<div class="buttons-group">' +
  34. '<button class="start-stop-button start"></button>' +
  35. '<button class="reset-button"></button>' +
  36. '<button class="mark-button"></button>' +
  37. '<button class="clear-marks-button"></button>' +
  38. '</div>' +
  39. '</div>' +
  40. '<div class="col-xs-4 col-sm-4 col-md-6 col-lg-7">' +
  41. '<div class="marks"></div>' +
  42. '<button class="remove"></button>' +
  43. '</div>' +
  44. '</div>' +
  45. '</div>';
  46. this.elem.innerHTML = mustache.render(this.template, {});
  47. this.counterElem = this.elem.querySelector('.counter');
  48. this.marksElem = this.elem.querySelector('.marks');
  49. this.running = false;
  50. this.previousTime = Date.now();
  51. this.tenthsOfSecond = 0;
  52. this.seconds = 0;
  53. this.minutes = 0;
  54. if(marks) {
  55. this.marks = marks;
  56. this.updateMarks();
  57. }
  58. else {
  59. this.marks = [];
  60. }
  61. if(counter) {
  62. this.minutes = parseInt(counter.split(":")[0]);
  63. this.seconds = parseInt(counter.split(":")[1]);
  64. this.tenthsOfSecond = parseInt(counter.split(":")[2]);
  65. this.updateView();
  66. }
  67. var that = this;
  68. this.startStopButton = this.elem.querySelector('.start-stop-button');
  69. this.startStopButton.onclick = function () {
  70. that.onStartStopClicked();
  71. };
  72. this.resetButton = this.elem.querySelector('.reset-button');
  73. this.resetButton.onclick = function () {
  74. that.onResetClicked();
  75. };
  76. this.markButton = this.elem.querySelector('.mark-button');
  77. this.markButton.onclick = function () {
  78. that.onMarkClicked();
  79. };
  80. this.clearButton = this.elem.querySelector('.clear-marks-button');
  81. this.clearButton.onclick = function () {
  82. that.onClearMarksClicked();
  83. };
  84. this.removeButton = this.elem.querySelector('.remove');
  85. this.removeButton.onclick = function () {
  86. that.onRemoveClicked();
  87. };
  88. }
  89. Stopwatch.prototype.onStartStopClicked = function () {
  90. if (!this.running) {
  91. this.running = true;
  92. this.tick();
  93. }
  94. else {
  95. this.running = false;
  96. }
  97. this.updateButtons();
  98. };
  99. Stopwatch.prototype.onResetClicked = function () {
  100. this.tenthsOfSecond = 0;
  101. this.seconds = 0;
  102. this.minutes = 0;
  103. if(this.running){
  104. this.onStartStopClicked();
  105. }
  106. else{
  107. this.running = false;
  108. }
  109. this.updateView();
  110. };
  111. Stopwatch.prototype.onMarkClicked = function () {
  112. if (this.marks.length >= 10) {
  113. this.marks.shift();
  114. }
  115. this.marks.push(pad(this.minutes) + ':' + pad(this.seconds) + ':' +
  116. pad(this.tenthsOfSecond));
  117. this.updateMarks();
  118. };
  119. Stopwatch.prototype.onClearMarksClicked = function () {
  120. this.marks = [];
  121. this.updateMarks();
  122. };
  123. Stopwatch.prototype.onRemoveClicked = function () {
  124. var stopwatchList = document.getElementById('stopwatch-list');
  125. stopwatchList.removeChild(this.elem);
  126. };
  127. Stopwatch.prototype.tick = function () {
  128. if (!this.running) {
  129. return;
  130. }
  131. var currentTime = Date.now();
  132. if ((currentTime - this.previousTime) > 100) {
  133. this.previousTime = currentTime;
  134. this.update();
  135. this.updateView();
  136. }
  137. requestAnimationFrame(this.tick.bind(this));
  138. };
  139. Stopwatch.prototype.update = function () {
  140. this.tenthsOfSecond += 1;
  141. if (this.tenthsOfSecond == 10) {
  142. this.tenthsOfSecond = 0;
  143. this.seconds += 1;
  144. }
  145. if (this.seconds == 60) {
  146. this.seconds = 0;
  147. this.minutes += 1;
  148. }
  149. };
  150. Stopwatch.prototype.updateView = function () {
  151. this.counterElem.innerHTML = pad(this.minutes) + ':' +
  152. pad(this.seconds) + ':' + pad(this.tenthsOfSecond);
  153. };
  154. Stopwatch.prototype.updateMarks = function () {
  155. this.marksElem.innerHTML = '';
  156. for (var i = 0; i < this.marks.length; i++) {
  157. this.marksElem.innerHTML += this.marks[i];
  158. if (i !== (this.marks.length -1)) {
  159. this.marksElem.innerHTML += ' - ';
  160. }
  161. }
  162. };
  163. Stopwatch.prototype.updateButtons = function () {
  164. if (this.running) {
  165. this.startStopButton.classList.add("stop");
  166. this.startStopButton.classList.remove("start");
  167. }
  168. else {
  169. this.startStopButton.classList.add("start");
  170. this.startStopButton.classList.remove("stop");
  171. }
  172. };
  173. // Button to add more stopwatches.
  174. var addButton = document.getElementById('add-stopwatch');
  175. addButton.onclick = function () {
  176. new Stopwatch();
  177. };
  178. env.getEnvironment(function(err, environment) {
  179. currentenv = environment;
  180. if (!environment.objectId) {
  181. // Start with five stopwatches.
  182. for (var i = 0; i < 5; i++) {
  183. new Stopwatch();
  184. }
  185. } else {
  186. activity.getDatastoreObject().loadAsText(function(error, metadata, data) {
  187. if (error==null && data!=null) {
  188. stopwatchData = JSON.parse(data);
  189. var i;
  190. for (i = 0; i < Object.keys(stopwatchData).length; i++) {
  191. // Generate saved stopwatches
  192. counter = stopwatchData[i]["counter"];
  193. marks = stopwatchData[i]["marks"];
  194. new Stopwatch(counter, marks);
  195. }
  196. }
  197. });
  198. }
  199. });
  200. });
  201. // Saving stopwatch data on stop.
  202. document.getElementById("stop-button").addEventListener('click', function (event) {
  203. var stopwatchData = document.getElementById("stopwatch-list").getElementsByTagName("li");
  204. stopwatchDict = {};
  205. var i;
  206. for (i = 0; i < stopwatchData.length; i++) {
  207. stopwatchDict[i] = {};
  208. (stopwatchDict[i])["counter"] = stopwatchData[i].getElementsByClassName("counter")[0].innerHTML;
  209. (stopwatchDict[i])["marks"] = stopwatchData[i].getElementsByClassName("marks")[0].innerHTML.split(" - ");
  210. }
  211. stopwatchJSON = JSON.stringify(stopwatchDict);
  212. activity.getDatastoreObject().setDataAsText(stopwatchJSON);
  213. activity.getDatastoreObject().save();
  214. });
  215. });