ui.data_converter.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * DevExtreme (ui/hierarchical_collection/ui.data_converter.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 Class = require("../../core/class");
  11. var extend = require("../../core/utils/extend").extend;
  12. var errors = require("../../ui/widget/ui.errors");
  13. var each = require("../../core/utils/iterator").each;
  14. var typeUtils = require("../../core/utils/type");
  15. var DataConverter = Class.inherit({
  16. ctor: function() {
  17. this._dataStructure = [];
  18. this._itemsCount = 0;
  19. this._visibleItemsCount = 0
  20. },
  21. _indexByKey: {},
  22. _convertItemsToNodes: function(items, parentKey) {
  23. var that = this;
  24. each(items, function(_, item) {
  25. var parentId = typeUtils.isDefined(parentKey) ? parentKey : that._getParentId(item);
  26. var node = that._convertItemToNode(item, parentId);
  27. that._dataStructure.push(node);
  28. that._checkForDuplicateId(node.internalFields.key);
  29. that._indexByKey[node.internalFields.key] = that._dataStructure.length - 1;
  30. if (that._itemHasChildren(item)) {
  31. that._convertItemsToNodes(that._dataAccessors.getters.items(item), node.internalFields.key)
  32. }
  33. })
  34. },
  35. _checkForDuplicateId: function(key) {
  36. if (typeUtils.isDefined(this._indexByKey[key])) {
  37. throw errors.Error("E1040", key)
  38. }
  39. },
  40. _getParentId: function(item) {
  41. return "plain" === this._dataType ? this._dataAccessors.getters.parentKey(item) : void 0
  42. },
  43. _itemHasChildren: function(item) {
  44. if ("plain" === this._dataType) {
  45. return
  46. }
  47. var items = this._dataAccessors.getters.items(item);
  48. return items && items.length
  49. },
  50. _getUniqueKey: function(item) {
  51. var keyGetter = this._dataAccessors.getters.key;
  52. var itemKey = keyGetter(item);
  53. var isCorrectKey = keyGetter && (itemKey || 0 === itemKey) && typeUtils.isPrimitive(itemKey);
  54. return isCorrectKey ? itemKey : this.getItemsCount()
  55. },
  56. _convertItemToNode: function(item, parentKey) {
  57. this._itemsCount++;
  58. false !== item.visible && this._visibleItemsCount++;
  59. var that = this;
  60. var node = {
  61. internalFields: {
  62. disabled: that._dataAccessors.getters.disabled(item, {
  63. defaultValue: false
  64. }),
  65. expanded: that._dataAccessors.getters.expanded(item, {
  66. defaultValue: false
  67. }),
  68. selected: that._dataAccessors.getters.selected(item, {
  69. defaultValue: false
  70. }),
  71. key: that._getUniqueKey(item),
  72. parentKey: typeUtils.isDefined(parentKey) ? parentKey : that._rootValue,
  73. item: that._makeObjectFromPrimitive(item),
  74. childrenKeys: []
  75. }
  76. };
  77. extend(node, item);
  78. delete node.items;
  79. return node
  80. },
  81. setChildrenKeys: function() {
  82. var that = this;
  83. each(this._dataStructure, function(_, node) {
  84. if (node.internalFields.parentKey === that._rootValue) {
  85. return
  86. }
  87. var parent = that.getParentNode(node);
  88. parent && parent.internalFields.childrenKeys.push(node.internalFields.key)
  89. })
  90. },
  91. _makeObjectFromPrimitive: function(item) {
  92. if (typeUtils.isPrimitive(item)) {
  93. var key = item;
  94. item = {};
  95. this._dataAccessors.setters.key(item, key)
  96. }
  97. return item
  98. },
  99. _convertToPublicNode: function(node, parent) {
  100. if (!node) {
  101. return null
  102. }
  103. var publicNode = {
  104. text: this._dataAccessors.getters.display(node),
  105. key: node.internalFields.key,
  106. selected: node.internalFields.selected,
  107. expanded: node.internalFields.expanded,
  108. disabled: node.internalFields.disabled,
  109. parent: parent || null,
  110. itemData: node.internalFields.item,
  111. children: [],
  112. items: []
  113. };
  114. if (publicNode.parent) {
  115. publicNode.parent.children.push(publicNode);
  116. publicNode.parent.items.push(publicNode)
  117. }
  118. return publicNode
  119. },
  120. convertToPublicNodes: function(data, parent) {
  121. if (!data.length) {
  122. return []
  123. }
  124. var that = this;
  125. var publicNodes = [];
  126. each(data, function(_, node) {
  127. node = typeUtils.isPrimitive(node) ? that._getByKey(node) : node;
  128. var publicNode = that._convertToPublicNode(node, parent);
  129. publicNode.children = that.convertToPublicNodes(node.internalFields.childrenKeys, publicNode);
  130. publicNodes.push(publicNode);
  131. node.internalFields.publicNode = publicNode
  132. });
  133. return publicNodes
  134. },
  135. setDataAccessors: function(accessors) {
  136. this._dataAccessors = accessors
  137. },
  138. _getByKey: function(key) {
  139. return this._dataStructure[this.getIndexByKey(key)] || null
  140. },
  141. getParentNode: function(node) {
  142. return this._getByKey(node.internalFields.parentKey)
  143. },
  144. getByKey: function getByKey(data, key) {
  145. var result = null;
  146. var that = this;
  147. var getByKey = function getByKey(data, key) {
  148. each(data, function(_, element) {
  149. var currentElementKey = element.internalFields && element.internalFields.key || that._dataAccessors.getters.key(element);
  150. var items = that._dataAccessors.getters.items(element);
  151. if (currentElementKey.toString() === key.toString()) {
  152. result = element;
  153. return false
  154. }
  155. if (items) {
  156. getByKey(items, key)
  157. }
  158. });
  159. return result
  160. };
  161. return getByKey(data, key)
  162. },
  163. getItemsCount: function() {
  164. return this._itemsCount
  165. },
  166. getVisibleItemsCount: function() {
  167. return this._visibleItemsCount
  168. },
  169. updateIndexByKey: function() {
  170. var that = this;
  171. this._indexByKey = {};
  172. each(this._dataStructure, function(index, node) {
  173. that._checkForDuplicateId(node.internalFields.key);
  174. that._indexByKey[node.internalFields.key] = index
  175. })
  176. },
  177. updateChildrenKeys: function() {
  178. this._indexByKey = {};
  179. this.removeChildrenKeys();
  180. this.updateIndexByKey();
  181. this.setChildrenKeys()
  182. },
  183. removeChildrenKeys: function() {
  184. this._indexByKey = {};
  185. each(this._dataStructure, function(index, node) {
  186. node.internalFields.childrenKeys = []
  187. })
  188. },
  189. getIndexByKey: function(key) {
  190. return this._indexByKey[key]
  191. },
  192. createPlainStructure: function(items, rootValue, dataType) {
  193. this._itemsCount = 0;
  194. this._visibleItemsCount = 0;
  195. this._rootValue = rootValue;
  196. this._dataType = dataType;
  197. this._indexByKey = {};
  198. this._convertItemsToNodes(items);
  199. this.setChildrenKeys();
  200. return this._dataStructure
  201. }
  202. });
  203. module.exports = DataConverter;