-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- KeyedSwitch displays exactly 1 child at a time specified by a string key.
- Loading branch information
1 parent
caec874
commit 4e28981
Showing
6 changed files
with
130 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import dom from "../util/dom"; | ||
|
||
/** | ||
* @param {{ | ||
* routeHandler: function(string):void | ||
* }} props | ||
*/ | ||
const Router = ({ routeHandler }) => { | ||
const onHashChange = () => routeHandler(window.location.hash.slice(1)); | ||
window.onhashchange = onHashChange; | ||
dom.schedule(onHashChange, 0); | ||
} | ||
|
||
/** | ||
* @param {string} route | ||
*/ | ||
Router.navigate = (route) => window.location.hash = route; | ||
|
||
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters