|
1 | 1 | /*
|
2 | 2 | * jQuery.counter plugin
|
3 | 3 | *
|
4 |
| - * Copyright (c) 2012 Sophilabs <contact@sophilabs.com> |
| 4 | + * Copyright (c) 2012 Sophilabs <hi@sophilabs.com> |
5 | 5 | * MIT License
|
6 | 6 | */
|
7 | 7 |
|
|
56 | 56 | while (digits.length < part.padding) {
|
57 | 57 | digits = '0' + digits;
|
58 | 58 | }
|
59 |
| - $.each(digits.split(''), function(j, digit) { |
| 59 | + $.each(split(digits, ''), function(j, digit) { |
60 | 60 | animate(e, i, j, digit);
|
61 | 61 | });
|
62 | 62 | i--;
|
|
68 | 68 | edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit);
|
69 | 69 | };
|
70 | 70 |
|
| 71 | + //from http://blog.stevenlevithan.com/archives/cross-browser-split |
| 72 | + var split = function(undef) { |
| 73 | + |
| 74 | + var nativeSplit = String.prototype.split, |
| 75 | + compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group |
| 76 | + self; |
| 77 | + |
| 78 | + self = function (str, separator, limit) { |
| 79 | + // If `separator` is not a regex, use `nativeSplit` |
| 80 | + if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
| 81 | + return nativeSplit.call(str, separator, limit); |
| 82 | + } |
| 83 | + var output = [], |
| 84 | + flags = (separator.ignoreCase ? "i" : "") + |
| 85 | + (separator.multiline ? "m" : "") + |
| 86 | + (separator.extended ? "x" : "") + // Proposed for ES6 |
| 87 | + (separator.sticky ? "y" : ""), // Firefox 3+ |
| 88 | + lastLastIndex = 0, |
| 89 | + // Make `global` and avoid `lastIndex` issues by working with a copy |
| 90 | + separator = new RegExp(separator.source, flags + "g"), |
| 91 | + separator2, match, lastIndex, lastLength; |
| 92 | + str += ""; // Type-convert |
| 93 | + if (!compliantExecNpcg) { |
| 94 | + // Doesn't need flags gy, but they don't hurt |
| 95 | + separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
| 96 | + } |
| 97 | + /* Values for `limit`, per the spec: |
| 98 | + * If undefined: 4294967295 // Math.pow(2, 32) - 1 |
| 99 | + * If 0, Infinity, or NaN: 0 |
| 100 | + * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
| 101 | + * If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
| 102 | + * If other: Type-convert, then use the above rules |
| 103 | + */ |
| 104 | + limit = limit === undef ? |
| 105 | + -1 >>> 0 : // Math.pow(2, 32) - 1 |
| 106 | + limit >>> 0; // ToUint32(limit) |
| 107 | + while (match = separator.exec(str)) { |
| 108 | + // `separator.lastIndex` is not reliable cross-browser |
| 109 | + lastIndex = match.index + match[0].length; |
| 110 | + if (lastIndex > lastLastIndex) { |
| 111 | + output.push(str.slice(lastLastIndex, match.index)); |
| 112 | + // Fix browsers whose `exec` methods don't consistently return `undefined` for |
| 113 | + // nonparticipating capturing groups |
| 114 | + if (!compliantExecNpcg && match.length > 1) { |
| 115 | + match[0].replace(separator2, function () { |
| 116 | + for (var i = 1; i < arguments.length - 2; i++) { |
| 117 | + if (arguments[i] === undef) { |
| 118 | + match[i] = undef; |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + if (match.length > 1 && match.index < str.length) { |
| 124 | + Array.prototype.push.apply(output, match.slice(1)); |
| 125 | + } |
| 126 | + lastLength = match[0].length; |
| 127 | + lastLastIndex = lastIndex; |
| 128 | + if (output.length >= limit) { |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + if (separator.lastIndex === match.index) { |
| 133 | + separator.lastIndex++; // Avoid an infinite loop |
| 134 | + } |
| 135 | + } |
| 136 | + if (lastLastIndex === str.length) { |
| 137 | + if (lastLength || !separator.test("")) { |
| 138 | + output.push(""); |
| 139 | + } |
| 140 | + } else { |
| 141 | + output.push(str.slice(lastLastIndex)); |
| 142 | + } |
| 143 | + return output.length > limit ? output.slice(0, limit) : output; |
| 144 | + }; |
| 145 | + |
| 146 | + return self; |
| 147 | + |
| 148 | + }(); |
| 149 | + |
71 | 150 | return this.each(function() {
|
72 | 151 | var e = $(this);
|
73 | 152 | var data = e.data('counter') || {};
|
74 | 153 | data.interval = parseInt(options.interval || e.attr('data-interval') || '1000', 10);
|
75 | 154 | data.down = ( options.direction || e.attr('data-direction') || 'down') == 'down';
|
76 | 155 | data.parts = [];
|
77 |
| - var initial = (options.initial || e.text()).split(/([^0-9]+)/); |
| 156 | + var initial = split(options.initial || e.text(), /([^0-9]+)/); |
78 | 157 | //WARN: Use attr() no data()
|
79 |
| - var format = (options.format || e.attr('data-format') || "23:59:59").split(/([^0-9]+)/); |
| 158 | + var format = split(options.format || e.attr('data-format') || "23:59:59", /([^0-9]+)/); |
80 | 159 | var stop = options.stop || e.attr('data-stop');
|
81 | 160 | if (stop) {
|
82 |
| - stop = stop.split(/([^0-9]+)/); |
| 161 | + stop = split(stop, /([^0-9]+)/); |
83 | 162 | }
|
84 | 163 | e.html('');
|
85 | 164 | $.each(format, function(index, value) {
|
86 | 165 | if (/^\d+$/.test(value)) {
|
87 | 166 | var part = {};
|
88 | 167 | part.index = index;
|
89 | 168 | part.padding = (value + '').length;
|
90 |
| - part.limit = parseFloat(value); |
91 |
| - part.value = parseFloat(initial[initial.length - format.length + index] || 0); |
| 169 | + part.limit = parseInt(value, 10); |
| 170 | + part.value = parseInt(initial[initial.length - format.length + index] || 0, 10); |
92 | 171 | part.value = part.value > part.limit ? part.limit : part.value;
|
93 |
| - part.stop = parseFloat(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit)); |
| 172 | + part.stop = parseInt(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit), 10); |
94 | 173 | part.stop = part.stop > part.limit ? part.limit : part.stop;
|
95 | 174 | part.stop = part.stop < 0 ? 0 : part.stop;
|
96 | 175 | var epart = $('<span>').addClass('part').addClass('part' + index);
|
|
0 commit comments