@progress/kendo-angular-menu
Version:
Kendo UI Angular Menu component
125 lines (124 loc) • 4.88 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 { Directive, Input } from '@angular/core';
import { MenuBase } from '../menu-base';
import { BindingDirectiveBase } from './binding-directive-base';
import { getter, last } from './utils';
import * as i0 from "@angular/core";
import * as i1 from "../menu-base";
const getField = (field, level) => Array.isArray(field) ? field[level] || last(field) : field;
/**
* Represents a directive that converts the provided hierarchical data to [MenuItems]({% slug api_menu_menuitem %}) and binds them to the Menu.
*
* @example
* ```html
* <kendo-menu [kendoMenuHierarchyBinding]="hierarchicalData" [textField]="'text'" [childrenField]="'items'">
* </kendo-menu>
* ```
*
* @remarks
* Applied to: {@link MenuComponent}.
*/
export class HierarchyBindingDirective extends BindingDirectiveBase {
/**
* Specifies the array of data which will be used to populate the Menu.
*/
data;
/**
* Defines the `text` field (or fields) of an item.
*/
textField;
/**
* Defines the `url` field (or fields) of an item.
*/
urlField;
/**
* Defines the `icon` field (or fields) of an item.
*/
iconField;
/**
* Defines the `svgIcon` field of the items.
*/
svgIconField;
/**
* Defines the `disabled` field (or fields) of an item.
*/
disabledField;
/**
* Defines the `cssClass` field (or fields) of an item.
*/
cssClassField;
/**
* Defines the `cssStyle` field (or fields) of an item.
*/
cssStyleField;
/**
* Defines the `separator` field (or fields) of the items.
*/
separatorField;
/**
* Defines the `children` field (or fields) of the items.
*/
childrenField;
constructor(menu) {
super(menu);
}
mapItems(items, level = 0) {
return items.map((item) => {
const menuItem = this.createItem(item, level);
const children = this.getChildren(item, level);
if (children) {
menuItem.items = this.mapItems(children, level + 1);
}
return menuItem;
});
}
createItem(item, level) {
const result = { data: item };
const fields = this.fields;
for (let idx = 0; idx < fields.length; idx++) {
const { target, source } = fields[idx];
result[target] = getter(getField(source, level))(item);
}
return result;
}
getChildren(item, level) {
if (this.childrenField) {
const field = getField(this.childrenField, level);
return item[field];
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: HierarchyBindingDirective, deps: [{ token: i1.MenuBase }], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: HierarchyBindingDirective, isStandalone: true, selector: "[kendoMenuHierarchyBinding]", inputs: { data: ["kendoMenuHierarchyBinding", "data"], textField: "textField", urlField: "urlField", iconField: "iconField", svgIconField: "svgIconField", disabledField: "disabledField", cssClassField: "cssClassField", cssStyleField: "cssStyleField", separatorField: "separatorField", childrenField: "childrenField" }, exportAs: ["kendoMenuHierarchyBinding"], usesInheritance: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: HierarchyBindingDirective, decorators: [{
type: Directive,
args: [{
exportAs: 'kendoMenuHierarchyBinding',
selector: '[kendoMenuHierarchyBinding]',
standalone: true
}]
}], ctorParameters: function () { return [{ type: i1.MenuBase }]; }, propDecorators: { data: [{
type: Input,
args: ["kendoMenuHierarchyBinding"]
}], textField: [{
type: Input
}], urlField: [{
type: Input
}], iconField: [{
type: Input
}], svgIconField: [{
type: Input
}], disabledField: [{
type: Input
}], cssClassField: [{
type: Input
}], cssStyleField: [{
type: Input
}], separatorField: [{
type: Input
}], childrenField: [{
type: Input
}] } });