snappymail/dev/Knoin/AbstractScreen.js

64 lines
999 B
JavaScript
Raw Normal View History

2016-07-07 07:11:13 +08:00
import crossroads from 'crossroads';
2019-07-05 03:19:24 +08:00
import { isArray, isNonEmptyArray, noop } from 'Common/Utils';
2016-07-07 07:11:13 +08:00
2019-07-05 03:19:24 +08:00
export class AbstractScreen {
2016-09-10 06:38:16 +08:00
oCross = null;
sScreenName;
aViewModels;
2019-07-05 03:19:24 +08:00
constructor(screenName, viewModels = []) {
2016-07-07 07:11:13 +08:00
this.sScreenName = screenName;
this.aViewModels = isArray(viewModels) ? viewModels : [];
}
/**
* @returns {Array}
*/
viewModels() {
return this.aViewModels;
}
/**
* @returns {string}
*/
screenName() {
return this.sScreenName;
}
/**
* @returns {?Array)}
*/
routes() {
return null;
}
/**
* @returns {?Object}
*/
__cross() {
return this.oCross;
}
/**
* @returns {void}
*/
__start() {
2019-07-05 03:19:24 +08:00
let route = null,
2016-07-07 07:11:13 +08:00
fMatcher = null;
const routes = this.routes();
2019-07-05 03:19:24 +08:00
if (isNonEmptyArray(routes)) {
fMatcher = (this.onRoute || noop).bind(this);
2016-07-07 07:11:13 +08:00
route = crossroads.create();
2016-10-19 01:52:43 +08:00
routes.forEach((item) => {
2019-07-05 03:19:24 +08:00
if (item && route) {
2016-09-10 06:38:16 +08:00
route.addRoute(item[0], fMatcher).rules = item[1];
}
2016-07-07 07:11:13 +08:00
});
this.oCross = route;
}
}
}