| 2 |
lars |
1 |
/* http://keith-wood.name/countdown.html
|
|
|
2 |
Countdown for jQuery v1.5.3.
|
|
|
3 |
Written by Keith Wood (kbwood{at}iinet.com.au) January 2008.
|
|
|
4 |
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
|
|
|
5 |
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
|
|
|
6 |
Please attribute the author if you use it. */
|
|
|
7 |
|
|
|
8 |
/* Display a countdown timer.
|
|
|
9 |
Attach it with options like:
|
|
|
10 |
$('div selector').countdown(
|
|
|
11 |
{until: new Date(2009, 1 - 1, 1, 0, 0, 0), onExpiry: happyNewYear}); */
|
|
|
12 |
|
|
|
13 |
(function($) { // Hide scope, no $ conflict
|
|
|
14 |
|
|
|
15 |
/* Countdown manager. */
|
|
|
16 |
function Countdown() {
|
|
|
17 |
this.regional = []; // Available regional settings, indexed by language code
|
|
|
18 |
this.regional[''] = { // Default regional settings
|
|
|
19 |
// The display texts for the counters
|
|
|
20 |
labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'],
|
|
|
21 |
// The display texts for the counters if only one
|
|
|
22 |
labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'],
|
|
|
23 |
compactLabels: ['y', 'm', 'w', 'd'], // The compact texts for the counters
|
|
|
24 |
timeSeparator: ':', // Separator for time periods
|
|
|
25 |
isRTL: false // True for right-to-left languages, false for left-to-right
|
|
|
26 |
};
|
|
|
27 |
this._defaults = {
|
|
|
28 |
until: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count down to
|
|
|
29 |
// or numeric for seconds offset, or string for unit offset(s):
|
|
|
30 |
// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
|
|
|
31 |
since: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count up from
|
|
|
32 |
// or numeric for seconds offset, or string for unit offset(s):
|
|
|
33 |
// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
|
|
|
34 |
timezone: null, // The timezone (hours or minutes from GMT) for the target times,
|
|
|
35 |
// or null for client local
|
|
|
36 |
format: 'dHMS', // Format for display - upper case for always, lower case only if non-zero,
|
|
|
37 |
// 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
|
|
|
38 |
layout: '', // Build your own layout for the countdown
|
|
|
39 |
compact: false, // True to display in a compact format, false for an expanded one
|
|
|
40 |
description: '', // The description displayed for the countdown
|
|
|
41 |
expiryUrl: '', // A URL to load upon expiry, replacing the current page
|
|
|
42 |
expiryText: '', // Text to display upon expiry, replacing the countdown
|
|
|
43 |
alwaysExpire: false, // True to trigger onExpiry even if never counted down
|
|
|
44 |
onExpiry: null, // Callback when the countdown expires -
|
|
|
45 |
// receives no parameters and 'this' is the containing division
|
|
|
46 |
onTick: null // Callback when the countdown is updated -
|
|
|
47 |
// receives int[7] being the breakdown by period (based on format)
|
|
|
48 |
// and 'this' is the containing division
|
|
|
49 |
};
|
|
|
50 |
$.extend(this._defaults, this.regional['']);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
var PROP_NAME = 'countdown';
|
|
|
54 |
|
|
|
55 |
var Y = 0; // Years
|
|
|
56 |
var O = 1; // Months
|
|
|
57 |
var W = 2; // Weeks
|
|
|
58 |
var D = 3; // Days
|
|
|
59 |
var H = 4; // Hours
|
|
|
60 |
var M = 5; // Minutes
|
|
|
61 |
var S = 6; // Seconds
|
|
|
62 |
|
|
|
63 |
$.extend(Countdown.prototype, {
|
|
|
64 |
/* Class name added to elements to indicate already configured with countdown. */
|
|
|
65 |
markerClassName: 'hasCountdown',
|
|
|
66 |
|
|
|
67 |
/* Shared timer for all countdowns. */
|
|
|
68 |
_timer: setInterval(function() { $.countdown._updateTargets(); }, 980),
|
|
|
69 |
/* List of currently active countdown targets. */
|
|
|
70 |
_timerTargets: [],
|
|
|
71 |
|
|
|
72 |
/* Override the default settings for all instances of the countdown widget.
|
|
|
73 |
@param options (object) the new settings to use as defaults */
|
|
|
74 |
setDefaults: function(options) {
|
|
|
75 |
this._resetExtraLabels(this._defaults, options);
|
|
|
76 |
extendRemove(this._defaults, options || {});
|
|
|
77 |
},
|
|
|
78 |
|
|
|
79 |
/* Convert a date/time to UTC.
|
|
|
80 |
@param tz (number) the hour or minute offset from GMT, e.g. +9, -360
|
|
|
81 |
@param year (Date) the date/time in that timezone or
|
|
|
82 |
(number) the year in that timezone
|
|
|
83 |
@param month (number, optional) the month (0 - 11) (omit if year is a Date)
|
|
|
84 |
@param day (number, optional) the day (omit if year is a Date)
|
|
|
85 |
@param hours (number, optional) the hour (omit if year is a Date)
|
|
|
86 |
@param mins (number, optional) the minute (omit if year is a Date)
|
|
|
87 |
@param secs (number, optional) the second (omit if year is a Date)
|
|
|
88 |
@param ms (number, optional) the millisecond (omit if year is a Date)
|
|
|
89 |
@return (Date) the equivalent UTC date/time */
|
|
|
90 |
UTCDate: function(tz, year, month, day, hours, mins, secs, ms) {
|
|
|
91 |
if (typeof year == 'object' && year.constructor == Date) {
|
|
|
92 |
ms = year.getMilliseconds();
|
|
|
93 |
secs = year.getSeconds();
|
|
|
94 |
mins = year.getMinutes();
|
|
|
95 |
hours = year.getHours();
|
|
|
96 |
day = year.getDate();
|
|
|
97 |
month = year.getMonth();
|
|
|
98 |
year = year.getFullYear();
|
|
|
99 |
}
|
|
|
100 |
var d = new Date();
|
|
|
101 |
d.setUTCFullYear(year);
|
|
|
102 |
d.setUTCDate(1);
|
|
|
103 |
d.setUTCMonth(month || 0);
|
|
|
104 |
d.setUTCDate(day || 1);
|
|
|
105 |
d.setUTCHours(hours || 0);
|
|
|
106 |
d.setUTCMinutes((mins || 0) - (Math.abs(tz) < 30 ? tz * 60 : tz));
|
|
|
107 |
d.setUTCSeconds(secs || 0);
|
|
|
108 |
d.setUTCMilliseconds(ms || 0);
|
|
|
109 |
return d;
|
|
|
110 |
},
|
|
|
111 |
|
|
|
112 |
/* Attach the countdown widget to a div.
|
|
|
113 |
@param target (element) the containing division
|
|
|
114 |
@param options (object) the initial settings for the countdown */
|
|
|
115 |
_attachCountdown: function(target, options) {
|
|
|
116 |
var $target = $(target);
|
|
|
117 |
if ($target.hasClass(this.markerClassName)) {
|
|
|
118 |
return;
|
|
|
119 |
}
|
|
|
120 |
$target.addClass(this.markerClassName);
|
|
|
121 |
var inst = {options: $.extend({}, options),
|
|
|
122 |
_periods: [0, 0, 0, 0, 0, 0, 0]};
|
|
|
123 |
$.data(target, PROP_NAME, inst);
|
|
|
124 |
this._changeCountdown(target);
|
|
|
125 |
},
|
|
|
126 |
|
|
|
127 |
/* Add a target to the list of active ones.
|
|
|
128 |
@param target (element) the countdown target */
|
|
|
129 |
_addTarget: function(target) {
|
|
|
130 |
if (!this._hasTarget(target)) {
|
|
|
131 |
this._timerTargets.push(target);
|
|
|
132 |
}
|
|
|
133 |
},
|
|
|
134 |
|
|
|
135 |
/* See if a target is in the list of active ones.
|
|
|
136 |
@param target (element) the countdown target
|
|
|
137 |
@return (boolean) true if present, false if not */
|
|
|
138 |
_hasTarget: function(target) {
|
|
|
139 |
return ($.inArray(target, this._timerTargets) > -1);
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
/* Remove a target from the list of active ones.
|
|
|
143 |
@param target (element) the countdown target */
|
|
|
144 |
_removeTarget: function(target) {
|
|
|
145 |
this._timerTargets = $.map(this._timerTargets,
|
|
|
146 |
function(value) { return (value == target ? null : value); }); // delete entry
|
|
|
147 |
},
|
|
|
148 |
|
|
|
149 |
/* Update each active timer target. */
|
|
|
150 |
_updateTargets: function() {
|
|
|
151 |
for (var i = 0; i < this._timerTargets.length; i++) {
|
|
|
152 |
this._updateCountdown(this._timerTargets[i]);
|
|
|
153 |
}
|
|
|
154 |
},
|
|
|
155 |
|
|
|
156 |
/* Redisplay the countdown with an updated display.
|
|
|
157 |
@param target (jQuery) the containing division
|
|
|
158 |
@param inst (object) the current settings for this instance */
|
|
|
159 |
_updateCountdown: function(target, inst) {
|
|
|
160 |
var $target = $(target);
|
|
|
161 |
inst = inst || $.data(target, PROP_NAME);
|
|
|
162 |
if (!inst) {
|
|
|
163 |
return;
|
|
|
164 |
}
|
|
|
165 |
$target.html(this._generateHTML(inst));
|
|
|
166 |
$target[(this._get(inst, 'isRTL') ? 'add' : 'remove') + 'Class']('countdown_rtl');
|
|
|
167 |
var onTick = this._get(inst, 'onTick');
|
|
|
168 |
if (onTick) {
|
|
|
169 |
onTick.apply(target, [inst._hold != 'lap' ? inst._periods :
|
|
|
170 |
this._calculatePeriods(inst, inst._show, new Date())]);
|
|
|
171 |
}
|
|
|
172 |
var expired = inst._hold != 'pause' &&
|
|
|
173 |
(inst._since ? inst._now.getTime() <= inst._since.getTime() :
|
|
|
174 |
inst._now.getTime() >= inst._until.getTime());
|
|
|
175 |
if (expired && !inst._expiring) {
|
|
|
176 |
inst._expiring = true;
|
|
|
177 |
if (this._hasTarget(target) || this._get(inst, 'alwaysExpire')) {
|
|
|
178 |
this._removeTarget(target);
|
|
|
179 |
var onExpiry = this._get(inst, 'onExpiry');
|
|
|
180 |
if (onExpiry) {
|
|
|
181 |
onExpiry.apply(target, []);
|
|
|
182 |
}
|
|
|
183 |
var expiryText = this._get(inst, 'expiryText');
|
|
|
184 |
if (expiryText) {
|
|
|
185 |
var layout = this._get(inst, 'layout');
|
|
|
186 |
inst.options.layout = expiryText;
|
|
|
187 |
this._updateCountdown(target, inst);
|
|
|
188 |
inst.options.layout = layout;
|
|
|
189 |
}
|
|
|
190 |
var expiryUrl = this._get(inst, 'expiryUrl');
|
|
|
191 |
if (expiryUrl) {
|
|
|
192 |
window.location = expiryUrl;
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
inst._expiring = false;
|
|
|
196 |
}
|
|
|
197 |
else if (inst._hold == 'pause') {
|
|
|
198 |
this._removeTarget(target);
|
|
|
199 |
}
|
|
|
200 |
$.data(target, PROP_NAME, inst);
|
|
|
201 |
},
|
|
|
202 |
|
|
|
203 |
/* Reconfigure the settings for a countdown div.
|
|
|
204 |
@param target (element) the containing division
|
|
|
205 |
@param options (object) the new settings for the countdown or
|
|
|
206 |
(string) an individual property name
|
|
|
207 |
@param value (any) the individual property value
|
|
|
208 |
(omit if options is an object) */
|
|
|
209 |
_changeCountdown: function(target, options, value) {
|
|
|
210 |
options = options || {};
|
|
|
211 |
if (typeof options == 'string') {
|
|
|
212 |
var name = options;
|
|
|
213 |
options = {};
|
|
|
214 |
options[name] = value;
|
|
|
215 |
}
|
|
|
216 |
var inst = $.data(target, PROP_NAME);
|
|
|
217 |
if (inst) {
|
|
|
218 |
this._resetExtraLabels(inst.options, options);
|
|
|
219 |
extendRemove(inst.options, options);
|
|
|
220 |
this._adjustSettings(inst);
|
|
|
221 |
$.data(target, PROP_NAME, inst);
|
|
|
222 |
var now = new Date();
|
|
|
223 |
if ((inst._since && inst._since < now) ||
|
|
|
224 |
(inst._until && inst._until > now)) {
|
|
|
225 |
this._addTarget(target);
|
|
|
226 |
}
|
|
|
227 |
this._updateCountdown(target, inst);
|
|
|
228 |
}
|
|
|
229 |
},
|
|
|
230 |
|
|
|
231 |
/* Reset any extra labelsn and compactLabelsn entries if changing labels.
|
|
|
232 |
@param base (object) the options to be updated
|
|
|
233 |
@param options (object) the new option values */
|
|
|
234 |
_resetExtraLabels: function(base, options) {
|
|
|
235 |
var changingLabels = false;
|
|
|
236 |
for (var n in options) {
|
|
|
237 |
if (n.match(/[Ll]abels/)) {
|
|
|
238 |
changingLabels = true;
|
|
|
239 |
break;
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
if (changingLabels) {
|
|
|
243 |
for (var n in base) { // Remove custom numbered labels
|
|
|
244 |
if (n.match(/[Ll]abels[0-9]/)) {
|
|
|
245 |
base[n] = null;
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
},
|
|
|
250 |
|
|
|
251 |
/* Remove the countdown widget from a div.
|
|
|
252 |
@param target (element) the containing division */
|
|
|
253 |
_destroyCountdown: function(target) {
|
|
|
254 |
var $target = $(target);
|
|
|
255 |
if (!$target.hasClass(this.markerClassName)) {
|
|
|
256 |
return;
|
|
|
257 |
}
|
|
|
258 |
this._removeTarget(target);
|
|
|
259 |
$target.removeClass(this.markerClassName).empty();
|
|
|
260 |
$.removeData(target, PROP_NAME);
|
|
|
261 |
},
|
|
|
262 |
|
|
|
263 |
/* Pause a countdown widget at the current time.
|
|
|
264 |
Stop it running but remember and display the current time.
|
|
|
265 |
@param target (element) the containing division */
|
|
|
266 |
_pauseCountdown: function(target) {
|
|
|
267 |
this._hold(target, 'pause');
|
|
|
268 |
},
|
|
|
269 |
|
|
|
270 |
/* Pause a countdown widget at the current time.
|
|
|
271 |
Stop the display but keep the countdown running.
|
|
|
272 |
@param target (element) the containing division */
|
|
|
273 |
_lapCountdown: function(target) {
|
|
|
274 |
this._hold(target, 'lap');
|
|
|
275 |
},
|
|
|
276 |
|
|
|
277 |
/* Resume a paused countdown widget.
|
|
|
278 |
@param target (element) the containing division */
|
|
|
279 |
_resumeCountdown: function(target) {
|
|
|
280 |
this._hold(target, null);
|
|
|
281 |
},
|
|
|
282 |
|
|
|
283 |
/* Pause or resume a countdown widget.
|
|
|
284 |
@param target (element) the containing division
|
|
|
285 |
@param hold (string) the new hold setting */
|
|
|
286 |
_hold: function(target, hold) {
|
|
|
287 |
var inst = $.data(target, PROP_NAME);
|
|
|
288 |
if (inst) {
|
|
|
289 |
if (inst._hold == 'pause' && !hold) {
|
|
|
290 |
inst._periods = inst._savePeriods;
|
|
|
291 |
var sign = (inst._since ? '-' : '+');
|
|
|
292 |
inst[inst._since ? '_since' : '_until'] =
|
|
|
293 |
this._determineTime(sign + inst._periods[0] + 'y' +
|
|
|
294 |
sign + inst._periods[1] + 'o' + sign + inst._periods[2] + 'w' +
|
|
|
295 |
sign + inst._periods[3] + 'd' + sign + inst._periods[4] + 'h' +
|
|
|
296 |
sign + inst._periods[5] + 'm' + sign + inst._periods[6] + 's');
|
|
|
297 |
this._addTarget(target);
|
|
|
298 |
}
|
|
|
299 |
inst._hold = hold;
|
|
|
300 |
inst._savePeriods = (hold == 'pause' ? inst._periods : null);
|
|
|
301 |
$.data(target, PROP_NAME, inst);
|
|
|
302 |
this._updateCountdown(target, inst);
|
|
|
303 |
}
|
|
|
304 |
},
|
|
|
305 |
|
|
|
306 |
/* Return the current time periods.
|
|
|
307 |
@param target (element) the containing division
|
|
|
308 |
@return (number[7]) the current periods for the countdown */
|
|
|
309 |
_getTimesCountdown: function(target) {
|
|
|
310 |
var inst = $.data(target, PROP_NAME);
|
|
|
311 |
return (!inst ? null : (!inst._hold ? inst._periods :
|
|
|
312 |
this._calculatePeriods(inst, inst._show, new Date())));
|
|
|
313 |
},
|
|
|
314 |
|
|
|
315 |
/* Get a setting value, defaulting if necessary.
|
|
|
316 |
@param inst (object) the current settings for this instance
|
|
|
317 |
@param name (string) the name of the required setting
|
|
|
318 |
@return (any) the setting's value or a default if not overridden */
|
|
|
319 |
_get: function(inst, name) {
|
|
|
320 |
return (inst.options[name] != null ?
|
|
|
321 |
inst.options[name] : $.countdown._defaults[name]);
|
|
|
322 |
},
|
|
|
323 |
|
|
|
324 |
/* Calculate interal settings for an instance.
|
|
|
325 |
@param inst (object) the current settings for this instance */
|
|
|
326 |
_adjustSettings: function(inst) {
|
|
|
327 |
var now = new Date();
|
|
|
328 |
var timezone = this._get(inst, 'timezone');
|
|
|
329 |
timezone = (timezone == null ? -new Date().getTimezoneOffset() : timezone);
|
|
|
330 |
inst._since = this._get(inst, 'since');
|
|
|
331 |
if (inst._since) {
|
|
|
332 |
inst._since = this.UTCDate(timezone, this._determineTime(inst._since, null));
|
|
|
333 |
}
|
|
|
334 |
inst._until = this.UTCDate(timezone, this._determineTime(this._get(inst, 'until'), now));
|
|
|
335 |
inst._show = this._determineShow(inst);
|
|
|
336 |
},
|
|
|
337 |
|
|
|
338 |
/* A time may be specified as an exact value or a relative one.
|
|
|
339 |
@param setting (string or number or Date) - the date/time value
|
|
|
340 |
as a relative or absolute value
|
|
|
341 |
@param defaultTime (Date) the date/time to use if no other is supplied
|
|
|
342 |
@return (Date) the corresponding date/time */
|
|
|
343 |
_determineTime: function(setting, defaultTime) {
|
|
|
344 |
var offsetNumeric = function(offset) { // e.g. +300, -2
|
|
|
345 |
var time = new Date();
|
|
|
346 |
time.setTime(time.getTime() + offset * 1000);
|
|
|
347 |
return time;
|
|
|
348 |
};
|
|
|
349 |
var offsetString = function(offset) { // e.g. '+2d', '-4w', '+3h +30m'
|
|
|
350 |
offset = offset.toLowerCase();
|
|
|
351 |
var time = new Date();
|
|
|
352 |
var year = time.getFullYear();
|
|
|
353 |
var month = time.getMonth();
|
|
|
354 |
var day = time.getDate();
|
|
|
355 |
var hour = time.getHours();
|
|
|
356 |
var minute = time.getMinutes();
|
|
|
357 |
var second = time.getSeconds();
|
|
|
358 |
var pattern = /([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g;
|
|
|
359 |
var matches = pattern.exec(offset);
|
|
|
360 |
while (matches) {
|
|
|
361 |
switch (matches[2] || 's') {
|
|
|
362 |
case 's': second += parseInt(matches[1], 10); break;
|
|
|
363 |
case 'm': minute += parseInt(matches[1], 10); break;
|
|
|
364 |
case 'h': hour += parseInt(matches[1], 10); break;
|
|
|
365 |
case 'd': day += parseInt(matches[1], 10); break;
|
|
|
366 |
case 'w': day += parseInt(matches[1], 10) * 7; break;
|
|
|
367 |
case 'o':
|
|
|
368 |
month += parseInt(matches[1], 10);
|
|
|
369 |
day = Math.min(day, $.countdown._getDaysInMonth(year, month));
|
|
|
370 |
break;
|
|
|
371 |
case 'y':
|
|
|
372 |
year += parseInt(matches[1], 10);
|
|
|
373 |
day = Math.min(day, $.countdown._getDaysInMonth(year, month));
|
|
|
374 |
break;
|
|
|
375 |
}
|
|
|
376 |
matches = pattern.exec(offset);
|
|
|
377 |
}
|
|
|
378 |
return new Date(year, month, day, hour, minute, second, 0);
|
|
|
379 |
};
|
|
|
380 |
var time = (setting == null ? defaultTime :
|
|
|
381 |
(typeof setting == 'string' ? offsetString(setting) :
|
|
|
382 |
(typeof setting == 'number' ? offsetNumeric(setting) : setting)));
|
|
|
383 |
if (time) time.setMilliseconds(0);
|
|
|
384 |
return time;
|
|
|
385 |
},
|
|
|
386 |
|
|
|
387 |
/* Determine the number of days in a month.
|
|
|
388 |
@param year (number) the year
|
|
|
389 |
@param month (number) the month
|
|
|
390 |
@return (number) the days in that month */
|
|
|
391 |
_getDaysInMonth: function(year, month) {
|
|
|
392 |
return 32 - new Date(year, month, 32).getDate();
|
|
|
393 |
},
|
|
|
394 |
|
|
|
395 |
/* Generate the HTML to display the countdown widget.
|
|
|
396 |
@param inst (object) the current settings for this instance
|
|
|
397 |
@return (string) the new HTML for the countdown display */
|
|
|
398 |
_generateHTML: function(inst) {
|
|
|
399 |
// Determine what to show
|
|
|
400 |
inst._periods = periods = (inst._hold ? inst._periods :
|
|
|
401 |
this._calculatePeriods(inst, inst._show, new Date()));
|
|
|
402 |
// Show all 'asNeeded' after first non-zero value
|
|
|
403 |
var shownNonZero = false;
|
|
|
404 |
var showCount = 0;
|
|
|
405 |
for (var period = 0; period < inst._show.length; period++) {
|
|
|
406 |
shownNonZero |= (inst._show[period] == '?' && periods[period] > 0);
|
|
|
407 |
inst._show[period] = (inst._show[period] == '?' && !shownNonZero ? null : inst._show[period]);
|
|
|
408 |
showCount += (inst._show[period] ? 1 : 0);
|
|
|
409 |
}
|
|
|
410 |
var compact = this._get(inst, 'compact');
|
|
|
411 |
var layout = this._get(inst, 'layout');
|
|
|
412 |
var labels = (compact ? this._get(inst, 'compactLabels') : this._get(inst, 'labels'));
|
|
|
413 |
var timeSeparator = this._get(inst, 'timeSeparator');
|
|
|
414 |
var description = this._get(inst, 'description') || '';
|
|
|
415 |
var showCompact = function(period) {
|
|
|
416 |
var labelsNum = $.countdown._get(inst, 'compactLabels' + periods[period]);
|
|
|
417 |
return (inst._show[period] ? periods[period] +
|
|
|
418 |
(labelsNum ? labelsNum[period] : labels[period]) + ' ' : '');
|
|
|
419 |
};
|
|
|
420 |
var showFull = function(period) {
|
|
|
421 |
var labelsNum = $.countdown._get(inst, 'labels' + periods[period]);
|
|
|
422 |
return (inst._show[period] ?
|
|
|
423 |
'<span class="countdown_section"><span class="countdown_amount">' +
|
|
|
424 |
periods[period] + '</span><br/>' +
|
|
|
425 |
(labelsNum ? labelsNum[period] : labels[period]) + '</span>' : '');
|
|
|
426 |
};
|
|
|
427 |
return (layout ? this._buildLayout(inst, layout, compact) :
|
|
|
428 |
((compact ? // Compact version
|
|
|
429 |
'<span class="countdown_row countdown_amount' +
|
|
|
430 |
(inst._hold ? ' countdown_holding' : '') + '">' +
|
|
|
431 |
showCompact(Y) + showCompact(O) + showCompact(W) + showCompact(D) +
|
|
|
432 |
(inst._show[H] ? this._minDigits(periods[H], 2) : '') +
|
|
|
433 |
(inst._show[M] ? (inst._show[H] ? timeSeparator : '') +
|
|
|
434 |
this._minDigits(periods[M], 2) : '') +
|
|
|
435 |
(inst._show[S] ? (inst._show[H] || inst._show[M] ? timeSeparator : '') +
|
|
|
436 |
this._minDigits(periods[S], 2) : '') :
|
|
|
437 |
// Full version
|
|
|
438 |
'<span class="countdown_row countdown_show' + showCount +
|
|
|
439 |
(inst._hold ? ' countdown_holding' : '') + '">' +
|
|
|
440 |
showFull(Y) + showFull(O) + showFull(W) + showFull(D) +
|
|
|
441 |
showFull(H) + showFull(M) + showFull(S)) + '</span>' +
|
|
|
442 |
(description ? '<span class="countdown_row countdown_descr">' + description + '</span>' : '')));
|
|
|
443 |
},
|
|
|
444 |
|
|
|
445 |
/* Construct a custom layout.
|
|
|
446 |
@param inst (object) the current settings for this instance
|
|
|
447 |
@param layout (string) the customised layout
|
|
|
448 |
@param compact (boolean) true if using compact labels
|
|
|
449 |
@return (string) the custom HTML */
|
|
|
450 |
_buildLayout: function(inst, layout, compact) {
|
|
|
451 |
var labels = this._get(inst, (compact ? 'compactLabels' : 'labels'));
|
|
|
452 |
var labelFor = function(index) {
|
|
|
453 |
return ($.countdown._get(inst,
|
|
|
454 |
(compact ? 'compactLabels' : 'labels') + inst._periods[index]) ||
|
|
|
455 |
labels)[index];
|
|
|
456 |
};
|
|
|
457 |
var digit = function(value, position) {
|
|
|
458 |
return Math.floor(value / position) % 10;
|
|
|
459 |
};
|
|
|
460 |
var subs = {desc: this._get(inst, 'description'), sep: this._get(inst, 'timeSeparator'),
|
|
|
461 |
yl: labelFor(Y), yn: inst._periods[Y], ynn: this._minDigits(inst._periods[Y], 2),
|
|
|
462 |
ynnn: this._minDigits(inst._periods[Y], 3), y1: digit(inst._periods[Y], 1),
|
|
|
463 |
y10: digit(inst._periods[Y], 10), y100: digit(inst._periods[Y], 100),
|
|
|
464 |
ol: labelFor(O), on: inst._periods[O], onn: this._minDigits(inst._periods[O], 2),
|
|
|
465 |
onnn: this._minDigits(inst._periods[O], 3), o1: digit(inst._periods[O], 1),
|
|
|
466 |
o10: digit(inst._periods[O], 10), o100: digit(inst._periods[O], 100),
|
|
|
467 |
wl: labelFor(W), wn: inst._periods[W], wnn: this._minDigits(inst._periods[W], 2),
|
|
|
468 |
wnnn: this._minDigits(inst._periods[W], 3), w1: digit(inst._periods[W], 1),
|
|
|
469 |
w10: digit(inst._periods[W], 10), w100: digit(inst._periods[W], 100),
|
|
|
470 |
dl: labelFor(D), dn: inst._periods[D], dnn: this._minDigits(inst._periods[D], 2),
|
|
|
471 |
dnnn: this._minDigits(inst._periods[D], 3), d1: digit(inst._periods[D], 1),
|
|
|
472 |
d10: digit(inst._periods[D], 10), d100: digit(inst._periods[D], 100),
|
|
|
473 |
hl: labelFor(H), hn: inst._periods[H], hnn: this._minDigits(inst._periods[H], 2),
|
|
|
474 |
hnnn: this._minDigits(inst._periods[H], 3), h1: digit(inst._periods[H], 1),
|
|
|
475 |
h10: digit(inst._periods[H], 10), h100: digit(inst._periods[H], 100),
|
|
|
476 |
ml: labelFor(M), mn: inst._periods[M], mnn: this._minDigits(inst._periods[M], 2),
|
|
|
477 |
mnnn: this._minDigits(inst._periods[M], 3), m1: digit(inst._periods[M], 1),
|
|
|
478 |
m10: digit(inst._periods[M], 10), m100: digit(inst._periods[M], 100),
|
|
|
479 |
sl: labelFor(S), sn: inst._periods[S], snn: this._minDigits(inst._periods[S], 2),
|
|
|
480 |
snnn: this._minDigits(inst._periods[S], 3), s1: digit(inst._periods[S], 1),
|
|
|
481 |
s10: digit(inst._periods[S], 10), s100: digit(inst._periods[S], 100)};
|
|
|
482 |
var html = layout;
|
|
|
483 |
// Replace period containers: {p<}...{p>}
|
|
|
484 |
for (var i = 0; i < 7; i++) {
|
|
|
485 |
var period = 'yowdhms'.charAt(i);
|
|
|
486 |
var re = new RegExp('\\{' + period + '<\\}(.*)\\{' + period + '>\\}', 'g');
|
|
|
487 |
html = html.replace(re, (inst._show[i] ? '$1' : ''));
|
|
|
488 |
}
|
|
|
489 |
// Replace period values: {pn}
|
|
|
490 |
$.each(subs, function(n, v) {
|
|
|
491 |
var re = new RegExp('\\{' + n + '\\}', 'g');
|
|
|
492 |
html = html.replace(re, v);
|
|
|
493 |
});
|
|
|
494 |
return html;
|
|
|
495 |
},
|
|
|
496 |
|
|
|
497 |
/* Ensure a numeric value has at least n digits for display.
|
|
|
498 |
@param value (number) the value to display
|
|
|
499 |
@param len (number) the minimum length
|
|
|
500 |
@return (string) the display text */
|
|
|
501 |
_minDigits: function(value, len) {
|
|
|
502 |
value = '0000000000' + value;
|
|
|
503 |
return value.substr(value.length - len);
|
|
|
504 |
},
|
|
|
505 |
|
|
|
506 |
/* Translate the format into flags for each period.
|
|
|
507 |
@param inst (object) the current settings for this instance
|
|
|
508 |
@return (string[7]) flags indicating which periods are requested (?) or
|
|
|
509 |
required (!) by year, month, week, day, hour, minute, second */
|
|
|
510 |
_determineShow: function(inst) {
|
|
|
511 |
var format = this._get(inst, 'format');
|
|
|
512 |
var show = [];
|
|
|
513 |
show[Y] = (format.match('y') ? '?' : (format.match('Y') ? '!' : null));
|
|
|
514 |
show[O] = (format.match('o') ? '?' : (format.match('O') ? '!' : null));
|
|
|
515 |
show[W] = (format.match('w') ? '?' : (format.match('W') ? '!' : null));
|
|
|
516 |
show[D] = (format.match('d') ? '?' : (format.match('D') ? '!' : null));
|
|
|
517 |
show[H] = (format.match('h') ? '?' : (format.match('H') ? '!' : null));
|
|
|
518 |
show[M] = (format.match('m') ? '?' : (format.match('M') ? '!' : null));
|
|
|
519 |
show[S] = (format.match('s') ? '?' : (format.match('S') ? '!' : null));
|
|
|
520 |
return show;
|
|
|
521 |
},
|
|
|
522 |
|
|
|
523 |
/* Calculate the requested periods between now and the target time.
|
|
|
524 |
@param inst (object) the current settings for this instance
|
|
|
525 |
@param show (string[7]) flags indicating which periods are requested/required
|
|
|
526 |
@param now (Date) the current date and time
|
|
|
527 |
@return (number[7]) the current time periods (always positive)
|
|
|
528 |
by year, month, week, day, hour, minute, second */
|
|
|
529 |
_calculatePeriods: function(inst, show, now) {
|
|
|
530 |
// Find endpoints
|
|
|
531 |
inst._now = now;
|
|
|
532 |
inst._now.setMilliseconds(0);
|
|
|
533 |
var until = new Date(inst._now.getTime());
|
|
|
534 |
if (inst._since && now.getTime() < inst._since.getTime()) {
|
|
|
535 |
inst._now = now = until;
|
|
|
536 |
}
|
|
|
537 |
else if (inst._since) {
|
|
|
538 |
now = inst._since;
|
|
|
539 |
}
|
|
|
540 |
else {
|
|
|
541 |
until.setTime(inst._until.getTime());
|
|
|
542 |
if (now.getTime() > inst._until.getTime()) {
|
|
|
543 |
inst._now = now = until;
|
|
|
544 |
}
|
|
|
545 |
}
|
|
|
546 |
// Calculate differences by period
|
|
|
547 |
var periods = [0, 0, 0, 0, 0, 0, 0];
|
|
|
548 |
if (show[Y] || show[O]) {
|
|
|
549 |
// Treat end of months as the same
|
|
|
550 |
var lastNow = $.countdown._getDaysInMonth(now.getFullYear(), now.getMonth());
|
|
|
551 |
var lastUntil = $.countdown._getDaysInMonth(until.getFullYear(), until.getMonth());
|
|
|
552 |
var sameDay = (until.getDate() == now.getDate() ||
|
|
|
553 |
(until.getDate() >= Math.min(lastNow, lastUntil) &&
|
|
|
554 |
now.getDate() >= Math.min(lastNow, lastUntil)));
|
|
|
555 |
var getSecs = function(date) {
|
|
|
556 |
return (date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds();
|
|
|
557 |
};
|
|
|
558 |
var months = Math.max(0,
|
|
|
559 |
(until.getFullYear() - now.getFullYear()) * 12 + until.getMonth() - now.getMonth() +
|
|
|
560 |
((until.getDate() < now.getDate() && !sameDay) ||
|
|
|
561 |
(sameDay && getSecs(until) < getSecs(now)) ? -1 : 0));
|
|
|
562 |
periods[Y] = (show[Y] ? Math.floor(months / 12) : 0);
|
|
|
563 |
periods[O] = (show[O] ? months - periods[Y] * 12 : 0);
|
|
|
564 |
// Adjust for months difference and end of month if necessary
|
|
|
565 |
var adjustDate = function(date, offset, last) {
|
|
|
566 |
var wasLastDay = (date.getDate() == last);
|
|
|
567 |
var lastDay = $.countdown._getDaysInMonth(date.getFullYear() + offset * periods[Y],
|
|
|
568 |
date.getMonth() + offset * periods[O]);
|
|
|
569 |
if (date.getDate() > lastDay) {
|
|
|
570 |
date.setDate(lastDay);
|
|
|
571 |
}
|
|
|
572 |
date.setFullYear(date.getFullYear() + offset * periods[Y]);
|
|
|
573 |
date.setMonth(date.getMonth() + offset * periods[O]);
|
|
|
574 |
if (wasLastDay) {
|
|
|
575 |
date.setDate(lastDay);
|
|
|
576 |
}
|
|
|
577 |
return date;
|
|
|
578 |
};
|
|
|
579 |
if (inst._since) {
|
|
|
580 |
until = adjustDate(until, -1, lastUntil);
|
|
|
581 |
}
|
|
|
582 |
else {
|
|
|
583 |
now = adjustDate(new Date(now.getTime()), +1, lastNow);
|
|
|
584 |
}
|
|
|
585 |
}
|
|
|
586 |
var diff = Math.floor((until.getTime() - now.getTime()) / 1000);
|
|
|
587 |
var extractPeriod = function(period, numSecs) {
|
|
|
588 |
periods[period] = (show[period] ? Math.floor(diff / numSecs) : 0);
|
|
|
589 |
diff -= periods[period] * numSecs;
|
|
|
590 |
};
|
|
|
591 |
extractPeriod(W, 604800);
|
|
|
592 |
extractPeriod(D, 86400);
|
|
|
593 |
extractPeriod(H, 3600);
|
|
|
594 |
extractPeriod(M, 60);
|
|
|
595 |
extractPeriod(S, 1);
|
|
|
596 |
return periods;
|
|
|
597 |
}
|
|
|
598 |
});
|
|
|
599 |
|
|
|
600 |
/* jQuery extend now ignores nulls!
|
|
|
601 |
@param target (object) the object to update
|
|
|
602 |
@param props (object) the new settings
|
|
|
603 |
@return (object) the updated object */
|
|
|
604 |
function extendRemove(target, props) {
|
|
|
605 |
$.extend(target, props);
|
|
|
606 |
for (var name in props) {
|
|
|
607 |
if (props[name] == null) {
|
|
|
608 |
target[name] = null;
|
|
|
609 |
}
|
|
|
610 |
}
|
|
|
611 |
return target;
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
/* Process the countdown functionality for a jQuery selection.
|
|
|
615 |
@param command (string) the command to run (optional, default 'attach')
|
|
|
616 |
@param options (object) the new settings to use for these countdown instances
|
|
|
617 |
@return (jQuery) for chaining further calls */
|
|
|
618 |
$.fn.countdown = function(options) {
|
|
|
619 |
var otherArgs = Array.prototype.slice.call(arguments, 1);
|
|
|
620 |
if (options == 'getTimes') {
|
|
|
621 |
return $.countdown['_' + options + 'Countdown'].
|
|
|
622 |
apply($.countdown, [this[0]].concat(otherArgs));
|
|
|
623 |
}
|
|
|
624 |
return this.each(function() {
|
|
|
625 |
if (typeof options == 'string') {
|
|
|
626 |
$.countdown['_' + options + 'Countdown'].apply($.countdown, [this].concat(otherArgs));
|
|
|
627 |
}
|
|
|
628 |
else {
|
|
|
629 |
$.countdown._attachCountdown(this, options);
|
|
|
630 |
}
|
|
|
631 |
});
|
|
|
632 |
};
|
|
|
633 |
|
|
|
634 |
/* Initialise the countdown functionality. */
|
|
|
635 |
$.countdown = new Countdown(); // singleton instance
|
|
|
636 |
|
|
|
637 |
})(jQuery);
|