@progress/kendo-angular-menu
Version:
Kendo UI Angular Menu component
303 lines (302 loc) • 12 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Component, Input, HostBinding, EventEmitter, Output, NgZone, Renderer2, Optional, TemplateRef, forwardRef, ViewContainerRef } from '@angular/core';
import { NgClass } from '@angular/common';
import { LocalizationService, L10N_PREFIX } from '@progress/kendo-angular-l10n';
import { validatePackage } from '@progress/kendo-licensing';
import { packageMetadata } from './package-metadata';
import { ItemsService } from './services/items.service';
import { ActionsService } from './services/actions.service';
import { NavigationService } from './services/navigation.service';
import { HoverService } from './services/hover.service';
import { normalize } from './open-on-click-settings';
import { inMenu } from './dom-queries';
import { ContextMenuService } from './context-menu/context-menu.service';
import { MenuBase } from './menu-base';
import { Keys, isDocumentAvailable, normalizeNumpadKeys } from '@progress/kendo-angular-common';
import { getSizeClass } from './utils';
import { ListComponent } from './rendering/list.component';
import * as i0 from "@angular/core";
import * as i1 from "./services/items.service";
import * as i2 from "./services/hover.service";
import * as i3 from "./services/actions.service";
import * as i4 from "./services/navigation.service";
import * as i5 from "@progress/kendo-angular-l10n";
import * as i6 from "./context-menu/context-menu.service";
/**
* Represents the [Kendo UI Menu component for Angular]({% slug overview_menu %}).
*
* @example
* ```ts
* _@Component({
* selector: 'my-app',
* template: `
* <kendo-menu [items]="items">
* </kendo-menu>
* `
* })
* class AppComponent {
* public items: any[] = [{ text: 'item1', items: [{ text: 'item1.1' }] }, { text: 'item2', disabled: true }];
* }
* ```
*
* @remarks
* Supported children components are: {@link MenuItemComponent}.
*/
export class MenuComponent extends MenuBase {
itemsService;
hover;
actions;
navigation;
localization;
ngZone;
renderer;
contextService;
/**
* Defines the container to which the popups will be appended.
*/
appendTo;
/**
* @hidden
*/
menuItemTemplate;
/**
* @hidden
*/
ariaRole = 'menubar';
/**
* @hidden
*/
menuItemLinkTemplate;
/**
* Fires when a Menu item is selected ([see example](slug:events_menu)).
*/
select = new EventEmitter();
/**
* Fires when a Menu item is opened ([see example](slug:events_menu)).
*/
open = new EventEmitter();
/**
* Fires when a Menu item is closed ([see example](slug:events_menu)).
*/
close = new EventEmitter();
/**
* @hidden
*/
get ariaOrientation() {
if (this.vertical) {
return 'vertical';
}
}
/**
* @hidden
*/
get isContextMenu() {
return Boolean(this.contextService);
}
get direction() {
return this.rtl;
}
get rtl() {
return this.localization.rtl;
}
/**
* @hidden
*/
get menuClasses() {
const sizeClass = getSizeClass(this.size);
const staticClasses = 'k-reset k-header k-menu';
if (this.isContextMenu) {
return `k-context-menu k-menu-group ${sizeClass}`;
}
return `${staticClasses} k-menu-${this.vertical ? 'vertical' : 'horizontal'}`;
}
closeClickSubscription;
contextKeyDownSubscription;
constructor(itemsService, hover, actions, navigation, localization, ngZone, renderer, contextService) {
super();
this.itemsService = itemsService;
this.hover = hover;
this.actions = actions;
this.navigation = navigation;
this.localization = localization;
this.ngZone = ngZone;
this.renderer = renderer;
this.contextService = contextService;
validatePackage(packageMetadata);
this.actions.owner = this;
if (contextService) {
contextService.items = this.itemsService;
this.contextKeyDownSubscription = contextService.keydown.subscribe(this.contextKeyDown.bind(this));
}
}
/**
* Opens or closes the specified Menu items.
*
* @param open - A Boolean value which indicates if the items will be opened or closed.
* @param indices - One or more values which represent the hierarchical indices of the items that will be opened or closed.
*/
toggle(open, ...indices) {
for (let idx = 0; idx < indices.length; idx++) {
const item = this.itemsService.get(indices[idx]);
if (item && !item.disabled) {
if (open) {
item.open();
}
else {
item.close();
}
}
}
}
/**
* @hidden
*/
focus(index) {
this.navigation.focusIndex(index);
}
ngOnChanges(changes) {
this.navigation.vertical = this.vertical;
this.hover.delay = this.hoverDelay;
if (changes.openOnClick) {
const openOnClick = this.openOnClick = normalize(this.openOnClick);
this.hover.openOnOver = !openOnClick;
if (openOnClick && openOnClick.toggle === 'click') {
this.attachCloseClick();
}
else {
this.unsubscribeClick();
}
}
}
ngAfterViewChecked() {
this.navigation.updateActive();
}
ngOnDestroy() {
this.unsubscribeClick();
if (this.contextService) {
this.contextService.items = null;
this.contextKeyDownSubscription.unsubscribe();
}
}
attachCloseClick() {
if (!this.closeClickSubscription && isDocumentAvailable()) {
this.ngZone.runOutsideAngular(() => {
this.closeClickSubscription = this.renderer.listen('document', 'click', (e) => {
if (!inMenu(e.target, this.itemsService)) {
this.hover.openOnOver = false;
this.actions.closeAll();
this.actions.execute();
}
});
});
}
}
unsubscribeClick() {
if (this.closeClickSubscription) {
this.closeClickSubscription();
}
}
contextKeyDown(e) {
if (!this.itemsService.hasItems) {
return;
}
const keyCode = normalizeNumpadKeys(e);
const rtl = this.localization.rtl;
const first = keyCode === Keys.ArrowDown || keyCode === Keys.ArrowRight;
const last = keyCode === Keys.ArrowUp || keyCode === Keys.ArrowLeft;
let index;
if ((first && !rtl) || (last && rtl)) {
index = 'first';
}
else if ((first && rtl) || (last && !rtl)) {
index = 'last';
}
if (index) {
e.preventDefault();
this.focus(index);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: MenuComponent, deps: [{ token: i1.ItemsService }, { token: i2.HoverService }, { token: i3.ActionsService }, { token: i4.NavigationService }, { token: i5.LocalizationService }, { token: i0.NgZone }, { token: i0.Renderer2 }, { token: i6.ContextMenuService, optional: true }], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: MenuComponent, isStandalone: true, selector: "kendo-menu", inputs: { appendTo: "appendTo", menuItemTemplate: "menuItemTemplate", ariaRole: "ariaRole", menuItemLinkTemplate: "menuItemLinkTemplate" }, outputs: { select: "select", open: "open", close: "close" }, host: { properties: { "class.k-rtl": "this.direction" } }, providers: [
ItemsService,
ActionsService,
NavigationService,
HoverService,
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.menu'
},
{
provide: MenuBase,
useExisting: forwardRef(() => MenuComponent)
}
], exportAs: ["kendoMenu"], usesInheritance: true, usesOnChanges: true, ngImport: i0, template: `
<ul
[attr.role]="ariaRole"
[attr.aria-orientation]="ariaOrientation"
kendoMenuList [items]="rootItems" [level]="0" [appendTo]="appendTo"
[size]="size" [vertical]="vertical" [rtl]="rtl" [animate]="animate" [openOnClick]="openOnClick"
[itemTemplate]="itemTemplate.first?.templateRef || menuItemTemplate"
[itemLinkTemplate]="itemLinkTemplate.first?.templateRef || menuItemLinkTemplate"
[ngClass]="menuClasses">
</ul>
`, isInline: true, dependencies: [{ kind: "component", type: ListComponent, selector: "[kendoMenuList]", inputs: ["appendTo", "items", "level", "index", "animate", "size", "vertical", "rtl", "openOnClick", "itemTemplate", "itemLinkTemplate"] }, { kind: "directive", type: NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: MenuComponent, decorators: [{
type: Component,
args: [{
exportAs: 'kendoMenu',
providers: [
ItemsService,
ActionsService,
NavigationService,
HoverService,
LocalizationService,
{
provide: L10N_PREFIX,
useValue: 'kendo.menu'
},
{
provide: MenuBase,
useExisting: forwardRef(() => MenuComponent)
}
],
selector: 'kendo-menu',
template: `
<ul
[attr.role]="ariaRole"
[attr.aria-orientation]="ariaOrientation"
kendoMenuList [items]="rootItems" [level]="0" [appendTo]="appendTo"
[size]="size" [vertical]="vertical" [rtl]="rtl" [animate]="animate" [openOnClick]="openOnClick"
[itemTemplate]="itemTemplate.first?.templateRef || menuItemTemplate"
[itemLinkTemplate]="itemLinkTemplate.first?.templateRef || menuItemLinkTemplate"
[ngClass]="menuClasses">
</ul>
`,
standalone: true,
imports: [ListComponent, NgClass]
}]
}], ctorParameters: function () { return [{ type: i1.ItemsService }, { type: i2.HoverService }, { type: i3.ActionsService }, { type: i4.NavigationService }, { type: i5.LocalizationService }, { type: i0.NgZone }, { type: i0.Renderer2 }, { type: i6.ContextMenuService, decorators: [{
type: Optional
}] }]; }, propDecorators: { appendTo: [{
type: Input
}], menuItemTemplate: [{
type: Input
}], ariaRole: [{
type: Input
}], menuItemLinkTemplate: [{
type: Input
}], select: [{
type: Output
}], open: [{
type: Output
}], close: [{
type: Output
}], direction: [{
type: HostBinding,
args: ['class.k-rtl']
}] } });