UNPKG

cyra-pure

Version:

single page application view engine

125 lines (124 loc) 4.43 kB
var Route_1 = require('./Route'); var Action_1 = require('./Action'); var config_1 = require('../config'); var util_1 = require('../util'); var Cyra = (function () { function Cyra() { } Cyra.startRouting = function () { var _this = this; var routing = function () { var hashData = util_1.getHashData(); _this.pageIndex = hashData.pageIndex; var path = hashData.path || Cyra.defaultPath; var matchedRoute = Cyra.getRouteByPath(path); if (matchedRoute) { Cyra.beforeSwitchPage(matchedRoute, hashData); } }; window.addEventListener('hashchange', routing); routing(); }; Cyra.getRouteByPath = function (path) { var routes = Cyra.models.routes; for (var i = 0, length_1 = routes.length; i < length_1; i++) { if (routes[i].path === path) { return routes[i]; } } }; Cyra.getPageByID = function (pageID, index) { var pageData = Cyra.models.userPages[pageID]; var page = pageData.pages[index]; if (!page) { page = new pageData.pageClass(); page.defineActions(); pageData.pages[index] = page; } return page; }; Cyra.beforeSwitchPage = function (matchedRoute, hashData) { if (Cyra.currentRoute) { var fromPageID = Cyra.currentRoute.pageID; Cyra.context.nextAction.fromPageID = fromPageID; Cyra.context.nextAction.toPageID = matchedRoute.pageID; var next = this.switchPage.bind(this, matchedRoute, hashData); Cyra.getPageByID(fromPageID, Cyra.currentIndex) .beforeLeaving(next, this.cancelSwitch, this.context.nextAction); } else { this.switchPage(matchedRoute, hashData); } }; Cyra.switchPage = function (matchedRoute, hashData) { Cyra.context.currentAction.toPageID = matchedRoute.pageID; if (Cyra.currentRoute) { Cyra.context.currentAction.fromPageID = Cyra.currentRoute.pageID; Cyra.getPageByID(Cyra.currentRoute.pageID, Cyra.currentIndex) .leaving(); } // update currentIndex and currentRoute Cyra.currentIndex = hashData.copyIndex; Cyra.currentRoute = matchedRoute; Cyra.getPageByID(matchedRoute.pageID, hashData.copyIndex) .entering(hashData.data, hashData.copyIndex); }; /** * 修正页面历史记录 */ Cyra.cancelSwitch = function () { history.forward(); }; /** * @function defineRoute And define page actions * @return {routes} */ Cyra.defineRoute = function (routeData) { for (var path in routeData) { var UserPage = routeData[path]; // 至少构造一个 page var page = new UserPage(); Cyra.models.userPages[page.id()] = { pageClass: UserPage, pages: [page] }; var route = new Route_1.Route(path, page.id()); Cyra.models.routes.push(route); // action 在route实例化之后就开始定义 page.defineActions(); for (var actionID in page.actions) { var action = new Action_1.Action(page.id(), page.actions[actionID]); Cyra.models.actions.push(action); } } return Cyra.models.routes; }; /** * @function initApp */ Cyra.initApp = function (obj) { var root = obj.root || config_1.default.DEFAULT_ROOT; Cyra.context.rootContainer = document.querySelector(root); Cyra.context.currentAction = Cyra.context.nextAction = new Action_1.Action(); Cyra.context.data = {}; Cyra.defaultPath = obj.index; Cyra.currentIndex = 0; Cyra.pageIndex = 0; config_1.default.ALWAYS_RELOAD = obj.alwaysReload || config_1.default.ALWAYS_RELOAD; Cyra.startRouting(); }; Cyra.models = { userPages: {}, actions: [], routes: [], }; /** * Context * @property {string} root - 全局容器的css选择器 * @property {Element} rootContainre - 全局容器 * @property {Action} currentAction - 当前动作 */ Cyra.context = {}; return Cyra; })(); exports.Cyra = Cyra;