Skip to content

Commit 3d6c8c9

Browse files
committed
code cleanups
1 parent 9a9ab80 commit 3d6c8c9

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

‎flapper.jquery.js‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
(function($) {
2+
3+
var Flapper = function($ele, options) {
4+
var _this = this;
5+
this.id = Math.floor(Math.random() * 1000) + 1;
6+
this.$ele = $ele;
7+
this.options = $.extend({}, this.defaults, options);
8+
9+
this.$div = $('<div class="flapper"><div class="stripe"></div></div>');
10+
this.$ele.hide().after(this.$div);
11+
12+
this.$ele.change(function(){
13+
_this.update();
14+
});
15+
16+
this.init();
17+
}
18+
19+
Flapper.prototype = {
20+
defaults: {
21+
width: 6,
22+
framerate: 100,
23+
padding: '&nbsp;',
24+
commafy: false,
25+
prefix: null,
26+
hidepadding: false,
27+
comma: ','
28+
},
29+
30+
init: function() {
31+
this.currval = this.$ele.val() || 0;
32+
this.$digits = [];
33+
this.targets = [];
34+
this.positions = [];
35+
36+
if (this.options.padding == null) {
37+
this.options.padding = '&nbsp;';
38+
this.options.hidepadding = true;
39+
}
40+
41+
for (i=0; i<this.options.width; i++) {
42+
this.$digits[i] = $('<span class="digit">'+this.options.padding+'</span>');
43+
this.$div.prepend(this.$digits[i]);
44+
this.positions[i] = this.options.padding;
45+
}
46+
47+
this.update();
48+
},
49+
50+
update: function() {
51+
var _this = this;
52+
if (this.timer) {
53+
clearInterval(this.timer);
54+
}
55+
56+
this.setDisplayTargets();
57+
58+
this.timer = setInterval(function(){
59+
var changed = false;
60+
61+
for (var i=0; i<_this.options.width; i++) {
62+
var current = _this.positions[i];
63+
var target = _this.targets[i];
64+
65+
if (current != target) {
66+
if (!target && current == 0) {
67+
_this.setDigit(i, _this.options.padding);
68+
_this.positions[i] = target;
69+
70+
} else if (isNaN(target) && current == 0) {
71+
_this.setDigit(i, target);
72+
73+
} else {
74+
_this.incrementDigit(i);
75+
changed = true;
76+
}
77+
}
78+
}
79+
80+
if (!changed) {
81+
clearInterval(_this.timer);
82+
83+
for (var i=0; i<_this.options.width; i++) {
84+
if (_this.targets[i] == null) {
85+
_this.$digits[i].hide();
86+
}
87+
}
88+
}
89+
90+
}, this.options.framerate);
91+
92+
},
93+
94+
setDisplayTargets: function() {
95+
var _this = this;
96+
var val = this.$ele.val();
97+
var dnew = this.getDigits(val);
98+
var padout = this.options.padding &&
99+
this.options.padding != '&nbsp;';
100+
var prefixed = false;
101+
var i = 0;
102+
var d = 0;
103+
104+
do {
105+
if (d < dnew.length || padout) {
106+
if (_this.options.commafy && (i % 4 == 3)) {
107+
_this.targets[i] = _this.options.comma;
108+
continue;
109+
}
110+
}
111+
112+
if (d < dnew.length) {
113+
_this.targets[i] = dnew[d];
114+
d++;
115+
116+
} else {
117+
118+
if (_this.options.prefix) {
119+
120+
if (!prefixed && !padout) {
121+
_this.targets[i] = _this.options.prefix;
122+
prefixed = true;
123+
continue;
124+
}
125+
126+
if (!prefixed && i == _this.options.width - 1) {
127+
_this.targets[i] = _this.options.prefix;
128+
prefixed = true;
129+
continue;
130+
}
131+
132+
}
133+
134+
if (_this.options.hidepadding) {
135+
_this.targets[i] = null;
136+
} else {
137+
_this.targets[i] = _this.options.padding;
138+
}
139+
}
140+
141+
} while (++i < _this.options.width);
142+
},
143+
144+
incrementDigit: function(i) {
145+
var n = parseInt(this.positions[i], 10);
146+
if (isNaN(n)) {
147+
n = 0;
148+
} else {
149+
n++;
150+
if (n > 9) {
151+
n -= 10;
152+
}
153+
}
154+
155+
this.$digits[i].show().html(n+'');
156+
this.positions[i] = n;
157+
},
158+
159+
setDigit: function(i, v) {
160+
this.$digits[i].show().html(v);
161+
this.positions[i] = v;
162+
},
163+
164+
getDigits: function(val, length) {
165+
var ret = [];
166+
var num = val;
167+
168+
if (num == 0) {
169+
return [0];
170+
}
171+
172+
while (num > 0) {
173+
ret.push(num % 10);
174+
num = Math.floor(num / 10);
175+
}
176+
177+
return ret;
178+
},
179+
}
180+
181+
$.fn.flapper = function(options) {
182+
this.each(function(){
183+
new Flapper($(this), options);
184+
})
185+
}
186+
187+
})(jQuery);

0 commit comments

Comments
 (0)