2018-06-20 01:07:09 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
|
|
|
import Service, { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
router: service(),
|
|
|
|
|
2018-06-27 18:26:52 +00:00
|
|
|
// currentURL is only used to listen to all transitions.
|
|
|
|
// currentRouteName has all information necessary to compute breadcrumbs,
|
|
|
|
// but it doesn't change when a transition to the same route with a different
|
|
|
|
// model occurs.
|
|
|
|
breadcrumbs: computed('router.currentURL', 'router.currentRouteName', function() {
|
2018-06-20 01:07:09 +00:00
|
|
|
const owner = getOwner(this);
|
2018-06-27 20:40:44 +00:00
|
|
|
const allRoutes = (this.get('router.currentRouteName') || '')
|
2018-06-20 01:07:09 +00:00
|
|
|
.split('.')
|
2018-06-27 20:40:44 +00:00
|
|
|
.without('')
|
2018-06-20 01:07:09 +00:00
|
|
|
.map((segment, index, allSegments) => allSegments.slice(0, index + 1).join('.'));
|
|
|
|
|
|
|
|
let crumbs = [];
|
|
|
|
allRoutes.forEach(routeName => {
|
|
|
|
const route = owner.lookup(`route:${routeName}`);
|
|
|
|
|
|
|
|
// Routes can reset the breadcrumb trail to start anew even
|
|
|
|
// if the route is deeply nested.
|
2019-03-26 07:46:44 +00:00
|
|
|
if (route.resetBreadcrumbs) {
|
2018-06-20 01:07:09 +00:00
|
|
|
crumbs = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Breadcrumbs are either an array of static crumbs
|
|
|
|
// or a function that returns breadcrumbs given the current
|
|
|
|
// model for the route's controller.
|
2019-03-26 07:46:44 +00:00
|
|
|
let breadcrumbs = route.breadcrumbs || [];
|
2018-06-20 01:07:09 +00:00
|
|
|
if (typeof breadcrumbs === 'function') {
|
2018-06-27 18:26:52 +00:00
|
|
|
breadcrumbs = breadcrumbs(route.get('controller.model')) || [];
|
2018-06-20 01:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crumbs.push(...breadcrumbs);
|
|
|
|
});
|
|
|
|
|
|
|
|
return crumbs;
|
|
|
|
}),
|
|
|
|
});
|