This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
bread-crumbs.js
158 lines (128 loc) · 4.65 KB
/
bread-crumbs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Component from '@ember/component';
import { copy } from 'ember-copy';
import { assert } from '@ember/debug';
import { deprecate } from '@ember/application/deprecations';
import { typeOf, isPresent } from '@ember/utils';
import {
setProperties,
getWithDefault,
computed,
get
} from '@ember/object';
import { getOwner } from '@ember/application';
import { A as emberArray } from '@ember/array';
import { classify } from '@ember/string';
import layout from '../templates/components/bread-crumbs';
import { inject as service } from '@ember/service';
const {
bool,
readOnly
} = computed;
export default Component.extend({
layout,
tagName: 'ol',
linkable: true,
reverse: false,
classNameBindings: ['breadCrumbClass'],
hasBlock: bool('template').readOnly(),
routing: service('-routing'),
currentUrl: readOnly('applicationRoute.router.url'),
currentRouteName: readOnly('applicationRoute.controller.currentRouteName'),
routeHierarchy: computed('currentUrl', 'currentRouteName', 'reverse', {
get() {
get(this, 'currentUrl');
const currentRouteName = getWithDefault(this, 'currentRouteName', false);
assert('[ember-crumbly] Could not find a current route', currentRouteName);
const routeNames = currentRouteName.split('.');
const filteredRouteNames = this._filterIndexAndLoadingRoutes(routeNames);
const crumbs = this._lookupBreadCrumb(routeNames, filteredRouteNames);
return get(this, 'reverse') ? crumbs.reverse() : crumbs;
}
}).readOnly(),
breadCrumbClass: computed('outputStyle', {
get() {
let className = 'breadcrumb';
const outputStyle = getWithDefault(this, 'outputStyle', '');
if (isPresent(outputStyle)) {
deprecate('outputStyle option will be deprecated in the next major release', false, { id: 'ember-crumbly.outputStyle', until: '2.0.0' });
}
const lowerCaseOutputStyle = outputStyle.toLowerCase();
if (lowerCaseOutputStyle === 'foundation') {
className = 'breadcrumbs';
}
return className;
}
}).readOnly(),
_guessRoutePath(routeNames, name, index) {
const routes = routeNames.slice(0, index + 1);
if (routes.length === 1) {
let path = `${name}.index`;
return (this._lookupRoute(path)) ? path : name;
}
return routes.join('.');
},
_filterIndexAndLoadingRoutes(routeNames) {
return routeNames.filter((name) => !(name === 'index' || name === 'loading'));
},
/*
* Lookup local route first and fallback to engine,
* I'm not exactly familiar with local vs engine routes,
* but my thinking is you should be able to override an
* engine route locally so it should take priority.
* I could be totally wrong here...
*/
_lookupRoute(routeName) {
return this._lookupLocalRoute(routeName) || this._lookupEngineRoute(routeName);
},
_lookupLocalRoute(routeName) {
return getOwner(this).lookup(`route:${routeName}`);
},
_lookupEngineRoute(routeName) {
const router = get(this, 'routing.router');
let engineInfo = router._engineInfoByRoute[routeName];
if (!engineInfo) {
return;
}
return router
._getEngineInstance(engineInfo)
.lookup(`route:${engineInfo.localFullName}`);
},
_lookupBreadCrumb(routeNames, filteredRouteNames) {
const defaultLinkable = get(this, 'linkable');
const pathLength = filteredRouteNames.length;
const breadCrumbs = emberArray();
filteredRouteNames.map((name, index) => {
let path = this._guessRoutePath(routeNames, name, index);
const route = this._lookupRoute(path);
const isHead = index === 0;
const isTail = index === pathLength - 1;
const crumbLinkable = (index === pathLength - 1) ? false : defaultLinkable;
assert(`[ember-crumbly] \`route:${path}\` was not found`, route);
const multipleBreadCrumbs = route.get('breadCrumbs');
if (multipleBreadCrumbs) {
multipleBreadCrumbs.forEach((breadCrumb) => {
breadCrumbs.pushObject(breadCrumb);
});
} else {
let breadCrumb = copy(getWithDefault(route, 'breadCrumb', {
title: classify(name)
}));
if (typeOf(breadCrumb) === 'null') {
return;
} else {
if (isPresent(breadCrumb.path)) {
path = breadCrumb.path;
}
setProperties(breadCrumb, {
path,
isHead,
isTail,
linkable: breadCrumb.hasOwnProperty('linkable') ? breadCrumb.linkable : crumbLinkable
});
}
breadCrumbs.pushObject(breadCrumb);
}
});
return emberArray(breadCrumbs.filter((breadCrumb) => typeOf(breadCrumb) !== 'undefined'));
}
});