@fleetbase/ember-ui
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
88 lines (72 loc) • 2.51 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
export default class LayoutMobileNavbarComponent extends Component {
router;
hostRouter;
abilities;
universe;
navbarNode;
sidebarNode;
extensions = [];
menuItems = [];
constructor(owner, { menuItems = [] }) {
super(...arguments);
this.extensions = getOwner(this).application.extensions ?? [];
this.menuItems = this.mergeMenuItems(menuItems);
}
mergeMenuItems(menuItems = []) {
const headerMenuItems = this.universe.headerMenuItems;
const visibleMenuItems = [];
for (let i = 0; i < headerMenuItems.length; i++) {
const menuItem = headerMenuItems[i];
if (this.abilities.can(`${menuItem.id} see extension`)) {
visibleMenuItems.pushObject(menuItem);
}
}
// Merge additionals
visibleMenuItems.pushObjects(menuItems);
// Callback to allow mutation of menu items
if (typeof this.args.mutateMenuItems === 'function') {
this.args.mutateMenuItems(menuItems);
}
return visibleMenuItems;
}
setupMobileNavbar(element) {
this.navbarNode = element;
this.sidebarNode = element.previousElementSibling.querySelector('nav.next-sidebar');
if (typeof this.args.onSetup === 'function') {
this.onSetup(this);
}
// when hostrouter transitions close sidebar automatically
this.getRouter().on('routeDidChange', this.closeSidebar.bind(this));
}
routeTo(route) {
this.getRouter()
.transitionTo(route)
.then(() => {
this.closeSidebar();
});
}
toggleSidebar() {
if (this.isSidebarOpen()) {
this.closeSidebar();
} else {
this.openSidebar();
}
}
isSidebarOpen() {
return this.sidebarNode?.classList?.contains('is-open');
}
closeSidebar() {
this.sidebarNode?.classList?.remove('is-open');
}
openSidebar() {
this.sidebarNode?.classList?.add('is-open');
}
getRouter() {
return this.router ?? this.hostRouter;
}
}