-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Charles Fries <[email protected]>
- Loading branch information
1 parent
c7b7f66
commit 276b805
Showing
8 changed files
with
143 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { inject as service } from '@ember/service'; | ||
import type BreadcrumbsService from '../services/breadcrumbs'; | ||
|
||
type TitleParts = [title: string]; | ||
type OptionalData = Record<string, unknown>; | ||
|
||
export interface BreadcrumbHelperSignature { | ||
Args: { | ||
Positional: TitleParts; | ||
Named: OptionalData; | ||
}; | ||
Return: void; | ||
} | ||
|
||
export default class BreadcrumbHelper extends Helper<BreadcrumbHelperSignature> { | ||
@service('breadcrumbs') declare breadcrumbsService: BreadcrumbsService; | ||
|
||
breadcrumbId: string | null = null; | ||
|
||
get breadcrumbWasAddedBefore() { | ||
return !!this.breadcrumbId; | ||
} | ||
|
||
compute(breadcrumbTitleParts: TitleParts, optionalData: OptionalData) { | ||
const breadcrumbTitle = breadcrumbTitleParts.join(''); | ||
|
||
const breadcrumbData = { | ||
data: optionalData, | ||
title: breadcrumbTitle, | ||
}; | ||
|
||
if (this.breadcrumbWasAddedBefore) { | ||
this.breadcrumbsService.updateBreadcrumb( | ||
this.breadcrumbId!, | ||
breadcrumbData, | ||
); | ||
} else { | ||
this.breadcrumbId = this.breadcrumbsService.addBreadcrumb(breadcrumbData); | ||
} | ||
} | ||
|
||
willDestroy() { | ||
super.willDestroy(); | ||
|
||
if (this.breadcrumbWasAddedBefore) { | ||
this.breadcrumbsService.removeBreadcrumb(this.breadcrumbId!); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { inject as service } from '@ember/service'; | ||
import type BreadcrumbsService from '../services/breadcrumbs'; | ||
|
||
type BreadcrumbData = { | ||
title: string; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export interface BreadcrumbsHelperSignature { | ||
Return: BreadcrumbData[]; | ||
} | ||
|
||
export default class BreadcrumbsHelper extends Helper<BreadcrumbsHelperSignature> { | ||
@service('breadcrumbs') declare breadcrumbsService: BreadcrumbsService; | ||
|
||
compute() { | ||
return this.breadcrumbsService.items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// For some reason these imports make the build fail | ||
// export { default as breadcrumb } from './helpers/breadcrumb'; | ||
// export { default as breadcrumbs } from './helpers/breadcrumbs'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { guidFor } from '@ember/object/internals'; | ||
import { scheduleOnce, next } from '@ember/runloop'; | ||
import Service from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
type BreadcrumbId = string; | ||
type BreadcrumbData = { | ||
title: string; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export default class BreadcrumbsService extends Service { | ||
@tracked breadcrumbData: Map<BreadcrumbId, BreadcrumbData> = new Map(); | ||
pendingBreadcrumbData: Map<BreadcrumbId, BreadcrumbData> | null = null; | ||
didForceRerender: boolean = false; | ||
|
||
get items() { | ||
return [...this.breadcrumbData.values()]; | ||
} | ||
|
||
addBreadcrumb(breadcrumbData: BreadcrumbData): string { | ||
this.scheduleDataUpdate(); | ||
|
||
const uniqueId = generateUniqueId(); | ||
this.pendingBreadcrumbData?.set(uniqueId, breadcrumbData); | ||
return uniqueId; | ||
} | ||
|
||
updateBreadcrumb(uniqueId: BreadcrumbId, breadcrumbData: BreadcrumbData) { | ||
if (!this.didForceRerender) { | ||
this.scheduleDataUpdate(); | ||
this.pendingBreadcrumbData?.set(uniqueId, breadcrumbData); | ||
} | ||
} | ||
|
||
removeBreadcrumb(uniqueId: BreadcrumbId): void { | ||
this.scheduleDataUpdate(); | ||
this.pendingBreadcrumbData?.delete(uniqueId); | ||
} | ||
|
||
scheduleDataUpdate(): void { | ||
if (!this.pendingBreadcrumbData) { | ||
this.pendingBreadcrumbData = new Map(this.breadcrumbData); | ||
} | ||
|
||
scheduleOnce('afterRender', this, this.updateBreadcrumbData); | ||
} | ||
|
||
updateBreadcrumbData(): void { | ||
this.didForceRerender = true; | ||
|
||
this.breadcrumbData = this.pendingBreadcrumbData!; | ||
this.pendingBreadcrumbData = null; | ||
|
||
next(this, this.resetRerenderFlag); | ||
} | ||
|
||
resetRerenderFlag(): void { | ||
this.didForceRerender = false; | ||
} | ||
} | ||
|
||
function generateUniqueId(): string { | ||
return guidFor({}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters