1/*! 2 * Countdown v0.1.0 3 * https://github.com/fengyuanchen/countdown 4 * 5 * Copyright 2014 Fengyuan Chen 6 * Released under the MIT license 7 */ 8 9(function (factory) { 10 if (typeof define === "function" && define.amd) { 11 // AMD. Register as anonymous module. 12 define(["jquery"], factory); 13 } else { 14 // Browser globals. 15 factory(jQuery); 16 } 17})(function ($) { 18 19 "use strict"; 20 21 var Countdown = function (element, options) { 22 this.$element = $(element); 23 this.defaults = $.extend({}, Countdown.defaults, this.$element.data(), $.isPlainObject(options) ? options : {}); 24 this.init(); 25 }; 26 27 Countdown.prototype = { 28 constructor: Countdown, 29 30 init: function () { 31 var content = this.$element.html(), 32 date = new Date(this.defaults.date || content); 33 34 if (date.getTime()) { 35 this.content = content; 36 this.date = date; 37 this.find(); 38 39 if (this.defaults.autoStart) { 40 this.start(); 41 } 42 } 43 }, 44 45 find: function () { 46 var $element = this.$element; 47 48 this.$days = $element.find("[data-days]"); 49 this.$hours = $element.find("[data-hours]"); 50 this.$minutes = $element.find("[data-minutes]"); 51 this.$seconds = $element.find("[data-seconds]"); 52 53 if ((this.$days.length + this.$hours.length + this.$minutes.length + this.$seconds.length) > 0) { 54 this.found = true; 55 } 56 }, 57 58 reset: function () { 59 if (this.found) { 60 this.output("days"); 61 this.output("hours"); 62 this.output("minutes"); 63 this.output("seconds"); 64 } else { 65 this.output(); 66 } 67 }, 68 69 ready: function () { 70 var date = this.date, 71 decisecond = 100, 72 second = 1000, 73 minute = 60000, 74 hour = 3600000, 75 day = 86400000, 76 remainder = {}, 77 diff; 78 79 if (!date) { 80 return false; 81 } 82 83 diff = date.getTime() - (new Date()).getTime(); 84 85 if (diff <= 0) { 86 this.end(); 87 return false; 88 } 89 90 remainder.days = diff; 91 remainder.hours = remainder.days % day; 92 remainder.minutes = remainder.hours % hour; 93 remainder.seconds = remainder.minutes % minute; 94 remainder.milliseconds = remainder.seconds % second; 95 96 this.days = Math.floor(remainder.days / day); 97 this.hours = Math.floor(remainder.hours / hour); 98 this.minutes = Math.floor(remainder.minutes / minute); 99 this.seconds = Math.floor(remainder.seconds / second); 100 this.deciseconds = Math.floor(remainder.milliseconds / decisecond); 101 102 return true; 103 }, 104 105 start: function () { 106 if (!this.active && this.ready()) { 107 this.active = true; 108 this.reset(); 109 this.autoUpdate = this.defaults.fast ? 110 setInterval($.proxy(this.fastUpdate, this), 100) : 111 setInterval($.proxy(this.update, this), 1000); 112 } 113 }, 114 115 stop: function () { 116 if (this.active) { 117 this.active = false; 118 clearInterval(this.autoUpdate); 119 } 120 }, 121 122 end: function () { 123 if (!this.date) { 124 return; 125 } 126 127 this.stop(); 128 129 this.days = 0; 130 this.hours = 0; 131 this.minutes = 0; 132 this.seconds = 0; 133 this.deciseconds = 0; 134 this.reset(); 135 this.defaults.end(); 136 }, 137 138 destroy: function () { 139 if (!this.date) { 140 return; 141 } 142 143 this.stop(); 144 145 this.$days = null; 146 this.$hours = null; 147 this.$minutes = null; 148 this.$seconds = null; 149 150 this.$element.empty().html(this.content); 151 this.$element.removeData("data-countdown"); 152 }, 153 154 fastUpdate: function () { 155 if (--this.deciseconds >= 0) { 156 this.output("deciseconds"); 157 } else { 158 this.deciseconds = 9; 159 this.update(); 160 } 161 }, 162 163 update: function () { 164 if (--this.seconds >= 0) { 165 this.output("seconds"); 166 } else { 167 this.seconds = 59; 168 169 if (--this.minutes >= 0) { 170 this.output("minutes"); 171 } else { 172 this.minutes = 59; 173 174 if (--this.hours >= 0) { 175 this.output("hours"); 176 } else { 177 this.hours = 23; 178 179 if (--this.days >= 0) { 180 this.output("days"); 181 } else { 182 this.end(); 183 } 184 } 185 } 186 } 187 }, 188 189 output: function (type) { 190 if (!this.found) { 191 this.$element.empty().html(this.template()); 192 return; 193 } 194 195 switch (type) { 196 case "deciseconds": 197 this.$seconds.text(this.getSecondsText()); 198 break; 199 200 case "seconds": 201 this.$seconds.text(this.seconds); 202 break; 203 204 case "minutes": 205 this.$minutes.text(this.minutes); 206 break; 207 208 case "hours": 209 this.$hours.text(this.hours); 210 break; 211 212 case "days": 213 this.$days.text(this.days); 214 break; 215 216 // No default 217 } 218 }, 219 220 template: function () { 221 return this.defaults.text 222 .replace("%s", this.days) 223 .replace("%s", this.hours) 224 .replace("%s", this.minutes)
225 .replace("%s", this.getSecondsText()); 226 }, 227 228 getSecondsText: function () { 229 return this.active && this.defaults.fast ? (this.seconds + "." + this.deciseconds) : this.seconds; 230 } 231 }; 232 233 // Default settings 234 Countdown.defaults = { 235 autoStart: true, 236 date: null, 237 fast: false, 238 end: $.noop, 239 text: "%s days, %s hours, %s minutes, %s seconds" 240 }; 241 242 // Set default settings 243 Countdown.setDefaults = function (options) { 244 $.extend(Countdown.defaults, options); 245 }; 246 247 // Register as jQuery plugin 248 $.fn.countdown = function (options) { 249 return this.each(function () { 250 var $this = $(this), 251 data = $this.data("date-countdown"); 252 253 if (!data) { 254 $this.data("data-countdown", (data = new Countdown(this, options))); 255 } 256 257 if (typeof options === "string" && $.isFunction(data[options])) { 258 data[options](); 259 } 260 }); 261 }; 262 263 $.fn.countdown.constructor = Countdown; 264 $.fn.countdown.setDefaults = Countdown.setDefaults; 265 266 $(function () { 267 $("[data-countdown]").countdown(); 268 }); 269 270});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.