github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/services/breadcrumbs.js (about)

     1  import { getOwner } from '@ember/application';
     2  import Service, { inject as service } from '@ember/service';
     3  import { computed } from '@ember/object';
     4  import classic from 'ember-classic-decorator';
     5  
     6  @classic
     7  export default class BreadcrumbsService extends Service {
     8    @service router;
     9  
    10    // currentURL is only used to listen to all transitions.
    11    // currentRouteName has all information necessary to compute breadcrumbs,
    12    // but it doesn't change when a transition to the same route with a different
    13    // model occurs.
    14    @computed('router.{currentURL,currentRouteName}')
    15    get breadcrumbs() {
    16      const owner = getOwner(this);
    17      const allRoutes = (this.get('router.currentRouteName') || '')
    18        .split('.')
    19        .without('')
    20        .map((segment, index, allSegments) => allSegments.slice(0, index + 1).join('.'));
    21  
    22      let crumbs = [];
    23      allRoutes.forEach(routeName => {
    24        const route = owner.lookup(`route:${routeName}`);
    25  
    26        // Routes can reset the breadcrumb trail to start anew even
    27        // if the route is deeply nested.
    28        if (route.resetBreadcrumbs) {
    29          crumbs = [];
    30        }
    31  
    32        // Breadcrumbs are either an array of static crumbs
    33        // or a function that returns breadcrumbs given the current
    34        // model for the route's controller.
    35        let breadcrumbs = route.breadcrumbs || [];
    36        if (typeof breadcrumbs === 'function') {
    37          breadcrumbs = breadcrumbs(route.get('controller.model')) || [];
    38        }
    39  
    40        crumbs.push(...breadcrumbs);
    41      });
    42  
    43      return crumbs;
    44    }
    45  }