-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyedSwitch.jsx
57 lines (53 loc) · 1.5 KB
/
KeyedSwitch.jsx
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
import dom from "../util/dom";
/**
* @constructor
* @param {{
* id: string,
* initialPane: (string | undefined),
* children: !Array<?function():void>,
* keyToIndex: !Object<string, number>,
* }} props
*/
const KeyedSwitch = function ({ id, initialPane, children, keyToIndex }) {
/** @type {number} */
this.selectedPane = dom.GEN ? 0 : initialPane ? keyToIndex[initialPane] : 0;
/** @const {!Array<?function():void>} */
this.initializers = children;
/** @const {!Object<string, number>} */
this.keyToIndex = keyToIndex;
/** @const {!HTMLDivElement} */
const Root = dom.div(id);
/** @const {!HTMLDivElement} */
this.root = Root;
return (
<Root modifiesChildren>
{children.modify((c, i) => {
c.nodisplay = initialPane ? c.key != initialPane : i != this.selectedPane;
delete c.key;
})}
</Root>
);
}
/**
* Shows the child with the given key and hides the currently shown child.
* If the child is being shown for the first time, initializes it.
*
* @param {string} key Key of the child to show
*/
KeyedSwitch.prototype.showPane = function (key) {
/** @const {number} */
const idx = this.keyToIndex[key];
/** @const {number} */
const old = this.selectedPane;
if (idx == old) return;
/** @const {?function():void} */
const f = this.initializers[idx];
if (f) {
f();
this.initializers[idx] = null;
}
this.selectedPane = idx;
dom.show(this.root.children[idx]);
dom.hide(this.root.children[old]);
}
export default KeyedSwitch;