devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
141 lines (137 loc) • 4.3 kB
JavaScript
/**
* DevExtreme (esm/viz/tree_map/selection.js)
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import TreeMapBase from "./tree_map.base";
import Node from "./node";
import {
expand
} from "../core/helpers";
import {
buildRectAppearance
} from "./common";
const proto = TreeMapBase.prototype;
const nodeProto = Node.prototype;
import {
normalizeEnum as _normalizeEnum
} from "../core/utils";
const MODE_NONE = 0;
const MODE_SINGLE = 1;
const MODE_MULTIPLE = 2;
const STATE_CODE = 2;
import "./api";
import "./states";
proto._eventsMap.onSelectionChanged = {
name: "selectionChanged"
};
expand(proto._handlers, "calculateAdditionalStates", (function(states, options) {
states[2] = options.selectionStyle ? buildRectAppearance(options.selectionStyle) : {}
}));
nodeProto.statesMap[2] = nodeProto.statesMap[3] = 2;
nodeProto.additionalStates.push(2);
expand(proto, "_onNodesCreated", (function() {
this._selectionList.length = 0
}));
expand(proto, "_extendProxyType", (function(proto) {
const that = this;
proto.select = function(state) {
that._selectNode(this._id, !!state)
};
proto.isSelected = function() {
return that._selectionList.includes(this._id)
};
that._selectionList = []
}));
TreeMapBase.addChange({
code: "SELECTION_MODE",
handler: function() {
const that = this;
const option = _normalizeEnum(that._getOption("selectionMode", true));
const selectionList = that._selectionList;
let tmp;
const mode = "none" === option ? 0 : "multiple" === option ? 2 : 1;
if (1 === mode && selectionList.length > 1) {
tmp = selectionList.pop();
that.clearSelection();
selectionList.push(tmp)
} else if (0 === mode) {
that.clearSelection()
}
that._selectionMode = mode
},
isThemeDependent: true,
isOptionChange: true,
option: "selectionMode"
});
expand(proto, "_applyTilesAppearance", (function() {
if (this._selectionList.length) {
bringSelectedTilesToForeground(this._nodes, this._selectionList)
}
}));
const tileToFront = [leafToFront, groupToFront];
function bringSelectedTilesToForeground(nodes, selectionList) {
let i;
const ii = selectionList.length;
let node;
for (i = 0; i < ii; ++i) {
node = nodes[selectionList[i]];
tileToFront[Number(node.isNode())](node.tile)
}
}
function leafToFront(content) {
content.toForeground()
}
function groupToFront(content) {
content.outer.toForeground();
content.inner.toForeground()
}
proto._applySelectionState = function(index, state) {
const node = this._nodes[index];
node.setState(2, state);
this._eventTrigger("selectionChanged", {
node: node.proxy
})
};
proto._selectNode = function(index, state) {
const that = this;
let selectionList;
let k;
let tmp;
if (0 !== that._selectionMode) {
that._context.suspend();
selectionList = that._selectionList;
k = selectionList.indexOf(index);
if (state && -1 === k) {
if (1 === that._selectionMode) {
if (selectionList.length) {
tmp = selectionList.pop();
that._applySelectionState(tmp, false)
}
}
selectionList.push(index);
that._applySelectionState(index, true)
} else if (!state && k >= 0) {
selectionList.splice(k, 1);
that._applySelectionState(index, false)
}
that._context.resume()
}
};
proto.clearSelection = function() {
const that = this;
const selectionList = that._selectionList;
let i;
const ii = selectionList.length;
if (0 !== that._selectionMode) {
that._context.suspend();
for (i = 0; i < ii; ++i) {
that._applySelectionState(selectionList[i], false)
}
selectionList.length = 0;
that._context.resume()
}
};