UNPKG

ajsfw

Version:
106 lines (105 loc) 5.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var logger = require("ajsfw/dbg/logger"); var exceptions = require("./exceptions"); var Router = (function () { function Router(viewComponentManager, routes) { logger.log(logger.LogType.Constructor, 0, "ajs.routing", this); logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Registering routes (" + (routes ? routes.length : 0) + ")", routes); this.__viewComponentManager = viewComponentManager; this.__routes = routes || []; this.__lastURL = ""; this.__lastViewComponentName = null; this.__lastViewComponentInstance = null; this.__currentRoute = { base: "", path: "", search: "", hash: "" }; logger.log(logger.LogType.Exit, 0, "ajs.routing", this); } Object.defineProperty(Router.prototype, "routes", { get: function () { return this.__routes; }, enumerable: true, configurable: true }); Object.defineProperty(Router.prototype, "currentRoute", { get: function () { return this.__currentRoute; }, enumerable: true, configurable: true }); Router.prototype.registerRoute = function (paths, viewComponentName) { logger.log(logger.LogType.Enter, 0, "ajs.routing", this); logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Registering route", paths); this.__routes.push({ paths: paths, viewComponentName: viewComponentName }); logger.log(logger.LogType.Exit, 0, "ajs.routing", this); }; Router.prototype.route = function (url) { logger.log(logger.LogType.Enter, 0, "ajs.routing", this); url = url || window.location.href; if (this.__lastURL !== url) { logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Maping route for '" + url + "'"); this.__lastURL = url; var viewComponentName = this.__getRouteViewComponent(url); logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Routing to " + viewComponentName); if (viewComponentName !== null) { if (this.__lastViewComponentName !== viewComponentName) { logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Routing to a different than previous component"); this.__lastViewComponentName = viewComponentName; this.__viewComponentManager.setRootViewComponent(viewComponentName); } else { logger.log(logger.LogType.Info, 0, "ajs.routing", this, "Notifying component the navigation occured"); this.__viewComponentManager.onNavigate(); } } else { logger.log(logger.LogType.Error, 0, "ajs.routing", this, "ViewComponent not found for the path specified"); throw new exceptions.RouteNotFoundException(); } } logger.log(logger.LogType.Exit, 0, "ajs.routing", this); }; Router.prototype.__getRouteViewComponent = function (url) { logger.log(logger.LogType.Enter, 0, "ajs.routing", this); url = url || window.location.href; var uriParser = document.createElement("a"); uriParser.href = url; for (var i = 0; i < this.__routes.length; i++) { for (var j = 0; j < this.__routes[i].paths.length; j++) { var rx = new RegExp(this.__routes[i].paths[j].base + this.__routes[i].paths[j].params, "g"); if (rx.test(window.location.pathname)) { var routeURI = uriParser.pathname + uriParser.search + uriParser.hash; var base = routeURI.match(this.__routes[i].paths[j].base)[0]; var path = routeURI.substr(base.length); if (base[0] === "/") { base = base.substr(1); } if (path.indexOf("#") !== -1) { path = path.substr(0, path.indexOf("#")); } if (path.indexOf("?") !== -1) { path = path.substr(0, path.indexOf("?")); } if (path[0] === "/") { path = path.substr(1); } if (path[path.length - 1] === "/") { path = path.substr(0, path.length - 1); } this.__currentRoute = { base: base, path: path, search: window.location.search.substr(1), hash: window.location.hash.substr(1) }; return this.__routes[i].viewComponentName; } } } logger.log(logger.LogType.Warning, 0, "ajs.routing", this, "Route not found"); logger.log(logger.LogType.Exit, 0, "ajs.routing", this); return null; }; return Router; }()); exports.Router = Router;