|
10 | 10 | else definition(context['$']); |
11 | 11 | }(this, function ($) { |
12 | 12 |
|
13 | | - var methods = { |
14 | | - init: function(options) { |
15 | | - |
16 | | - options = options || {}; |
17 | | - |
18 | | - var checkStop = function(data) { |
19 | | - var stop = 0; |
20 | | - var current = 0; |
21 | | - $.each(data.parts, function(i, part) { |
22 | | - stop += (stop * part.limit) + part.stop; |
23 | | - current += (current * part.limit) + part.value; |
24 | | - }); |
25 | | - return data.down ? stop >= current : stop <= current; |
26 | | - }; |
| 13 | + var checkStop = function(data) { |
| 14 | + var stop = 0; |
| 15 | + var current = 0; |
| 16 | + $.each(data.parts, function(i, part) { |
| 17 | + stop += (stop * part.limit) + part.stop; |
| 18 | + current += (current * part.limit) + part.value; |
| 19 | + }); |
| 20 | + return data.down ? stop >= current : stop <= current; |
| 21 | + }; |
27 | 22 |
|
28 | | - var tick = function() { |
29 | | - var e = $(this); |
30 | | - var data = e.data('counter'); |
31 | | - var i = data.parts.length - 1; |
32 | | - while(i >= 0 ) { |
33 | | - var part = data.parts[i]; |
34 | | - part.value += data.down ? -1 : 1; |
35 | | - if (data.down && part.value < 0) { |
36 | | - part.value = part.limit; |
37 | | - } else if (!data.down && part.value > part.limit) { |
38 | | - part.value = 0; |
39 | | - } else { |
40 | | - break; |
41 | | - } |
42 | | - i--; |
43 | | - } |
44 | | - refresh(e, i); |
45 | | - if (checkStop(data)) { |
46 | | - clearInterval(data.intervalId); |
47 | | - e.trigger("counterStop"); |
48 | | - } |
49 | | - }; |
| 23 | + var tick = function() { |
| 24 | + var e = $(this); |
| 25 | + var data = e.data('counter'); |
| 26 | + var i = data.parts.length - 1; |
| 27 | + while(i >= 0) { |
| 28 | + var part = data.parts[i]; |
| 29 | + part.value += data.down ? -1 : 1; |
| 30 | + if (data.down && part.value < 0) { |
| 31 | + part.value = part.limit; |
| 32 | + } else if (!data.down && part.value > part.limit) { |
| 33 | + part.value = 0; |
| 34 | + } else { |
| 35 | + break; |
| 36 | + } |
| 37 | + i--; |
| 38 | + } |
| 39 | + refresh(e, i); |
| 40 | + if (checkStop(data)) { |
| 41 | + clearInterval(data.intervalId); |
| 42 | + e.trigger("counterStop"); |
| 43 | + } |
| 44 | + }; |
50 | 45 |
|
51 | | - var refresh = function(e, to) { |
52 | | - var data = e.data('counter'); |
53 | | - var i = data.parts.length - 1; |
54 | | - while (i >= to) { |
55 | | - var part = data.parts[i]; |
56 | | - var digits = part.value + ''; |
57 | | - while (digits.length < part.padding) { |
58 | | - digits = '0' + digits; |
59 | | - } |
60 | | - $.each(split(digits, ''), function(j, digit) { |
61 | | - animate(e, i, j, digit); |
62 | | - }); |
63 | | - i--; |
64 | | - } |
65 | | - }; |
| 46 | + var refresh = function(e, to) { |
| 47 | + var data = e.data('counter'); |
| 48 | + var i = data.parts.length - 1; |
| 49 | + var animateIJ = function(j, digit) { |
| 50 | + animate(e, i, j, digit); |
| 51 | + }; |
| 52 | + while (i >= to) { |
| 53 | + var part = data.parts[i]; |
| 54 | + var digits = part.value + ''; |
| 55 | + while (digits.length < part.padding) { |
| 56 | + digits = '0' + digits; |
| 57 | + } |
| 58 | + $.each(split(digits, ''), animateIJ); |
| 59 | + i--; |
| 60 | + } |
| 61 | + }; |
66 | 62 |
|
67 | | - var animate = function(e, ipart, idigit, digit) { |
68 | | - var edigit = $($(e.children('span.part').get(ipart)).find('span.digit').get(idigit)); |
69 | | - edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit); |
70 | | - }; |
| 63 | + var animate = function(e, ipart, idigit, digit) { |
| 64 | + var edigit = $($(e.children('span.part').get(ipart)).find('span.digit').get(idigit)); |
| 65 | + edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit); |
| 66 | + }; |
71 | 67 |
|
72 | | - //from http://blog.stevenlevithan.com/archives/cross-browser-split |
73 | | - var split = function(undef) { |
| 68 | + //from http://blog.stevenlevithan.com/archives/cross-browser-split |
| 69 | + var split = function(undef) { |
74 | 70 |
|
75 | | - var nativeSplit = String.prototype.split, |
76 | | - compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group |
77 | | - self; |
| 71 | + var nativeSplit = String.prototype.split, |
| 72 | + compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group |
| 73 | + self; |
78 | 74 |
|
79 | | - self = function (str, separator, limit) { |
80 | | - // If `separator` is not a regex, use `nativeSplit` |
81 | | - if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
82 | | - return nativeSplit.call(str, separator, limit); |
83 | | - } |
84 | | - var output = [], |
85 | | - flags = (separator.ignoreCase ? "i" : "") + |
86 | | - (separator.multiline ? "m" : "") + |
87 | | - (separator.extended ? "x" : "") + // Proposed for ES6 |
88 | | - (separator.sticky ? "y" : ""), // Firefox 3+ |
89 | | - lastLastIndex = 0, |
90 | | - // Make `global` and avoid `lastIndex` issues by working with a copy |
91 | | - separator = new RegExp(separator.source, flags + "g"), |
92 | | - separator2, match, lastIndex, lastLength; |
93 | | - str += ""; // Type-convert |
94 | | - if (!compliantExecNpcg) { |
95 | | - // Doesn't need flags gy, but they don't hurt |
96 | | - separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
97 | | - } |
98 | | - /* Values for `limit`, per the spec: |
99 | | - * If undefined: 4294967295 // Math.pow(2, 32) - 1 |
100 | | - * If 0, Infinity, or NaN: 0 |
101 | | - * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
102 | | - * If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
103 | | - * If other: Type-convert, then use the above rules |
104 | | - */ |
105 | | - limit = limit === undef ? |
106 | | - -1 >>> 0 : // Math.pow(2, 32) - 1 |
107 | | - limit >>> 0; // ToUint32(limit) |
108 | | - while (match = separator.exec(str)) { |
109 | | - // `separator.lastIndex` is not reliable cross-browser |
110 | | - lastIndex = match.index + match[0].length; |
111 | | - if (lastIndex > lastLastIndex) { |
112 | | - output.push(str.slice(lastLastIndex, match.index)); |
113 | | - // Fix browsers whose `exec` methods don't consistently return `undefined` for |
114 | | - // nonparticipating capturing groups |
115 | | - if (!compliantExecNpcg && match.length > 1) { |
116 | | - match[0].replace(separator2, function () { |
117 | | - for (var i = 1; i < arguments.length - 2; i++) { |
118 | | - if (arguments[i] === undef) { |
119 | | - match[i] = undef; |
120 | | - } |
121 | | - } |
122 | | - }); |
123 | | - } |
124 | | - if (match.length > 1 && match.index < str.length) { |
125 | | - Array.prototype.push.apply(output, match.slice(1)); |
| 75 | + self = function (str, separator, limit) { |
| 76 | + // If `separator` is not a regex, use `nativeSplit` |
| 77 | + if (Object.prototype.toString.call(separator) !== "[object RegExp]") { |
| 78 | + return nativeSplit.call(str, separator, limit); |
| 79 | + } |
| 80 | + var output = [], |
| 81 | + flags = (separator.ignoreCase ? "i" : "") + |
| 82 | + (separator.multiline ? "m" : "") + |
| 83 | + (separator.extended ? "x" : "") + // Proposed for ES6 |
| 84 | + (separator.sticky ? "y" : ""), // Firefox 3+ |
| 85 | + lastLastIndex = 0, |
| 86 | + // Make `global` and avoid `lastIndex` issues by working with a copy |
| 87 | + separator = new RegExp(separator.source, flags + "g"), |
| 88 | + separator2, match, lastIndex, lastLength; |
| 89 | + str += ""; // Type-convert |
| 90 | + if (!compliantExecNpcg) { |
| 91 | + // Doesn't need flags gy, but they don't hurt |
| 92 | + separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); |
| 93 | + } |
| 94 | + /* Values for `limit`, per the spec: |
| 95 | + * If undefined: 4294967295 // Math.pow(2, 32) - 1 |
| 96 | + * If 0, Infinity, or NaN: 0 |
| 97 | + * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; |
| 98 | + * If negative number: 4294967296 - Math.floor(Math.abs(limit)) |
| 99 | + * If other: Type-convert, then use the above rules |
| 100 | + */ |
| 101 | + limit = limit === undef ? |
| 102 | + -1 >>> 0 : // Math.pow(2, 32) - 1 |
| 103 | + limit >>> 0; // ToUint32(limit) |
| 104 | + while (match = separator.exec(str)) { |
| 105 | + // `separator.lastIndex` is not reliable cross-browser |
| 106 | + lastIndex = match.index + match[0].length; |
| 107 | + if (lastIndex > lastLastIndex) { |
| 108 | + output.push(str.slice(lastLastIndex, match.index)); |
| 109 | + // Fix browsers whose `exec` methods don't consistently return `undefined` for |
| 110 | + // nonparticipating capturing groups |
| 111 | + if (!compliantExecNpcg && match.length > 1) { |
| 112 | + match[0].replace(separator2, function () { |
| 113 | + for (var i = 1; i < arguments.length - 2; i++) { |
| 114 | + if (arguments[i] === undef) { |
| 115 | + match[i] = undef; |
| 116 | + } |
126 | 117 | } |
127 | | - lastLength = match[0].length; |
128 | | - lastLastIndex = lastIndex; |
129 | | - if (output.length >= limit) { |
130 | | - break; |
131 | | - } |
132 | | - } |
133 | | - if (separator.lastIndex === match.index) { |
134 | | - separator.lastIndex++; // Avoid an infinite loop |
135 | | - } |
| 118 | + }); |
136 | 119 | } |
137 | | - if (lastLastIndex === str.length) { |
138 | | - if (lastLength || !separator.test("")) { |
139 | | - output.push(""); |
140 | | - } |
141 | | - } else { |
142 | | - output.push(str.slice(lastLastIndex)); |
| 120 | + if (match.length > 1 && match.index < str.length) { |
| 121 | + Array.prototype.push.apply(output, match.slice(1)); |
| 122 | + } |
| 123 | + lastLength = match[0].length; |
| 124 | + lastLastIndex = lastIndex; |
| 125 | + if (output.length >= limit) { |
| 126 | + break; |
143 | 127 | } |
144 | | - return output.length > limit ? output.slice(0, limit) : output; |
145 | | - }; |
146 | | - |
147 | | - return self; |
148 | | - |
149 | | - }(); |
| 128 | + } |
| 129 | + if (separator.lastIndex === match.index) { |
| 130 | + separator.lastIndex++; // Avoid an infinite loop |
| 131 | + } |
| 132 | + } |
| 133 | + if (lastLastIndex === str.length) { |
| 134 | + if (lastLength || !separator.test("")) { |
| 135 | + output.push(""); |
| 136 | + } |
| 137 | + } else { |
| 138 | + output.push(str.slice(lastLastIndex)); |
| 139 | + } |
| 140 | + return output.length > limit ? output.slice(0, limit) : output; |
| 141 | + }; |
| 142 | + return self; |
| 143 | + }(); |
150 | 144 |
|
| 145 | + var methods = { |
| 146 | + init: function(options) { |
| 147 | + options = options || {}; |
151 | 148 | return this.each(function() { |
152 | 149 | var e = $(this); |
153 | 150 | var data = e.data('counter') || {}; |
|
170 | 167 | part.limit = parseInt(value, 10); |
171 | 168 | part.value = parseInt(initial[initial.length - format.length + index] || 0, 10); |
172 | 169 | part.value = part.value > part.limit ? part.limit : part.value; |
| 170 | + part.reset = part.value; |
173 | 171 | part.stop = parseInt(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit), 10); |
174 | 172 | part.stop = part.stop > part.limit ? part.limit : part.stop; |
175 | 173 | part.stop = part.stop < 0 ? 0 : part.stop; |
|
178 | 176 | while (digits.length < part.padding) { |
179 | 177 | digits = '0' + digits; |
180 | 178 | } |
181 | | - for(var i=0; i < digits.length; i++) { |
182 | | - epart.append($('<span>').addClass('digit digit' + digits.charAt(i)).text(digits.charAt(i))); |
183 | | - } |
| 179 | + $.each(split(digits, ''), function(dindex, dvalue){ |
| 180 | + epart.append($('<span>').addClass('digit digit' + dvalue).text(dvalue)); |
| 181 | + }); |
184 | 182 | e.append(epart); |
185 | 183 | data.parts.push(part); |
186 | 184 | } else { |
|
196 | 194 | return this; |
197 | 195 | }); |
198 | 196 | }, |
| 197 | + play: function() { |
| 198 | + return this.each(function() { |
| 199 | + var e = $(this); |
| 200 | + var data = e.data('counter'); |
| 201 | + if (!data.intervalId) { |
| 202 | + data.intervalId = setInterval($.proxy(tick, this), data.interval); |
| 203 | + } |
| 204 | + }); |
| 205 | + }, |
| 206 | + reset: function() { |
| 207 | + return this.each(function() { |
| 208 | + var e = $(this); |
| 209 | + var data = e.data('counter'); |
| 210 | + $.each(data.parts, function(pindex, pvalue){ |
| 211 | + pvalue.value = pvalue.reset; |
| 212 | + }); |
| 213 | + refresh($(this), 0); |
| 214 | + if (data.intervalId) { |
| 215 | + clearInterval(data.intervalId); |
| 216 | + data.intervalId = setInterval($.proxy(tick, this), data.interval); |
| 217 | + } |
| 218 | + }); |
| 219 | + }, |
199 | 220 | stop: function() { |
200 | | - var e = $(this); |
201 | | - var data = e.data('counter'); |
202 | | - clearInterval(data.intervalId); |
203 | | - e.trigger("counterStop"); |
204 | | - return this; |
| 221 | + return this.each(function() { |
| 222 | + var e = $(this); |
| 223 | + var data = e.data('counter'); |
| 224 | + clearInterval(data.intervalId); |
| 225 | + data.intervalId = 0; |
| 226 | + e.trigger("counterStop"); |
| 227 | + }); |
205 | 228 | } |
206 | | - } |
| 229 | + }; |
207 | 230 |
|
208 | 231 | $.fn.counter = function(method) { |
209 | 232 | // Method calling logic |
|
0 commit comments