-
Notifications
You must be signed in to change notification settings - Fork 19
/
widget.js
257 lines (215 loc) · 6.47 KB
/
widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var measurement = require('alloy/measurement'),
init = false,
value = 0, // current progress value (0 ... 100)
options;
var defaults = {
width: 100,
height: 100,
backgroundColor: '#fff',
progressColor: '#000',
progressBackgroundColor: '#666',
progressBackgroundGradient: undefined,
formatValue: function(v){return v;},
showText: false
};
if (arguments[0]){
createView(arguments[0]);
}
function createView(_args){
options = _.defaults(_args, defaults);
// we need a square
if (options.width !== options.height){
options.height = options.width;
}
// container properties
["width", "height", "left", "right", "top", "bottom", "zIndex", "backgroundColor"].forEach(function(prop){
_.has(options, prop) && ($.container[prop] = options[prop]);
});
// add margin
if (_.has(options, "margin")){
$.container.width += options.margin;
$.container.height += options.margin;
}
// make it a circle
$.container.setBorderRadius((OS_ANDROID ? measurement.dpToPX($.container.width) : $.container.width) / 2);
// adjust the layers dimensions (half width)
["leftlayer", "innerrotationlayer", "rightlayer"].forEach(function(layer){
$[layer].width = options.width / 2;
$[layer].height = options.height;
});
// adjust the layers dimensions (full width)
["baselayer", "progresslayer", "rotationlayer"].forEach(function(layer){
$[layer].width = options.width;
$[layer].height = options.height;
});
if (options.progressWidth){
// the wider the progressWidth, the narrower the centerlayer
$.centerlayer.width = options.width - options.progressWidth;
$.centerlayer.height = options.height - options.progressWidth;
} else {
$.centerlayer.visible = false;
}
// layer positioning
$.leftlayer.left = 0;
$.rightlayer.right = 0;
$.rotationlayer.left = 0;
$.innerrotationlayer.right = 0;
// layer colors
$.progresslayer.backgroundColor = options.progressColor;
$.progresslayer.backgroundGradient = options.progressGradient;
$.leftlayer.backgroundColor = options.progressBackgroundColor || options.backgroundColor;
$.leftlayer.backgroundGradient = options.progressBackgroundGradient;
$.innerrotationlayer.backgroundColor = options.progressBackgroundColor || options.backgroundColor;
$.innerrotationlayer.backgroundGradient = options.progressBackgroundGradient;
$.rightlayer.backgroundColor = options.progressColor;
$.rightlayer.backgroundGradient = options.progressGradient;
$.centerlayer.backgroundColor = options.backgroundColor;
// squares become circles like
["baselayer", "progresslayer", "centerlayer"].forEach(function(layer){
var layerwidth = OS_ANDROID ? measurement.dpToPX($[layer].width) : $[layer].width;
$[layer].borderRadius = layerwidth / 2;
});
// style label
if (options.showText){
$.label.font = options.font;
$.label.color = options.color;
}
// we're ready to go
init = true;
// set initial value
setValue(options.value || 0);
// make this methods public to the view
$.container.setValue = setValue;
$.container.getValue = getValue;
$.container.animate = animate;
return $.container;
};
/**
* Updates the progress view
**/
function updateUi(){
if (!init) return;
// get the angle from percentage value
var angle = parseFloat(value / 100 * 360);
$.rightlayer.setVisible( angle > 180 );
$.leftlayer.setVisible( angle <= 180 );
// rotation
$.rotationlayer.transform = Ti.UI.create2DMatrix().rotate(angle);
// update label
options.showText && value > 0 && $.label.setText(options.formatValue(value));
$.label.visible = options.showText;
}
/**
* returns the current value
* @return {Number} value
**/
function getValue(){
return value;
}
/**
* sets the current value
* @param {Number} value
**/
function setValue(_value){
if (_.isNumber(_value) && _value >= 0 && _value <= 100){
value = _value;
updateUi();
} else {
Ti.API.error("[circularprogress]: value (was "+_value+") must be a number between 0 and 100");
}
}
/**
* dynamically set the text
* @param {String} _text
*/
function setText(_text){
options.showText && ($.label.text = _text);
updateUi();
}
/**
* get the label text
*/
function getText(){
return $.label.text;
}
/**
* dynamically set the progress color
* @param {String} _color
*/
function setProgressColor(_color){
$.progresslayer.backgroundColor = _color;
$.rightlayer.backgroundColor = _color;
}
/**
* dynamically set the progress backgroundColor
* @param {String} _color
*/
function setProgressBackgroundColor(_color){
$.leftlayer.backgroundColor = _color;
$.innerrotationlayer.backgroundColor = _color;
}
/**
* dynamically set the visibility of text
* @param {Boolean} _flag
*/
function setShowText(_flag){
options.showText = !!_flag;
updateUi();
}
/**
* animate to a given value
* @param {Object} _args
*/
function animate(_args, _callback){
var value = _args.value || 0;
var duration = _args.duration || 100;
var angle = parseFloat(value / 100 * 360);
var callback = _callback || function(){};
// create the animation
var animation = Ti.UI.createAnimation({
duration: duration,
transform: Ti.UI.create2DMatrix().rotate(angle)
});
animation.addEventListener("complete", function onAnimationComplete(e){
callback();
animation.removeEventListener("complete", onAnimationComplete);
});
if (value > 50){
// find the timeout to switch the layers
// as we can't listen to the animation progress
var switchTimeout = duration * 100 / (value * 2);
setTimeout(function(){
$.rightlayer.setVisible(true);
$.leftlayer.setVisible(false);
}, switchTimeout);
}
// workaround https://jira.appcelerator.org/browse/TIMOB-17260
JSON.stringify($.rotationlayer.size);
// animate it
$.rotationlayer.animate(animation);
// update label
options.showText && $.label.setText(options.formatValue(value));
}
Object.keys(defaults).forEach(function(key){
Object.defineProperty($, key, {
get: function(){ return options[key]; },
set: function(value){ options[key] = value; }
});
});
function hide(){
$.container.hide();
}
function show(){
$.container.show();
}
exports.createView = createView;
exports.setValue = setValue;
exports.getValue = getValue;
exports.animate = animate;
exports.setText = setText;
exports.getText = getText;
exports.setProgressColor = setProgressColor;
exports.setProgressBackgroundColor = setProgressBackgroundColor;
exports.setShowText = setShowText;
exports.hide = hide;
exports.show = show;