UNPKG

7.86 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _vue = require("vue");
9
10var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
11
12var _propsUtil = require("../_util/props-util");
13
14var _warning = _interopRequireDefault(require("../_util/warning"));
15
16var _configProvider = require("../config-provider");
17
18var _BreadcrumbItem = _interopRequireDefault(require("./BreadcrumbItem"));
19
20var _menu = _interopRequireDefault(require("../menu"));
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
25
26function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
27
28function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
29
30function _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); }
31
32function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
33
34function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
35
36function _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; }
37
38function _isSlot(s) {
39 return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !(0, _vue.isVNode)(s);
40}
41
42var BreadcrumbProps = {
43 prefixCls: _vueTypes.default.string,
44 routes: {
45 type: Array
46 },
47 params: _vueTypes.default.any,
48 separator: _vueTypes.default.VNodeChild,
49 itemRender: {
50 type: Function
51 }
52};
53
54function getBreadcrumbName(route, params) {
55 if (!route.breadcrumbName) {
56 return null;
57 }
58
59 var paramsKeys = Object.keys(params).join('|');
60 var name = route.breadcrumbName.replace(new RegExp(":(".concat(paramsKeys, ")"), 'g'), function (replacement, key) {
61 return params[key] || replacement;
62 });
63 return name;
64}
65
66function defaultItemRender(opt) {
67 var route = opt.route,
68 params = opt.params,
69 routes = opt.routes,
70 paths = opt.paths;
71 var isLastItem = routes.indexOf(route) === routes.length - 1;
72 var name = getBreadcrumbName(route, params);
73 return isLastItem ? (0, _vue.createVNode)("span", null, _isSlot(name) ? name : {
74 default: function _default() {
75 return [name];
76 }
77 }) : (0, _vue.createVNode)("a", {
78 "href": "#/".concat(paths.join('/'))
79 }, _isSlot(name) ? name : {
80 default: function _default() {
81 return [name];
82 }
83 });
84}
85
86var _default2 = (0, _vue.defineComponent)({
87 name: 'ABreadcrumb',
88 props: BreadcrumbProps,
89 setup: function setup() {
90 return {
91 configProvider: (0, _vue.inject)('configProvider', _configProvider.defaultConfigProvider)
92 };
93 },
94 methods: {
95 getPath: function getPath(path, params) {
96 path = (path || '').replace(/^\//, '');
97 Object.keys(params).forEach(function (key) {
98 path = path.replace(":".concat(key), params[key]);
99 });
100 return path;
101 },
102 addChildPath: function addChildPath(paths) {
103 var childPath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
104 var params = arguments.length > 2 ? arguments[2] : undefined;
105
106 var originalPaths = _toConsumableArray(paths);
107
108 var path = this.getPath(childPath, params);
109
110 if (path) {
111 originalPaths.push(path);
112 }
113
114 return originalPaths;
115 },
116 genForRoutes: function genForRoutes(_ref) {
117 var _this = this;
118
119 var _ref$routes = _ref.routes,
120 routes = _ref$routes === void 0 ? [] : _ref$routes,
121 _ref$params = _ref.params,
122 params = _ref$params === void 0 ? {} : _ref$params,
123 separator = _ref.separator,
124 _ref$itemRender = _ref.itemRender,
125 itemRender = _ref$itemRender === void 0 ? defaultItemRender : _ref$itemRender;
126 var paths = [];
127 return routes.map(function (route) {
128 var _slot3;
129
130 var path = _this.getPath(route.path, params);
131
132 if (path) {
133 paths.push(path);
134 }
135
136 var tempPaths = [].concat(paths); // generated overlay by route.children
137
138 var overlay = null;
139
140 if (route.children && route.children.length) {
141 var _slot2;
142
143 overlay = (0, _vue.createVNode)(_menu.default, null, _isSlot(_slot2 = route.children.map(function (child) {
144 var _slot;
145
146 return (0, _vue.createVNode)(_menu.default.Item, {
147 "key": child.path || child.breadcrumbName
148 }, _isSlot(_slot = itemRender({
149 route: child,
150 params: params,
151 routes: routes,
152 paths: _this.addChildPath(tempPaths, child.path, params)
153 })) ? _slot : {
154 default: function _default() {
155 return [_slot];
156 }
157 });
158 })) ? _slot2 : {
159 default: function _default() {
160 return [_slot2];
161 }
162 });
163 }
164
165 return (0, _vue.createVNode)(_BreadcrumbItem.default, {
166 "overlay": overlay,
167 "separator": separator,
168 "key": path || route.breadcrumbName
169 }, _isSlot(_slot3 = itemRender({
170 route: route,
171 params: params,
172 routes: routes,
173 paths: tempPaths
174 })) ? _slot3 : {
175 default: function _default() {
176 return [_slot3];
177 }
178 });
179 });
180 }
181 },
182 render: function render() {
183 var crumbs;
184 var customizePrefixCls = this.prefixCls,
185 routes = this.routes,
186 _this$params = this.params,
187 params = _this$params === void 0 ? {} : _this$params,
188 $slots = this.$slots;
189 var getPrefixCls = this.configProvider.getPrefixCls;
190 var prefixCls = getPrefixCls('breadcrumb', customizePrefixCls);
191 var children = (0, _propsUtil.filterEmpty)((0, _propsUtil.getSlot)(this));
192 var separator = (0, _propsUtil.getComponent)(this, 'separator');
193 var itemRender = this.itemRender || $slots.itemRender || defaultItemRender;
194
195 if (routes && routes.length > 0) {
196 // generated by route
197 crumbs = this.genForRoutes({
198 routes: routes,
199 params: params,
200 separator: separator,
201 itemRender: itemRender
202 });
203 } else if (children.length) {
204 crumbs = children.map(function (element, index) {
205 (0, _warning.default)(_typeof(element.type) === 'object' && (element.type.__ANT_BREADCRUMB_ITEM || element.type.__ANT_BREADCRUMB_SEPARATOR), 'Breadcrumb', "Only accepts Breadcrumb.Item and Breadcrumb.Separator as it's children");
206 return (0, _vue.cloneVNode)(element, {
207 separator: separator,
208 key: index
209 });
210 });
211 }
212
213 return (0, _vue.createVNode)("div", {
214 "class": prefixCls
215 }, _isSlot(crumbs) ? crumbs : {
216 default: function _default() {
217 return [crumbs];
218 }
219 });
220 }
221});
222
223exports.default = _default2;
\No newline at end of file