boa-handler-history
Version:
A b-o-a handler for History API
40 lines (39 loc) • 1.25 kB
JavaScript
;
var url = require('url');
var parse = function (urlString) {
var hash = url.parse(urlString).hash;
var path = hash && hash.length > 1 ? hash.substring(1) : '/';
return path;
};
var HashHistory = (function () {
function HashHistory(callback) {
this.callback = callback;
this.window = Function('return this')();
}
HashHistory.prototype.back = function () {
this.window.history.back();
};
HashHistory.prototype.go = function (path, replace) {
if (replace === void 0) { replace = false; }
var hash = '#' + path;
if (replace) {
var urlObj = url.parse(this.window.location.href);
urlObj.hash = hash;
this.window.location.replace(url.format(urlObj));
}
else {
this.window.location.hash = hash;
}
};
HashHistory.prototype.start = function () {
var _this = this;
window.addEventListener('hashchange', function (event) {
var path = parse(event.newURL);
_this.callback(path);
}, false);
var path = parse(this.window.location.href);
this.callback(path);
};
return HashHistory;
}());
exports.HashHistory = HashHistory;