-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathember-scrollable.js
501 lines (449 loc) · 14.2 KB
/
ember-scrollable.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
import Ember from 'ember';
import { computed } from '@ember/object';
import { deprecate } from '@ember/application/deprecations';
import { isPresent } from '@ember/utils';
import { inject as service } from '@ember/service';
import { scheduleOnce, debounce, throttle } from '@ember/runloop';
import Component from '@ember/component';
import InboundActionsMixin from 'ember-component-inbound-actions/inbound-actions';
import DomMixin from 'ember-lifeline/mixins/dom';
import layout from '../templates/components/ember-scrollable';
import { Horizontal, Vertical } from '../classes/scrollable';
const hideDelay = Ember.testing ? 16 : 1000;
const PAGE_JUMP_MULTIPLE = 7 / 8;
export const THROTTLE_TIME_LESS_THAN_60_FPS_IN_MS = 1; // 60 fps -> 1 sec / 60 = 16ms
const scrollbarSelector = '.tse-scrollbar';
const contentSelector = '.tse-content';
export default Component.extend(InboundActionsMixin, DomMixin, {
layout,
classNameBindings: [':ember-scrollable', ':tse-scrollable', 'horizontal', 'vertical'],
/**
* If true, a scrollbar will be shown horizontally
*
* @property horizontal
* @public
* @type Boolean
* @default false
*/
horizontal: null,
/**
* If true, a scrollbar will be shown vertically
*
* @property vertical
* @public
* @type Boolean
*/
vertical: null,
/**
* Indicates whether the scrollbar should auto hide after a given period of time (see hideDelay),
* or remain persitent alongside the content to be scrolled.
*
* @property autoHide
* @public
* @type Boolean
* @default true
*/
autoHide: true,
scrollBuffer: 50,
/**
* Number indicating offset from anchor point (top for vertical, left for horizontal) where the scroll handle
* should be rendered.
*
* @property scrollTo
* @public
* @type Number
*/
scrollTo: computed('vertical', {
get() {
return this.get('vertical') ? this.get('scrollToY') : this.get('scrollToX');
},
set(key, value) {
// TODO this is deprecated. remove eventually.
deprecate('Using the `scrollTo` property directly has been deprecated, please prefer being explicit by using `scrollToX` and `scrollToY`.');
const prop = this.get('vertical') ? 'scrollToY' : 'scrollToX';
this.set(prop, value);
return value;
}
}),
/**
* Position in pixels for which to scroll horizontal scrollbar.
*
* @property scrollToX
* @public
* @type Number
*/
scrollToX: 0,
/**
* Position in pixels for which to scroll vertical scrollbar.
*
* @property scrollToY
* @public
* @type Number
*/
scrollToY: 0,
/**
* Callback when the content is scrolled horizontally.
*
* @method onScrollX
* @public
* @type Function
*/
onScrollX() {},
/**
* Callback when the content is scrolled vertically.
*
* @method onScrollY
* @public
* @type Function
*/
onScrollY() {},
/**
* Local reference the horizontal scrollbar.
*
* @property horizontalScrollbar
* @private
*/
horizontalScrollbar: null,
/**
* Local reference the vertical scrollbar.
*
* @property verticalScrollbar
* @private
*/
verticalScrollbar: null,
scrollbarThickness: service(),
didReceiveAttrs() {
const horizontal = this.get('horizontal');
const vertical = this.get('horizontal');
// Keep backwards compatible functionality wherein vertical is default when neither vertical or horizontal are explicitly set
if (!horizontal && !isPresent(vertical)) {
this.set('vertical', true);
}
},
didInsertElement() {
this._super(...arguments);
this.setupElements();
scheduleOnce('afterRender', this, this.createScrollbarAndShowIfNecessary);
this.addEventListener(window, 'mouseup', this.endDrag);
this.setupResize();
},
willDestroyElement() {
this._super(...arguments);
this.$().off('transitionend webkitTransitionEnd', this._resizeHandler);
},
/**
* Inidcates that the horizontal scrollbar is dragging at this moment in time.
* @property isHorizontalDragging
* @private
*/
isHorizontalDragging: false,
/**
* Inidcates that the vertical scrollbar is dragging at this moment in time.
* @property isVerticalDragging
* @private
*/
isVerticalDragging: false,
/**
* Size in pixels of the handle within the horizontal scrollbar.
* Determined by a ration between the scroll content and the scroll viewport
*
* @property horizontalHandleSize
* @private
*/
horizontalHandleSize: null,
/**
* Size in pixels of the handle within the vertical scrollbar.
* Determined by a ration between the scroll content and the scroll viewport
*
* @property verticalHandleSize
* @private
*/
verticalHandleSize: null,
/**
* Amount in pixels offset from the anchor (leftmost point of horizontal scrollbar)
*
* @property horizontalHandleOffset
* @private
*/
horizontalHandleOffset: 0,
/**
* Amount in pixels offset from the anchor (topmost point of vertical scrollbar)
*
* @property verticalHandleOffest
* @private
*/
verticalHandleOffest: 0,
/**
*
* @property dragOffset
* @private
*/
dragOffset: 0,
contentSize(sizeAttr) {
return this._contentElement[sizeAttr]();
},
setupElements() {
this._contentElement = this.$(`${contentSelector}:first`);
},
/**
* Used to create/reset scrollbar(s) if they are necessary
*
* @method createScrollbarAndShowIfNecessary
*/
createScrollbarAndShowIfNecessary() {
this.createScrollbar().map((scrollbar) => {
this.checkScrolledToBottom(scrollbar);
if (scrollbar.isNecessary) {
this.showScrollbar();
}
});
},
_resizeHandler() {
debounce(this, this.resizeScrollbar, 16);
},
setupResize() {
this.addEventListener(window, 'resize', this._resizeHandler, true);
},
resizeScrollContent() {
const width = this.$().width();
const height = this.$().height();
const scrollbarThickness = this.get('scrollbarThickness.thickness');
const hasHorizontal = this.get('horizontal');
const hasVertical = this.get('vertical');
if (hasHorizontal && hasVertical) {
this.set('scrollContentWidth', width + scrollbarThickness);
this.set('scrollContentHeight', height + scrollbarThickness);
} else if (hasHorizontal) {
this.set('scrollContentWidth', width);
this.set('scrollContentHeight', height + scrollbarThickness);
this._contentElement.height(height);
} else {
this.set('scrollContentWidth', width + scrollbarThickness);
this.set('scrollContentHeight', height);
}
},
/**
* Creates the corresponding scrollbar(s) from the configured `vertical` and `horizontal` properties
*
* @method createScrollbar
* @return {Array} Scrollbar(s) that were created
*/
createScrollbar() {
if (this.get('isDestroyed')) {
return [];
}
const scrollbars = [];
this.resizeScrollContent();
if (this.get('vertical')) {
const verticalScrollbar = new Vertical({
scrollbarElement: this.$(`${scrollbarSelector}.vertical`),
contentElement: this._contentElement
});
this.set('verticalScrollbar', verticalScrollbar);
this.updateScrollbarAndSetupProperties(0, 'vertical');
scrollbars.push(verticalScrollbar);
}
if (this.get('horizontal')) {
const horizontalScrollbar = new Horizontal({
scrollbarElement: this.$(`${scrollbarSelector}.horizontal`),
contentElement: this._contentElement
});
this.set('horizontalScrollbar', horizontalScrollbar);
this.updateScrollbarAndSetupProperties(0, 'horizontal');
scrollbars.push(horizontalScrollbar);
}
return scrollbars;
},
/**
* Show the scrollbar(s) when the user moves within the scroll viewport
*
* @method mouseMove
* @private
*/
mouseMove() {
if (this.get('autoHide')) {
throttle(this, this.showScrollbar, THROTTLE_TIME_LESS_THAN_60_FPS_IN_MS);
}
},
/**
* Called on mouse up to indicate dragging is over.
*
* @method endDrag
* @param e
* @private
*/
endDrag(e) {
/* eslint no-unused-vars: 0 */
this.set('isVerticalDragging', false);
this.set('isHorizontalDragging', false);
},
/**
* Calculates and setups the correct handle position using the scrollbar offset and size
*
* @method updateScrollbarAndSetupProperties
* @param scrollOffset
* @param scrollbarDirection
* @private
*/
updateScrollbarAndSetupProperties(scrollOffset, scrollbarDirection) {
const { handleOffset, handleSize } = this.get(`${scrollbarDirection}Scrollbar`).getHandlePositionAndSize(scrollOffset);
this.set(`${scrollbarDirection}HandleOffset`, handleOffset);
this.set(`${scrollbarDirection}HandleSize`, handleSize);
},
/**
* Callback for the scroll event emitted by the container of the content that can scroll.
* Here we update the scrollbar to reflect the scrolled position.
*
* @method scrolled
* @param event
* @param scrollOffset
* @param scrollDirection 'vertical' or 'horizontal'
* @private
*/
scrolled(event, scrollOffset, scrollDirection) {
this.updateScrollbarAndSetupProperties(scrollOffset, scrollDirection);
this.showScrollbar();
this.checkScrolledToBottom(this.get(`${scrollDirection}Scrollbar`), scrollOffset);
const direction = scrollDirection === 'vertical' ? 'Y' : 'X';
this.get(`onScroll${direction}`)(scrollOffset);
// synchronize scrollToX / scrollToY
this.set(`scrollTo${direction}`, scrollOffset);
// TODO this is deprecated. remove eventually.
this.sendScroll(event, scrollOffset);
},
checkScrolledToBottom(scrollbar, scrollOffset) {
let scrollBuffer = this.get('scrollBuffer');
if (scrollbar.isScrolledToBottom(scrollBuffer, scrollOffset)) {
debounce(this, this.sendScrolledToBottom, 100);
}
},
sendScrolledToBottom() {
if (this.get('onScrolledToBottom')) {
this.get('onScrolledToBottom')();
}
},
sendScroll(event, scrollOffset) {
if (this.get('onScroll')) {
deprecate('Using the `onScroll` callback has deprecated in favor of the explicit `onScrollX` and `onScrollY callbacks');
this.get('onScroll')(scrollOffset, event);
}
},
resizeScrollbar() {
if (this.get('horizontalScrollbar')) {
this.updateScrollbarAndSetupProperties(this.get('scrollToX'), 'horizontal');
}
if (this.get('verticalScrollbar')) {
this.updateScrollbarAndSetupProperties(this.get('scrollToY'), 'vertical');
}
},
showScrollbar() {
if (this.get('isDestroyed')) {
return;
}
this.set('showHandle', true);
if (!this.get('autoHide')) {
return;
}
debounce(this, this.hideScrollbar, hideDelay);
},
hideScrollbar() {
if (this.get('isDestroyed')) {
return;
}
this.set('showHandle', false);
},
/**
* Sets scrollTo{X,Y} by using the ratio of offset to content size.
* Called when the handle in ember-scrollbar is dragged.
*
* @method updateScrollToProperty
* @param scrollProp {String} String indicating the scrollTo attribute to be updated ('scrollToX'|'scrollToY')
* @param dragPerc {Number} A Number from 0 - 1 indicating the position of the handle as percentage of the scrollbar
* @param sizeAttr {String} String indicating the attribute used to get to the size of the content ('height'|'width')
* @private
*/
updateScrollToProperty(scrollProp, dragPerc, sizeAttr) {
const srcollTo = dragPerc * this.contentSize(sizeAttr);
this.set(scrollProp, srcollTo);
},
/**
* Sets is{Horizontal,Vertical}Dragging to true or false when the handle starts or ends dragging
*
* @method toggleDraggingBoundary
* @param isDraggingProp 'isHorizontalDragging' or 'isVerticalDragging'
* @param startOrEnd true if starting to drag, false if ending
* @private
*/
toggleDraggingBoundary(isDraggingProp, startOrEnd) {
this.set(isDraggingProp, startOrEnd);
},
/**
* Jumps a page because user clicked on scroll bar not scroll bar handle.
*
* @method jumpScroll
* @param jumpPositive if true the user clicked between the handle and the end, if false, the user clicked between the
* anchor and the handle
* @param scrollToProp 'scrollToX' or 'scrollToY'
* @param sizeAttr 'width' or 'height'
* @private
*/
jumpScroll(jumpPositive, scrollToProp, sizeAttr) {
const scrollOffset = this.get(scrollToProp);
let jumpAmt = PAGE_JUMP_MULTIPLE * this.contentSize(sizeAttr);
let scrollPos = jumpPositive ? scrollOffset - jumpAmt : scrollOffset + jumpAmt;
this.set(scrollToProp, scrollPos);
},
actions: {
/**
* Update action should be called when size of the scroll area changes
*/
recalculate() {
// TODO this is effectively the same as `update`, except for update returns the passed in value. Keep one, and rename `resizeScrollbar` to be clear.
this.resizeScrollbar();
},
/**
* Can be called when scrollbars change as a result of value change,
*
* for example
* ```
* {{#as-scrollable as |scrollbar|}}
* {{#each (compute scrollbar.update rows) as |row|}}
* {{row.title}}
* {{/each}}
* {{/as-scrollable}}
* ```
*/
update(value) {
scheduleOnce('afterRender', this, this.resizeScrollbar);
return value;
},
/**
* Scroll Top action should be called when when the scroll area should be scrolled top manually
*/
scrollTop() {
// TODO some might expect the `scrollToY` action to be called here
this.set('scrollToY', 0);
},
scrolled() {
scheduleOnce('afterRender', this, 'scrolled', ...arguments);
},
horizontalDrag(dragPerc) {
scheduleOnce('afterRender', this, 'updateScrollToProperty', 'scrollToX', dragPerc, 'width');
},
verticalDrag(dragPerc) {
scheduleOnce('afterRender', this, 'updateScrollToProperty', 'scrollToY', dragPerc, 'height');
},
horizontalJumpTo(jumpPositive) {
this.jumpScroll(jumpPositive, 'scrollToX', 'width');
},
verticalJumpTo(jumpPositive) {
this.jumpScroll(jumpPositive, 'scrollToY', 'height');
},
horizontalDragBoundary(isStart) {
this.toggleDraggingBoundary('isHorizontalDragging', isStart);
},
verticalBoundaryEvent(isStart) {
this.toggleDraggingBoundary('isVerticalDragging', isStart);
}
}
});