ajsfw
Version:
Ajs Framework
120 lines (119 loc) • 5.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var logger = require("ajsfw/dbg/logger");
var Navigator = (function () {
function Navigator(router, redirections) {
var _this = this;
logger.log(logger.LogType.Constructor, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Registering redirections (" + (redirections ? redirections.length : 0) + ")", redirections);
this._canNavigate = false;
this.__router = router;
this._lastUrl = null;
this._redirections = redirections || [];
logger.log(logger.LogType.DomAddListener, 0, "ajs.navigation", this, "window.popstate");
window.addEventListener("popstate", function (event) { _this.__onPopState(event); });
logger.log(logger.LogType.DomAddListener, 0, "ajs.navigation", this, "window.hashchange");
window.addEventListener("hashchange", function (event) { _this.__onHashChange(event); });
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
}
Object.defineProperty(Navigator.prototype, "lastUrl", {
get: function () { return this._lastUrl; },
enumerable: true,
configurable: true
});
Object.defineProperty(Navigator.prototype, "redirections", {
get: function () { return this._redirections; },
enumerable: true,
configurable: true
});
Object.defineProperty(Navigator.prototype, "canNavigate", {
get: function () { return this._canNavigate; },
set: function (value) { this._canNavigate = value; },
enumerable: true,
configurable: true
});
Navigator.prototype.registerRedirection = function (path, target) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Registering redirection (" + path + " : " + target + ")");
this._redirections.push({
path: path,
target: target
});
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
};
Navigator.prototype.navigated = function () {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Navigation event occured: " + window.location.href);
if (window.location.href !== this._lastUrl && this._canNavigate) {
this._lastUrl = window.location.href;
if (!this.__redirect(window.location.pathname)) {
this.__router.route();
}
}
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
};
Navigator.prototype.navigate = function (url) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Navigating to: " + url);
if (window.location.href !== url && window.location.href !== window.location.origin + url) {
this._lastUrl = url;
window.history.pushState({}, "", url);
if (!this.__redirect(url)) {
this.__router.route();
}
}
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
};
Navigator.prototype.linkClicked = function (event) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
var rv = true;
if (!event.ctrlKey && !event.shiftKey && !event.altKey) {
try {
var element = event.target;
while (element !== null && !(element instanceof HTMLAnchorElement)) {
element = element.parentElement;
}
if (element instanceof HTMLAnchorElement) {
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Link clicked: " + element.href, element);
this.navigate(element.pathname);
}
}
catch (e) {
throw new Error(e);
}
finally {
rv = false;
}
}
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
return rv;
};
Navigator.prototype.__onPopState = function (event) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "window.popstate event occured");
this.navigated();
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
};
Navigator.prototype.__onHashChange = function (event) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "window.hashchange event occured");
this.navigated();
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
};
Navigator.prototype.__redirect = function (url) {
logger.log(logger.LogType.Enter, 0, "ajs.navigation", this);
logger.log(logger.LogType.Info, 0, "ajs.navigation", this, "Redirecting to " + url);
var redirected = false;
for (var i = 0; i < this._redirections.length; i++) {
if (this._redirections[i].path === url) {
redirected = true;
this.__router.route(this._redirections[i].target);
break;
}
}
logger.log(logger.LogType.Exit, 0, "ajs.navigation", this);
return redirected;
};
return Navigator;
}());
exports.Navigator = Navigator;