UNPKG

vimo-dt

Version:

A Vue2.x UI Project For Mobile & HyBrid

173 lines (153 loc) 5.49 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); exports.setupHistory = setupHistory; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var History = exports.History = function () { function History(router, config, platform) { var _this2 = this; _classCallCheck(this, History); this._history = []; this._direction = 'forward'; this._router = router; this._config = config; this._platform = platform; this.isReplace = false; this.usePushWindow = this._config.getBoolean('usePushWindow', false); if (this._router) { var _this = this; var replaceCopy = this._router.replace; replaceCopy = replaceCopy.bind(this._router); this._router.replace = function () { var args = Array.from(arguments); var to = _this._router.resolve(args[0]).resolved; var from = _this._history[_this.length - 1]; if (to.fullPath !== from.fullPath || to.name !== from.name) { _this.isReplace = true; replaceCopy.apply(null, arguments); } }; if (this.usePushWindow) { var backCopy = this._router.back; backCopy = backCopy.bind(this._router); this._router.back = function () { var isHandled = _this._platform.popWindow && _this._platform.popWindow(); if (!isHandled) { backCopy.apply(null, arguments); } }; var goCopy = this._router.go; goCopy = goCopy.bind(this._router); this._router.go = function (n) { var isHandled = _this._platform.popTo && _this._platform.popTo(n); if (!isHandled) { goCopy.apply(null, arguments); } }; } this._router.beforeEach(function (to, from, next) { if (_this2.isReplace) { _this2.isReplace = false; _this2._history.pop(); _this2._history.push(to); next(); } else if (_this2.length <= 1) { _this2._pushHistory({ to: to, from: from, next: next }); } else { for (var i = _this2.length - 2; i > -1; i--) { var _previous = _this2._history[i]; if (to.name === _previous.name) { _this2._popHistory(next, i); return; } } _this2._pushHistory({ to: to, from: from, next: next }); } }); } } _createClass(History, [{ key: '_pushHistory', value: function _pushHistory(_ref) { var to = _ref.to, from = _ref.from, next = _ref.next; if (to.fullPath === from.fullPath && to.name === from.name) { return; } var isHandled = false; if (this.usePushWindow && from.matched.length !== 0 && to.matched.length !== 0) { var url = ''; var mode = this._router.mode; if (mode === 'hash') { url = '' + window.location.origin + window.location.pathname + window.location.search + '#' + to.fullPath; } else { console.error('history.js::只支持 mode: "hash"'); } isHandled = this._platform.pushWindow && this._platform.pushWindow(url); } if (!isHandled) { this._direction = 'forward'; this._history.push(to); next(); } } }, { key: '_popHistory', value: function _popHistory(next) { var i = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; this._direction = 'backward'; this._history = this._history.splice(0, i + 1); next(); } }, { key: 'getDirection', value: function getDirection() { return this._direction; } }, { key: 'canGoBack', value: function canGoBack() { return this.length > 1; } }, { key: 'getHistory', value: function getHistory() { return this._history; } }, { key: 'toRoot', value: function toRoot() { var isHandled = this._platform.popToRoot && this._platform.popToRoot(); if (!isHandled) { if (this._router.options.routes) { var routes = this._router.options.routes; for (var i = 0, len = routes.length; len > i; i++) { var route = routes[i]; if (route && route.meta && route.meta.root) { console.log(route); this._direction = 'backward'; this._router.replace(route); this._history = []; return; } } } if (!isHandled) { this._router.go(1 - this.length); } } } }, { key: 'length', get: function get() { return this._history.length; } }]); return History; }(); function setupHistory(router, config, platform) { return new History(router, config, platform); }