@lvxiaowu/antd4
Version:
antd4-components
152 lines (116 loc) • 4.51 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import { makeAutoObservable } from 'mobx';
import { resetStore } from '../utils';
var Store = /*#__PURE__*/_createClass(function Store() {
var _this = this;
_classCallCheck(this, Store);
this.collapsed = false;
this.trigger = function () {
if (_this.sideFixed) {
_this.fixedSideVisible = !_this.fixedSideVisible;
} else {
_this.collapsed = !_this.collapsed;
}
};
this.sideFixed = false;
this.fixedSideVisible = false;
this.onBreakpoint = function (broken) {
_this.sideFixed = broken;
if (broken) {
_this.collapsed = false;
} else {
_this.fixedSideVisible = false;
}
};
this.sideNavClick = function (history, key) {
history.push(key);
if (_this.sideFixed) {
_this.fixedSideVisible = !_this.fixedSideVisible;
}
};
this.sideMenuConfig = [];
this.setSideMenuConfig = function (menuConfig) {
_this.sideMenuConfig = menuConfig;
if (_this.sideFixed) {
_this.fixedSideVisible = true;
}
};
this.findParent = function (path, menu, list) {
if (list === undefined) {
list = [];
}
if (!menu || menu.length === 0) {
return list;
}
var len = menu.length;
for (var i = 0; i < len; i++) {
var item = menu[i];
var tmpPath = list.concat(item);
var itemPath = item.path || '';
if (itemPath.indexOf(path) === 0) {
var _ret = function () {
var itemSplit = itemPath.split('/').filter(Boolean);
var pathSplit = path.split('/').filter(Boolean);
var strictEqual = pathSplit.every(function (value, index) {
return value === itemSplit[index];
});
if (strictEqual) {
return {
v: tmpPath
};
}
}();
if (_typeof(_ret) === "object") return _ret.v;
}
if (item.children) {
var match = _this.findParent(path, item.children, tmpPath);
if (match) {
return match;
}
}
}
};
this.findActiveSideItem = function (menu, pathname) {
if (!menu || !pathname) {
return undefined;
}
var len = menu.length;
for (var i = 0; i < len; i++) {
var item = menu[i]; // 第一层模糊匹配
if (pathname.indexOf(item.path) === 0) {
var _ret2 = function () {
/*
判断是否严格相等,否则不匹配,比如pathname为/home/car-type
那么menu中有两项:/home/car-type和/home/car都会被模糊匹配到
所以需要进行更严格的删选,匹配/分割开的每一段路径都相同的话才算严格匹配到
*/
var itemSplit = item.path.split('/').filter(Boolean);
var pathnameSplit = pathname.split('/').filter(Boolean);
var strictEqual = itemSplit.every(function (value, index) {
return value === pathnameSplit[index];
});
if (strictEqual) {
return {
v: item
};
}
}();
if (_typeof(_ret2) === "object") return _ret2.v;
}
if (item.children) {
var match = _this.findActiveSideItem(item.children, pathname);
if (match) {
return match;
}
}
}
};
this.reset = function () {
resetStore(_this, Store);
};
makeAutoObservable(this);
});
export default new Store();