vue-2-breadcrumbs
Version:
Breadcrumbs for Vue.js 2.0
201 lines (192 loc) • 9.51 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}
function pathSeparate(path) {
var ROOT = '/';
var SEPARATOR = '/';
return path.slice(ROOT.length).split(SEPARATOR).reverse();
}
var VueBreadcrumbs = /** @class */ (function () {
function VueBreadcrumbs() {
}
VueBreadcrumbs.prototype.install = function (Vue, options) {
if (options === void 0) { options = {}; }
if (options.template) {
options.render = undefined;
}
Object.defineProperties(Vue.prototype, {
$breadcrumbs: {
get: function () {
var _this = this;
function findParents(routeName, matches) {
var _a;
if (matches === void 0) { matches = []; }
var routeParents = this.$router.resolve({ name: routeName }).route.matched;
var routeParentLast = routeParents.pop();
if (routeParentLast) {
matches.unshift(routeParentLast);
var breadcrumb = (_a = routeParentLast.meta) === null || _a === void 0 ? void 0 : _a.breadcrumb;
if (breadcrumb === undefined) {
if (routeParentLast.name) {
breadcrumb = routeParentLast.name;
}
}
if (typeof breadcrumb === 'function') {
breadcrumb = breadcrumb.call(this, this.$route.params);
}
if (breadcrumb === null || breadcrumb === void 0 ? void 0 : breadcrumb.parent) {
return findParents.call(this, breadcrumb.parent, matches);
}
}
return routeParents.concat(matches);
}
function resolveByPath(route, params) {
var _a;
var label = pathSeparate(route.path)[0];
var isRoot = route.parent === undefined;
var isRootChildren = label.length === 0;
var isDynamicPath = label.startsWith(':');
if (isRoot && isRootChildren) {
// TODO: add options home name
// const label = 'Home';
route.meta.breadcrumb = 'Home';
return;
}
if (isRoot) {
route.meta.breadcrumb = label;
return;
}
if (isRootChildren && ((_a = route.meta) === null || _a === void 0 ? void 0 : _a.breadcrumb)) {
delete route.meta.breadcrumb;
return;
}
if (isDynamicPath) {
var key = label.slice(1);
if (Reflect.has(params, key)) {
route.meta.breadcrumb = params[key];
return;
}
}
route.meta.breadcrumb = label;
return;
}
return this.$route.matched
.flatMap(function (route) {
var _a;
var routeRecord = [route];
var breadcrumb = (_a = route.meta) === null || _a === void 0 ? void 0 : _a.breadcrumb;
if (breadcrumb === undefined) {
resolveByPath(route, _this.$route.params);
}
if (typeof breadcrumb === 'function') {
breadcrumb = breadcrumb.call(_this, _this.$route.params);
}
if (breadcrumb === null || breadcrumb === void 0 ? void 0 : breadcrumb.parent) {
var matched = findParents.call(_this, breadcrumb.parent, []);
routeRecord = __spreadArrays(matched, routeRecord);
}
return routeRecord;
})
.map(function (route) { return route.path.length === 0
? (__assign(__assign({}, route), { path: '/' }))
: route; });
}
}
});
Vue.component('Breadcrumbs', Vue.extend(__assign({ methods: {
getBreadcrumb: function (bc) {
var name = bc;
if (typeof name === 'function') {
name = name.call(this, this.$route.params);
}
if (typeof name === 'object') {
name = name.label;
}
return name;
},
getPath: function (crumb) {
var path = crumb.path;
for (var _i = 0, _a = Object.entries(this.$route.params); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
path = path.replace(":" + key, value);
}
return path;
}
}, render: function (createElement) {
var _this = this;
if (this.$breadcrumbs.length) {
return createElement('ol', {
class: {
'breadcrumb': true
}
}, this.$breadcrumbs.map(function (crumb, index) {
var _a;
if ((_a = crumb === null || crumb === void 0 ? void 0 : crumb.meta) === null || _a === void 0 ? void 0 : _a.breadcrumb) {
var label = _this.getBreadcrumb(crumb.meta.breadcrumb);
if ((label === null || label === void 0 ? void 0 : label.length) > 0) {
var item = void 0;
if (index !== _this.$breadcrumbs.length - 1) {
item = createElement('router-link', {
props: {
to: { path: _this.getPath(crumb) },
tag: 'a'
}
}, " " + label);
}
else {
item = createElement('span', " " + label);
}
return createElement('li', {
class: {
'breadcrumb-item': true
},
props: {
key: index
}
}, [
item
]);
}
}
return createElement();
}));
}
return createElement();
} }, options)));
};
return VueBreadcrumbs;
}());
var index = new VueBreadcrumbs();
// Automatic installation if Vue has been added to the global scope.
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(new VueBreadcrumbs());
}
exports.default = index;