-
Notifications
You must be signed in to change notification settings - Fork 36
/
ViewStack.js
345 lines (312 loc) · 10.6 KB
/
ViewStack.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
/** @module deliteful/ViewStack */
define([
"dcl/dcl",
"ibm-decor/sniff",
"delite/register",
"delite/DisplayContainer",
"requirejs-dplugins/css!./ViewStack/ViewStack.css",
"requirejs-dplugins/css!./ViewStack/transitions/slide.css",
"requirejs-dplugins/css!./ViewStack/transitions/reveal.css"
], function (dcl, has, register, DisplayContainer) {
function setVisibility (node, val) {
if (node) {
if (val) {
node.style.visibility = "visible";
node.style.display = "";
} else {
node.style.visibility = "hidden";
node.style.display = "none";
}
}
}
function setReverse (node) {
if (node) {
node.classList.add("-d-view-stack-reverse");
}
}
function cleanCSS (node) {
if (node) {
node.className = node.className.split(/ +/).filter(function (x) {
return !/^-d-view-stack/.test(x);
}).join(" ");
}
}
function transitionClass (s) {
return "-d-view-stack-" + s;
}
function mix (a, b) {
for (var n in b) {
a[n] = b[n];
}
}
/**
* ViewStack container widget. Display one child at a time.
*
* The first child is displayed by default.
* The methods 'show' is used to change the visible child.
*
* Styling
* The following CSS attributes must not be changed.
* ViewStack node: position, box-sizing, overflow-x
* ViewStack children: position, box-sizing, width, height
*
* @example
* <d-view-stack id="vs">
* <div id="childA">...</div>
* <div id="childB">...</div>
* <div id="childC">...</div>
* </d-view-stack>
* <button onclick="vs.show(childB, {transition: 'reveal', reverse: true})">...</button>
* @class module:deliteful/ViewStack
* @augments module:delite/DisplayContainer
*/
return register("d-view-stack", [HTMLElement, DisplayContainer], /** @lends module:deliteful/ViewStack# */{
/**
* The name of the CSS class of this widget.
* @member {string}
* @default "d-view-stack"
*/
baseClass: "d-view-stack",
/**
* The transition type used if not specified in the second argument of the show method.
* Transitions type are: "none", "slide", "reveal", "flip", "fade".
* @member {string}
* @default "slide"
*/
transition: "slide",
/**
* If true, the transition animation is reversed.
* This attribute is supported by "slide" and "reveal" transition types.
* @member {boolean}
* @default false
*/
reverse: false,
/**
* The selected child id, can be set explicitly or through the show() method.
* The effect of setting this property (i.e. getting the value through the getter) might be
* asynchronous when an animated transition occurs.
* @member {string}
* @default ""
*/
selectedChildId: dcl.prop({
set: function (child) {
if (this.ownerDocument.getElementById(child)) {
if (this.attached) {
this.show(child);
} else {
this._pendingChild = child;
}
}
},
get: function () {
return this._visibleChild ? this._visibleChild.id : "";
},
enumerable: true,
configurable: true
}),
_pendingChild: null,
constructor: function () {
this._transitionTiming = {"default": 0, "chrome": 20, "ios": 20, "android": 100, "ff": 100, "ie": 20};
for (var o in this._transitionTiming) {
if (has(o) && this._timing < this._transitionTiming[o]) {
this._timing = this._transitionTiming[o];
}
}
},
connectedCallback: function () {
var noTransition = {transition: "none"};
if (this._pendingChild) {
this.show(this._pendingChild, noTransition);
this._pendingChild = null;
} else if (!this._visibleChild && this.children.length > 0) {
this.show(this.children[0], noTransition);
}
},
_timing: 0,
_setChildrenVisibility: function () {
var cdn = this.children;
if (!this._visibleChild && cdn.length > 0) {
this._visibleChild = cdn[0];
}
for (var i = 0; i < cdn.length; i++) {
setVisibility(cdn[i], cdn[i] === this._visibleChild);
}
},
/*
* @private
*/
onAddChild: dcl.superCall(function (sup) {
return function (node) {
var res = sup.call(this, node);
this._setChildrenVisibility();
return res;
};
}),
removeChild: dcl.superCall(function (sup) {
return function (node) {
sup.call(this, node);
if (this._visibleChild === node) {
this._visibleChild = null;
}
};
}),
afterInitializeRendering: function () {
this._setChildrenVisibility();
},
/**
* Shows the immediately following sibling of the ViewStack visible element.
* The parameter 'params' is optional. If not specified, this.transition, and this.reverse are used.
* @param {Object} [params] - Optional params. A hash like {transition: "reveal", reverse: true}.
* The transition value can be "slide", "overlay", "fade" or "flip". Reverse transition applies to "slide"
* and "reveal". Transition is internally set to "none" if the ViewStack is not visible.
* @returns {Promise} A promise that will be resolved when the display and transition effect will have
* been performed.
*/
showNext: function (params) {
// Shows the next child in the container.
return this._showPreviousNext("nextElementSibling", params);
},
/**
* Shows the immediately preceding sibling of the ViewStack visible element.
* The parameter 'params' is optional. If not specified, this.transition, and reverse = true are used.
* @param {Object} [params] - Optional params. A hash like {transition: "reveal", reverse: true}.
* The transition value can be "slide", "overlay", "fade" or "flip". Reverse transition applies to "slide"
* and "reveal". Transition is internally set to "none" if the ViewStack is not visible.
* Reverse is set to true if not specified.
* @returns {Promise} A promise that will be resolved when the display and transition effect will have
* been performed.
*/
showPrevious: function (params) {
// Shows the previous child in the container.
var args = {reverse: true};
mix(args, params || {});
return this._showPreviousNext("previousElementSibling", args);
},
_showPreviousNext: function (direction, props) {
var ret = null;
if (!this._visibleChild && this.children.length > 0) {
this._visibleChild = this.children[0];
}
if (this._visibleChild) {
var target = this._visibleChild[direction];
if (target) {
ret = this.show(target, props);
}
}
return ret;
},
_doTransition: function (origin, target, event, transition, reverse) {
var promises = [];
this.addClass("-d-view-stack-transition");
if (transition !== "none") {
if (origin) {
promises.push(this._startNodeTransition(origin));
origin.classList.add(transitionClass(transition));
}
if (target) {
promises.push(this._startNodeTransition(target));
target.classList.add(transitionClass(transition), "-d-view-stack-in");
}
if (reverse) {
setReverse(origin);
setReverse(target);
}
// TODO: figure out why the delay is needed
this.defer(function () {
if (target) {
target.classList.add("-d-view-stack-transition");
}
if (origin) {
origin.classList.add("-d-view-stack-transition", "-d-view-stack-out");
}
if (reverse) {
setReverse(origin);
setReverse(target);
}
if (target) {
target.classList.add("-d-view-stack-in");
}
}, this._timing);
} else {
if (origin !== target) {
setVisibility(origin, false);
}
}
return Promise.all(promises).then(function () {
this.removeClass("-d-view-stack-transition");
target.classList.remove("-d-view-stack-transition", "-d-view-stack-in");
origin.classList.remove("-d-view-stack-transition", "-d-view-stack-out");
}.bind(this));
},
changeDisplay: function (widget, event) {
// Resolved when display is completed.
if (!widget || widget.parentNode !== this) {
return Promise.resolve();
}
var origin = this._visibleChild;
// Needed because the CSS state of a node can be incorrect
// if a previous transitionend has been dropped
cleanCSS(origin);
cleanCSS(widget);
setVisibility(widget, true);
this._visibleChild = widget;
var transition = (origin === widget) ? "none" : (event.transition || this.transition);
var reverse = this.effectiveDir === "ltr" ? event.reverse : !event.reverse;
return this._doTransition(origin, widget, event, transition, reverse);
},
/**
* Shows a child of the ViewStack. The parameter 'params' is optional. If not specified,
* `this.transition`, and `this.reverse` are used.
* This method must be called to display a particular destination child on this container.
* @param {Element|string} dest - Element or Element id that points to the child this container must
* show or hide.
* @param {Object} [params] - A hash like {transition: "reveal", reverse: true}. The transition value
* can be "slide", "overlay", "fade" or "flip". Reverse transition applies to "slide" and
* "reveal". Transition is internally set to "none" if the ViewStack is not visible.
* @returns {Promise} A promise that will be resolved when the display and transition effect will have
* been performed.
*/
show: dcl.superCall(function (sup) {
return function (dest, params) {
// Check visibility of the ViewStack, forces transition:"none" if not visible.
// - Transitions events are broken if the ViewStack is not visible
var parent = this;
while (parent && parent.style.display !== "none" && parent !== this.ownerDocument.body) {
parent = parent.parentNode;
}
if (has("ie") === 9 || parent !== this.ownerDocument.body) {
if (!params) {
params = {};
}
params.transition = "none";
}
if (this._visibleChild && this._visibleChild.parentNode !== this) {
// The visible child has been removed.
this._visibleChild = null;
}
if (!this._visibleChild && this.children.length > 0) {
// The default visible child is the first one.
this._visibleChild = this.children[0];
}
return sup.apply(this, [dest, params]);
};
}),
_startNodeTransition: function (node) {
return new Promise(function (resolve) {
// Set up fail-safe in case we don't get the "transitionend" event below.
var failsafe = this.defer(resolve, 1000);
// Set up listener for when the animation completes.
node.addEventListener("transitionend", function after () {
node.removeEventListener("transitionend", after);
failsafe.remove();
resolve();
});
}.bind(this)).then(this._afterNodeTransitionHandler.bind(this, node));
},
_afterNodeTransitionHandler: function (node) {
var isVisibleChild = this._visibleChild === node;
setVisibility(node, isVisibleChild);
cleanCSS(node);
}
});
});