celerichain-ember-uii
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
88 lines (68 loc) • 2.95 kB
JavaScript
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { computed, action } from '@ember/object';
import { isBlank } from '@ember/utils';
import { bool } from '@ember/object/computed';
import isMenuItemActive from '../../../../utils/is-menu-item-active';
export default class LayoutHeaderDropdownItemComponent extends Component {
router;
hostRouter;
isInteractive;
isAnchor;
isSeperator;
get isLink() {
return typeof this.args.item.route === 'string' && typeof this.args.item.onClick !== 'function';
}
get isComponent() {
return typeof this.args.item.component === 'string' && typeof this.args.item.onClick !== 'function';
}
get isTextOnly() {
const { isAnchor, isLink, isComponent, isSeperator, isInteractive } = this;
const { text } = this.args.item;
return [isAnchor, isLink, isComponent, isSeperator, isInteractive].every((prop) => prop === false) && text;
}
get active() {
const { route, item } = this.args;
const router = this.getRouter();
const currentRoute = router.currentRouteName;
// if interactive use
if (this.isInteractive && !isBlank(item)) {
return isMenuItemActive(item.section, item.slug, item.view);
}
return typeof route === 'string' && currentRoute.startsWith(route);
}
onClick(event) {
const { url, target, route, model, onClick, options } = this.args;
const router = this.getRouter();
const anchor = event.target?.closest('a');
if (anchor && anchor.attributes?.disabled && anchor.attributes.disabled !== 'disabled="false"') {
return;
}
if (target && url) {
return window.open(url, target);
}
if (url) {
return (window.location.href = url);
}
if (typeof onClick === 'function') {
return onClick();
}
if (!isBlank(options) && route && model) {
return router.transitionTo(route, model, options);
}
if (!isBlank(options) && route) {
return router.transitionTo(route, options);
}
if (route && model) {
return router.transitionTo(route, model);
}
if (route) {
return router.transitionTo(route);
}
}
getRouter() {
return this.router ?? this.hostRouter;
}
}