UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

226 lines (223 loc) • 8.57 kB
/** * DevExtreme (cjs/ui/hierarchical_collection/ui.hierarchical_collection_widget.js) * Version: 23.2.6 * Build date: Wed May 01 2024 * * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; exports.default = void 0; var _renderer = _interopRequireDefault(require("../../core/renderer")); var _data = require("../../core/utils/data"); var _extend = require("../../core/utils/extend"); var _iterator = require("../../core/utils/iterator"); var _devices = _interopRequireDefault(require("../../core/devices")); var _icon = require("../../core/utils/icon"); var _ui = _interopRequireDefault(require("./ui.data_adapter")); var _uiCollection_widget = _interopRequireDefault(require("../collection/ui.collection_widget.edit")); var _bindable_template = require("../../core/templates/bindable_template"); var _type = require("../../core/utils/type"); var _common = require("../../core/utils/common"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj } } function _extends() { _extends = Object.assign ? Object.assign.bind() : function(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key] } } } return target }; return _extends.apply(this, arguments) } const DISABLED_STATE_CLASS = "dx-state-disabled"; const ITEM_URL_CLASS = "dx-item-url"; const HierarchicalCollectionWidget = _uiCollection_widget.default.inherit({ _getDefaultOptions: function() { return (0, _extend.extend)(this.callBase(), { keyExpr: "id", displayExpr: "text", selectedExpr: "selected", disabledExpr: "disabled", itemsExpr: "items", hoverStateEnabled: true, parentIdExpr: "parentId", expandedExpr: "expanded" }) }, _defaultOptionsRules: function() { return this.callBase().concat([{ device: function() { return "desktop" === _devices.default.real().deviceType && !_devices.default.isSimulator() }, options: { focusStateEnabled: true } }]) }, _init: function() { this.callBase(); this._initAccessors(); this._initDataAdapter(); this._initDynamicTemplates() }, _initDataSource: function() { this.callBase(); this._dataSource && this._dataSource.paginate(false) }, _initDataAdapter: function() { const accessors = this._createDataAdapterAccessors(); this._dataAdapter = new _ui.default((0, _extend.extend)({ dataAccessors: { getters: accessors.getters, setters: accessors.setters }, items: this.option("items") }, this._getDataAdapterOptions())) }, _getDataAdapterOptions: _common.noop, _getItemExtraPropNames: _common.noop, _initDynamicTemplates: function() { const fields = ["text", "html", "items", "icon"].concat(this._getItemExtraPropNames()); this._templateManager.addDefaultTemplates({ item: new _bindable_template.BindableTemplate(this._addContent.bind(this), fields, this.option("integrationOptions.watchMethod"), { text: this._displayGetter, items: this._itemsGetter }) }) }, _addContent: function($container, itemData) { $container.html(itemData.html).append(this._getIconContainer(itemData)).append(this._getTextContainer(itemData)) }, _getLinkContainer: function(iconContainer, textContainer, _ref) { let { linkAttr: linkAttr, url: url } = _ref; const linkAttributes = (0, _type.isObject)(linkAttr) ? linkAttr : {}; return (0, _renderer.default)("<a>").addClass("dx-item-url").attr(_extends({}, linkAttributes, { href: url })).append(iconContainer).append(textContainer) }, _getIconContainer: function(itemData) { if (!itemData.icon) { return } const $imageContainer = (0, _icon.getImageContainer)(itemData.icon); if ($imageContainer.is("img")) { $imageContainer.attr("alt", "".concat(this.NAME, " item icon")) } return $imageContainer }, _getTextContainer: function(itemData) { return (0, _renderer.default)("<span>").text(itemData.text) }, _initAccessors: function() { const that = this; (0, _iterator.each)(this._getAccessors(), (function(_, accessor) { that._compileAccessor(accessor) })); this._compileDisplayGetter() }, _getAccessors: function() { return ["key", "selected", "items", "disabled", "parentId", "expanded"] }, _getChildNodes: function(node) { const that = this; const arr = []; (0, _iterator.each)(node.internalFields.childrenKeys, (function(_, key) { const childNode = that._dataAdapter.getNodeByKey(key); arr.push(childNode) })); return arr }, _hasChildren: function(node) { return node && node.internalFields.childrenKeys.length }, _compileAccessor: function(optionName) { const getter = "_" + optionName + "Getter"; const setter = "_" + optionName + "Setter"; const optionExpr = this.option(optionName + "Expr"); if (!optionExpr) { this[getter] = _common.noop; this[setter] = _common.noop; return } else if ((0, _type.isFunction)(optionExpr)) { this[setter] = function(obj, value) { obj[optionExpr()] = value }; this[getter] = function(obj) { return obj[optionExpr()] }; return } this[getter] = (0, _data.compileGetter)(optionExpr); this[setter] = (0, _data.compileSetter)(optionExpr) }, _createDataAdapterAccessors: function() { const that = this; const accessors = { getters: {}, setters: {} }; (0, _iterator.each)(this._getAccessors(), (function(_, accessor) { const getterName = "_" + accessor + "Getter"; const setterName = "_" + accessor + "Setter"; const newAccessor = "parentId" === accessor ? "parentKey" : accessor; accessors.getters[newAccessor] = that[getterName]; accessors.setters[newAccessor] = that[setterName] })); accessors.getters.display = !this._displayGetter ? itemData => itemData.text : this._displayGetter; return accessors }, _initMarkup: function() { this.callBase(); this._addWidgetClass() }, _addWidgetClass: function() { this._focusTarget().addClass(this._widgetClass()) }, _widgetClass: _common.noop, _renderItemFrame: function(index, itemData) { const $itemFrame = this.callBase.apply(this, arguments); $itemFrame.toggleClass("dx-state-disabled", !!this._disabledGetter(itemData)); return $itemFrame }, _optionChanged: function(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(); this.callBase(args); break; default: this.callBase(args) } } }); var _default = HierarchicalCollectionWidget; exports.default = _default; module.exports = exports.default; module.exports.default = exports.default;