Guest User

Untitled

a guest
Apr 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. var CountdownTimer = {
  2. endTime:null,
  3. timerId:null,
  4. minutes:0,
  5. maxWidth:0,
  6. maxTime:(25 * 60 * 1000),
  7. isBreak:false,
  8.  
  9. normalColors:["#e47a80", "#eb363b"],
  10. breakColors: ["#97c751", "#759f58"],
  11.  
  12. start: function(minutes, isBreak) {
  13. if (this.maxWidth == 0)
  14. this.maxWidth = $('#timer-bar').width();
  15.  
  16. if (this.timerId)
  17. this.cancel();
  18.  
  19. this.minutes = minutes;
  20. this.isBreak = isBreak;
  21.  
  22. this.setupTimer();
  23. this.flashBar();
  24. },
  25.  
  26. setupTimer:function() {
  27. $('#timer-finished').fadeOut();
  28.  
  29. var date = new Date();
  30. this.endTime = date.getTime() + (this.minutes * 60 * 1000);
  31. this.timerId = setInterval(this.tick, 1000);
  32.  
  33. this.setBarToTime(this.minutes * 60 * 1000);
  34. $('title').text("Started " +
  35. (this.isBreak ? this.minutes + " minute break." : "pomodoro."));
  36. },
  37.  
  38. flashBar:function() {
  39. var lightColor, darkColor;
  40. if (this.isBreak)
  41. {
  42. lightColor = this.breakColors[0];
  43. darkColor = this.breakColors[1];
  44. } else {
  45. lightColor = this.normalColors[0];
  46. darkColor = this.normalColors[1];
  47. }
  48. $('#timer-bar').animate({backgroundColor:lightColor})
  49. .animate({backgroundColor:darkColor})
  50. .css('background-color', darkColor);
  51. },
  52.  
  53. setBarToTime:function(time) {
  54. $('#timer-bar').animate({width:(time/this.maxTime) * this.maxWidth});
  55. },
  56.  
  57. cancel:function() {
  58. clearInterval(this.timerId);
  59. },
  60.  
  61. tick:function() {
  62. // NOTE: Called externally, so must use "CountdownTimer"
  63. // instead of "this".
  64. CountdownTimer.tock();
  65. },
  66.  
  67. tock:function() {
  68. var date = new Date();
  69. var remainingTime = this.endTime - date.getTime();
  70. if (remainingTime <= 0) {
  71. this.cancel();
  72. this.setBarToTime(0);
  73. this.showCompletedInLog();
  74. this.showCompletedMessage();
  75. } else {
  76. this.setBarToTime(remainingTime);
  77. }
  78. },
  79.  
  80. showCompletedInLog:function() {
  81. var logClass = "timer-log-" +
  82. this.minutes + (this.isBreak ? "-break" : "");
  83. $('#timer-log').append('<div class="' + logClass + '"></div>');
  84. },
  85.  
  86. showCompletedMessage:function() {
  87. if ($('#timer-finished').length == 0)
  88. $('#timer').append('<div id="timer-finished"></div>');
  89. var message = "Finished " +
  90. (this.isBreak ? this.minutes + " minute break." : "one pomodoro.");
  91. $('title').text(message);
  92. $('#timer-finished').text(message).fadeIn(1000);
  93. },
  94. };
  95.  
  96. $.fn.extend({
  97. taskStates:['task-empty', 'task-x', 'task-apostrophe', 'task-dash'],
  98.  
  99. resetTaskStateClassNames:function() {
  100. var elements = this;
  101. $.each($.fn.taskStates, function() {
  102. elements.removeClass(this);
  103. })
  104. return this;
  105. },
  106.  
  107. resetTaskState:function() {
  108. this.resetTaskStateClassNames();
  109. return this.each(function() {
  110. $(this).data('taskStateIndex', 0)
  111. .addClass($.fn.taskStates[0]);
  112. });
  113. },
  114.  
  115. toggleTaskState:function() {
  116. this.resetTaskStateClassNames();
  117.  
  118. return this.each(function() {
  119. var element = $(this);
  120. var taskStateIndex = element.data('taskStateIndex') || 0;
  121. taskStateIndex = (taskStateIndex + 1) % $.fn.taskStates.length;
  122.  
  123. element.data('taskStateIndex', taskStateIndex)
  124. .addClass($.fn.taskStates[taskStateIndex]);
  125. });
  126. },
  127.  
  128. });
  129.  
  130. $(function () {
  131.  
  132. $('#button-25').click(function(e) {
  133. e.preventDefault();
  134. CountdownTimer.start(25);
  135. });
  136.  
  137. $('#button-5-break').click(function(e) {
  138. e.preventDefault();
  139. CountdownTimer.start(5, true);
  140. });
  141.  
  142. $('#button-25-break').click(function(e) {
  143. e.preventDefault();
  144. CountdownTimer.start(25, true);
  145. });
  146.  
  147. $('.completion a').live("click", function(e) {
  148. $(this).toggleTaskState();
  149. return false;
  150. })
  151.  
  152. $('#add').click(function(e) {
  153. var taskItem = $('#tasks ul li:first').clone();
  154. taskItem
  155. .find('.completion a').resetTaskState()
  156. .end()
  157. .find('input[type="text"]').val("");
  158. $('#tasks ul').append(taskItem);
  159. taskItem.find('input[type="text"]:first').focus();
  160. return false;
  161. });
  162.  
  163. $('#add').click().click();
  164.  
  165. $('#tasks ul').sortable({handle:".handle"}).disableSelection();
  166.  
  167. $('input[type="text"]:first').focus();
  168.  
  169. });
Add Comment
Please, Sign In to add comment