-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ti.scroller.js
428 lines (343 loc) · 11.5 KB
/
ti.scroller.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
let scrollingViewCount = 1;
let scrollingViewsArray = [];
// Device values
let platformWidth = Ti.Platform.displayCaps.platformWidth;
let logicalDensityFactor = Ti.Platform.displayCaps.logicalDensityFactor;
let deviceWidth = (OS_IOS) ? platformWidth : Math.round(platformWidth / logicalDensityFactor);
function ScrollingView(_args) {
let _scrollingView = this;
if (!_args) return false;
// !Testing Some Events: EXPERIMENTAL
let event = {
paused: (source) => { },
resumed: (source) => { },
complete: (source) => { },
}
// Attributes
let debug = _args.debug ?? false;
let random = _args.random ?? false;
let autoplay = _args.autoplay ?? true;
let speed = _args.speed ? _args.speed * 10 : 50;
let id = _args.id ?? '__tiScrollingViewId' + scrollingViewCount++;
let delay = _args.delay ? _args.delay * 1000 : 0;
let name = _args.name ?? id;
let messages = Array.isArray(_args.messages) ? _args.messages : _args.messages ? [_args.messages] : [];
if (_args.message) {
messages = Array.isArray(_args.message) ? _args.message : _args.message ? [_args.message] : [];
}
// Local settings
let sideLabel;
let paused = false;
let currentMessage = -1;
let animationStatus = 'completed';
let defaultBackgroundColor = '#BF000000';
let backgroundColor = _args.backgroundColor ?? defaultBackgroundColor;
// The main Animation
let animation = Ti.UI.createAnimation({
curve: Ti.UI.ANIMATION_CURVE_LINEAR
});
animation.addEventListener('complete', () => {
logger('Complete event');
event.complete(this);
animationStatus = 'completed';
if (!paused) {
setTimeout(() => {
_scrollingView.play();
}, delay);
}
});
animation.addEventListener('start', () => {
animationStatus = 'playing';
});
// Methods
_scrollingView.update = function(_args) {
logger('Update method');
// Reset Background Color
backgroundColor = _args.backgroundColor ?? mainScrollingView.backgroundColor;
// Update Random
if (_args.random) {
random = true;
messages = messages.sort(() => Math.random() - 0.5);
}
// Update Delay
if (_args.delay >= 0) {
delay = _args.delay * 1000;
}
// Update Speed
if (_args.speed >= 0) {
speed = _args.speed * 10;
}
// debug mode
debug = _args.debug ?? debug;
// name
name = _args.name ?? name;
autoplay = _args.autoplay ?? autoplay;
// height
_args.height = _args.height ?? mainScrollingView.height;
// fontSize
if (!_args.font) {
_args.font = {
fontSize: scrollingLabel.font.fontSize,
fontFamily: scrollingLabel.font.fontFamily,
fontWeight: scrollingLabel.font.fontWeight,
}
} else {
_args.font.fontSize = _args.font.fontSize ?? scrollingLabel.font.fontSize;
_args.font.fontFamily = _args.font.fontFamily ?? scrollingLabel.font.fontFamily;
_args.font.fontWeight = _args.font.fontWeight ?? scrollingLabel.font.fontWeight;
}
// Update Scrolling View Label Properties
applyPropertiesToScrollingLabel(_args);
// Update Side Label Properties
applyPropertiesToSideLabel(_args, 250);
// Update Scrolling View Properties
applyPropertiesToMainScrollingView(_args, 250);
// Update Messages
if (_args.messages) {
_scrollingView.updateMessages(_args.messages);
} else if (_args.message) {
_scrollingView.updateMessages(_args.message);
}
}
_scrollingView.updateMessages = function(_messages) {
if (_messages === undefined || _messages === '') return false;
logger('Update messages method');
currentMessage = -1;
messages = Array.isArray(_messages) ? _messages : [_messages];
if (random) {
messages = messages.sort(() => Math.random() - 0.5);
}
if (autoplay && !paused) {
_scrollingView.play();
}
}
_scrollingView.updateMessage = _scrollingView.updateMessages;
_scrollingView.updateBackground = function(_backgroundColor) {
logger('Update backgorund method');
backgroundColor = _backgroundColor;
if (sideLabel) {
sideLabel.animate({
duration: 250,
backgroundColor: colorLuminance(backgroundColor, -0.20)
});
}
mainScrollingView.animate({
duration: 250,
backgroundColor: backgroundColor
});
};
_scrollingView.updateLabel = function(_label) {
if (_label && sideLabel) {
sideLabel.applyProperties({
text: ` ${_label} `,
});
}
}
_scrollingView.getView = function() {
return mainScrollingView;
}
_scrollingView.play = function() {
if (animationStatus === 'completed') {
paused = false;
play();
}
}
_scrollingView.animate = function() {
logger('`animate()` method is DEPRECATED: Will be deleted in v.2.0.0', true);
}
_scrollingView.pause = function() {
logger('Pause method');
event.paused(this);
paused = true;
}
_scrollingView.resume = function() {
logger('Resume method');
event.resumed(this);
paused = false;
_scrollingView.play();
}
// Main View
let mainScrollingView = Ti.UI.createView({
width: Ti.UI.FILL,
height: Ti.UI.SIZE
});
let scrollingLabel = Ti.UI.createLabel({
height: Ti.UI.SIZE,
right: deviceWidth
});
mainScrollingView.add(scrollingLabel);
// Expose Methods for Alloy Projects
mainScrollingView.play = _scrollingView.play;
mainScrollingView.pause = _scrollingView.pause;
mainScrollingView.resume = _scrollingView.resume;
mainScrollingView.update = _scrollingView.update;
mainScrollingView.updateLabel = _scrollingView.updateLabel;
mainScrollingView.updateMessage = _scrollingView.updateMessage;
mainScrollingView.updateMessages = _scrollingView.updateMessages;
mainScrollingView.updateBackground = _scrollingView.updateBackground;
// !DEPRECATED: Will be deleted in v.2.0.0
mainScrollingView.animate = _scrollingView.animate;
if (_args.shadow) {
mainScrollingView.applyProperties({
viewShadowOffset: { x: 0, y: 2 }, viewShadowRadius: 4, viewShadowColor: defaultBackgroundColor
});
}
// More Properties
if (random) {
messages = messages.sort(() => Math.random() - 0.5);
}
// Optional sidelabel
if (_args.label) {
addSidelabel(_args);
}
applyPropertiesToScrollingLabel(_args);
applyPropertiesToMainScrollingView(_args);
if (_args.autoplay || _args.message || _args.messages) {
_scrollingView.play();
}
// !Helper Functions
function play() {
if (messages.length && autoplay) {
logger('Play method');
(currentMessage < messages.length - 1) ? currentMessage++ : currentMessage = 0;
// Just to recalculate the width of the new text
let _label = Ti.UI.createLabel({
height: Ti.UI.SIZE,
text: messages[currentMessage],
font: { fontFamily: scrollingLabel.font.fontFamily ?? undefined, fontSize: scrollingLabel.font.fontSize ?? '14dp', fontWeight: scrollingLabel.font.fontWeight ?? 'normal' }
});
let width = Math.round(1 + _label.toImage().width / logicalDensityFactor);
scrollingLabel.applyProperties({
width: width,
left: deviceWidth,
text: messages[currentMessage],
});
animation.applyProperties({
left: -parseInt(width),
duration: 1000 * ((width + deviceWidth) / speed),
});
// timeout in order to prevent this Warning:
// [WARN] New layout set while view [object TiUILabel] animating: Will relayout after animation.
setTimeout(() => {
scrollingLabel.animate(animation);
}, 500);
}
}
function addSidelabel(_args) {
logger('Add side label');
sideLabel = Ti.UI.createLabel({
left: 0,
width: Ti.UI.SIZE,
verticalAlign: Ti.UI.TEXT_VERTICAL_ALIGNMENT_CENTER,
viewShadowOffset: { x: 4, y: 0 }, viewShadowRadius: 4, viewShadowColor: defaultBackgroundColor
});
applyPropertiesToSideLabel(_args);
mainScrollingView.add(sideLabel);
}
function applyPropertiesToMainScrollingView(_args, duration = 0) {
logger('Apply properties to scrolling view');
if (duration) {
mainScrollingView.animate({
duration: duration,
height: _args.height ?? 28,
top: _args.top ?? undefined,
backgroundColor: backgroundColor,
bottom: _args.bottom ?? undefined
});
} else {
mainScrollingView.applyProperties({
height: _args.height ?? 28,
top: _args.top ?? undefined,
backgroundColor: backgroundColor,
bottom: _args.bottom ?? undefined
});
}
}
function applyPropertiesToScrollingLabel(_args) {
logger('Apply properties to scrolling view’s label');
scrollingLabel.applyProperties({
height: _args.height ?? 28,
color: _args.color ?? '#fff',
font: { fontFamily: (_args.font && _args.font.fontFamily) ?? undefined, fontSize: (_args.font && _args.font.fontSize) ?? '14dp', fontWeight: (_args.font && _args.font.fontWeight) ?? 'normal' }
});
}
function applyPropertiesToSideLabel(_args, duration = 0) {
logger('Apply properties to side label');
if (sideLabel) {
if (duration) {
applyPropertiesToSideLabelWithAnimation(_args, duration);
} else {
sideLabel.applyProperties({
height: _args.height ?? 28,
color: _args.color ?? '#fff',
backgroundColor: colorLuminance(backgroundColor, -0.20),
text: _args.label === '' ? '' : (_args.label === undefined) ? sideLabel.text : ` ${_args.label} `,
font: { fontFamily: (_args.font && _args.font.fontFamily) ?? undefined, fontSize: (_args.font && _args.font.fontSize) ?? '14dp', fontWeight: 'bold' },
});
}
} else {
addSidelabel(_args);
}
}
function applyPropertiesToSideLabelWithAnimation(_args, duration = 0) {
logger('Apply properties to side label with animation');
sideLabel.applyProperties({
text: _args.label === '' ? '' : (_args.label === undefined) ? sideLabel.text : ` ${_args.label} `,
font: { fontFamily: (_args.font && _args.font.fontFamily) ?? undefined, fontSize: (_args.font && _args.font.fontSize) ?? '14dp', fontWeight: 'bold' }
});
sideLabel.animate({
duration: duration,
height: _args.height ?? 28,
color: _args.color ?? '#fff',
backgroundColor: colorLuminance(backgroundColor, -0.20),
});
}
function logger(_message, forceLog = false) {
if (debug || forceLog) console.warn(`:: ti.scroller :: ${name}: ${_message}`);
}
scrollingViewsArray.push(mainScrollingView);
return _scrollingView;
}
module.exports = ScrollingView;
// !Event Orientation Change Listener
Ti.Gesture.addEventListener('orientationchange', (e) => {
if (e.orientation === Ti.UI.PORTRAIT || e.orientation === Ti.UI.UPSIDE_PORTRAIT || e.orientation === Ti.UI.LANDSCAPE_LEFT || e.orientation === Ti.UI.LANDSCAPE_RIGHT) {
if (e.orientation === Ti.UI.PORTRAIT || e.orientation === Ti.UI.UPSIDE_PORTRAIT) {
platformWidth = Ti.Platform.displayCaps.platformWidth;
} else if (e.orientation === Ti.UI.LANDSCAPE_LEFT || e.orientation === Ti.UI.LANDSCAPE_RIGHT) {
platformWidth = (OS_IOS) ? Ti.Platform.displayCaps.platformWidth : Ti.Platform.displayCaps.platformHeight;
}
deviceWidth = (OS_IOS) ? platformWidth : Math.round(platformWidth / logicalDensityFactor);
}
});
Ti.App.addEventListener('paused', function() {
scrollingViewsArray.forEach(_scrollingView => {
_scrollingView.pause();
});
});
Ti.App.addEventListener('resume', function() {
scrollingViewsArray.forEach(_scrollingView => {
_scrollingView.resume();
});
});
function colorLuminance(_hex, _lum) {
// validate _hex string
_hex = String(_hex).replace(/[^0-9a-f]/gi, '');
if (_hex.length < 6) {
_hex = _hex[0] + _hex[0] + _hex[1] + _hex[1] + _hex[2] + _hex[2];
}
_lum = _lum || 0;
// convert to decimal and change luminosity
let rgb = '#',
c, i;
for (i = 0; i < 3; i++) {
c = parseInt(_hex.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, c + c * _lum), 255)).toString(16);
rgb += ('00' + c).substr(c.length);
}
return rgb;
}
// ScrollingView Component for Alloy Projects
module.exports.createScrollingView = (args) => {
return new ScrollingView(args).getView();
};