Skip to content

Commit

Permalink
fix: delete old breadcrumbs and replace with bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaiWithJai committed Nov 30, 2021
1 parent 8d5aabd commit 5f9fc61
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 65 deletions.
6 changes: 3 additions & 3 deletions ui/app/components/breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class Breadcrumb extends Component {
@service bucket;
@service breadcrumbs;

@action register() {
this.bucket.registerBreadcrumb(this);
this.breadcrumbs.registerBreadcrumb(this);
}

@action deregister() {
this.bucket.deregisterBreadcrumb(this);
this.breadcrumbs.deregisterBreadcrumb(this);
}

constructor() {
Expand Down
4 changes: 2 additions & 2 deletions ui/app/components/breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class Breadcrumbs extends Component {
@service bucket;
@service breadcrumbs;

get crumbs() {
return this.bucket.crumbs;
return this.breadcrumbs.crumbs;
}
}
54 changes: 15 additions & 39 deletions ui/app/services/breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
import { getOwner } from '@ember/application';
import Service, { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import classic from 'ember-classic-decorator';
import Service from '@ember/service';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { schedule } from '@ember/runloop';

@classic
export default class BreadcrumbsService extends Service {
@service router;
export default class BucketService extends Service {
@tracked crumbs = [];

// 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.
@computed('router.{currentURL,currentRouteName}')
get breadcrumbs() {
const owner = getOwner(this);
const allRoutes = (this.get('router.currentRouteName') || '')
.split('.')
.without('')
.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.
if (route.resetBreadcrumbs) {
crumbs = [];
}

// Breadcrumbs are either an array of static crumbs
// or a function that returns breadcrumbs given the current
// model for the route's controller.
let breadcrumbs = route.breadcrumbs || [];
if (typeof breadcrumbs === 'function') {
breadcrumbs = breadcrumbs(route.get('controller.model')) || [];
}

crumbs.push(...breadcrumbs);
@action registerBreadcrumb(crumb) {
schedule('actions', this, () => {
console.log('register crumb: ', crumb);
this.crumbs = [...this.crumbs, crumb];
});
}

@action deregisterBreadcrumb(crumb) {
const newCrumbs = this.crumbs.filter(c => c !== crumb);

return crumbs;
this.crumbs = newCrumbs;
}
}
21 changes: 0 additions & 21 deletions ui/app/services/bucket.js

This file was deleted.

0 comments on commit 5f9fc61

Please sign in to comment.