Skip to content

Commit

Permalink
feat: add nuxtjs support, close #115
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Apr 16, 2021
1 parent bb0a6e8 commit cbacf68
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type Breadcrumbs = {
parent: string
}

function pathSeparate(path: string): string[] {
const ROOT = '/';
const SEPARATOR = '/';
return path.slice(ROOT.length).split(SEPARATOR).reverse();
}

class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
public install(Vue: VueConstructor<Vue>, options: Dictionary<any> = {}) {

Expand Down Expand Up @@ -47,36 +53,39 @@ class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
return routeParents.concat(matches);
}

function resolveByName(route: RouteRecord, params: Record<string, any>): string {
route.meta.breadcrumb = {};

function resolveByPath(route: RouteRecord, params: Record<string, any>): void {
const [label] = pathSeparate(route.path);
const isRoot: boolean = route.parent === undefined;
const isRootChildren = isRoot === false && route.path.split('/').reverse()[0].length === 0;
let label = route.name;
const isRootChildren = label.length === 0;
const isDynamicPath = label.startsWith(':');

if (isRoot && isRootChildren) {
// TODO: add options home name
// const label = 'Home';
route.meta.breadcrumb = 'Home';
return;
}

if (isRoot && label === undefined) {
label = route.path.replace(/\//, '');
if (isRoot) {
route.meta.breadcrumb = label;
return;
}

if (isRootChildren) {
if (label && route.parent) {
route.parent.meta.breadcrumb = label;
}
if (isRootChildren && route.meta?.breadcrumb) {
delete route.meta.breadcrumb;
return;
}

if (isRoot === false && isRootChildren === false) {
label = label?.split('-').reverse()[0];

if (label && Reflect.has(params, label)) {
label = params[label];
if (isDynamicPath) {
const key = label.slice(1);
if (Reflect.has(params, key)) {
route.meta.breadcrumb = params[key];
return;
}

route.meta.breadcrumb = label;
}

return label ?? ''
route.meta.breadcrumb = label
return;
}

return this.$route.matched
Expand All @@ -85,7 +94,7 @@ class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
let breadcrumb = route.meta?.breadcrumb;

if (breadcrumb === undefined) {
breadcrumb = resolveByName(route, this.$route.params);
resolveByPath(route, this.$route.params);
}

if (typeof breadcrumb === 'function') {
Expand Down

0 comments on commit cbacf68

Please sign in to comment.