| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- /**
- * DevExtreme (ui/hierarchical_collection/ui.data_adapter.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 Class = require("../../core/class");
- var commonUtils = require("../../core/utils/common");
- var iteratorUtils = require("../../core/utils/iterator");
- var each = require("../../core/utils/iterator").each;
- var typeUtils = require("../../core/utils/type");
- var extend = require("../../core/utils/extend").extend;
- var errors = require("../../ui/widget/ui.errors");
- var getOperationBySearchMode = require("../../ui/widget/ui.search_box_mixin").getOperationBySearchMode;
- var inArray = require("../../core/utils/array").inArray;
- var query = require("../../data/query");
- var storeHelper = require("../../data/store_helper");
- var HierarchicalDataConverter = require("./ui.data_converter");
- var EXPANDED = "expanded";
- var SELECTED = "selected";
- var DISABLED = "disabled";
- var DataAdapter = Class.inherit({
- ctor: function(options) {
- this.options = {};
- extend(this.options, this._defaultOptions(), options);
- this.options.dataConverter.setDataAccessors(this.options.dataAccessors);
- this._selectedNodesKeys = [];
- this._expandedNodesKeys = [];
- this._dataStructure = [];
- this._createInternalDataStructure();
- this.getTreeNodes()
- },
- setOption: function(name, value) {
- this.options[name] = value;
- if ("recursiveSelection" === name) {
- this._updateSelection()
- }
- },
- _defaultOptions: function() {
- return {
- dataAccessors: void 0,
- items: [],
- multipleSelection: true,
- recursiveSelection: false,
- recursiveExpansion: false,
- rootValue: 0,
- searchValue: "",
- dataType: "tree",
- searchMode: "contains",
- dataConverter: new HierarchicalDataConverter,
- onNodeChanged: commonUtils.noop,
- sort: null
- }
- },
- _createInternalDataStructure: function() {
- this._initialDataStructure = this.options.dataConverter.createPlainStructure(this.options.items, this.options.rootValue, this.options.dataType);
- this._dataStructure = this.options.searchValue.length ? this.search(this.options.searchValue) : this._initialDataStructure;
- this.options.dataConverter._dataStructure = this._dataStructure;
- this._updateSelection();
- this._updateExpansion()
- },
- _updateSelection: function() {
- if (this.options.recursiveSelection) {
- this._setChildrenSelection();
- this._setParentSelection()
- }
- this._selectedNodesKeys = this._updateNodesKeysArray(SELECTED)
- },
- _updateExpansion: function(key) {
- if (this.options.recursiveExpansion) {
- key ? this._updateOneBranch(key) : this._setParentExpansion()
- }
- this._expandedNodesKeys = this._updateNodesKeysArray(EXPANDED)
- },
- _updateNodesKeysArray: function(property) {
- var that = this;
- var array = [];
- each(that._getDataBySelectionMode(), function(_, node) {
- if (!that._isNodeVisible(node)) {
- return
- }
- if (node.internalFields[property]) {
- if (property === EXPANDED || that.options.multipleSelection) {
- array.push(node.internalFields.key)
- } else {
- array.length && that.toggleSelection(array[0], false, true);
- array = [node.internalFields.key]
- }
- }
- });
- return array
- },
- _getDataBySelectionMode: function() {
- return this.options.multipleSelection ? this.getData() : this.getFullData()
- },
- _isNodeVisible: function(node) {
- return false !== node.internalFields.item.visible
- },
- _getByKey: function(data, key) {
- return data === this._dataStructure ? this.options.dataConverter._getByKey(key) : this.options.dataConverter.getByKey(data, key)
- },
- _setChildrenSelection: function() {
- var that = this;
- each(this._dataStructure, function(_, node) {
- if (!node.internalFields.childrenKeys.length) {
- return
- }
- var isSelected = node.internalFields.selected;
- true === isSelected && that._toggleChildrenSelection(node, isSelected)
- })
- },
- _setParentSelection: function() {
- var that = this;
- each(this._dataStructure, function(_, node) {
- var parent = that.options.dataConverter.getParentNode(node);
- if (parent && node.internalFields.parentKey !== that.options.rootValue) {
- that._iterateParents(node, function(parent) {
- var newParentState = that._calculateSelectedState(parent);
- that._setFieldState(parent, SELECTED, newParentState)
- })
- }
- })
- },
- _setParentExpansion: function() {
- var that = this;
- each(this._dataStructure, function(_, node) {
- if (!node.internalFields.expanded) {
- return
- }
- that._updateOneBranch(node.internalFields.key)
- })
- },
- _updateOneBranch: function(key) {
- var that = this;
- var node = this.getNodeByKey(key);
- that._iterateParents(node, function(parent) {
- that._setFieldState(parent, EXPANDED, true)
- })
- },
- _iterateChildren: function(node, recursive, callback) {
- var that = this;
- each(node.internalFields.childrenKeys, function(_, key) {
- var child = that.getNodeByKey(key);
- typeUtils.isFunction(callback) && callback(child);
- if (child.internalFields.childrenKeys.length && recursive) {
- that._iterateChildren(child, recursive, callback)
- }
- })
- },
- _iterateParents: function(node, callback) {
- if (node.internalFields.parentKey === this.options.rootValue) {
- return
- }
- var parent = this.options.dataConverter.getParentNode(node);
- if (parent) {
- typeUtils.isFunction(callback) && callback(parent);
- if (parent.internalFields.parentKey !== this.options.rootValue) {
- this._iterateParents(parent, callback)
- }
- }
- },
- _calculateSelectedState: function(node) {
- var itemsCount = node.internalFields.childrenKeys.length;
- var selectedItemsCount = 0;
- var invisibleItemsCount = 0;
- var result = false;
- for (var i = 0; i <= itemsCount - 1; i++) {
- var childNode = this.getNodeByKey(node.internalFields.childrenKeys[i]);
- var isChildInvisible = false === childNode.internalFields.item.visible;
- var childState = childNode.internalFields.selected;
- if (isChildInvisible) {
- invisibleItemsCount++;
- continue
- }
- if (childState) {
- selectedItemsCount++
- } else {
- if (void 0 === childState) {
- selectedItemsCount += .5
- }
- }
- }
- if (selectedItemsCount) {
- result = selectedItemsCount === itemsCount - invisibleItemsCount ? true : void 0
- }
- return result
- },
- _toggleChildrenSelection: function(node, state) {
- var that = this;
- this._iterateChildren(node, true, function(child) {
- if (that._isNodeVisible(child)) {
- that._setFieldState(child, SELECTED, state)
- }
- })
- },
- _setFieldState: function(node, field, state) {
- if (node.internalFields[field] === state) {
- return
- }
- node.internalFields[field] = state;
- if (node.internalFields.publicNode) {
- node.internalFields.publicNode[field] = state
- }
- this.options.dataAccessors.setters[field](node.internalFields.item, state);
- this.options.onNodeChanged(node)
- },
- _markChildren: function(keys) {
- var that = this;
- each(keys, function(_, key) {
- var index = that.getIndexByKey(key);
- var node = that.getNodeByKey(key);
- that._dataStructure[index] = 0;
- node.internalFields.childrenKeys.length && that._markChildren(node.internalFields.childrenKeys)
- })
- },
- _removeNode: function(key) {
- var node = this.getNodeByKey(key);
- this._dataStructure[this.getIndexByKey(key)] = 0;
- this._markChildren(node.internalFields.childrenKeys);
- var that = this;
- var counter = 0;
- var items = extend([], this._dataStructure);
- each(items, function(index, item) {
- if (!item) {
- that._dataStructure.splice(index - counter, 1);
- counter++
- }
- })
- },
- _addNode: function(item) {
- var dataConverter = this.options.dataConverter;
- var node = dataConverter._convertItemToNode(item, this.options.dataAccessors.getters.parentKey(item));
- this._dataStructure = this._dataStructure.concat(node);
- this._initialDataStructure = this._initialDataStructure.concat(node);
- dataConverter._dataStructure = dataConverter._dataStructure.concat(node)
- },
- _updateFields: function() {
- this.options.dataConverter.updateChildrenKeys();
- this._updateSelection();
- this._updateExpansion()
- },
- getSelectedNodesKeys: function() {
- return this._selectedNodesKeys
- },
- getExpandedNodesKeys: function() {
- return this._expandedNodesKeys
- },
- getData: function() {
- return this._dataStructure
- },
- getFullData: function() {
- return this._initialDataStructure
- },
- getNodeByItem: function(item) {
- var result = null;
- each(this._dataStructure, function(_, node) {
- if (node.internalFields.item === item) {
- result = node;
- return false
- }
- });
- return result
- },
- getNodesByItems: function(items) {
- var that = this;
- var nodes = [];
- each(items, function(_, item) {
- var node = that.getNodeByItem(item);
- node && nodes.push(node)
- });
- return nodes
- },
- getNodeByKey: function(key, data) {
- return this._getByKey(data || this._getDataBySelectionMode(), key)
- },
- getTreeNodes: function() {
- return this.options.dataConverter.convertToPublicNodes(this.getRootNodes())
- },
- getItemsCount: function() {
- return this.options.dataConverter.getItemsCount()
- },
- getVisibleItemsCount: function() {
- return this.options.dataConverter.getVisibleItemsCount()
- },
- getPublicNode: function(node) {
- return node.internalFields.publicNode
- },
- getRootNodes: function() {
- return this.getChildrenNodes(this.options.rootValue)
- },
- getChildrenNodes: function(parentKey) {
- return query(this._dataStructure).filter(["internalFields.parentKey", parentKey]).toArray()
- },
- getIndexByKey: function(key) {
- return this.options.dataConverter.getIndexByKey(key)
- },
- addItem: function(item) {
- this._addNode(item);
- this._updateFields()
- },
- removeItem: function(key) {
- this._removeNode(key);
- this._updateFields()
- },
- toggleSelection: function(key, state, selectRecursive) {
- var isSingleModeUnselect = this._isSingleModeUnselect(state);
- var node = this._getByKey(selectRecursive || isSingleModeUnselect ? this._initialDataStructure : this._dataStructure, key);
- this._setFieldState(node, SELECTED, state);
- if (this.options.recursiveSelection && !selectRecursive) {
- state ? this._setChildrenSelection() : this._toggleChildrenSelection(node, state);
- this._setParentSelection()
- }
- this._selectedNodesKeys = this._updateNodesKeysArray(SELECTED)
- },
- _isSingleModeUnselect: function(selectionState) {
- return !this.options.multipleSelection && !selectionState
- },
- toggleNodeDisabledState: function(key, state) {
- var node = this.getNodeByKey(key);
- this._setFieldState(node, DISABLED, state)
- },
- toggleSelectAll: function(state) {
- if (!typeUtils.isDefined(state)) {
- return
- }
- var that = this;
- var lastSelectedKey = that._selectedNodesKeys[that._selectedNodesKeys.length - 1];
- var dataStructure = that._isSingleModeUnselect(state) ? this._initialDataStructure : this._dataStructure;
- each(dataStructure, function(index, node) {
- if (!that._isNodeVisible(node)) {
- return
- }
- that._setFieldState(node, SELECTED, state)
- });
- that._selectedNodesKeys = that._updateNodesKeysArray(SELECTED);
- if (!state && that.options.selectionRequired) {
- that.toggleSelection(lastSelectedKey, true)
- }
- },
- isAllSelected: function() {
- if (this.getSelectedNodesKeys().length) {
- return this.getSelectedNodesKeys().length === this.getVisibleItemsCount() ? true : void 0
- } else {
- return false
- }
- },
- toggleExpansion: function(key, state) {
- var node = this.getNodeByKey(key);
- this._setFieldState(node, EXPANDED, state);
- if (state) {
- this._updateExpansion(key)
- }
- this._expandedNodesKeys = this._updateNodesKeysArray(EXPANDED)
- },
- isFiltered: function(item) {
- return !this.options.searchValue.length || !!this._filterDataStructure(this.options.searchValue, [item]).length
- },
- _createCriteria: function(selector, value, operation) {
- var searchFilter = [];
- if (!Array.isArray(selector)) {
- return [selector, operation, value]
- }
- iteratorUtils.each(selector, function(i, item) {
- searchFilter.push([item, operation, value], "or")
- });
- searchFilter.pop();
- return searchFilter
- },
- _filterDataStructure: function(filterValue, dataStructure) {
- var selector = this.options.searchExpr || this.options.dataAccessors.getters.display;
- var operation = getOperationBySearchMode(this.options.searchMode);
- var criteria = this._createCriteria(selector, filterValue, operation);
- dataStructure = dataStructure || this._initialDataStructure;
- return query(dataStructure).filter(criteria).toArray()
- },
- search: function(searchValue) {
- var that = this;
- var matches = this._filterDataStructure(searchValue);
- var dataConverter = this.options.dataConverter;
- function lookForParents(matches, index) {
- var length = matches.length;
- while (index < length) {
- var node = matches[index];
- if (node.internalFields.parentKey === that.options.rootValue) {
- index++;
- continue
- }
- var parent = dataConverter.getParentNode(node);
- if (!parent) {
- errors.log("W1007", node.internalFields.parentKey, node.internalFields.key);
- index++;
- continue
- }
- if (!parent.internalFields.expanded) {
- that._setFieldState(parent, EXPANDED, true)
- }
- if (inArray(parent, matches) > -1) {
- index++;
- continue
- }
- matches.splice(index, 0, parent);
- lookForParents(matches, index)
- }
- }
- lookForParents(matches, 0);
- if (this.options.sort) {
- matches = storeHelper.queryByOptions(query(matches), {
- sort: this.options.sort
- }).toArray()
- }
- dataConverter._indexByKey = {};
- each(matches, function(index, node) {
- node.internalFields.childrenKeys = [];
- dataConverter._indexByKey[node.internalFields.key] = index
- });
- dataConverter._dataStructure = matches;
- dataConverter.setChildrenKeys();
- return dataConverter._dataStructure
- }
- });
- module.exports = DataAdapter;
|