selection.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * DevExtreme (viz/tree_map/selection.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var proto = require("./tree_map.base").prototype;
  11. var nodeProto = require("./node").prototype;
  12. var expand = require("../core/helpers").expand;
  13. var common = require("./common");
  14. var _buildRectAppearance = common.buildRectAppearance;
  15. var _normalizeEnum = require("../core/utils").normalizeEnum;
  16. var _inArray = require("../../core/utils/array").inArray;
  17. var MODE_NONE = 0;
  18. var MODE_SINGLE = 1;
  19. var MODE_MULTIPLE = 2;
  20. var STATE_CODE = 2;
  21. require("./api");
  22. require("./states");
  23. proto._eventsMap.onSelectionChanged = {
  24. name: "selectionChanged"
  25. };
  26. expand(proto._handlers, "calculateAdditionalStates", function(states, options) {
  27. states[2] = options.selectionStyle ? _buildRectAppearance(options.selectionStyle) : {}
  28. });
  29. nodeProto.statesMap[2] = nodeProto.statesMap[3] = STATE_CODE;
  30. nodeProto.additionalStates.push(2);
  31. expand(proto, "_onNodesCreated", function() {
  32. this._selectionList.length = 0
  33. });
  34. expand(proto, "_extendProxyType", function(proto) {
  35. var that = this;
  36. proto.select = function(state) {
  37. that._selectNode(this._id, !!state)
  38. };
  39. proto.isSelected = function() {
  40. return _inArray(this._id, that._selectionList) >= 0
  41. };
  42. that._selectionList = []
  43. });
  44. require("./tree_map.base").addChange({
  45. code: "SELECTION_MODE",
  46. handler: function() {
  47. var that = this;
  48. var option = _normalizeEnum(that._getOption("selectionMode", true));
  49. var selectionList = that._selectionList;
  50. var mode = "none" === option ? MODE_NONE : "multiple" === option ? MODE_MULTIPLE : MODE_SINGLE;
  51. if (mode === MODE_SINGLE && selectionList.length > 1) {
  52. var tmp = selectionList.pop();
  53. that.clearSelection();
  54. selectionList.push(tmp)
  55. } else {
  56. if (mode === MODE_NONE) {
  57. that.clearSelection()
  58. }
  59. }
  60. that._selectionMode = mode
  61. },
  62. isThemeDependent: true,
  63. isOptionChange: true,
  64. option: "selectionMode"
  65. });
  66. expand(proto, "_applyTilesAppearance", function() {
  67. if (this._selectionList.length) {
  68. bringSelectedTilesToForeground(this._nodes, this._selectionList)
  69. }
  70. });
  71. function bringSelectedTilesToForeground(nodes, selectionList) {
  72. var i;
  73. var ii = selectionList.length;
  74. for (i = 0; i < ii; ++i) {
  75. var node = nodes[selectionList[i]];
  76. tileToFront[Number(node.isNode())](node.tile)
  77. }
  78. }
  79. var tileToFront = [leafToFront, groupToFront];
  80. function leafToFront(content) {
  81. content.toForeground()
  82. }
  83. function groupToFront(content) {
  84. content.outer.toForeground();
  85. content.inner.toForeground()
  86. }
  87. proto._applySelectionState = function(index, state) {
  88. var node = this._nodes[index];
  89. node.setState(STATE_CODE, state);
  90. this._eventTrigger("selectionChanged", {
  91. node: node.proxy
  92. })
  93. };
  94. proto._selectNode = function(index, state) {
  95. var that = this;
  96. if (that._selectionMode !== MODE_NONE) {
  97. that._context.suspend();
  98. var selectionList = that._selectionList;
  99. var k = _inArray(index, selectionList);
  100. if (state && k === -1) {
  101. if (that._selectionMode === MODE_SINGLE) {
  102. if (selectionList.length) {
  103. var tmp = selectionList.pop();
  104. that._applySelectionState(tmp, false)
  105. }
  106. }
  107. selectionList.push(index);
  108. that._applySelectionState(index, true)
  109. } else {
  110. if (!state && k >= 0) {
  111. selectionList.splice(k, 1);
  112. that._applySelectionState(index, false)
  113. }
  114. }
  115. that._context.resume()
  116. }
  117. };
  118. proto.clearSelection = function() {
  119. var that = this;
  120. var selectionList = that._selectionList;
  121. var i;
  122. var ii = selectionList.length;
  123. if (that._selectionMode !== MODE_NONE) {
  124. that._context.suspend();
  125. for (i = 0; i < ii; ++i) {
  126. that._applySelectionState(selectionList[i], false)
  127. }
  128. selectionList.length = 0;
  129. that._context.resume()
  130. }
  131. };