ui.list.edit.strategy.grouped.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * DevExtreme (ui/list/ui.list.edit.strategy.grouped.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 $ = require("../../core/renderer");
  11. var isNumeric = require("../../core/utils/type").isNumeric;
  12. var each = require("../../core/utils/iterator").each;
  13. var queryByOptions = require("../../data/store_helper").queryByOptions;
  14. var query = require("../../data/query");
  15. var EditStrategy = require("../collection/ui.collection_widget.edit.strategy.plain");
  16. var LIST_ITEM_CLASS = "dx-list-item";
  17. var LIST_GROUP_CLASS = "dx-list-group";
  18. var SELECTION_SHIFT = 20;
  19. var SELECTION_MASK = 2303;
  20. var combineIndex = function(indices) {
  21. return (indices.group << SELECTION_SHIFT) + indices.item
  22. };
  23. var splitIndex = function(combinedIndex) {
  24. return {
  25. group: combinedIndex >> SELECTION_SHIFT,
  26. item: combinedIndex & SELECTION_MASK
  27. }
  28. };
  29. var GroupedEditStrategy = EditStrategy.inherit({
  30. _groupElements: function() {
  31. return this._collectionWidget._itemContainer().find("." + LIST_GROUP_CLASS)
  32. },
  33. _groupItemElements: function($group) {
  34. return $group.find("." + LIST_ITEM_CLASS)
  35. },
  36. getIndexByItemData: function(itemData) {
  37. var groups = this._collectionWidget.option("items");
  38. var index = false;
  39. if (!itemData) {
  40. return false
  41. }
  42. if (itemData.items && itemData.items.length) {
  43. itemData = itemData.items[0]
  44. }
  45. each(groups, function(groupIndex, group) {
  46. if (!group.items) {
  47. return false
  48. }
  49. each(group.items, function(itemIndex, item) {
  50. if (item !== itemData) {
  51. return true
  52. }
  53. index = {
  54. group: groupIndex,
  55. item: itemIndex
  56. };
  57. return false
  58. });
  59. if (index) {
  60. return false
  61. }
  62. });
  63. return index
  64. },
  65. getItemDataByIndex: function(index) {
  66. var items = this._collectionWidget.option("items");
  67. if (isNumeric(index)) {
  68. return this.itemsGetter()[index]
  69. }
  70. return index && items[index.group] && items[index.group].items[index.item] || null
  71. },
  72. itemsGetter: function() {
  73. var resultItems = [];
  74. var items = this._collectionWidget.option("items");
  75. for (var i = 0; i < items.length; i++) {
  76. if (items[i] && items[i].items) {
  77. resultItems = resultItems.concat(items[i].items)
  78. } else {
  79. resultItems.push(items[i])
  80. }
  81. }
  82. return resultItems
  83. },
  84. deleteItemAtIndex: function(index) {
  85. var indices = splitIndex(index);
  86. var itemGroup = this._collectionWidget.option("items")[indices.group].items;
  87. itemGroup.splice(indices.item, 1)
  88. },
  89. getKeysByItems: function(items) {
  90. var plainItems = [];
  91. for (var i = 0; i < items.length; i++) {
  92. if (items[i] && items[i].items) {
  93. plainItems = plainItems.concat(items[i].items)
  94. } else {
  95. plainItems.push(items[i])
  96. }
  97. }
  98. var result = [];
  99. for (i = 0; i < plainItems.length; i++) {
  100. result.push(this._collectionWidget.keyOf(plainItems[i]))
  101. }
  102. return result
  103. },
  104. getIndexByKey: function(key, items) {
  105. var groups = items || this._collectionWidget.option("items");
  106. var index = -1;
  107. var that = this;
  108. each(groups, function(groupIndex, group) {
  109. if (!group.items) {
  110. return
  111. }
  112. var keys = that.getKeysByItems(group.items);
  113. each(keys, function(keyIndex, itemKey) {
  114. if (that._equalKeys(itemKey, key)) {
  115. index = {
  116. group: groupIndex,
  117. item: keyIndex
  118. };
  119. return false
  120. }
  121. });
  122. if (index !== -1) {
  123. return false
  124. }
  125. });
  126. return index
  127. },
  128. _getGroups: function(items) {
  129. var dataSource = this._collectionWidget.getDataSource();
  130. var group = dataSource && dataSource.group();
  131. if (group) {
  132. return queryByOptions(query(items), {
  133. group: group
  134. }).toArray()
  135. }
  136. return this._collectionWidget.option("items")
  137. },
  138. getItemsByKeys: function(keys, items) {
  139. var result = [];
  140. each(keys, function(_, key) {
  141. var getItemMeta = function(groups) {
  142. var index = this.getIndexByKey(key, groups);
  143. var group = index && groups[index.group];
  144. if (!group) {
  145. return
  146. }
  147. return {
  148. groupKey: group.key,
  149. item: group.items[index.item]
  150. }
  151. }.bind(this);
  152. var itemMeta = getItemMeta(this._getGroups(items));
  153. if (!itemMeta) {
  154. return
  155. }
  156. var groupKey = itemMeta.groupKey;
  157. var item = itemMeta.item;
  158. var selectedGroup;
  159. each(result, function(_, item) {
  160. if (item.key === groupKey) {
  161. selectedGroup = item;
  162. return false
  163. }
  164. });
  165. if (!selectedGroup) {
  166. selectedGroup = {
  167. key: groupKey,
  168. items: []
  169. };
  170. result.push(selectedGroup)
  171. }
  172. selectedGroup.items.push(item)
  173. }.bind(this));
  174. return result
  175. },
  176. moveItemAtIndexToIndex: function(movingIndex, destinationIndex) {
  177. var items = this._collectionWidget.option("items");
  178. var movingIndices = splitIndex(movingIndex);
  179. var destinationIndices = splitIndex(destinationIndex);
  180. var movingItemGroup = items[movingIndices.group].items;
  181. var destinationItemGroup = items[destinationIndices.group].items;
  182. var movedItemData = movingItemGroup[movingIndices.item];
  183. movingItemGroup.splice(movingIndices.item, 1);
  184. destinationItemGroup.splice(destinationIndices.item, 0, movedItemData)
  185. },
  186. _isItemIndex: function(index) {
  187. return index && isNumeric(index.group) && isNumeric(index.item)
  188. },
  189. _getNormalizedItemIndex: function(itemElement) {
  190. var $item = $(itemElement);
  191. var $group = $item.closest("." + LIST_GROUP_CLASS);
  192. if (!$group.length) {
  193. return -1
  194. }
  195. return combineIndex({
  196. group: this._groupElements().index($group),
  197. item: this._groupItemElements($group).index($item)
  198. })
  199. },
  200. _normalizeItemIndex: function(index) {
  201. return combineIndex(index)
  202. },
  203. _denormalizeItemIndex: function(index) {
  204. return splitIndex(index)
  205. },
  206. _getItemByNormalizedIndex: function(index) {
  207. var indices = splitIndex(index);
  208. var $group = this._groupElements().eq(indices.group);
  209. return this._groupItemElements($group).eq(indices.item)
  210. },
  211. _itemsFromSameParent: function(firstIndex, secondIndex) {
  212. return splitIndex(firstIndex).group === splitIndex(secondIndex).group
  213. }
  214. });
  215. module.exports = GroupedEditStrategy;