-
Notifications
You must be signed in to change notification settings - Fork 28
/
Scrollable.js
235 lines (218 loc) · 7.24 KB
/
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
/** @module delite/Scrollable */
define([
"dcl/dcl",
"./ScrollAnimation",
"./Widget",
"requirejs-dplugins/css!./Scrollable/Scrollable.css"
], function (dcl, ScrollAnimation, Widget) {
/**
* A mixin which adds scrolling capabilities to a widget.
*
* When mixed into a host widget, this mixin brings scrolling capabilities
* based on the overflow: auto CSS property.
*
* By default, the scrolling capabilities are added to the widget
* node itself. The host widget can chose the node thanks to the property
* `scrollableNode` which must be set at latest in its `initializeRendering()`
* method.
*
* During interactive or programmatic scrolling, native "scroll"
* events are emitted, and can be listened to as follows (here,
* `scrollWidget` is the widget into which this mixin is mixed):
*
* ```js
* scrollWidget.on("scroll", function (event) {
* ...
* });
* ```
*
* For widgets that customize the `scrollableNode` property,
* the events should be listened to on `widget.scrollableNode`:
*
* ```js
* scrollWidget.on("scroll", function (event) {
* ...
* }, scrollWidget.scrollableNode);
* ```
*
* @mixin module:delite/Scrollable
* @augments module:delite/Widget
*/
return dcl(Widget, /** @lends module:delite/Scrollable# */{
declaredClass: "delite/Scrollable",
/**
* The direction of the interactive scroll. Possible values are:
* "vertical", "horizontal", "both, and "none".
*
* Note that scrolling programmatically using `scrollTo()` is
* possible on both horizontal and vertical directions independently
* on the value of `scrollDirection`.
* @member {string}
* @default "vertical"
*/
scrollDirection: "vertical",
/**
* Designates the descendant node of this widget which is made scrollable.
*
* If not set otherwise before the `initializeRendering()`
* method of the mixin is executed, it is set by default to this widget
* itself (`this`).
*
* Note that this property can be set only at construction time, at latest
* in the `initializeRendering()` method of the host widget into which this class is mixed.
* It should not be changed afterwards.
* Typically, this property can be set by a host widget which needs scrolling
* capabilities on one its descendant nodes.
*
* @member {Element}
* @default null
*/
scrollableNode: null,
afterInitializeRendering: function () {
if (!this.scrollableNode) {
this.scrollableNode = this; // If unspecified, defaults to 'this'.
}
},
refreshRendering: function (oldVals) {
if ("scrollDirection" in oldVals) {
this.scrollableNode.classList.toggle("d-scrollable", this.scrollDirection !== "none");
this.scrollableNode.classList.toggle(
"d-scrollable-h", /^(both|horizontal)$/.test(this.scrollDirection));
this.scrollableNode.classList.toggle("d-scrollable-v", /^(both|vertical)$/.test(this.scrollDirection));
}
},
disconnectedCallback: function () {
this._stopAnimation();
},
/**
* Returns true if container's scroll has reached the maximum limit at
* the top of the contents. Returns false otherwise.
* @example
* var scrollableNode = scrollableWidget.scrollableNode;
* scrollableNode.on("scroll", function () {
* if (scrollableWidget.isTopScroll()) {
* console.log("Scroll reached the maximum limit at the top");
* }
* }
* @returns {boolean}
*/
isTopScroll: function () {
return this.scrollableNode.scrollTop === 0;
},
/**
* Returns true if container's scroll has reached the maximum limit at
* the bottom of the contents. Returns false otherwise.
* @example
* var scrollableNode = scrollableWidget.scrollableNode;
* scrollableNode.on("scroll", function () {
* if (scrollableWidget.isBottomScroll()) {
* console.log("Scroll reached the maximum limit at the bottom");
* }
* }
* @returns {boolean}
*/
isBottomScroll: function () {
var scrollableNode = this.scrollableNode;
return scrollableNode.offsetHeight + scrollableNode.scrollTop >=
scrollableNode.scrollHeight;
},
/**
* Returns true if container's scroll has reached the maximum limit at
* the left of the contents. Returns false otherwise.
* @example
* var scrollableNode = scrollableWidget.scrollableNode;
* scrollableNode.on("scroll", function () {
* if (scrollableWidget.isLeftScroll()) {
* console.log("Scroll reached the maximum limit at the left");
* }
* }
* @returns {boolean}
*/
isLeftScroll: function () {
return this.scrollableNode.scrollLeft === 0;
},
/**
* Returns true if container's scroll has reached the maximum limit at
* the right of the contents. Returns false otherwise.
* @example
* var scrollableNode = scrollableWidget.scrollableNode;
* scrollableNode.on("scroll", function () {
* if (scrollableWidget.isRightScroll()) {
* console.log("Scroll reached the maximum limit at the right");
* }
* }
* @returns {boolean}
*/
isRightScroll: function () {
var scrollableNode = this.scrollableNode;
return scrollableNode.offsetWidth + scrollableNode.scrollLeft >= scrollableNode.scrollWidth;
},
/**
* Returns the current amount of scroll, as an object with x and y properties
* for the horizontal and vertical scroll amount.
* This is a convenience method and it is not supposed to be overridden.
* @returns {Object}
*/
getCurrentScroll: function () {
return {x: this.scrollableNode.scrollLeft, y: this.scrollableNode.scrollTop};
},
/**
* Scrolls by the given amount.
* @param {Object} by - The scroll amount. An object with x and/or y properties, for example
* `{x: 0, y: -5}` or `{y: -29}`.
* @param {number} duration - Duration of scrolling animation in milliseconds.
* If 0 or unspecified, scrolls without animation.
*/
scrollBy: function (by, duration) {
var to = {};
if (by.x !== undefined) {
to.x = this.scrollableNode.scrollLeft + by.x;
}
if (by.y !== undefined) {
to.y = this.scrollableNode.scrollTop + by.y;
}
this.scrollTo(to, duration);
},
/**
* Scrolls to the given position.
* @param {Object} to - The scroll destination position. An object with w and/or y properties,
* for example `{x: 0, y: -5}` or `{y: -29}`.
* @param {number} [duration] - Duration of scrolling animation in milliseconds.
* If 0 or unspecified, scrolls without animation.
*/
scrollTo: function (to, duration) {
var scrollableNode = this.scrollableNode;
this._stopAnimation();
if (!duration || duration <= 0) { // shortcut
if (to.x !== undefined) {
scrollableNode.scrollLeft = to.x;
}
if (to.y !== undefined) {
scrollableNode.scrollTop = to.y;
}
} else {
var from = {
x: to.x !== undefined ? scrollableNode.scrollLeft : undefined,
y: to.y !== undefined ? scrollableNode.scrollTop : undefined
};
this._animation = new ScrollAnimation(scrollableNode, {
from: from,
to: to,
duration: duration
});
this._animation.on("ended", function () {
this._animation = null;
}.bind(this), true);
}
},
/**
* Stops the scrolling animation if it is currently playing.
* Does nothing otherwise.
*/
_stopAnimation: function () {
if (this._animation) {
this._animation.stop();
}
}
});
});