-
Notifications
You must be signed in to change notification settings - Fork 36
/
SidePane.js
431 lines (384 loc) · 11.5 KB
/
SidePane.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
/** @module deliteful/SidePane */
define([
"dcl/dcl",
"ibm-decor/sniff",
"delite/register",
"delite/DisplayContainer",
"requirejs-dplugins/css!./SidePane/SidePane.css"
], function (dcl, has, register, DisplayContainer) {
function prefix (v) {
return "-d-side-pane-" + v;
}
function setVisibility (node, val) {
if (val) {
node.style.visibility = "visible";
node.style.display = "block";
} else {
node.style.visibility = "hidden";
node.style.display = "none";
}
}
function getNextSibling (node) {
do {
node = node.nextElementSibling;
} while (node && node.nodeType !== 1);
return node;
}
/**
* Dispatched after SidePane is shown.
* @example
* mySidePane.on("sidepane-after-show", function (evt) {
* firstField.focus();
* });
* @event module:deliteful/SidePane#sidepane-after-show
*/
/**
* A widget displayed on the side of the screen.
*
* It can be displayed on top of the page
* (mode=overlay) or can push the content of the page (mode=push or mode=reveal).
* SidePane is a widget hidden by default.
* This widget must be a sibling of html's body element.
* If mode is set to "push" or "reveal", the width of the SidePane can't be changed in the markup
* (15em by default).
* However it can be changed in SidePane.less (@PANE_WIDTH variable).
* In "push" and "reveal" mode, the pushed element is the first sibling of the SidePane which has
* of type element (nodeType == 1) and not a SidePane.
* @example
* <body>
* <d-side-pane>
* SidePane content
* </d-side-pane>
* <div>
* Main application
* </div>
* </body>
* @class module:deliteful/SidePane
* @augments module:delite/DisplayContainer
*/
return register("d-side-pane", [ HTMLElement, DisplayContainer ], /** @lends module:deliteful/SidePane#*/ {
/**
* The name of the CSS class of this widget.
* @member {string}
* @default "d-side-pane"
*/
baseClass: "d-side-pane",
/**
* Can be "overlay", "reveal" or "push".
* In overlay mode, the pane is shown on top of the page.
* In reveal and push modes, The page is moved to make the pane visible. The difference between
* these two modes is the animated transition: in reveal mode, the pane does not move, it is
* already under the page. In push mode, the pane slide with the page.
* @member {string}
* @default "push"
*/
mode: "push",
/**
* Can be "start" or "end". If set to "start", the panel is displayed on the
* left side in LTR mode.
* @member {string}
* @default "push"
*/
position: "start",
/**
* Enable/Disable animations.
* @member {boolean}
* @default true
*/
animate: true,
/**
* Enables the swipe closing of the pane.
* @member {boolean}
* @default true
*/
swipeClosing: true,
_transitionTiming: { "default": 0, "chrome": 50, "ios": 20, "android": 100, "ff": 100 },
_timing: 0,
_visible: false,
_opening: false,
_originX: NaN,
_originY: NaN,
connectedCallback: function () {
this.parentNode.style.overflow = "hidden";
},
show: dcl.superCall(function (sup) {
return function () {
if (arguments.length > 0) {
return sup.apply(this, arguments).then(function (value) {
return this._open().then(function () {
return value;
});
}.bind(this));
} else {
return this._open();
}
};
}),
hide: dcl.superCall(function (sup) {
return function () {
if (arguments.length > 0) {
return sup.apply(this, arguments).then(function (value) {
return this._close().then(function () {
return value;
});
}.bind(this));
} else {
return this._close();
}
};
}),
/**
* This method is called to toggle the visibility of the SidePane.
* @returns {Promise} A promise that will be resolved when the display & transition effect will have been
* performed.
*/
toggle: function () {
return this._visible ? this.hide() : this.show();
},
/**
* Open the pane.
* @private
*/
_open: function () {
var promise;
var nextElement = getNextSibling(this);
var animate = this.animate && has("ie") !== 9;
if (!this._visible) {
if (animate) {
this.addClass(prefix("animate"));
if (nextElement) {
nextElement.classList.add(prefix("animate"));
}
}
if (this.mode === "reveal") {
if (nextElement) {
promise = this._setAfterTransitionHandlers(nextElement);
}
} else {
promise = this._setAfterTransitionHandlers(this);
}
setVisibility(this, true);
if (animate) {
this.defer(this._openImpl, this._timing);
} else {
this._openImpl();
promise = new Promise(function (resolve) {
this.defer(resolve, this._timing);
}.bind(this));
}
}
return (promise || Promise.resolve(true)).then(function () {
this.emit("sidepane-after-show");
}.bind(this));
},
/**
* Close the pane.
* @private
*/
_close: function () {
var promise;
if (this._visible) {
if (this.mode === "reveal") {
var nextElement = getNextSibling(this);
if (nextElement) {
promise = this._setAfterTransitionHandlers(nextElement);
}
} else {
promise = this._setAfterTransitionHandlers(this);
}
if (this.animate && has("ie") !== 9) {
// This defer should be useless but is needed for Firefox, see #25
this.defer(function () {
this._hideImpl();
}, this._timing);
} else {
this._hideImpl();
setVisibility(this, false);
}
}
return promise || Promise.resolve(true);
},
_setAfterTransitionHandlers: function (node) {
var self = this, holder = { node: node };
var promise = new Promise(function (resolve) {
holder.handle = function () {
self._afterTransitionHandle(holder, resolve);
};
});
node.addEventListener("webkitTransitionEnd", holder.handle);
node.addEventListener("transitionend", holder.handle); // IE10 + FF
return promise;
},
_afterTransitionHandle: function (holder, resolve) {
this.removeClass(prefix("under"));
if (!this._visible) {
setVisibility(this, false);
}
holder.node.removeEventListener("webkitTransitionEnd", holder.handle);
holder.node.removeEventListener("transitionend", holder.handle);
resolve();
},
beforeInitializeRendering: function () {
this._transitionTiming = { "default": 0, "chrome": 20, "ios": 20, "android": 100, "ff": 100 };
for (var o in this._transitionTiming) {
if (has(o) && this._timing < this._transitionTiming[o]) {
this._timing = this._transitionTiming[o];
}
}
},
afterInitializeRendering: function () {
this._resetInteractions();
setVisibility(this, false);
},
_refreshMode: function (nextElement) {
this.removeClass([ prefix("push"), prefix("overlay"), prefix("reveal") ].join(" "))
.addClass(prefix(this.mode));
if (nextElement && this._visible) {
nextElement.classList.toggle(prefix("translated"), this.mode !== "overlay");
}
if (this.mode === "reveal" && !this._visible) {
// Needed by FF only for the first opening.
this.removeClass(prefix("ontop"))
.addClass(prefix("under"));
} else if (this.mode === "overlay") {
this.removeClass(prefix("under"))
.addClass(prefix("ontop"));
} else {
this.removeClass([ prefix("under"), prefix("ontop") ].join(" "));
}
},
_refreshPosition: function (nextElement) {
this.removeClass([ prefix("start"), prefix("end") ].join(" "))
.addClass(prefix(this.position));
if (nextElement && this._visible) {
nextElement.classList.remove(prefix("start"), prefix("end"));
nextElement.classList.add(prefix(this.position));
}
},
refreshRendering: function (props) {
if (!("mode" in props || "position" in props || "animate" in props)) {
return;
}
var nextElement = getNextSibling(this);
// Always remove animation during a refresh. Avoid to see the pane moving on mode changes.
// Not very reliable on IE11.
this.removeClass(prefix("animate"));
if (nextElement) {
nextElement.classList.remove(prefix("animate"));
nextElement.classList.toggle("d-rtl", this.effectiveDir === "rtl");
}
if ("mode" in props) {
this._refreshMode(nextElement);
}
if ("position" in props) {
this._refreshPosition(nextElement);
}
this.toggleClass(prefix("hidden"), !this._visible)
.toggleClass(prefix("visible"), this._visible);
// Re-enable animation
if (this.animate) {
this.defer(function () {
this.addClass(prefix("animate"));
if (nextElement) {
nextElement.classList.add(prefix("animate"));
}
}, this._timing);
}
},
_openImpl: function () {
if (!this._visible) {
this._visible = true;
this.removeClass(prefix("hidden"))
.addClass(prefix("visible"));
if (this.mode === "push" || this.mode === "reveal") {
var nextElement = getNextSibling(this);
if (nextElement) {
nextElement.classList.remove(prefix("nottranslated"), prefix("start"), prefix("end"));
nextElement.classList.add(prefix(this.position), prefix("translated"));
}
}
}
},
_hideImpl: function () {
if (this._visible) {
this._visible = false;
this._opening = false;
this.ownerDocument.body.classList.remove(prefix("no-select"));
this.removeClass(prefix("visible"))
.addClass(prefix("hidden"));
if (this.mode === "push" || this.mode === "reveal") {
var nextElement = getNextSibling(this);
if (nextElement) {
nextElement.classList.remove(prefix("translated"), prefix("start"), prefix("end"));
nextElement.classList.add(prefix(this.position), prefix("nottranslated"));
}
}
}
},
_isLeft: function () {
return (this.position === "start" && this.effectiveDir === "ltr") ||
(this.position === "end" && this.effectiveDir === "rtl");
},
_pointerDownHandler: function (event) {
this._originX = event.pageX;
this._originY = event.pageY;
if (this._visible || (this._isLeft() && !this._visible && this._originX <= 10) ||
(!this._isLeft() && !this._visible && this._originX >= this.ownerDocument.width - 10)) {
this._opening = !this._visible;
this._pressHandle.remove();
this._moveHandle = this.on("pointermove", this._pointerMoveHandler.bind(this));
this._releaseHandle = this.on("pointerup", this._pointerUpHandler.bind(this));
this.ownerDocument.body.classList.add(prefix("no-select"));
}
},
_pointerMoveHandler: function (event) {
if (!this._opening && Math.abs(event.pageY - this._originY) > 10) {
this._resetInteractions();
} else {
var pos = event.pageX;
if (this._isLeft()) {
if (this._visible) {
if (this._originX < pos) {
this._originX = pos;
}
if ((this.swipeClosing && this._originX - pos) > 10) {
this._close();
this._originX = pos;
}
}
} else {
if (this._visible) {
if (this._originX > pos) {
this._originX = pos;
}
if ((this.swipeClosing && pos - this._originX) > 10) {
this._close();
this._originX = pos;
}
}
}
}
},
_pointerUpHandler: function () {
this._opening = false;
this.ownerDocument.body.classList.remove(prefix("no-select"));
this._resetInteractions();
},
_resetInteractions: function () {
if (this._releaseHandle) {
this._releaseHandle.remove();
}
if (this._moveHandle) {
this._moveHandle.remove();
}
if (this._pressHandle) {
this._pressHandle.remove();
}
if (this.swipeClosing) {
this._pressHandle = this.on("pointerdown", this._pointerDownHandler.bind(this));
}
this._originX = NaN;
this._originY = NaN;
}
});
});