UNPKG

cyra

Version:

single page application view engine

49 lines (48 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var util_1 = require("../../util"); var HistoryPath = (function () { function HistoryPath(obj) { this.hasPushed = !!window.history.state; this.routing = obj.routing; } /** * 开始路由 */ HistoryPath.prototype.start = function () { var _this = this; window.addEventListener('popstate', function (e) { if (_this.hasPushed) { _this.routing(_this.getParams); } }); this.routing(this.getParams); }; /** * 更新 URL * @param {UrlObject} urlObject */ HistoryPath.prototype.updatePath = function (urlObject, isShadow) { var path = util_1.getUrlByState(urlObject); var title = urlObject.title; if (isShadow) { window.history.replaceState(urlObject, title, path); } else { window.history.pushState(urlObject, title, path); } this.hasPushed = true; this.routing(this.getParams); }; /** * 从 URL 上获取 Path 和参数 * @return {UrlObject} */ HistoryPath.prototype.getParams = function () { var hasState = !!(window.history.state && Object.keys(window.history.state).length); var urlObject = hasState ? window.history.state : util_1.getUrlObject(); return urlObject; }; return HistoryPath; }()); exports.default = HistoryPath;