@happysanta/router
Version:
Router for VKUI
274 lines (226 loc) • 10.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Route = exports.MODAL_KEY = exports.POPUP_KEY = void 0;
var _workWithPathRegexp = require("../workWithPathRegexp");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
/**
* @ignore
*/
var POPUP_KEY = 'p';
/**
* @ignore
*/
exports.POPUP_KEY = POPUP_KEY;
var MODAL_KEY = 'm';
exports.MODAL_KEY = MODAL_KEY;
var routeUniqueId = 1;
function getNextUniqId() {
return routeUniqueId++;
}
function searchParamsToObject(searchParams) {
var obj = {};
var _iterator = _createForOfIteratorHelper(searchParams),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _step$value = _slicedToArray(_step.value, 2),
_key = _step$value[0],
value = _step$value[1];
obj[_key] = value;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return obj;
}
var Route = /*#__PURE__*/function () {
/**
* @type {Page}
*/
function Route(structure, pageId, params) {
_classCallCheck(this, Route);
_defineProperty(this, "structure", void 0);
_defineProperty(this, "pageId", void 0);
_defineProperty(this, "params", {});
_defineProperty(this, "uniqId", void 0);
this.structure = structure;
this.pageId = pageId;
this.params = params;
this.uniqId = getNextUniqId();
}
_createClass(Route, [{
key: "clone",
value: function clone() {
var copy = new Route(this.structure.clone(), this.pageId, _objectSpread({}, this.params));
copy.uniqId = this.uniqId;
return copy;
}
}, {
key: "getLocation",
value: function getLocation() {
return (0, _workWithPathRegexp.generatePath)(this.pageId, this.params);
}
}, {
key: "getPageId",
value: function getPageId() {
return this.pageId;
}
}, {
key: "getPanelId",
value: function getPanelId() {
if (this.structure.isInfinityPanel) {
return "_".concat(this.structure.panelId, "..").concat(String(this.uniqId));
}
return this.structure.panelId;
}
}, {
key: "getPanelIdWithoutInfinity",
value: function getPanelIdWithoutInfinity() {
return this.structure.panelId;
}
}, {
key: "getViewId",
value: function getViewId() {
return this.structure.viewId;
}
}, {
key: "getRootId",
value: function getRootId() {
return this.structure.rootId;
}
}, {
key: "getParams",
value: function getParams() {
return this.params;
}
}, {
key: "setParams",
value: function setParams() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.params = _objectSpread(_objectSpread({}, this.params), params);
return this;
}
}, {
key: "isPopup",
value: function isPopup() {
return !!this.getPopupId();
}
}, {
key: "getPopupId",
value: function getPopupId() {
var _this$params$POPUP_KE;
return ((_this$params$POPUP_KE = this.params[POPUP_KEY]) === null || _this$params$POPUP_KE === void 0 ? void 0 : _this$params$POPUP_KE.toString()) || null;
}
}, {
key: "setPopupId",
value: function setPopupId(popupId) {
this.params[POPUP_KEY] = popupId;
return this;
}
}, {
key: "isModal",
value: function isModal() {
return !!this.getModalId();
}
}, {
key: "hasOverlay",
value: function hasOverlay() {
return this.isModal() || this.isPopup();
}
}, {
key: "getModalId",
value: function getModalId() {
var _this$params$MODAL_KE;
return ((_this$params$MODAL_KE = this.params[MODAL_KEY]) === null || _this$params$MODAL_KE === void 0 ? void 0 : _this$params$MODAL_KE.toString()) || null;
}
}, {
key: "setModalId",
value: function setModalId(modalId) {
this.params[MODAL_KEY] = modalId;
return this;
}
}, {
key: "out",
value: function out() {// $TSFixMe
}
}, {
key: "in",
value: function _in() {// $TSFixMe
}
}], [{
key: "getParamsFromPath",
value: function getParamsFromPath(location) {
if (location.includes('?')) {
var _location$split = location.split('?', 2),
_location$split2 = _slicedToArray(_location$split, 2),
qs = _location$split2[1];
return searchParamsToObject(new URLSearchParams(qs));
} else {
return {};
}
}
/**
* @param {RouteList} routeList
* @param location "info?w=about&show=1" то, что лежит в window.location.hash
* @param noSlash
*/
}, {
key: "fromLocation",
value: function fromLocation(routeList, location) {
var noSlash = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
var params = Route.getParamsFromPath(location);
location = location.replace('#', '');
if (noSlash && location.length && !location.startsWith('/')) {
location = "/".concat(location);
}
if (noSlash && !location.length) {
location = "/".concat(location);
}
location = location.split('?', 2).shift() || (noSlash ? '/' : '');
var match = null;
for (var pageId in routeList) {
if (routeList.hasOwnProperty(pageId)) {
match = (0, _workWithPathRegexp.matchPath)(location, pageId);
if (match && match.isExact) {
break;
}
}
}
if (!match) {
throw new Error('ROUTE_NOT_FOUND');
}
var ps = routeList[match.path];
if (!ps) {
throw new Error("Router fail: cant find structure in routes for ".concat(location));
}
return new Route(ps, match.path, _objectSpread(_objectSpread({}, params), match.params));
}
}, {
key: "fromPageId",
value: function fromPageId(routeList, pageId, params) {
var ps = routeList[pageId];
if (!ps) {
throw new Error("Router fail: cant find structure in routes for ".concat(pageId));
}
return new Route(ps, pageId, params || {});
}
}]);
return Route;
}();
exports.Route = Route;