helpers.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * DevExtreme (viz/core/helpers.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 _extend = require("../../core/utils/extend").extend;
  11. var windowUtils = require("../../core/utils/window");
  12. var noop = require("../../core/utils/common").noop;
  13. var isServerSide = !windowUtils.hasWindow();
  14. function Flags() {
  15. this.reset()
  16. }
  17. Flags.prototype = {
  18. constructor: Flags,
  19. add: function(codes) {
  20. var i;
  21. var ii = codes.length;
  22. var flags = this._flags;
  23. for (i = 0; i < ii; ++i) {
  24. flags[codes[i]] = 1
  25. }
  26. this._k += ii
  27. },
  28. has: function(code) {
  29. return this._flags[code] > 0
  30. },
  31. count: function() {
  32. return this._k
  33. },
  34. reset: function() {
  35. this._flags = {};
  36. this._k = 0
  37. }
  38. };
  39. function combineMaps(baseMap, thisMap) {
  40. return baseMap !== thisMap ? _extend({}, baseMap, thisMap) : _extend({}, baseMap)
  41. }
  42. function combineLists(baseList, thisList) {
  43. return baseList !== thisList ? baseList.concat(thisList) : baseList.slice()
  44. }
  45. function buildTotalChanges(proto) {
  46. proto._totalChangesOrder = proto._optionChangesOrder.concat(proto._layoutChangesOrder, proto._customChangesOrder)
  47. }
  48. function addChange(settings) {
  49. var proto = this.prototype;
  50. var code = settings.code;
  51. proto["_change_" + code] = settings.handler;
  52. if (settings.isThemeDependent) {
  53. proto._themeDependentChanges.push(code)
  54. }
  55. if (settings.option) {
  56. proto._optionChangesMap[settings.option] = code
  57. }(settings.isOptionChange ? proto._optionChangesOrder : proto._customChangesOrder).push(code);
  58. buildTotalChanges(proto)
  59. }
  60. function createChainExecutor() {
  61. var executeChain = function executeChain() {
  62. var i;
  63. var ii = executeChain._chain.length;
  64. var result;
  65. for (i = 0; i < ii; ++i) {
  66. result = executeChain._chain[i].apply(this, arguments)
  67. }
  68. return result
  69. };
  70. executeChain._chain = [];
  71. executeChain.add = function(item) {
  72. executeChain._chain.push(item)
  73. };
  74. executeChain.copy = function(executor) {
  75. executeChain._chain = executor._chain.slice()
  76. };
  77. return executeChain
  78. }
  79. function expand(target, name, expander) {
  80. var current = target[name];
  81. if (!current) {
  82. current = expander
  83. } else {
  84. if (!current.add) {
  85. current = createChainExecutor();
  86. current.add(target[name]);
  87. current.add(expander)
  88. } else {
  89. if (false === Object.prototype.hasOwnProperty.call(target, name)) {
  90. current = createChainExecutor();
  91. current.copy(target[name])
  92. }
  93. current.add(expander)
  94. }
  95. }
  96. target[name] = current
  97. }
  98. function addPlugin(plugin) {
  99. var proto = this.prototype;
  100. proto._plugins.push(plugin);
  101. plugin.fontFields && proto._fontFields.push.apply(proto._fontFields, plugin.fontFields);
  102. if (plugin.members) {
  103. _extend(this.prototype, plugin.members)
  104. }
  105. if (plugin.customize) {
  106. plugin.customize(this)
  107. }
  108. if (plugin.extenders) {
  109. Object.keys(plugin.extenders).forEach(function(key) {
  110. var func = plugin.extenders[key];
  111. expand(proto, key, func)
  112. }, this)
  113. }
  114. }
  115. exports.replaceInherit = isServerSide ? function(widget) {
  116. var _inherit = widget.inherit;
  117. widget.inherit = function() {
  118. var result = _inherit.apply(this, arguments);
  119. var proto = result.prototype;
  120. ["_plugins", "_eventsMap", "_initialChanges", "_themeDependentChanges", "_optionChangesMap", "_optionChangesOrder", "_layoutChangesOrder", "_customChangesOrder", "_totalChangesOrder"].forEach(function(key) {
  121. proto[key] = {}
  122. });
  123. result.addPlugin = noop;
  124. return result
  125. };
  126. widget.addChange = noop;
  127. widget.addPlugin = noop
  128. } : function(widget) {
  129. var _inherit = widget.inherit;
  130. widget.inherit = function() {
  131. var proto = this.prototype;
  132. var plugins = proto._plugins;
  133. var fontFields = proto._fontFields;
  134. var eventsMap = proto._eventsMap;
  135. var initialChanges = proto._initialChanges;
  136. var themeDependentChanges = proto._themeDependentChanges;
  137. var optionChangesMap = proto._optionChangesMap;
  138. var partialOptionChangesMap = proto._partialOptionChangesMap;
  139. var partialOptionChangesPath = proto._partialOptionChangesPath;
  140. var optionChangesOrder = proto._optionChangesOrder;
  141. var layoutChangesOrder = proto._layoutChangesOrder;
  142. var customChangesOrder = proto._customChangesOrder;
  143. var result = _inherit.apply(this, arguments);
  144. proto = result.prototype;
  145. proto._plugins = combineLists(plugins, proto._plugins);
  146. proto._fontFields = combineLists(fontFields, proto._fontFields);
  147. proto._eventsMap = combineMaps(eventsMap, proto._eventsMap);
  148. proto._initialChanges = combineLists(initialChanges, proto._initialChanges);
  149. proto._themeDependentChanges = combineLists(themeDependentChanges, proto._themeDependentChanges);
  150. proto._optionChangesMap = combineMaps(optionChangesMap, proto._optionChangesMap);
  151. proto._partialOptionChangesMap = combineMaps(partialOptionChangesMap, proto._partialOptionChangesMap);
  152. proto._partialOptionChangesPath = combineMaps(partialOptionChangesPath, proto._partialOptionChangesPath);
  153. proto._optionChangesOrder = combineLists(optionChangesOrder, proto._optionChangesOrder);
  154. proto._layoutChangesOrder = combineLists(layoutChangesOrder, proto._layoutChangesOrder);
  155. proto._customChangesOrder = combineLists(customChangesOrder, proto._customChangesOrder);
  156. buildTotalChanges(proto);
  157. result.addPlugin = addPlugin;
  158. return result
  159. };
  160. widget.prototype._plugins = [];
  161. widget.prototype._fontFields = [];
  162. widget.addChange = addChange;
  163. widget.addPlugin = addPlugin
  164. };
  165. exports.changes = function() {
  166. return new Flags
  167. };
  168. exports.expand = expand;