-
Notifications
You must be signed in to change notification settings - Fork 36
/
ViewIndicator.js
98 lines (92 loc) · 2.73 KB
/
ViewIndicator.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
/** @module deliteful/ViewIndicator */
define([
"dcl/dcl",
"delite/register",
"delite/Widget",
"delite/hc",
"requirejs-dplugins/css!./ViewIndicator/ViewIndicator.css"
], function (dcl, register, Widget) {
/**
* ViewIndicator widget. Indicates which view is currently visible in a ViewStack.
*
* @example
* <d-view-stack id="vs">
* <div id="childA">...</div>
* <div id="childB">...</div>
* <div id="childC">...</div>
* </d-view-stack>
* <d-view-indicator id="vi" viewstack="vs"></d-view-indicator>
* @class module:deliteful/ViewIndicator
* @augments module:delite/Widget
*/
return register("d-view-indicator", [HTMLElement, Widget], /** @lends module:deliteful/ViewIndicator# */{
/**
* The name of the CSS class of this widget.
* @member {string}
* @default "d-view-indicator"
*/
baseClass: "d-view-indicator",
/**
* The ViewStack widget associated with the indicator.
*/
viewStack: null,
constructor: function () {
this.on("pointerdown", function (e) {
if (typeof e.target._vsChildIndex === "number") {
this.viewStack.show(this.viewStack.children[e.target._vsChildIndex]);
} else {
var next = (e.clientX - this.offsetLeft) > this.offsetWidth / 2;
if (this.effectiveDir === "rtl") {
next = !next;
}
if (next) {
this.viewStack.showNext();
} else {
this.viewStack.showPrevious({reverse: true});
}
}
}.bind(this));
},
refreshRendering: function (props) {
if ("viewStack" in props) {
this._attachViewStack();
this._refreshDots();
}
},
/**
* Sets event listeners to the ViewStack so as to update dots when children are shown/hidden
* @private
*/
_attachViewStack: function () {
if (this._afterAttachHandle) {
this._afterAttachHandle.remove();
this._afterAttachHandle = null;
}
if (this._afterShowHandle) {
this._afterShowHandle.remove();
this._afterShowHandle = null;
}
if (this.viewStack) {
var rd = this._refreshDots.bind(this);
this._afterAttachHandle = this.on("customelement-attached", rd, this.viewStack);
this._afterShowHandle = this.on("delite-after-show", rd, this.viewStack);
}
},
/**
* Refreshes the dots from the SwapView's contents.
* @private
*/
_refreshDots: function () {
// TODO: avoid recreating all the dots when only the visible child has changed
this.innerHTML = "";
for (var i = 0; i < this.viewStack.children.length; i++) {
var child = this.viewStack.children[i];
var dot = this.ownerDocument.createElement("div");
dot.className = "-d-view-indicator-dot" +
(child.style.visibility === "visible" ? " -d-view-indicator-dot-selected" : "");
dot._vsChildIndex = i;
this.appendChild(dot);
}
}
});
});