ui.list.edit.provider.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * DevExtreme (ui/list/ui.list.edit.provider.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 noop = require("../../core/utils/common").noop;
  12. var Class = require("../../core/class");
  13. var extend = require("../../core/utils/extend").extend;
  14. var each = require("../../core/utils/iterator").each;
  15. var errors = require("../widget/ui.errors");
  16. var decoratorRegistry = require("./ui.list.edit.decorator_registry");
  17. require("./ui.list.edit.decorator.static");
  18. require("./ui.list.edit.decorator.switchable.button");
  19. require("./ui.list.edit.decorator.switchable.slide");
  20. require("./ui.list.edit.decorator.swipe");
  21. require("./ui.list.edit.decorator.context");
  22. require("./ui.list.edit.decorator.selection");
  23. require("./ui.list.edit.decorator.reorder");
  24. var editOptionsRegistry = [];
  25. var registerOption = function(enabledFunc, decoratorTypeFunc, decoratorSubTypeFunc) {
  26. editOptionsRegistry.push({
  27. enabled: enabledFunc,
  28. decoratorType: decoratorTypeFunc,
  29. decoratorSubType: decoratorSubTypeFunc
  30. })
  31. };
  32. registerOption(function() {
  33. return this.option("menuItems").length
  34. }, function() {
  35. return "menu"
  36. }, function() {
  37. return this.option("menuMode")
  38. });
  39. registerOption(function() {
  40. return !this.option("menuItems").length && this.option("allowItemDeleting")
  41. }, function() {
  42. var mode = this.option("itemDeleteMode");
  43. return "toggle" === mode || "slideButton" === mode || "swipe" === mode || "static" === mode ? "delete" : "menu"
  44. }, function() {
  45. var mode = this.option("itemDeleteMode");
  46. if ("slideItem" === mode) {
  47. mode = "slide"
  48. }
  49. if ("hold" === mode) {
  50. mode = "context"
  51. }
  52. return mode
  53. });
  54. registerOption(function() {
  55. return "none" !== this.option("selectionMode") && this.option("showSelectionControls")
  56. }, function() {
  57. return "selection"
  58. }, function() {
  59. return "default"
  60. });
  61. registerOption(function() {
  62. return this.option("allowItemReordering")
  63. }, function() {
  64. return "reorder"
  65. }, function() {
  66. return "default"
  67. });
  68. var LIST_ITEM_BEFORE_BAG_CLASS = "dx-list-item-before-bag";
  69. var LIST_ITEM_AFTER_BAG_CLASS = "dx-list-item-after-bag";
  70. var DECORATOR_BEFORE_BAG_CREATE_METHOD = "beforeBag";
  71. var DECORATOR_AFTER_BAG_CREATE_METHOD = "afterBag";
  72. var DECORATOR_MODIFY_ELEMENT_METHOD = "modifyElement";
  73. var DECORATOR_AFTER_RENDER_METHOD = "afterRender";
  74. var DECORATOR_GET_EXCLUDED_SELECTORS_METHOD = "getExcludedSelectors";
  75. var EditProvider = Class.inherit({
  76. ctor: function(list) {
  77. this._list = list;
  78. this._fetchRequiredDecorators()
  79. },
  80. dispose: function() {
  81. if (this._decorators && this._decorators.length) {
  82. each(this._decorators, function(_, decorator) {
  83. decorator.dispose()
  84. })
  85. }
  86. },
  87. _fetchRequiredDecorators: function() {
  88. this._decorators = [];
  89. each(editOptionsRegistry, function(_, option) {
  90. var optionEnabled = option.enabled.call(this._list);
  91. if (optionEnabled) {
  92. var decoratorType = option.decoratorType.call(this._list);
  93. var decoratorSubType = option.decoratorSubType.call(this._list);
  94. var decorator = this._createDecorator(decoratorType, decoratorSubType);
  95. this._decorators.push(decorator)
  96. }
  97. }.bind(this))
  98. },
  99. _createDecorator: function(type, subType) {
  100. var decoratorClass = this._findDecorator(type, subType);
  101. return new decoratorClass(this._list)
  102. },
  103. _findDecorator: function(type, subType) {
  104. var foundDecorator = decoratorRegistry.registry[type][subType];
  105. if (!foundDecorator) {
  106. throw errors.Error("E1012", type, subType)
  107. }
  108. return foundDecorator
  109. },
  110. modifyItemElement: function(args) {
  111. var $itemElement = $(args.itemElement);
  112. var config = {
  113. $itemElement: $itemElement
  114. };
  115. this._prependBeforeBags($itemElement, config);
  116. this._appendAfterBags($itemElement, config);
  117. this._applyDecorators(DECORATOR_MODIFY_ELEMENT_METHOD, config)
  118. },
  119. afterItemsRendered: function() {
  120. this._applyDecorators(DECORATOR_AFTER_RENDER_METHOD)
  121. },
  122. _prependBeforeBags: function($itemElement, config) {
  123. var $beforeBags = this._collectDecoratorsMarkup(DECORATOR_BEFORE_BAG_CREATE_METHOD, config, LIST_ITEM_BEFORE_BAG_CLASS);
  124. $itemElement.prepend($beforeBags)
  125. },
  126. _appendAfterBags: function($itemElement, config) {
  127. var $afterBags = this._collectDecoratorsMarkup(DECORATOR_AFTER_BAG_CREATE_METHOD, config, LIST_ITEM_AFTER_BAG_CLASS);
  128. $itemElement.append($afterBags)
  129. },
  130. _collectDecoratorsMarkup: function(method, config, containerClass) {
  131. var $collector = $("<div>");
  132. each(this._decorators, function() {
  133. var $container = $("<div>").addClass(containerClass);
  134. this[method](extend({
  135. $container: $container
  136. }, config));
  137. if ($container.children().length) {
  138. $collector.append($container)
  139. }
  140. });
  141. return $collector.children()
  142. },
  143. _applyDecorators: function(method, config) {
  144. each(this._decorators, function() {
  145. this[method](config)
  146. })
  147. },
  148. _handlerExists: function(name) {
  149. if (!this._decorators) {
  150. return false
  151. }
  152. var decorators = this._decorators;
  153. var length = decorators.length;
  154. for (var i = 0; i < length; i++) {
  155. if (decorators[i][name] !== noop) {
  156. return true
  157. }
  158. }
  159. return false
  160. },
  161. _eventHandler: function(name, $itemElement, e) {
  162. if (!this._decorators) {
  163. return false
  164. }
  165. var response = false;
  166. var decorators = this._decorators;
  167. var length = decorators.length;
  168. for (var i = 0; i < length; i++) {
  169. response = decorators[i][name]($itemElement, e);
  170. if (response) {
  171. break
  172. }
  173. }
  174. return response
  175. },
  176. handleClick: function($itemElement, e) {
  177. return this._eventHandler("handleClick", $itemElement, e)
  178. },
  179. handleKeyboardEvents: function(currentFocusedIndex, moveFocusUp) {
  180. return this._eventHandler("handleKeyboardEvents", currentFocusedIndex, moveFocusUp)
  181. },
  182. handleEnterPressing: function() {
  183. return this._eventHandler("handleEnterPressing")
  184. },
  185. contextMenuHandlerExists: function() {
  186. return this._handlerExists("handleContextMenu")
  187. },
  188. handleContextMenu: function($itemElement, e) {
  189. return this._eventHandler("handleContextMenu", $itemElement, e)
  190. },
  191. getExcludedItemSelectors: function() {
  192. var excludedSelectors = [];
  193. this._applyDecorators(DECORATOR_GET_EXCLUDED_SELECTORS_METHOD, excludedSelectors);
  194. return excludedSelectors.join(",")
  195. }
  196. });
  197. module.exports = EditProvider;