Guest User

Untitled

a guest
May 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. (function($){
  2.  
  3. $.widget('pixel.countdown', {
  4. options: {
  5. milestones: [
  6. { label: "one_minute", minute: 1 },
  7. { label: "five_minutes", minute: 5, second: 30 }
  8. ],
  9. interval: 1, // Interval in seconds to refresh the widget
  10. formatter: function(min, sec){
  11. if(min === 0 && sec === 0){
  12. return "now.";
  13. } else {
  14. var second_label = sec + (sec == 1 ? " second." : " seconds.");
  15. if( min === 0){
  16. return "in " + second_label;
  17. } else {
  18. return "in " + min + (min == 1 ? " minute" : " minutes") + " and " + second_label;
  19. }
  20. }
  21. }
  22. },
  23. _create: function(){
  24. var dateTime = this.element.attr('datetime') || this.element.text(),
  25. hour,
  26. min,
  27. setup = false;
  28.  
  29. dateTime = dateTime.split('T');
  30.  
  31. if( dateTime.length === 2 ){
  32. // Create a new date based on the year, month, day
  33. this._endTime = new Date(dateTime[0]);
  34.  
  35. // Get hours and minutes
  36. dateTime = dateTime[1].split(':');
  37. hour = parseInt( dateTime[0], 10);
  38. min = parseInt( dateTime[1], 10);
  39.  
  40. if( !isNaN(hour) && !isNaN(min) ){
  41. this._endTime.setUTCHours(hour, min);
  42. setup = true;
  43. }
  44. }
  45.  
  46. // In this case we fail silently, and remove the widget
  47. // We could throw an error instead if wanted.
  48. if (!setup){
  49. this.destroy(); // Undo everything we did
  50. return false;
  51. }
  52.  
  53. this._originalText = this.element.text();
  54. this.milestones = this._cleanMilestones();
  55. this.widgetEventPrefix = "countdown_";
  56.  
  57. this._timer = window.setInterval($.proxy(this.update, this), this.options.interval * 1000);
  58. this.update(); // call it initially to avoid 1 second delay
  59. },
  60. timeLeft: function(){
  61. var left = this._endTime - (new Date()).getTime(),
  62. min = left / 1000 / 60,
  63. sec = min % 1 * 60;
  64.  
  65. min = parseInt(min, 10);
  66. sec = Math.round(sec);
  67.  
  68. return left <= 0 ? [0, 0] : [min, sec];
  69. },
  70. update: function(){
  71. var timeLeft = this.timeLeft(),
  72. vTL = timeLeft[0] + (timeLeft[1] / 100),
  73. label;
  74.  
  75. if(this.milestones.length && this.milestones[0].time > vTL){
  76. label = this.milestones.shift().label;
  77. this._trigger('milestone_reached', null, label);
  78. }
  79.  
  80. if(vTL === 0){
  81. window.clearInterval(this._timer);
  82. this._trigger('complete');
  83. }
  84.  
  85. this.element.text(
  86. this.options.formatter.apply(this.element, timeLeft)
  87. );
  88. },
  89.  
  90. _cleanMilestones: function(){
  91. var timeLeft = this.timeLeft(),
  92. vTL = timeLeft[0] + (timeLeft[1] / 100),
  93. milestones = $.map(this.options.milestones, function(ms){
  94. // Create a new literal that contains only the label
  95. // and the min/sec as a single value
  96. return { label: ms.label, time: ms.minute + ((ms.second || 0) / 100) };
  97. }),
  98. size = milestones.length;
  99.  
  100.  
  101.  
  102. // Sort largest to smallest
  103. milestones.sort(function(a,b){
  104. if (a.time === b.time) return 0;
  105. else return a.time > b.time ? -1 : 1;
  106. });
  107.  
  108. // Remove expired milestones
  109. while(milestones.length && milestones[0].time > vTL){
  110. milestones.shift();
  111. }
  112.  
  113. return milestones;
  114. },
  115.  
  116. _setOption: function(key, value){
  117. if (this.options[key] === value) {
  118. return; // Do nothing, the value didn't change
  119. }
  120.  
  121. switch (key){
  122. case "interval":
  123. // Store the new value
  124. this.options.interval = value;
  125.  
  126. // Clear the old timer
  127. window.clearInterval(this._timer);
  128.  
  129. // Set a new timer based on the new interval
  130. this._timer = window.setInterval(
  131. $.proxy(this.update, this),
  132. this.options.interval * 1000
  133. );
  134. break;
  135.  
  136. case "formatter":
  137. this.options.formatter = value;
  138. this.update();
  139. break;
  140.  
  141. case "milestones":
  142. this.options.milestones = value;
  143. this.milestones = this._cleanMilestones();
  144. break;
  145.  
  146. default:
  147. this.options[key] = value;
  148. }
  149.  
  150. return this;
  151. },
  152.  
  153. destroy: function(){
  154. window.clearInterval( this._timer );
  155. this.element.text( this._originalText);
  156.  
  157. // Call the parent destroy method
  158. $.Widget.prototype.destroy.call(this);
  159. }
  160. });
  161.  
  162. }(jQuery));
Add Comment
Please, Sign In to add comment