ui.text_editor.mask.strategy.base.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * DevExtreme (ui/text_box/ui.text_editor.mask.strategy.base.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. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. exports.default = void 0;
  14. var _events_engine = require("../../events/core/events_engine");
  15. var _events_engine2 = _interopRequireDefault(_events_engine);
  16. var _utils = require("../../events/utils");
  17. var _browser = require("../../core/utils/browser");
  18. var _browser2 = _interopRequireDefault(_browser);
  19. var _array = require("../../core/utils/array");
  20. var _dom = require("../../core/utils/dom");
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {
  23. "default": obj
  24. }
  25. }
  26. function _classCallCheck(instance, Constructor) {
  27. if (!(instance instanceof Constructor)) {
  28. throw new TypeError("Cannot call a class as a function")
  29. }
  30. }
  31. function _defineProperties(target, props) {
  32. for (var i = 0; i < props.length; i++) {
  33. var descriptor = props[i];
  34. descriptor.enumerable = descriptor.enumerable || false;
  35. descriptor.configurable = true;
  36. if ("value" in descriptor) {
  37. descriptor.writable = true
  38. }
  39. Object.defineProperty(target, descriptor.key, descriptor)
  40. }
  41. }
  42. function _createClass(Constructor, protoProps, staticProps) {
  43. if (protoProps) {
  44. _defineProperties(Constructor.prototype, protoProps)
  45. }
  46. if (staticProps) {
  47. _defineProperties(Constructor, staticProps)
  48. }
  49. Object.defineProperty(Constructor, "prototype", {
  50. writable: false
  51. });
  52. return Constructor
  53. }
  54. var MASK_EVENT_NAMESPACE = "dxMask";
  55. var BLUR_EVENT = "blur beforedeactivate";
  56. var EMPTY_CHAR = " ";
  57. var BaseMaskStrategy = function() {
  58. function BaseMaskStrategy(editor) {
  59. _classCallCheck(this, BaseMaskStrategy);
  60. this.editor = editor;
  61. this.DIRECTION = {
  62. FORWARD: "forward",
  63. BACKWARD: "backward"
  64. };
  65. this.NAME = this._getStrategyName()
  66. }
  67. _createClass(BaseMaskStrategy, [{
  68. key: "_getStrategyName",
  69. value: function() {
  70. return "base"
  71. }
  72. }, {
  73. key: "editorOption",
  74. value: function() {
  75. var _this$editor;
  76. return (_this$editor = this.editor).option.apply(_this$editor, arguments)
  77. }
  78. }, {
  79. key: "editorInput",
  80. value: function() {
  81. return this.editor._input()
  82. }
  83. }, {
  84. key: "editorCaret",
  85. value: function(newCaret) {
  86. if (!newCaret) {
  87. return this.editor._caret()
  88. }
  89. this.editor._caret(newCaret)
  90. }
  91. }, {
  92. key: "getHandler",
  93. value: function(handlerName) {
  94. var handler = this["_".concat(handlerName, "Handler")] || function() {};
  95. return handler.bind(this)
  96. }
  97. }, {
  98. key: "attachEvents",
  99. value: function() {
  100. var _this = this;
  101. var $input = this.editorInput();
  102. this.getHandleEventNames().forEach(function(eventName) {
  103. var subscriptionName = (0, _utils.addNamespace)(eventName.toLowerCase(), MASK_EVENT_NAMESPACE);
  104. _events_engine2.default.on($input, subscriptionName, _this.getEventHandler(eventName))
  105. });
  106. this._attachChangeEventHandlers()
  107. }
  108. }, {
  109. key: "getHandleEventNames",
  110. value: function() {
  111. return ["focusIn", "focusOut", "keyDown", "input", "paste", "cut", "drop"]
  112. }
  113. }, {
  114. key: "getEventHandler",
  115. value: function(eventName) {
  116. return this["_".concat(eventName, "Handler")].bind(this)
  117. }
  118. }, {
  119. key: "detachEvents",
  120. value: function() {
  121. _events_engine2.default.off(this.editorInput(), ".".concat(MASK_EVENT_NAMESPACE))
  122. }
  123. }, {
  124. key: "_attachChangeEventHandlers",
  125. value: function() {
  126. if ((0, _array.inArray)("change", this.editorOption("valueChangeEvent").split(" ")) === -1) {
  127. return
  128. }
  129. _events_engine2.default.on(this.editorInput(), (0, _utils.addNamespace)(BLUR_EVENT, MASK_EVENT_NAMESPACE), function(e) {
  130. this._suppressCaretChanging(this._changeHandler, [e]);
  131. this._changeHandler(e)
  132. }.bind(this.editor))
  133. }
  134. }, {
  135. key: "_focusInHandler",
  136. value: function() {
  137. this.editor._showMaskPlaceholder();
  138. this.editor._direction(this.DIRECTION.FORWARD);
  139. if (!this.editor._isValueEmpty() && this.editorOption("isValid")) {
  140. this.editor._adjustCaret()
  141. } else {
  142. var caret = this.editor._maskRulesChain.first();
  143. this._caretTimeout = setTimeout(function() {
  144. this._caret({
  145. start: caret,
  146. end: caret
  147. })
  148. }.bind(this.editor), 0)
  149. }
  150. }
  151. }, {
  152. key: "_focusOutHandler",
  153. value: function(event) {
  154. this.editor._changeHandler(event);
  155. if ("onFocus" === this.editorOption("showMaskMode") && this.editor._isValueEmpty()) {
  156. this.editorOption("text", "");
  157. this.editor._renderDisplayText("")
  158. }
  159. }
  160. }, {
  161. key: "_cutHandler",
  162. value: function(event) {
  163. var caret = this.editorCaret();
  164. var selectedText = this.editorInput().val().substring(caret.start, caret.end);
  165. this.editor._maskKeyHandler(event, function() {
  166. return (0, _dom.clipboardText)(event, selectedText)
  167. })
  168. }
  169. }, {
  170. key: "_dropHandler",
  171. value: function() {
  172. this._clearDragTimer();
  173. this._dragTimer = setTimeout(function() {
  174. this.option("value", this._convertToValue(this._input().val()))
  175. }.bind(this.editor))
  176. }
  177. }, {
  178. key: "_clearDragTimer",
  179. value: function() {
  180. clearTimeout(this._dragTimer)
  181. }
  182. }, {
  183. key: "_keyDownHandler",
  184. value: function() {
  185. this._keyPressHandled = false
  186. }
  187. }, {
  188. key: "_pasteHandler",
  189. value: function(event) {
  190. var editor = this.editor;
  191. this._keyPressHandled = true;
  192. var caret = this.editorCaret();
  193. editor._maskKeyHandler(event, function() {
  194. var pastedText = (0, _dom.clipboardText)(event);
  195. var restText = editor._maskRulesChain.text().substring(caret.end);
  196. var accepted = editor._handleChain({
  197. text: pastedText,
  198. start: caret.start,
  199. length: pastedText.length
  200. });
  201. var newCaret = caret.start + accepted;
  202. editor._handleChain({
  203. text: restText,
  204. start: newCaret,
  205. length: restText.length
  206. });
  207. editor._caret({
  208. start: newCaret,
  209. end: newCaret
  210. })
  211. })
  212. }
  213. }, {
  214. key: "_autoFillHandler",
  215. value: function(event) {
  216. var _this2 = this;
  217. var editor = this.editor;
  218. var inputVal = this.editorInput().val();
  219. this._inputHandlerTimer = setTimeout(function() {
  220. _this2._keyPressHandled = true;
  221. if (_this2._isAutoFill()) {
  222. _this2._keyPressHandled = true;
  223. editor._maskKeyHandler(event, function() {
  224. editor._handleChain({
  225. text: inputVal,
  226. start: 0,
  227. length: inputVal.length
  228. })
  229. });
  230. editor._validateMask()
  231. }
  232. })
  233. }
  234. }, {
  235. key: "_isAutoFill",
  236. value: function() {
  237. var $input = this.editor._input();
  238. var result = false;
  239. if (_browser2.default.msie && _browser2.default.version > 11) {
  240. result = $input.hasClass("edge-autofilled")
  241. } else {
  242. if (_browser2.default.webkit) {
  243. var input = $input.get(0);
  244. result = input && input.matches(":-webkit-autofill")
  245. }
  246. }
  247. return result
  248. }
  249. }, {
  250. key: "runWithoutEventProcessing",
  251. value: function(action) {
  252. var keyPressHandled = this._keyPressHandled;
  253. this._keyPressHandled = true;
  254. action();
  255. this._keyPressHandled = keyPressHandled
  256. }
  257. }, {
  258. key: "_backspaceHandler",
  259. value: function() {}
  260. }, {
  261. key: "_delHandler",
  262. value: function(event) {
  263. var editor = this.editor;
  264. this._keyPressHandled = true;
  265. editor._maskKeyHandler(event, function() {
  266. return !editor._hasSelection() && editor._handleKey(EMPTY_CHAR)
  267. })
  268. }
  269. }, {
  270. key: "clean",
  271. value: function() {
  272. this._clearDragTimer();
  273. clearTimeout(this._backspaceHandlerTimeout);
  274. clearTimeout(this._caretTimeout);
  275. clearTimeout(this._inputHandlerTimer)
  276. }
  277. }]);
  278. return BaseMaskStrategy
  279. }();
  280. exports.default = BaseMaskStrategy;