-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSlideable.js
700 lines (631 loc) · 14.7 KB
/
Slideable.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/**
* Contains the declaration for the {@link module:layout/Slideable~Slideable} kind.
* @module layout/Slideable
*/
var
kind = require('enyo/kind'),
dom = require('enyo/dom'),
platform = require('enyo/platform'),
Animator = require('enyo/Animator'),
Control = require('enyo/Control');
/**
* Fires when the Slideable finishes animating.
*
* @event module:layout/Slideable~Slideable#onAnimateFinish
* @type {enyo.Animator}
* @public
*/
/**
* Fires when the position (i.e., [value]{@link module:layout/Slideable~Slideable#value}) of the
* Slideable changes.
*
* @event module:layout/Slideable~Slideable#onChange
* @type {Object}
* @public
*/
/**
* {@link module:layout/Slideable~Slideable} is a control that may be dragged either horizontally
* or vertically between a minimum and a maximum value. When released from
* dragging, a Slideable will animate to its minimum or maximum position,
* depending on the direction of the drag.
*
* The [min]{@link module:layout/Slideable~Slideable#min} value specifies a position to the left of,
* or above, the initial position, to which the Slideable may be dragged.
* The [max]{@link module:layout/Slideable~Slideable#max} value specifies a position to the right of,
* or below, the initial position, to which the Slideable may be dragged.
* The [value]{@link module:layout/Slideable~Slideable#value} property specifies the current position
* of the Slideable, between the minimum and maximum positions.
*
* `min`, `max`, and `value` may be specified in units of 'px' or '%'.
*
* The [axis]{@link module:layout/Slideable~Slideable#axis} property determines whether the Slideable
* slides left-to-right ('h') or up-and-down ('v').
*
* The following control is placed 90% off the screen to the right, and slides
* to its natural position:
*
* ```javascript
* var
* kind = require('enyo/kind'),
* Slideable = require('layout/Slideable');
*
* {kind: Slideable, value: -90, min: -90, unit: '%',
* classes: 'enyo-fit', style: 'width: 300px;',
* components: [
* {content: 'stuff'}
* ]
* }
* ```
*
* NOTE: If Slideable is used with [Accessibility]{@link module:enyo/AccessibilitySupport} the focus
* event can cause the screen to scroll as the browser attempts to position the contents on the
* screen. To prevent this, in the container of the Slideable set the `accessibilityPreventScroll`
* property to `true`:
*
* ```javascript
* accessibilityPreventScroll: true
* ```
*
* This issue is not unique to Slideable and can occur with other controls that extend beyond the
* viewport.
*
* @class Slideable
* @extends module:enyo/Control~Control
* @ui
* @public
*/
module.exports = kind(
/** @lends module:layout/Slideable~Slideable.prototype */ {
/**
* @private
*/
name: 'enyo.Slideable',
/**
* @private
*/
kind: Control,
/**
* @lends module:layout/Slideable~Slideable.prototype
* @private
*/
published: {
/**
* Direction of sliding; valid values are `'h'` for horizonal or `'v'` for vertical.
*
* @type {String}
* @default 'h'
* @public
*/
axis: 'h',
/**
* Current position of the Slideable (a value between
* [min]{@link module:layout/Slideable~Slideable#min} and [max]{@link module:layout/Slideable~Slideable#max}).
*
* @type {Number}
* @default 0
* @public
*/
value: 0,
/**
* Unit for [min]{@link module:layout/Slideable~Slideable#min}, [max]{@link module:layout/Slideable~Slideable#max},
* and [value]{@link module:layout/Slideable~Slideable#value}; valid values are `'px'` or `'%'`.
*
* @type {String}
* @default 'px'
* @public
*/
unit: 'px',
/**
* The minimum value to slide to.
*
* @type {Number}
* @default 0
* @public
*/
min: 0,
/**
* The maximum value to slide to.
*
* @type {Number}
* @default 0
* @public
*/
max: 0,
/**
* When truthy, applies CSS styles to allow GPU compositing of slideable
* content, if allowed by the platform.
*
* @type {String}
* @default 'auto'
* @public
*/
accelerated: 'auto',
/**
* Set to `false` to prevent the Slideable from dragging with elasticity
* past its [min]{@link module:layout/Slideable~Slideable#min} or [max]{@link module:layout/Slideable~Slideable#max}
* value.
*
* @type {Boolean}
* @default true
* @public
*/
overMoving: true,
/**
* Indicates whether dragging is allowed. Set to `false` to disable dragging.
*
* @type {Boolean}
* @default true
* @public
*/
draggable: true
},
/**
* @private
*/
events: {
onAnimateFinish: '',
onChange: ''
},
/**
* Set to `true` to prevent drag events from bubbling beyond the Slideable.
*
* @private
*/
preventDragPropagation: false,
/**
* @private
*/
tools: [
{kind: Animator, onStep: 'animatorStep', onEnd: 'animatorComplete'}
],
/**
* @private
*/
handlers: {
ondragstart: 'dragstart',
ondrag: 'drag',
ondragfinish: 'dragfinish'
},
/**
* @private
*/
kDragScalar: 1,
/**
* @private
*/
dragEventProp: 'dx',
/**
* @private
*/
unitModifier: false,
/**
* @private
*/
canTransform: false,
/**
* Indicates which property of the drag event is used to position the control.
*
* @private
*/
dragMoveProp: 'dx',
/**
* Indicates which property of the drag event is used to allow dragging.
*
* @private
*/
shouldDragProp: 'horizontal',
/**
* The transform property to modify, provided that
* [canTransform]{@link module:layout/Slideable~Slideable#canTransform} is `true`.
*
* @private
*/
transform: 'translateX',
/**
* The dimension attribute to modify; will be either `'height'` or `'width'`.
*
* @private
*/
dimension: 'width',
/**
* The position attribute to modify; will be either `'top'` or `'left'`.
*
* @private
*/
boundary: 'left',
/**
* @method
* @private
*/
create: kind.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
this.acceleratedChanged();
this.transformChanged();
this.axisChanged();
this.valueChanged();
this.addClass('enyo-slideable');
};
}),
/**
* @method
* @private
*/
initComponents: kind.inherit(function (sup) {
return function () {
this.createComponents(this.tools);
sup.apply(this, arguments);
};
}),
/**
* @method
* @private
*/
rendered: kind.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
this.canModifyUnit();
this.updateDragScalar();
};
}),
/**
* @method
* @private
*/
handleResize: kind.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
this.updateDragScalar();
};
}),
/**
* If transforms can't be used and inline style is using 'px' while
* [unit]{@link module:layout/Slideable~Slideable#unit} is `'%'`, this sets the
* [unitModifier]{@link module:layout/Slideable~Slideable#unitModifier} property to the current
* value of [dimension]{@link module:layout/Slideable~Slideable#dimension}.
*
* @private
*/
canModifyUnit: function () {
if (!this.canTransform) {
var b = this.getInitialStyleValue(this.hasNode(), this.boundary);
// If inline style of 'px' exists, while unit is '%'
if (b.match(/px/i) && (this.unit === '%')) {
// Set unitModifier - used to over-ride '%'
this.unitModifier = this.getBounds()[this.dimension];
}
}
},
/**
* @private
*/
getInitialStyleValue: function (node, boundary) {
var s = dom.getComputedStyle(node);
if (s) {
return s.getPropertyValue(boundary);
}
return '0';
},
/**
* @private
*/
updateBounds: function (value, dimensions) {
var bounds = {};
bounds[this.boundary] = value;
this.setBounds(bounds, this.unit);
this.setInlineStyles(value, dimensions);
},
/**
* @private
*/
updateDragScalar: function () {
if (this.unit == '%') {
var d = this.getBounds()[this.dimension];
this.kDragScalar = d ? 100 / d : 1;
if (!this.canTransform) {
this.updateBounds(this.value, 100);
}
}
},
/**
* @private
*/
transformChanged: function () {
this.canTransform = dom.canTransform();
},
/**
* @private
*/
acceleratedChanged: function () {
if (!platform.android || platform.android <= 2) {
dom.accelerate(this, this.accelerated);
}
},
/**
* @private
*/
axisChanged: function () {
var h = this.axis == 'h';
this.dragMoveProp = h ? 'dx' : 'dy';
this.shouldDragProp = h ? 'horizontal' : 'vertical';
this.transform = h ? 'translateX' : 'translateY';
this.dimension = h ? 'width' : 'height';
this.boundary = h ? 'left' : 'top';
},
/**
* @private
*/
setInlineStyles: function (value, dimensions) {
var inBounds = {};
if (this.unitModifier) {
inBounds[this.boundary] = this.percentToPixels(value, this.unitModifier);
inBounds[this.dimension] = this.unitModifier;
this.setBounds(inBounds);
} else {
if (dimensions) {
inBounds[this.dimension] = dimensions;
} else {
inBounds[this.boundary] = value;
}
this.setBounds(inBounds, this.unit);
}
},
/**
* @fires module:layout/Slideable~Slideable#onChange
* @private
*/
valueChanged: function (last) {
var v = this.value;
if (this.isOob(v) && !this.isAnimating()) {
this.value = this.overMoving ? this.dampValue(v) : this.clampValue(v);
}
// FIXME: android cannot handle nested compositing well so apply acceleration only if needed
// desktop chrome doesn't like this code path so avoid...
if (platform.android > 2) {
if (this.value) {
if (last === 0 || last === undefined) {
dom.accelerate(this, this.accelerated);
}
} else {
dom.accelerate(this, false);
}
}
// If platform supports transforms
if (this.canTransform) {
dom.transformValue(this, this.transform, this.value + this.unit);
// else update inline styles
} else {
this.setInlineStyles(this.value, false);
}
this.doChange();
},
/**
* @private
*/
getAnimator: function () {
return this.$.animator;
},
/**
* @private
*/
isAtMin: function () {
return this.value <= this.calcMin();
},
/**
* @private
*/
isAtMax: function () {
return this.value >= this.calcMax();
},
/**
* @private
*/
calcMin: function () {
return this.min;
},
/**
* @private
*/
calcMax: function () {
return this.max;
},
/**
* @private
*/
clampValue: function (value) {
var min = this.calcMin();
var max = this.calcMax();
return Math.max(min, Math.min(value, max));
},
/**
* @private
*/
dampValue: function (value) {
return this.dampBound(this.dampBound(value, this.min, 1), this.max, -1);
},
/**
* @private
*/
dampBound: function (value, boundary, sign) {
var v = value;
if (v * sign < boundary * sign) {
v = boundary + (v - boundary) / 4;
}
return v;
},
/**
* Calculates the pixel value corresponding to the specified `percent` and
* `dimension`.
*
* @param {Number} percent
* @param {Number} dimension
*
* @return {Number}
* @private
*/
percentToPixels: function (percent, dimension) {
return Math.floor((dimension / 100) * percent);
},
/**
* @private
*/
pixelsToPercent: function (value) {
var boundary = this.unitModifier ? this.getBounds()[this.dimension] : this.container.getBounds()[this.dimension];
return (value / boundary) * 100;
},
/**
* @private
*/
shouldDrag: function (event) {
return this.draggable && event[this.shouldDragProp];
},
/**
* Determines whether the specified value is out of bounds (i.e., greater than
* [max]{@link module:layout/Slideable~Slideable#max} or less than [min]{@link module:layout/Slideable~Slideable#min}).
*
* @param {Number} value - The value to check.
* @return {Boolean} `true` if `value` is out of bounds; otherwise, `false`.
* @private
*/
isOob: function (value) {
return value > this.calcMax() || value < this.calcMin();
},
/**
* @private
*/
dragstart: function (sender, event) {
if (this.shouldDrag(event)) {
event.preventDefault();
this.$.animator.stop();
event.dragInfo = {};
this.dragging = true;
this.drag0 = this.value;
this.dragd0 = 0;
return this.preventDragPropagation;
}
},
/**
* Updates [value]{@link module:layout/Slideable~Slideable#value} during a drag and determines the
* direction of the drag.
*
* @private
*/
drag: function (sender, event) {
if (this.dragging) {
event.preventDefault();
var d = this.canTransform ? event[this.dragMoveProp] * this.kDragScalar : this.pixelsToPercent(event[this.dragMoveProp]);
var v = this.drag0 + d;
var dd = d - this.dragd0;
this.dragd0 = d;
if (dd) {
event.dragInfo.minimizing = dd < 0;
}
this.setValue(v);
return this.preventDragPropagation;
}
},
/**
* @private
*/
dragfinish: function (sender, event) {
if (this.dragging) {
this.dragging = false;
this.completeDrag(event);
event.preventTap();
return this.preventDragPropagation;
}
},
/**
* Animates the control to either the [min]{@link module:layout/Slideable~Slideable#min} or
* [max]{@link module:layout/Slideable~Slideable#max} value when dragging completes, based on the
* direction of the drag (determined in [drag()]{@link module:layout/Slideable~Slideable#drag}).
*
* @private
*/
completeDrag: function (event) {
if (this.value !== this.calcMax() && this.value != this.calcMin()) {
this.animateToMinMax(event.dragInfo.minimizing);
}
},
/**
* @private
*/
isAnimating: function () {
return this.$.animator.isAnimating();
},
/**
* @private
*/
play: function (start, end) {
this.$.animator.play({
startValue: start,
endValue: end,
node: this.hasNode()
});
},
/**
* Animates to the given value.
*
* @param {Number} value - The value to animate to.
* @public
*/
animateTo: function (value) {
this.play(this.value, value);
},
/**
* Animates to the [minimum]{@link module:layout/Slideable~Slideable#min} value.
*
* @public
*/
animateToMin: function () {
this.animateTo(this.calcMin());
},
/**
* Animates to the [maximum]{@link module:layout/Slideable~Slideable#max} value.
*
* @public
*/
animateToMax: function () {
this.animateTo(this.calcMax());
},
/**
* Helper method to toggle animation to either the [min]{@link module:layout/Slideable~Slideable#min}
* or [max]{@link module:layout/Slideable~Slideable#max} value.
*
* @param {Boolean} min - Whether to animate to the minimum value.
* @private
*/
animateToMinMax: function (min) {
if (min) {
this.animateToMin();
} else {
this.animateToMax();
}
},
/**
* Updates the [value]{@link module:layout/Slideable~Slideable#value} property during animation.
*
* @private
*/
animatorStep: function (sender) {
this.setValue(sender.value);
return true;
},
/**
* @fires module:layout/Slideable~Slideable#onAnimateFinish
* @private
*/
animatorComplete: function (sender) {
this.doAnimateFinish(sender);
return true;
},
/**
* Toggles animation to either the [min]{@link module:layout/Slideable~Slideable#min} or
* [max]{@link module:layout/Slideable~Slideable#max} value.
*
* @public
*/
toggleMinMax: function () {
this.animateToMinMax(!this.isAtMin());
}
});