/** * DevExtreme (ui/tree_view/ui.tree_view.base.js) * Version: 19.1.16 * Build date: Tue Oct 18 2022 * * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; var _renderer = require("../../core/renderer"); var _renderer2 = _interopRequireDefault(_renderer); var _dom_adapter = require("../../core/dom_adapter"); var _events_engine = require("../../events/core/events_engine"); var _message = require("../../localization/message"); var _message2 = _interopRequireDefault(_message); var _click = require("../../events/click"); var _click2 = _interopRequireDefault(_click); var _common = require("../../core/utils/common"); var _common2 = _interopRequireDefault(_common); var _window = require("../../core/utils/window"); var _window2 = _interopRequireDefault(_window); var _type = require("../../core/utils/type"); var _extend = require("../../core/utils/extend"); var _iterator = require("../../core/utils/iterator"); var _dom = require("../../core/utils/dom"); var _check_box = require("../check_box"); var _check_box2 = _interopRequireDefault(_check_box); var _ui = require("../hierarchical_collection/ui.hierarchical_collection_widget"); var _ui2 = _interopRequireDefault(_ui); var _utils = require("../../events/utils"); var _pointer = require("../../events/pointer"); var _double_click = require("../../events/double_click"); var _double_click2 = _interopRequireDefault(_double_click); var _fx = require("../../animation/fx"); var _fx2 = _interopRequireDefault(_fx); var _ui3 = require("../scroll_view/ui.scrollable"); var _ui4 = _interopRequireDefault(_ui3); var _load_indicator = require("../load_indicator"); var _load_indicator2 = _interopRequireDefault(_load_indicator); var _deferred = require("../../core/utils/deferred"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj } } var WIDGET_CLASS = "dx-treeview"; var NODE_CLASS = "".concat(WIDGET_CLASS, "-node"); var NODE_CONTAINER_CLASS = "".concat(NODE_CLASS, "-container"); var NODE_LOAD_INDICATOR_CLASS = "".concat(NODE_CLASS, "-loadindicator"); var OPENED_NODE_CONTAINER_CLASS = "".concat(NODE_CLASS, "-container-opened"); var IS_LEAF = "".concat(NODE_CLASS, "-is-leaf"); var ITEM_CLASS = "".concat(WIDGET_CLASS, "-item"); var ITEM_WITH_CHECKBOX_CLASS = "".concat(ITEM_CLASS, "-with-checkbox"); var ITEM_WITHOUT_CHECKBOX_CLASS = "".concat(ITEM_CLASS, "-without-checkbox"); var ITEM_DATA_KEY = "".concat(ITEM_CLASS, "-data"); var TOGGLE_ITEM_VISIBILITY_CLASS = "".concat(WIDGET_CLASS, "-toggle-item-visibility"); var LOAD_INDICATOR_CLASS = "".concat(WIDGET_CLASS, "-loadindicator"); var LOAD_INDICATOR_WRAPPER_CLASS = "".concat(WIDGET_CLASS, "-loadindicator-wrapper"); var TOGGLE_ITEM_VISIBILITY_OPENED_CLASS = "".concat(WIDGET_CLASS, "-toggle-item-visibility-opened"); var SELECT_ALL_ITEM_CLASS = "".concat(WIDGET_CLASS, "-select-all-item"); var INVISIBLE_STATE_CLASS = "dx-state-invisible"; var DISABLED_STATE_CLASS = "dx-state-disabled"; var SELECTED_ITEM_CLASS = "dx-state-selected"; var EXPAND_EVENT_NAMESPACE = "dxTreeView_expand"; var DATA_ITEM_ID = "data-item-id"; var TreeViewBase = _ui2.default.inherit({ _supportedKeys: function(e) { var _this = this; var click = function(e) { var $itemElement = (0, _renderer2.default)(_this.option("focusedElement")); if (!$itemElement.length) { return } e.target = $itemElement; e.currentTarget = $itemElement; _this._itemClickHandler(e, $itemElement.children("." + ITEM_CLASS)); var expandEventName = _this._getEventNameByOption(_this.option("expandEvent")); var expandByClick = expandEventName === (0, _utils.addNamespace)(_click2.default.name, EXPAND_EVENT_NAMESPACE); if (expandByClick) { _this._expandEventHandler(e) } }; var select = function(e) { e.preventDefault(); _this._changeCheckBoxState((0, _renderer2.default)(_this.option("focusedElement"))) }; var toggleExpandedNestedItems = function(state, e) { if (!this.option("expandAllEnabled")) { return } e.preventDefault(); var $rootElement = (0, _renderer2.default)(this.option("focusedElement")); if (!$rootElement.length) { return } var rootItem = this._getItemData($rootElement.find(".".concat(ITEM_CLASS))); this._toggleExpandedNestedItems([rootItem], state) }; return (0, _extend.extend)(this.callBase(), { enter: this._showCheckboxes() ? select : click, space: this._showCheckboxes() ? select : click, asterisk: toggleExpandedNestedItems.bind(this, true), minus: toggleExpandedNestedItems.bind(this, false) }) }, _changeCheckBoxState: function($element) { var checkboxInstance = this._getCheckBoxInstance($element); var currentState = checkboxInstance.option("value"); if (!checkboxInstance.option("disabled")) { this._updateItemSelection(!currentState, $element.find("." + ITEM_CLASS).get(0), true, $element) } }, _toggleExpandedNestedItems: function(items, state) { if (!items) { return } for (var i = 0, len = items.length; i < len; i++) { var item = items[i]; var node = this._dataAdapter.getNodeByItem(item); this._toggleExpandedState(node, state); this._toggleExpandedNestedItems(item.items, state) } }, _getNodeElement: function(node, cache) { var normalizedKey = _common2.default.normalizeKey(node.internalFields.key); if (cache) { if (!cache.$nodeByKey) { cache.$nodeByKey = {}; this.$element().find(".".concat(NODE_CLASS)).each(function() { var $node = (0, _renderer2.default)(this); var key = $node.attr(DATA_ITEM_ID); cache.$nodeByKey[key] = $node }) } return cache.$nodeByKey[normalizedKey] || (0, _renderer2.default)() } return this.$element().find("[".concat(DATA_ITEM_ID, "='").concat(normalizedKey, "']")) }, _activeStateUnit: "." + ITEM_CLASS, _widgetClass: function() { return WIDGET_CLASS }, _getDefaultOptions: function() { return (0, _extend.extend)(this.callBase(), { animationEnabled: true, dataStructure: "tree", deferRendering: true, expandAllEnabled: false, hasItemsExpr: "hasItems", selectNodesRecursive: true, expandNodesRecursive: true, showCheckBoxesMode: "none", selectAllText: _message2.default.format("dxList-selectAll"), onItemSelectionChanged: null, onItemExpanded: null, onItemCollapsed: null, scrollDirection: "vertical", virtualModeEnabled: false, rootValue: 0, focusStateEnabled: false, selectionMode: "multiple", expandEvent: "dblclick", selectByClick: false, createChildren: null, onSelectAllValueChanged: null }) }, _initSelectedItems: _common2.default.noop, _syncSelectionOptions: _common2.default.asyncNoop, _fireSelectionChanged: function() { var selectionChangePromise = this._selectionChangePromise; (0, _deferred.when)(selectionChangePromise).done(function() { this._createActionByOption("onSelectionChanged", { excludeValidators: ["disabled", "readOnly"] })() }.bind(this)) }, _createSelectAllValueChangedAction: function() { this._selectAllValueChangedAction = this._createActionByOption("onSelectAllValueChanged", { excludeValidators: ["disabled", "readOnly"] }) }, _fireSelectAllValueChanged: function(value) { this._selectAllValueChangedAction({ value: value }) }, _checkBoxModeChange: function(value, previousValue) { if ("none" === previousValue || "none" === value) { this.repaint(); return } var selectAllExists = this._$selectAllItem && this._$selectAllItem.length; switch (value) { case "selectAll": !selectAllExists && this._renderSelectAllItem(); break; case "normal": if (selectAllExists) { this._$selectAllItem.remove(); delete this._$selectAllItem } } }, _removeSelection: function() { var that = this; (0, _iterator.each)(this._dataAdapter.getFullData(), function(_, node) { if (!that._hasChildren(node)) { return } that._dataAdapter.toggleSelection(node.internalFields.key, false, true) }) }, _optionChanged: function(args) { var name = args.name, value = args.value, previousValue = args.previousValue; switch (name) { case "selectAllText": if (this._$selectAllItem) { this._$selectAllItem.dxCheckBox("instance").option("text", value) } break; case "showCheckBoxesMode": this._checkBoxModeChange(value, previousValue); break; case "scrollDirection": this._scrollableContainer.option("direction", value); break; case "items": delete this._$selectAllItem; this.callBase(args); break; case "dataSource": this.callBase(args); this._initDataAdapter(); this._filter = {}; break; case "hasItemsExpr": this._initAccessors(); this.repaint(); break; case "expandEvent": this._initExpandEvent(); break; case "deferRendering": case "dataStructure": case "rootValue": case "createChildren": case "expandNodesRecursive": case "onItemSelectionChanged": case "onItemExpanded": case "onItemCollapsed": case "expandAllEnabled": case "animationEnabled": case "virtualModeEnabled": case "selectByClick": break; case "selectionMode": this._initDataAdapter(); this.callBase(args); break; case "onSelectAllValueChanged": this._createSelectAllValueChangedAction(); break; case "selectNodesRecursive": this._dataAdapter.setOption("recursiveSelection", args.value); this.repaint(); break; default: this.callBase(args) } }, _initDataSource: function() { if (this._useCustomChildrenLoader()) { this._loadChildrenByCustomLoader(null).done(function(newItems) { if (newItems && newItems.length) { this.option("items", newItems) } }.bind(this)) } else { this.callBase(); this._isVirtualMode() && this._initVirtualMode() } }, _initVirtualMode: function() { var filter = this._filter; if (!filter.custom) { filter.custom = this._dataSource.filter() } if (!filter.internal) { filter.internal = [this.option("parentIdExpr"), this.option("rootValue")] } }, _useCustomChildrenLoader: function() { return (0, _type.isFunction)(this.option("createChildren")) && this._isDataStructurePlain() }, _loadChildrenByCustomLoader: function(parentNode) { var invocationResult = this.option("createChildren").call(this, parentNode); if (Array.isArray(invocationResult)) { return (new _deferred.Deferred).resolve(invocationResult).promise() } if (invocationResult && (0, _type.isFunction)(invocationResult.then)) { return (0, _deferred.fromPromise)(invocationResult) } return (new _deferred.Deferred).resolve([]).promise() }, _combineFilter: function() { if (!this._filter.custom || !this._filter.custom.length) { return this._filter.internal } return [this._filter.custom, this._filter.internal] }, _dataSourceLoadErrorHandler: function() { this._renderEmptyMessage() }, _init: function() { this._filter = {}; this.callBase(); this._initStoreChangeHandlers() }, _dataSourceChangedHandler: function(newItems) { var items = this.option("items"); if (this._initialized && this._isVirtualMode() && items.length) { return } this.option("items", newItems) }, _removeTreeViewLoadIndicator: function() { if (!this._treeViewLoadIndicator) { return } this._treeViewLoadIndicator.remove(); this._treeViewLoadIndicator = null }, _createTreeViewLoadIndicator: function() { this._treeViewLoadIndicator = (0, _renderer2.default)("