UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

229 lines (228 loc) • 7.98 kB
/** * DevExtreme (esm/__internal/ui/hierarchical_collection/hierarchical_collection_widget.js) * Version: 26.1.4 * Build date: Fri Jul 24 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import devices from "../../../core/devices"; import $ from "../../../core/renderer"; import { BindableTemplate } from "../../../core/templates/bindable_template"; import { noop } from "../../../core/utils/common"; import { compileGetter, compileSetter } from "../../../core/utils/data"; import { getImageContainer } from "../../../core/utils/icon"; import { each } from "../../../core/utils/iterator"; import { isFunction, isObject } from "../../../core/utils/type"; import CollectionWidgetAsync from "../../../ui/collection/ui.collection_widget.async"; import DataAdapter from "./data_adapter"; const DISABLED_STATE_CLASS = "dx-state-disabled"; const ITEM_URL_CLASS = "dx-item-url"; class HierarchicalCollectionWidget extends CollectionWidgetAsync { _getDefaultOptions() { return Object.assign({}, super._getDefaultOptions(), { keyExpr: "id", displayExpr: "text", selectedExpr: "selected", disabledExpr: "disabled", itemsExpr: "items", hoverStateEnabled: true, parentIdExpr: "parentId", expandedExpr: "expanded" }) } _defaultOptionsRules() { return super._defaultOptionsRules().concat([{ device: () => "desktop" === devices.real().deviceType && !devices.isSimulator(), options: { focusStateEnabled: true } }]) } _init() { super._init(); this._initAccessors(); this._initDataAdapter(); this._initDynamicTemplates() } _initDataSource() { var _this$_dataSource; super._initDataSource(); null === (_this$_dataSource = this._dataSource) || void 0 === _this$_dataSource || _this$_dataSource.paginate(false) } _initDataAdapter() { const accessors = this._createDataAdapterAccessors(); const { items: items = [] } = this.option(); this._dataAdapter = new DataAdapter(Object.assign({ dataAccessors: { getters: accessors.getters, setters: accessors.setters }, items: items }, this._getDataAdapterOptions())) } _getDataAdapterOptions() { return {} } _getItemExtraPropNames() { return [] } _initDynamicTemplates() { const fields = ["text", "html", "items", "icon"].concat(this._getItemExtraPropNames()); this._templateManager.addDefaultTemplates({ item: new BindableTemplate(this._addContent.bind(this), fields, this.option("integrationOptions.watchMethod"), { text: this._displayGetter, items: this._itemsGetter }) }) } _addContent($container, itemData) { $container.html(itemData.html).append(this._getIconContainer(itemData)).append(this._getTextContainer(itemData)) } _getLinkContainer(iconContainer, textContainer, itemData) { const { linkAttr: linkAttr, url: url } = itemData; const linkAttributes = isObject(linkAttr) ? linkAttr : {}; return $("<a>").addClass("dx-item-url").attr(Object.assign({}, linkAttributes, { href: url })).append(iconContainer).append(textContainer) } _clickByLink(link) { link.addEventListener("click", e => { e.stopPropagation() }, { once: true }); link.click() } _getIconContainer(itemData) { if (!itemData.icon) { return } const $imageContainer = getImageContainer(itemData.icon); if ($imageContainer.is("img")) { var _this$NAME; const componentName = null !== (_this$NAME = this.NAME) && void 0 !== _this$NAME && _this$NAME.startsWith("dxPrivateComponent") ? "" : `${this.NAME} `; $imageContainer.attr("alt", `${componentName}item icon`) } return $imageContainer } _getTextContainer(itemData) { return $("<span>").text(itemData.text) } _initAccessors() { each(this._getAccessors(), (_index, accessor) => { this._compileAccessor(accessor) }); this._compileDisplayGetter() } _getAccessors() { return ["key", "selected", "items", "disabled", "parentId", "expanded"] } _getChildNodes(node) { const arr = []; each(node.internalFields.childrenKeys, (_, key) => { var _this$_dataAdapter; const childNode = null === (_this$_dataAdapter = this._dataAdapter) || void 0 === _this$_dataAdapter ? void 0 : _this$_dataAdapter.getNodeByKey(key); arr.push(childNode) }); return arr } _hasChildren(node) { var _node$internalFields; return Boolean(null === node || void 0 === node || null === (_node$internalFields = node.internalFields) || void 0 === _node$internalFields || null === (_node$internalFields = _node$internalFields.childrenKeys) || void 0 === _node$internalFields ? void 0 : _node$internalFields.length) } _compileAccessor(optionName) { const getter = `_${optionName}Getter`; const setter = `_${optionName}Setter`; const optionExpr = this.option(`${optionName}Expr`); if (!optionExpr) { this[getter] = noop; this[setter] = noop; return } if (isFunction(optionExpr)) { this[setter] = (obj, value) => { obj[optionExpr()] = value }; this[getter] = obj => obj[optionExpr()]; return } this[getter] = compileGetter(optionExpr); this[setter] = compileSetter(optionExpr) } _createDataAdapterAccessors() { const accessors = { getters: {}, setters: {} }; each(this._getAccessors(), (_index, accessor) => { const getterName = `_${accessor}Getter`; const setterName = `_${accessor}Setter`; const newAccessor = "parentId" === accessor ? "parentKey" : accessor; accessors.getters[newAccessor] = this[getterName]; accessors.setters[newAccessor] = this[setterName] }); accessors.getters.display = this._displayGetter ?? (itemData => itemData.text); return accessors } _initMarkup() { super._initMarkup(); this._addWidgetClass() } _addWidgetClass() { this._focusTarget().addClass(this._widgetClass()) } _widgetClass() { return "" } _renderItemFrame(index, itemData, $itemContainer) { const $itemFrame = super._renderItemFrame(index, itemData, $itemContainer); $itemFrame.toggleClass("dx-state-disabled", !!this._disabledGetter(itemData)); return $itemFrame } _optionChanged(args) { switch (args.name) { case "displayExpr": case "keyExpr": this._initAccessors(); this._initDynamicTemplates(); this.repaint(); break; case "itemsExpr": case "selectedExpr": case "disabledExpr": case "expandedExpr": case "parentIdExpr": this._initAccessors(); this._initDataAdapter(); this.repaint(); break; case "items": this._initDataAdapter(); super._optionChanged(args); break; default: super._optionChanged(args) } } } export default HierarchicalCollectionWidget;