ui.text_editor.mask.rule.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * DevExtreme (ui/text_box/ui.text_editor.mask.rule.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 inArray = require("../../core/utils/array").inArray;
  13. var typeUtils = require("../../core/utils/type");
  14. var noop = require("../../core/utils/common").noop;
  15. var isFunction = typeUtils.isFunction;
  16. var EMPTY_CHAR = " ";
  17. var BaseMaskRule = Class.inherit({
  18. ctor: function(config) {
  19. this._value = EMPTY_CHAR;
  20. extend(this, config)
  21. },
  22. next: function(rule) {
  23. if (!arguments.length) {
  24. return this._next
  25. }
  26. this._next = rule
  27. },
  28. text: noop,
  29. value: noop,
  30. rawValue: noop,
  31. handle: noop,
  32. _prepareHandlingArgs: function(args, config) {
  33. config = config || {};
  34. var handlingProperty = Object.prototype.hasOwnProperty.call(args, "value") ? "value" : "text";
  35. args[handlingProperty] = typeUtils.isDefined(config.str) ? config.str : args[handlingProperty];
  36. args.start = typeUtils.isDefined(config.start) ? config.start : args.start;
  37. args.length = typeUtils.isDefined(config.length) ? config.length : args.length;
  38. args.index = args.index + 1;
  39. return args
  40. },
  41. reset: noop,
  42. clear: noop,
  43. first: function(index) {
  44. index = index || 0;
  45. return this.next().first(index + 1)
  46. },
  47. isAccepted: function() {
  48. return false
  49. },
  50. adjustedCaret: function(caret, isForwardDirection, char) {
  51. return isForwardDirection ? this._adjustedForward(caret, 0, char) : this._adjustedBackward(caret, 0, char)
  52. },
  53. _adjustedForward: noop,
  54. _adjustedBackward: noop,
  55. isValid: noop
  56. });
  57. var EmptyMaskRule = BaseMaskRule.inherit({
  58. next: noop,
  59. handle: function() {
  60. return 0
  61. },
  62. text: function() {
  63. return ""
  64. },
  65. value: function() {
  66. return ""
  67. },
  68. first: function() {
  69. return 0
  70. },
  71. rawValue: function() {
  72. return ""
  73. },
  74. adjustedCaret: function() {
  75. return 0
  76. },
  77. isValid: function() {
  78. return true
  79. }
  80. });
  81. var MaskRule = BaseMaskRule.inherit({
  82. text: function() {
  83. return (this._value !== EMPTY_CHAR ? this._value : this.maskChar) + this.next().text()
  84. },
  85. value: function() {
  86. return this._value + this.next().value()
  87. },
  88. rawValue: function() {
  89. return this._value + this.next().rawValue()
  90. },
  91. handle: function(args) {
  92. var str = Object.prototype.hasOwnProperty.call(args, "value") ? args.value : args.text;
  93. if (!str || !str.length || !args.length) {
  94. return 0
  95. }
  96. if (args.start) {
  97. return this.next().handle(this._prepareHandlingArgs(args, {
  98. start: args.start - 1
  99. }))
  100. }
  101. var char = str[0];
  102. var rest = str.substring(1);
  103. this._tryAcceptChar(char, args);
  104. return this._accepted() ? this.next().handle(this._prepareHandlingArgs(args, {
  105. str: rest,
  106. length: args.length - 1
  107. })) + 1 : this.handle(this._prepareHandlingArgs(args, {
  108. str: rest,
  109. length: args.length - 1
  110. }))
  111. },
  112. clear: function(args) {
  113. this._tryAcceptChar(EMPTY_CHAR, args);
  114. this.next().clear(this._prepareHandlingArgs(args))
  115. },
  116. reset: function() {
  117. this._accepted(false);
  118. this.next().reset()
  119. },
  120. _tryAcceptChar: function(char, args) {
  121. this._accepted(false);
  122. if (!this._isAllowed(char, args)) {
  123. return
  124. }
  125. var acceptedChar = char === EMPTY_CHAR ? this.maskChar : char;
  126. args.fullText = args.fullText.substring(0, args.index) + acceptedChar + args.fullText.substring(args.index + 1);
  127. this._accepted(true);
  128. this._value = char
  129. },
  130. _accepted: function(value) {
  131. if (!arguments.length) {
  132. return !!this._isAccepted
  133. }
  134. this._isAccepted = !!value
  135. },
  136. first: function(index) {
  137. return this._value === EMPTY_CHAR ? index || 0 : this.callBase(index)
  138. },
  139. _isAllowed: function(char, args) {
  140. if (char === EMPTY_CHAR) {
  141. return true
  142. }
  143. return this._isValid(char, args)
  144. },
  145. _isValid: function(char, args) {
  146. var allowedChars = this.allowedChars;
  147. if (allowedChars instanceof RegExp) {
  148. return allowedChars.test(char)
  149. }
  150. if (isFunction(allowedChars)) {
  151. return allowedChars(char, args.index, args.fullText)
  152. }
  153. if (Array.isArray(allowedChars)) {
  154. return inArray(char, allowedChars) > -1
  155. }
  156. return allowedChars === char
  157. },
  158. isAccepted: function(caret) {
  159. return 0 === caret ? this._accepted() : this.next().isAccepted(caret - 1)
  160. },
  161. _adjustedForward: function(caret, index, char) {
  162. if (index >= caret) {
  163. return index
  164. }
  165. return this.next()._adjustedForward(caret, index + 1, char) || index + 1
  166. },
  167. _adjustedBackward: function(caret, index) {
  168. if (index >= caret - 1) {
  169. return caret
  170. }
  171. return this.next()._adjustedBackward(caret, index + 1) || index + 1
  172. },
  173. isValid: function(args) {
  174. return this._isValid(this._value, args) && this.next().isValid(this._prepareHandlingArgs(args))
  175. }
  176. });
  177. var StubMaskRule = MaskRule.inherit({
  178. value: function() {
  179. return this.next().value()
  180. },
  181. handle: function(args) {
  182. var hasValueProperty = Object.prototype.hasOwnProperty.call(args, "value");
  183. var str = hasValueProperty ? args.value : args.text;
  184. if (!str.length || !args.length) {
  185. return 0
  186. }
  187. if (args.start || hasValueProperty) {
  188. return this.next().handle(this._prepareHandlingArgs(args, {
  189. start: args.start && args.start - 1
  190. }))
  191. }
  192. var char = str[0];
  193. var rest = str.substring(1);
  194. this._tryAcceptChar(char);
  195. var nextArgs = this._isAllowed(char) ? this._prepareHandlingArgs(args, {
  196. str: rest,
  197. length: args.length - 1
  198. }) : args;
  199. return this.next().handle(nextArgs) + 1
  200. },
  201. clear: function(args) {
  202. this._accepted(false);
  203. this.next().clear(this._prepareHandlingArgs(args))
  204. },
  205. _tryAcceptChar: function(char) {
  206. this._accepted(this._isValid(char))
  207. },
  208. _isValid: function(char) {
  209. return char === this.maskChar
  210. },
  211. first: function(index) {
  212. index = index || 0;
  213. return this.next().first(index + 1)
  214. },
  215. _adjustedForward: function(caret, index, char) {
  216. if (index >= caret && char === this.maskChar) {
  217. return index
  218. }
  219. if (caret === index + 1 && this._accepted()) {
  220. return caret
  221. }
  222. return this.next()._adjustedForward(caret, index + 1, char)
  223. },
  224. _adjustedBackward: function(caret, index) {
  225. if (index >= caret - 1) {
  226. return 0
  227. }
  228. return this.next()._adjustedBackward(caret, index + 1)
  229. },
  230. isValid: function(args) {
  231. return this.next().isValid(this._prepareHandlingArgs(args))
  232. }
  233. });
  234. module.exports.MaskRule = MaskRule;
  235. module.exports.StubMaskRule = StubMaskRule;
  236. module.exports.EmptyMaskRule = EmptyMaskRule;