1(function (global, factory) { 2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : 3 typeof define === 'function' && define.amd ? define(['exports'], factory) : 4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.IMask = {})); 5})(this, (function (exports) { 'use strict'; 6 7 function _typeof(obj) { 8 "@babel/helpers - typeof"; 9 10 return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { 11 return typeof obj; 12 } : function (obj) { 13 return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; 14 }, _typeof(obj); 15 } 16 17 function _classCallCheck(instance, Constructor) { 18 if (!(instance instanceof Constructor)) { 19 throw new TypeError("Cannot call a class as a function"); 20 } 21 } 22 23 function _defineProperties(target, props) { 24 for (var i = 0; i < props.length; i++) { 25 var descriptor = props[i]; 26 descriptor.enumerable = descriptor.enumerable || false; 27 descriptor.configurable = true; 28 if ("value" in descriptor) descriptor.writable = true; 29 Object.defineProperty(target, descriptor.key, descriptor); 30 } 31 } 32 33 function _createClass(Constructor, protoProps, staticProps) { 34 if (protoProps) _defineProperties(Constructor.prototype, protoProps); 35 if (staticProps) _defineProperties(Constructor, staticProps); 36 Object.defineProperty(Constructor, "prototype", { 37 writable: false 38 }); 39 return Constructor; 40 } 41 42 function _defineProperty(obj, key, value) { 43 if (key in obj) { 44 Object.defineProperty(obj, key, { 45 value: value, 46 enumerable: true, 47 configurable: true, 48 writable: true 49 }); 50 } else { 51 obj[key] = value; 52 } 53 54 return obj; 55 } 56 57 function _inherits(subClass, superClass) { 58 if (typeof superClass !== "function" && superClass !== null) { 59 throw new TypeError("Super expression must either be null or a function"); 60 } 61 62 subClass.prototype = Object.create(superClass && superClass.prototype, { 63 constructor: { 64 value: subClass, 65 writable: true, 66 configurable: true 67 } 68 }); 69 Object.defineProperty(subClass, "prototype", { 70 writable: false 71 }); 72 if (superClass) _setPrototypeOf(subClass, superClass); 73 } 74 75 function _getPrototypeOf(o) { 76 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { 77 return o.__proto__ || Object.getPrototypeOf(o); 78 }; 79 return _getPrototypeOf(o); 80 } 81 82 function _setPrototypeOf(o, p) { 83 _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { 84 o.__proto__ = p; 85 return o; 86 }; 87 return _setPrototypeOf(o, p); 88 } 89 90 function _isNativeReflectConstruct() { 91 if (typeof Reflect === "undefined" || !Reflect.construct) return false; 92 if (Reflect.construct.sham) return false; 93 if (typeof Proxy === "function") return true; 94 95 try { 96 Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); 97 return true; 98 } catch (e) { 99 return false; 100 } 101 } 102 103 function _objectWithoutPropertiesLoose(source, excluded) { 104 if (source == null) return {}; 105 var target = {}; 106 var sourceKeys = Object.keys(source); 107 var key, i; 108 109 for (i = 0; i < sourceKeys.length; i++) { 110 key = sourceKeys[i]; 111 if (excluded.indexOf(key) >= 0) continue; 112 target[key] = source[key]; 113 } 114 115 return target; 116 } 117 118 function _objectWithoutProperties(source, excluded) { 119 if (source == null) return {}; 120 121 var target = _objectWithoutPropertiesLoose(source, excluded); 122 123 var key, i; 124 125 if (Object.getOwnPropertySymbols) { 126 var sourceSymbolKeys = Object.getOwnPropertySymbols(source); 127 128 for (i = 0; i < sourceSymbolKeys.length; i++) { 129 key = sourceSymbolKeys[i]; 130 if (excluded.indexOf(key) >= 0) continue; 131 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; 132 target[key] = source[key]; 133 } 134 } 135 136 return target; 137 } 138 139 function _assertThisInitialized(self) { 140 if (self === void 0) { 141 throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); 142 } 143 144 return self; 145 } 146 147 function _possibleConstructorReturn(self, call) { 148 if (call && (typeof call === "object" || typeof call === "function")) { 149 return call; 150 } else if (call !== void 0) { 151 throw new TypeError("Derived constructors may only return object or undefined"); 152 } 153 154 return _assertThisInitialized(self); 155 } 156 157 function _createSuper(Derived) { 158 var hasNativeReflectConstruct = _isNativeReflectConstruct(); 159 160 return function _createSuperInternal() { 161 var Super = _getPrototypeOf(Derived), 162 result; 163 164 if (hasNativeReflectConstruct) { 165 var NewTarget = _getPrototypeOf(this).constructor; 166 167 result = Reflect.construct(Super, arguments, NewTarget); 168 } else { 169 result = Super.apply(this, arguments); 170 } 171 172 return _possibleConstructorReturn(this, result); 173 }; 174 } 175 176 function _superPropBase(object, property) { 177 while (!Object.prototype.hasOwnProperty.call(object, property)) { 178 object = _getPrototypeOf(object); 179 if (object === null) break; 180 } 181 182 return object; 183 } 184 185 function _get() { 186 if (typeof Reflect !== "undefined" && Reflect.get) { 187 _get = Reflect.get.bind(); 188 } else { 189 _get = function _get(target, property, receiver) { 190 var base = _superPropBase(target, property); 191 192 if (!base) return; 193 var desc = Object.getOwnPropertyDescriptor(base, property); 194 195 if (desc.get) { 196 return desc.get.call(arguments.length < 3 ? target : receiver); 197 } 198 199 return desc.value; 200 }; 201 } 202 203 return _get.apply(this, arguments); 204 } 205 206 function set(target, property, value, receiver) { 207 if (typeof Reflect !== "undefined" && Reflect.set) { 208 set = Reflect.set; 209 } else { 210 set = function set(target, property, value, receiver) { 211 var base = _superPropBase(target, property); 212 213 var desc; 214 215 if (base) { 216 desc = Object.getOwnPropertyDescriptor(base, property); 217 218 if (desc.set) { 219 desc.set.call(receiver, value); 220 return true; 221 } else if (!desc.writable) { 222 return false;
223 } 224 } 225 226 desc = Object.getOwnPropertyDescriptor(receiver, property); 227 228 if (desc) { 229 if (!desc.writable) { 230 return false; 231 } 232 233 desc.value = value; 234 Object.defineProperty(receiver, property, desc); 235 } else { 236 _defineProperty(receiver, property, value); 237 } 238 239 return true; 240 }; 241 } 242 243 return set(target, property, value, receiver); 244 } 245 246 function _set(target, property, value, receiver, isStrict) { 247 var s = set(target, property, value, receiver || target); 248 249 if (!s && isStrict) { 250 throw new Error('failed to set property'); 251 } 252 253 return value; 254 } 255 256 function _slicedToArray(arr, i) { 257 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); 258 } 259 260 function _toConsumableArray(arr) { 261 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); 262 } 263 264 function _arrayWithoutHoles(arr) { 265 if (Array.isArray(arr)) return _arrayLikeToArray(arr); 266 } 267 268 function _arrayWithHoles(arr) { 269 if (Array.isArray(arr)) return arr; 270 } 271 272 function _iterableToArray(iter) { 273 if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); 274 } 275 276 function _iterableToArrayLimit(arr, i) { 277 var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; 278 279 if (_i == null) return; 280 var _arr = []; 281 var _n = true; 282 var _d = false; 283 284 var _s, _e; 285 286 try { 287 for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { 288 _arr.push(_s.value); 289 290 if (i && _arr.length === i) break; 291 } 292 } catch (err) { 293 _d = true; 294 _e = err; 295 } finally { 296 try { 297 if (!_n && _i["return"] != null) _i["return"](); 298 } finally { 299 if (_d) throw _e; 300 } 301 } 302 303 return _arr; 304 } 305 306 function _unsupportedIterableToArray(o, minLen) { 307 if (!o) return; 308 if (typeof o === "string") return _arrayLikeToArray(o, minLen); 309 var n = Object.prototype.toString.call(o).slice(8, -1); 310 if (n === "Object" && o.constructor) n = o.constructor.name; 311 if (n === "Map" || n === "Set") return Array.from(o); 312 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); 313 } 314 315 function _arrayLikeToArray(arr, len) { 316 if (len == null || len > arr.length) len = arr.length; 317 318 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; 319 320 return arr2; 321 } 322 323 function _nonIterableSpread() { 324 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); 325 } 326 327 function _nonIterableRest() { 328 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); 329 } 330 331 /** 332 Provides details of changing model value 333 @param {Object} [details] 334 @param {string} [details.inserted] - Inserted symbols 335 @param {boolean} [details.skip] - Can skip chars 336 @param {number} [details.removeCount] - Removed symbols count 337 @param {number} [details.tailShift] - Additional offset if any changes occurred before tail 338 */ 339 var ChangeDetails = /*#__PURE__*/function () { 340 /** Inserted symbols */ 341 342 /** Can skip chars */ 343 344 /** Additional offset if any changes occurred before tail */ 345 346 /** Raw inserted is used by dynamic mask */ 347 function ChangeDetails(details) { 348 _classCallCheck(this, ChangeDetails); 349 350 Object.assign(this, { 351 inserted: '', 352 rawInserted: '', 353 skip: false, 354 tailShift: 0 355 }, details); 356 } 357 /** 358 Aggregate changes 359 @returns {ChangeDetails} `this` 360 */ 361 362 363 _createClass(ChangeDetails, [{ 364 key: "aggregate", 365 value: function aggregate(details) { 366 this.rawInserted += details.rawInserted; 367 this.skip = this.skip || details.skip; 368 this.inserted += details.inserted; 369 this.tailShift += details.tailShift; 370 return this; 371 } 372 /** Total offset considering all changes */ 373 374 }, {
375 key: "offset", 376 get: function get() { 377 return this.tailShift + this.inserted.length; 378 } 379 }]); 380 381 return ChangeDetails; 382 }(); 383 384 /** Checks if value is string */ 385 386 function isString(str) { 387 return typeof str === 'string' || str instanceof String; 388 } 389 /** 390 Direction 391 @prop {string} NONE 392 @prop {string} LEFT 393 @prop {string} FORCE_LEFT 394 @prop {string} RIGHT 395 @prop {string} FORCE_RIGHT 396 */ 397 398 var DIRECTION = { 399 NONE: 'NONE', 400 LEFT: 'LEFT', 401 FORCE_LEFT: 'FORCE_LEFT', 402 RIGHT: 'RIGHT', 403 FORCE_RIGHT: 'FORCE_RIGHT' 404 }; 405 /** */ 406 407 function forceDirection(direction) { 408 switch (direction) { 409 case DIRECTION.LEFT: 410 return DIRECTION.FORCE_LEFT; 411 412 case DIRECTION.RIGHT: 413 return DIRECTION.FORCE_RIGHT; 414 415 default: 416 return direction; 417 } 418 } 419 /** Escapes regular expression control chars */ 420 421 function escapeRegExp(str) { 422 return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); 423 } 424 function normalizePrepare(prep) { 425 return Array.isArray(prep) ? prep : [prep, new ChangeDetails()]; 426 } // cloned from https://github.com/epoberezkin/fast-deep-equal with small changes 427 428 function objectIncludes(b, a) { 429 if (a === b) return true; 430 var arrA = Array.isArray(a), 431 arrB = Array.isArray(b), 432 i; 433 434 if (arrA && arrB) { 435 if (a.length != b.length) return false; 436 437 for (i = 0; i < a.length; i++) { 438 if (!objectIncludes(a[i], b[i])) return false; 439 } 440 441 return true; 442 } 443 444 if (arrA != arrB) return false; 445 446 if (a && b && _typeof(a) === 'object' && _typeof(b) === 'object') { 447 var dateA = a instanceof Date, 448 dateB = b instanceof Date; 449 if (dateA && dateB) return a.getTime() == b.getTime(); 450 if (dateA != dateB) return false; 451 var regexpA = a instanceof RegExp, 452 regexpB = b instanceof RegExp; 453 if (regexpA && regexpB) return a.toString() == b.toString(); 454 if (regexpA != regexpB) return false; 455 var keys = Object.keys(a); // if (keys.length !== Object.keys(b).length) return false; 456 457 for (i = 0; i < keys.length; i++) { 458 // $FlowFixMe ... ??? 459 if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; 460 } 461 462 for (i = 0; i < keys.length; i++) { 463 if (!objectIncludes(b[keys[i]], a[keys[i]])) return false; 464 } 465 466 return true; 467 } else if (a && b && typeof a === 'function' && typeof b === 'function') { 468 return a.toString() === b.toString(); 469 } 470 471 return false; 472 } 473 /** Selection range */ 474 475 /** Provides details of changing input */ 476 477 var ActionDetails = /*#__PURE__*/function () { 478 /** Current input value */ 479 480 /** Current cursor position */ 481 482 /** Old input value */ 483 484 /** Old selection */ 485 function ActionDetails(value, cursorPos, oldValue, oldSelection) { 486 _classCallCheck(this, ActionDetails); 487 488 this.value = value;
489 this.cursorPos = cursorPos; 490 this.oldValue = oldValue; 491 this.oldSelection = oldSelection; // double check if left part was changed (autofilling, other non-standard input triggers) 492 493 while (this.value.slice(0, this.startChangePos) !== this.oldValue.slice(0, this.startChangePos)) { 494 --this.oldSelection.start; 495 } 496 } 497 /** 498 Start changing position 499 @readonly 500 */ 501 502 503 _createClass(ActionDetails, [{ 504 key: "startChangePos", 505 get: function get() { 506 return Math.min(this.cursorPos, this.oldSelection.start); 507 } 508 /** 509 Inserted symbols count 510 @readonly 511 */ 512 513 }, { 514 key: "insertedCount", 515 get: function get() { 516 return this.cursorPos - this.startChangePos; 517 } 518 /** 519 Inserted symbols 520 @readonly 521 */ 522 523 }, { 524 key: "inserted", 525 get: function get() { 526 return this.value.substr(this.startChangePos, this.insertedCount); 527 } 528 /** 529 Removed symbols count 530 @readonly 531 */ 532 533 }, { 534 key: "removedCount", 535 get: function get() { 536 // Math.max for opposite operation 537 return Math.max(this.oldSelection.end - this.startChangePos || // for Delete 538 this.oldValue.length - this.value.length, 0); 539 } 540 /** 541 Removed symbols 542 @readonly 543 */ 544 545 }, { 546 key: "removed", 547 get: function get() { 548 return this.oldValue.substr(this.startChangePos, this.removedCount); 549 } 550 /** 551 Unchanged head symbols 552 @readonly 553 */ 554 555 }, { 556 key: "head", 557 get: function get() { 558 return this.value.substring(0, this.startChangePos); 559 } 560 /** 561 Unchanged tail symbols 562 @readonly 563 */ 564 565 }, { 566 key: "tail", 567 get: function get() { 568 return this.value.substring(this.startChangePos + this.insertedCount); 569 } 570 /** 571 Remove direction 572 @readonly 573 */ 574 575 }, { 576 key: "removeDirection", 577 get: function get() { 578 if (!this.removedCount || this.insertedCount) return DIRECTION.NONE; // align right if delete at right 579 580 return (this.oldSelection.end === this.cursorPos || this.oldSelection.start === this.cursorPos) && // if not range removed (event with backspace) 581 this.oldSelection.end === this.oldSelection.start ? DIRECTION.RIGHT : DIRECTION.LEFT; 582 } 583 }]); 584 585 return ActionDetails; 586 }(); 587 588 /** Provides details of continuous extracted tail */ 589 var ContinuousTailDetails = /*#__PURE__*/function () { 590 /** Tail value as string */ 591 592 /** Tail start position */ 593 594 /** Start position */ 595 function ContinuousTailDetails() { 596 var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; 597 var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; 598 var stop = arguments.length > 2 ? arguments[2] : undefined; 599 600 _classCallCheck(this, ContinuousTailDetails); 601 602 this.value = value; 603 this.from = from; 604 this.stop = stop; 605 } 606 607 _createClass(ContinuousTailDetails, [{ 608 key: "toString", 609 value: function toString() { 610 return this.value; 611 } 612 }, { 613 key: "extend", 614 value: function extend(tail) { 615 this.value += String(tail); 616 } 617 }, { 618 key: "appendTo", 619 value: function appendTo(masked) { 620 return masked.append(this.toString(), { 621 tail: true 622 }).aggregate(masked._appendPlaceholder()); 623 } 624 }, { 625 key: "state", 626 get: function get() { 627 return { 628 value: this.value, 629 from: this.from, 630 stop: this.stop 631 }; 632 }, 633 set: function set(state) { 634 Object.assign(this, state); 635 } 636 }, { 637 key: "unshift", 638 value: function unshift(beforePos) { 639 if (!this.value.length || beforePos != null && this.from >= beforePos) return ''; 640 var shiftChar = this.value[0]; 641 this.value = this.value.slice(1); 642 return shiftChar; 643 } 644 }, { 645 key: "shift", 646 value: function shift() { 647 if (!this.value.length) return ''; 648 var shiftChar = this.value[this.value.length - 1]; 649 this.value = this.value.slice(0, -1); 650 return shiftChar; 651 } 652 }]); 653 654 return ContinuousTailDetails; 655 }(); 656 657 /** 658 * Applies mask on element. 659 * @constructor 660 * @param {HTMLInputElement|HTMLTextAreaElement|MaskElement} el - Element to apply mask 661 * @param {Object} opts - Custom mask options 662 * @return {InputMask} 663 */ 664 function IMask(el) { 665 var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 666 // currently available only for input-like elements 667 return new IMask.InputMask(el, opts); 668 } 669 670 /** Supported mask type */ 671 672 /** Provides common masking stuff */ 673 var Masked = /*#__PURE__*/function () { 674 // $Shape<MaskedOptions>; TODO after fix https://github.com/facebook/flow/issues/4773 675 676 /** @type {Mask} */ 677 678 /** */ 679 // $FlowFixMe no ideas 680 681 /** Transforms value before mask processing */ 682 683 /** Validates if value is acceptable */ 684 685 /** Does additional processing in the end of editing */ 686 687 /** Format typed value to string */ 688 689 /** Parse strgin to get typed value */ 690 691 /** Enable characters overwriting */ 692 693 /** */ 694 695 /** */ 696 function Masked(opts) { 697 _classCallCheck(this, Masked); 698 699 this._value = ''; 700 701 this._update(Object.assign({}, Masked.DEFAULTS, opts)); 702 703 this.isInitialized = true; 704 } 705 /** Sets and applies new options */ 706 707 708 _createClass(Masked, [{ 709 key: "updateOptions", 710 value: function updateOptions(opts) { 711 if (!Object.keys(opts).length) return; // $FlowFixMe 712 713 this.withValueRefresh(this._update.bind(this, opts)); 714 } 715 /** 716 Sets new options 717 @protected 718 */ 719 720 }, {
721 key: "_update", 722 value: function _update(opts) { 723 Object.assign(this, opts); 724 } 725 /** Mask state */ 726 727 }, { 728 key: "state", 729 get: function get() { 730 return { 731 _value: this.value 732 }; 733 }, 734 set: function set(state) { 735 this._value = state._value; 736 } 737 /** Resets value */ 738 739 }, { 740 key: "reset", 741 value: function reset() { 742 this._value = ''; 743 } 744 /** */ 745 746 }, { 747 key: "value", 748 get: function get() { 749 return this._value; 750 }, 751 set: function set(value) { 752 this.resolve(value); 753 } 754 /** Resolve new value */ 755 756 }, { 757 key: "resolve", 758 value: function resolve(value) { 759 this.reset(); 760 this.append(value, { 761 input: true 762 }, ''); 763 this.doCommit(); 764 return this.value; 765 } 766 /** */ 767 768 }, { 769 key: "unmaskedValue", 770 get: function get() { 771 return this.value; 772 }, 773 set: function set(value) { 774 this.reset(); 775 this.append(value, {}, ''); 776 this.doCommit(); 777 } 778 /** */ 779 780 }, { 781 key: "typedValue", 782 get: function get() { 783 return this.doParse(this.value); 784 }, 785 set: function set(value) { 786 this.value = this.doFormat(value); 787 } 788 /** Value that includes raw user input */ 789 790 }, { 791 key: "rawInputValue", 792 get: function get() { 793 return this.extractInput(0, this.value.length, { 794 raw: true 795 }); 796 }, 797 set: function set(value) { 798 this.reset(); 799 this.append(value, { 800 raw: true 801 }, ''); 802 this.doCommit(); 803 } 804 /** */ 805 806 }, { 807 key: "isComplete", 808 get: function get() { 809 return true; 810 } 811 /** */ 812 813 }, { 814 key: "isFilled", 815 get: function get() { 816 return this.isComplete; 817 } 818 /** Finds nearest input position in direction */ 819 820 }, { 821 key: "nearestInputPos", 822 value: function nearestInputPos(cursorPos, direction) { 823 return cursorPos; 824 } 825 /** Extracts value in range considering flags */ 826 827 }, { 828 key: "extractInput", 829 value: function extractInput() { 830 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 831 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 832 return this.value.slice(fromPos, toPos); 833 } 834 /** Extracts tail in range */ 835 836 }, { 837 key: "extractTail", 838 value: function extractTail() { 839 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 840 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 841 return new ContinuousTailDetails(this.extractInput(fromPos, toPos), fromPos); 842 } 843 /** Appends tail */ 844 // $FlowFixMe no ideas 845 846 }, { 847 key: "appendTail", 848 value: function appendTail(tail) { 849 if (isString(tail)) tail = new ContinuousTailDetails(String(tail)); 850 return tail.appendTo(this); 851 } 852 /** Appends char */ 853 854 }, { 855 key: "_appendCharRaw", 856 value: function _appendCharRaw(ch) { 857 if (!ch) return new ChangeDetails(); 858 this._value += ch; 859 return new ChangeDetails({ 860 inserted: ch, 861 rawInserted: ch 862 }); 863 } 864 /** Appends char */ 865 866 }, { 867 key: "_appendChar", 868 value: function _appendChar(ch) { 869 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 870 var checkTail = arguments.length > 2 ? arguments[2] : undefined; 871 var consistentState = this.state; 872 var details; 873 874 var _normalizePrepare = normalizePrepare(this.doPrepare(ch, flags)); 875 876 var _normalizePrepare2 = _slicedToArray(_normalizePrepare, 2); 877 878 ch = _normalizePrepare2[0]; 879 details = _normalizePrepare2[1]; 880 details = details.aggregate(this._appendCharRaw(ch, flags)); 881 882 if (details.inserted) { 883 var consistentTail; 884 var appended = this.doValidate(flags) !== false; 885 886 if (appended && checkTail != null) { 887 // validation ok, check tail 888 var beforeTailState = this.state; 889 890 if (this.overwrite === true) { 891 consistentTail = checkTail.state; 892 checkTail.unshift(this.value.length); 893 } 894 895 var tailDetails = this.appendTail(checkTail); 896 appended = tailDetails.rawInserted === checkTail.toString(); // not ok, try shift 897 898 if (!(appended && tailDetails.inserted) && this.overwrite === 'shift') { 899 this.state = beforeTailState; 900 consistentTail = checkTail.state; 901 checkTail.shift(); 902 tailDetails = this.appendTail(checkTail); 903 appended = tailDetails.rawInserted === checkTail.toString(); 904 } // if ok, rollback state after tail 905 906 907 if (appended && tailDetails.inserted) this.state = beforeTailState; 908 } // revert all if something went wrong 909 910 911 if (!appended) { 912 details = new ChangeDetails(); 913 this.state = consistentState; 914 if (checkTail && consistentTail) checkTail.state = consistentTail; 915 } 916 } 917 918 return details; 919 } 920 /** Appends optional placeholder at end */ 921 922 }, {
923 key: "_appendPlaceholder", 924 value: function _appendPlaceholder() { 925 return new ChangeDetails(); 926 } 927 /** Appends optional eager placeholder at end */ 928 929 }, { 930 key: "_appendEager", 931 value: function _appendEager() { 932 return new ChangeDetails(); 933 } 934 /** Appends symbols considering flags */ 935 // $FlowFixMe no ideas 936 937 }, { 938 key: "append", 939 value: function append(str, flags, tail) { 940 if (!isString(str)) throw new Error('value should be string'); 941 var details = new ChangeDetails(); 942 var checkTail = isString(tail) ? new ContinuousTailDetails(String(tail)) : tail; 943 if (flags !== null && flags !== void 0 && flags.tail) flags._beforeTailState = this.state; 944 945 for (var ci = 0; ci < str.length; ++ci) { 946 details.aggregate(this._appendChar(str[ci], flags, checkTail)); 947 } // append tail but aggregate only tailShift 948 949 950 if (checkTail != null) { 951 details.tailShift += this.appendTail(checkTail).tailShift; // TODO it's a good idea to clear state after appending ends 952 // but it causes bugs when one append calls another (when dynamic dispatch set rawInputValue) 953 // this._resetBeforeTailState(); 954 } 955 956 if (this.eager && flags !== null && flags !== void 0 && flags.input && str) { 957 details.aggregate(this._appendEager()); 958 } 959 960 return details; 961 } 962 /** */ 963 964 }, { 965 key: "remove", 966 value: function remove() { 967 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 968 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 969 this._value = this.value.slice(0, fromPos) + this.value.slice(toPos); 970 return new ChangeDetails(); 971 } 972 /** Calls function and reapplies current value */ 973 974 }, { 975 key: "withValueRefresh", 976 value: function withValueRefresh(fn) { 977 if (this._refreshing || !this.isInitialized) return fn(); 978 this._refreshing = true; 979 var rawInput = this.rawInputValue; 980 var value = this.value; 981 var ret = fn(); 982 this.rawInputValue = rawInput; // append lost trailing chars at end 983 984 if (this.value && this.value !== value && value.indexOf(this.value) === 0) { 985 this.append(value.slice(this.value.length), {}, ''); 986 } 987 988 delete this._refreshing; 989 return ret; 990 } 991 /** */ 992 993 }, { 994 key: "runIsolated", 995 value: function runIsolated(fn) { 996 if (this._isolated || !this.isInitialized) return fn(this); 997 this._isolated = true; 998 var state = this.state; 999 var ret = fn(this); 1000 this.state = state; 1001 delete this._isolated; 1002 return ret; 1003 } 1004 /** 1005 Prepares string before mask processing 1006 @protected 1007 */ 1008 1009 }, { 1010 key: "doPrepare", 1011 value: function doPrepare(str) { 1012 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 1013 return this.prepare ? this.prepare(str, this, flags) : str; 1014 } 1015 /** 1016 Validates if value is acceptable 1017 @protected 1018 */ 1019 1020 }, { 1021 key: "doValidate", 1022 value: function doValidate(flags) { 1023 return (!this.validate || this.validate(this.value, this, flags)) && (!this.parent || this.parent.doValidate(flags)); 1024 } 1025 /** 1026 Does additional processing in the end of editing 1027 @protected 1028 */ 1029 1030 }, { 1031 key: "doCommit", 1032 value: function doCommit() { 1033 if (this.commit) this.commit(this.value, this); 1034 } 1035 /** */ 1036 1037 }, { 1038 key: "doFormat", 1039 value: function doFormat(value) { 1040 return this.format ? this.format(value, this) : value; 1041 } 1042 /** */ 1043 1044 }, { 1045 key: "doParse", 1046 value: function doParse(str) { 1047 return this.parse ? this.parse(str, this) : str; 1048 } 1049 /** */ 1050 1051 }, { 1052 key: "splice", 1053 value: function splice(start, deleteCount, inserted, removeDirection) { 1054 var flags = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { 1055 input: true 1056 }; 1057 var tailPos = start + deleteCount; 1058 var tail = this.extractTail(tailPos); 1059 var oldRawValue; 1060 1061 if (this.eager) { 1062 removeDirection = forceDirection(removeDirection); 1063 oldRawValue = this.extractInput(0, tailPos, { 1064 raw: true 1065 }); 1066 } 1067 1068 var startChangePos = this.nearestInputPos(start, deleteCount > 1 && start !== 0 && !this.eager ? DIRECTION.NONE : removeDirection); 1069 var details = new ChangeDetails({ 1070 tailShift: startChangePos - start // adjust tailShift if start was aligned 1071 1072 }).aggregate(this.remove(startChangePos)); 1073 1074 if (this.eager && removeDirection !== DIRECTION.NONE && oldRawValue === this.rawInputValue) { 1075 if (removeDirection === DIRECTION.FORCE_LEFT) { 1076 var valLength; 1077 1078 while (oldRawValue === this.rawInputValue && (valLength = this.value.length)) { 1079 details.aggregate(new ChangeDetails({ 1080 tailShift: -1 1081 })).aggregate(this.remove(valLength - 1)); 1082 } 1083 } else if (removeDirection === DIRECTION.FORCE_RIGHT) { 1084 tail.unshift(); 1085 } 1086 } 1087 1088 return details.aggregate(this.append(inserted, flags, tail)); 1089 } 1090 }, {
1091 key: "maskEquals", 1092 value: function maskEquals(mask) { 1093 return this.mask === mask; 1094 } 1095 }, { 1096 key: "typedValueEquals", 1097 value: function typedValueEquals(value) { 1098 var tval = this.typedValue; 1099 return value === tval || Masked.EMPTY_VALUES.includes(value) && Masked.EMPTY_VALUES.includes(tval) || this.doFormat(value) === this.doFormat(this.typedValue); 1100 } 1101 }]); 1102 1103 return Masked; 1104 }(); 1105 Masked.DEFAULTS = { 1106 format: function format(v) { 1107 return v; 1108 }, 1109 parse: function parse(v) { 1110 return v; 1111 } 1112 }; 1113 Masked.EMPTY_VALUES = [undefined, null, '']; 1114 IMask.Masked = Masked; 1115 1116 /** Get Masked class by mask type */ 1117 1118 function maskedClass(mask) { 1119 if (mask == null) { 1120 throw new Error('mask property should be defined'); 1121 } // $FlowFixMe 1122 1123 1124 if (mask instanceof RegExp) return IMask.MaskedRegExp; // $FlowFixMe 1125 1126 if (isString(mask)) return IMask.MaskedPattern; // $FlowFixMe 1127 1128 if (mask instanceof Date || mask === Date) return IMask.MaskedDate; // $FlowFixMe 1129 1130 if (mask instanceof Number || typeof mask === 'number' || mask === Number) return IMask.MaskedNumber; // $FlowFixMe 1131 1132 if (Array.isArray(mask) || mask === Array) return IMask.MaskedDynamic; // $FlowFixMe 1133 1134 if (IMask.Masked && mask.prototype instanceof IMask.Masked) return mask; // $FlowFixMe 1135 1136 if (mask instanceof IMask.Masked) return mask.constructor; // $FlowFixMe 1137 1138 if (mask instanceof Function) return IMask.MaskedFunction; 1139 console.warn('Mask not found for mask', mask); // eslint-disable-line no-console 1140 // $FlowFixMe 1141 1142 return IMask.Masked; 1143 } 1144 /** Creates new {@link Masked} depending on mask type */ 1145 1146 function createMask(opts) { 1147 // $FlowFixMe 1148 if (IMask.Masked && opts instanceof IMask.Masked) return opts; 1149 opts = Object.assign({}, opts); 1150 var mask = opts.mask; // $FlowFixMe 1151 1152 if (IMask.Masked && mask instanceof IMask.Masked) return mask; 1153 var MaskedClass = maskedClass(mask); 1154 if (!MaskedClass) throw new Error('Masked class is not found for provided mask, appropriate module needs to be import manually before creating mask.'); 1155 return new MaskedClass(opts); 1156 } 1157 IMask.createMask = createMask; 1158 1159 var _excluded$4 = ["mask"]; 1160 var DEFAULT_INPUT_DEFINITIONS = { 1161 '0': /\d/, 1162 'a': /[\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFB
1162D3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]/, 1163 // http://stackoverflow.com/a/22075070 1164 '*': /./ 1165 }; 1166 /** */ 1167 1168 var PatternInputDefinition = /*#__PURE__*/function () { 1169 /** */ 1170 1171 /** */ 1172 1173 /** */ 1174 1175 /** */ 1176 1177 /** */ 1178 1179 /** */ 1180 1181 /** */ 1182 function PatternInputDefinition(opts) { 1183 _classCallCheck(this, PatternInputDefinition); 1184 1185 var mask = opts.mask, 1186 blockOpts = _objectWithoutProperties(opts, _excluded$4); 1187 1188 this.masked = createMask({ 1189 mask: mask 1190 }); 1191 Object.assign(this, blockOpts); 1192 } 1193 1194 _createClass(PatternInputDefinition, [{ 1195 key: "reset", 1196 value: function reset() { 1197 this.isFilled = false; 1198 this.masked.reset(); 1199 } 1200 }, { 1201 key: "remove", 1202 value: function remove() { 1203 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 1204 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 1205 1206 if (fromPos === 0 && toPos >= 1) { 1207 this.isFilled = false; 1208 return this.masked.remove(fromPos, toPos); 1209 } 1210 1211 return new ChangeDetails(); 1212 } 1213 }, { 1214 key: "value", 1215 get: function get() { 1216 return this.masked.value || (this.isFilled && !this.isOptional ? this.placeholderChar : ''); 1217 } 1218 }, { 1219 key: "unmaskedValue", 1220 get: function get() { 1221 return this.masked.unmaskedValue; 1222 } 1223 }, { 1224 key: "isComplete", 1225 get: function get() { 1226 return Boolean(this.masked.value) || this.isOptional; 1227 } 1228 }, { 1229 key: "_appendChar", 1230 value: function _appendChar(ch) { 1231 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 1232 if (this.isFilled) return new ChangeDetails(); 1233 var state = this.masked.state; // simulate input 1234 1235 var details = this.masked._appendChar(ch, flags); 1236 1237 if (details.inserted && this.doValidate(flags) === false) { 1238 details.inserted = details.rawInserted = ''; 1239 this.masked.state = state; 1240 } 1241 1242 if (!details.inserted && !this.isOptional && !this.lazy && !flags.input) { 1243 details.inserted = this.placeholderChar; 1244 } 1245 1246 details.skip = !details.inserted && !this.isOptional; 1247 this.isFilled = Boolean(details.inserted); 1248 return details; 1249 } 1250 }, { 1251 key: "append", 1252 value: function append() { 1253 var _this$masked; 1254 1255 // TODO probably should be done via _appendChar 1256 return (_this$masked = this.masked).append.apply(_this$masked, arguments); 1257 } 1258 }, { 1259 key: "_appendPlaceholder", 1260 value: function _appendPlaceholder() { 1261 var details = new ChangeDetails(); 1262 if (this.isFilled || this.isOptional) return details; 1263 this.isFilled = true; 1264 details.inserted = this.placeholderChar; 1265 return details; 1266 } 1267 }, { 1268 key: "_appendEager", 1269 value: function _appendEager() { 1270 return new ChangeDetails(); 1271 } 1272 }, { 1273 key: "extractTail", 1274 value: function extractTail() { 1275 var _this$masked2; 1276 1277 return (_this$masked2 = this.masked).extractTail.apply(_this$masked2, arguments); 1278 } 1279 }, { 1280 key: "appendTail", 1281 value: function appendTail() { 1282 var _this$masked3; 1283 1284 return (_this$masked3 = this.masked).appendTail.apply(_this$masked3, arguments); 1285 } 1286 }, { 1287 key: "extractInput", 1288 value: function extractInput() { 1289 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 1290 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 1291 var flags = arguments.length > 2 ? arguments[2] : undefined; 1292 return this.masked.extractInput(fromPos, toPos, flags); 1293 } 1294 }, { 1295 key: "nearestInputPos", 1296 value: function nearestInputPos(cursorPos) { 1297 var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DIRECTION.NONE; 1298 var minPos = 0; 1299 var maxPos = this.value.length; 1300 var boundPos = Math.min(Math.max(cursorPos, minPos), maxPos); 1301 1302 switch (direction) { 1303 case DIRECTION.LEFT: 1304 case DIRECTION.FORCE_LEFT: 1305 return this.isComplete ? boundPos : minPos; 1306 1307 case DIRECTION.RIGHT: 1308 case DIRECTION.FORCE_RIGHT: 1309 return this.isComplete ? boundPos : maxPos; 1310 1311 case DIRECTION.NONE: 1312 default: 1313 return boundPos; 1314 } 1315 } 1316 }, {
1317 key: "doValidate", 1318 value: function doValidate() { 1319 var _this$masked4, _this$parent; 1320 1321 return (_this$masked4 = this.masked).doValidate.apply(_this$masked4, arguments) && (!this.parent || (_this$parent = this.parent).doValidate.apply(_this$parent, arguments)); 1322 } 1323 }, { 1324 key: "doCommit", 1325 value: function doCommit() { 1326 this.masked.doCommit(); 1327 } 1328 }, { 1329 key: "state", 1330 get: function get() { 1331 return { 1332 masked: this.masked.state, 1333 isFilled: this.isFilled 1334 }; 1335 }, 1336 set: function set(state) { 1337 this.masked.state = state.masked; 1338 this.isFilled = state.isFilled; 1339 } 1340 }]); 1341 1342 return PatternInputDefinition; 1343 }(); 1344 1345 var PatternFixedDefinition = /*#__PURE__*/function () { 1346 /** */ 1347 1348 /** */ 1349 1350 /** */ 1351 1352 /** */ 1353 1354 /** */ 1355 1356 /** */ 1357 function PatternFixedDefinition(opts) { 1358 _classCallCheck(this, PatternFixedDefinition); 1359 1360 Object.assign(this, opts); 1361 this._value = ''; 1362 this.isFixed = true; 1363 } 1364 1365 _createClass(PatternFixedDefinition, [{ 1366 key: "value", 1367 get: function get() { 1368 return this._value; 1369 } 1370 }, { 1371 key: "unmaskedValue", 1372 get: function get() { 1373 return this.isUnmasking ? this.value : ''; 1374 } 1375 }, { 1376 key: "reset", 1377 value: function reset() { 1378 this._isRawInput = false; 1379 this._value = ''; 1380 } 1381 }, { 1382 key: "remove", 1383 value: function remove() { 1384 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 1385 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this._value.length; 1386 this._value = this._value.slice(0, fromPos) + this._value.slice(toPos); 1387 if (!this._value) this._isRawInput = false; 1388 return new ChangeDetails(); 1389 } 1390 }, { 1391 key: "nearestInputPos", 1392 value: function nearestInputPos(cursorPos) { 1393 var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DIRECTION.NONE; 1394 var minPos = 0; 1395 var maxPos = this._value.length; 1396 1397 switch (direction) { 1398 case DIRECTION.LEFT: 1399 case DIRECTION.FORCE_LEFT: 1400 return minPos; 1401 1402 case DIRECTION.NONE: 1403 case DIRECTION.RIGHT: 1404 case DIRECTION.FORCE_RIGHT: 1405 default: 1406 return maxPos; 1407 } 1408 } 1409 }, { 1410 key: "extractInput", 1411 value: function extractInput() { 1412 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 1413 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this._value.length; 1414 var flags = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; 1415 return flags.raw && this._isRawInput && this._value.slice(fromPos, toPos) || ''; 1416 } 1417 }, { 1418 key: "isComplete", 1419 get: function get() { 1420 return true; 1421 } 1422 }, { 1423 key: "isFilled", 1424 get: function get() { 1425 return Boolean(this._value); 1426 } 1427 }, { 1428 key: "_appendChar", 1429 value: function _appendChar(ch) { 1430 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 1431 var details = new ChangeDetails(); 1432 if (this._value) return details; 1433 var appended = this.char === ch; 1434 var isResolved = appended && (this.isUnmasking || flags.input || flags.raw) && (!flags.raw || !this.eager) && !flags.tail; 1435 if (isResolved) details.rawInserted = this.char; 1436 this._value = details.inserted = this.char; 1437 this._isRawInput = isResolved && (flags.raw || flags.input); 1438 return details; 1439 } 1440 }, { 1441 key: "_appendEager", 1442 value: function _appendEager() { 1443 return this._appendChar(this.char, { 1444 tail: true 1445 }); 1446 } 1447 }, { 1448 key: "_appendPlaceholder", 1449 value: function _appendPlaceholder() { 1450 var details = new ChangeDetails(); 1451 if (this._value) return details; 1452 this._value = details.inserted = this.char; 1453 return details; 1454 } 1455 }, {
1456 key: "extractTail", 1457 value: function extractTail() { 1458 arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 1459 return new ContinuousTailDetails(''); 1460 } // $FlowFixMe no ideas 1461 1462 }, { 1463 key: "appendTail", 1464 value: function appendTail(tail) { 1465 if (isString(tail)) tail = new ContinuousTailDetails(String(tail)); 1466 return tail.appendTo(this); 1467 } 1468 }, { 1469 key: "append", 1470 value: function append(str, flags, tail) { 1471 var details = this._appendChar(str[0], flags); 1472 1473 if (tail != null) { 1474 details.tailShift += this.appendTail(tail).tailShift; 1475 } 1476 1477 return details; 1478 } 1479 }, { 1480 key: "doCommit", 1481 value: function doCommit() {} 1482 }, { 1483 key: "state", 1484 get: function get() { 1485 return { 1486 _value: this._value, 1487 _isRawInput: this._isRawInput 1488 }; 1489 }, 1490 set: function set(state) { 1491 Object.assign(this, state); 1492 } 1493 }]); 1494 1495 return PatternFixedDefinition; 1496 }(); 1497 1498 var _excluded$3 = ["chunks"]; 1499 1500 var ChunksTailDetails = /*#__PURE__*/function () { 1501 /** */ 1502 function ChunksTailDetails() { 1503 var chunks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; 1504 var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; 1505 1506 _classCallCheck(this, ChunksTailDetails); 1507 1508 this.chunks = chunks; 1509 this.from = from; 1510 } 1511 1512 _createClass(ChunksTailDetails, [{ 1513 key: "toString", 1514 value: function toString() { 1515 return this.chunks.map(String).join(''); 1516 } // $FlowFixMe no ideas 1517 1518 }, { 1519 key: "extend", 1520 value: function extend(tailChunk) { 1521 if (!String(tailChunk)) return; 1522 if (isString(tailChunk)) tailChunk = new ContinuousTailDetails(String(tailChunk)); 1523 var lastChunk = this.chunks[this.chunks.length - 1]; 1524 var extendLast = lastChunk && ( // if stops are same or tail has no stop 1525 lastChunk.stop === tailChunk.stop || tailChunk.stop == null) && // if tail chunk goes just after last chunk 1526 tailChunk.from === lastChunk.from + lastChunk.toString().length; 1527 1528 if (tailChunk instanceof ContinuousTailDetails) { 1529 // check the ability to extend previous chunk 1530 if (extendLast) { 1531 // extend previous chunk 1532 lastChunk.extend(tailChunk.toString()); 1533 } else { 1534 // append new chunk 1535 this.chunks.push(tailChunk); 1536 } 1537 } else if (tailChunk instanceof ChunksTailDetails) { 1538 if (tailChunk.stop == null) { 1539 // unwrap floating chunks to parent, keeping `from` pos 1540 var firstTailChunk; 1541 1542 while (tailChunk.chunks.length && tailChunk.chunks[0].stop == null) { 1543 firstTailChunk = tailChunk.chunks.shift(); 1544 firstTailChunk.from += tailChunk.from; 1545 this.extend(firstTailChunk); 1546 } 1547 } // if tail chunk still has value 1548 1549 1550 if (tailChunk.toString()) { 1551 // if chunks contains stops, then popup stop to container 1552 tailChunk.stop = tailChunk.blockIndex; 1553 this.chunks.push(tailChunk); 1554 } 1555 } 1556 } 1557 }, { 1558 key: "appendTo", 1559 value: function appendTo(masked) { 1560 // $FlowFixMe 1561 if (!(masked instanceof IMask.MaskedPattern)) { 1562 var tail = new ContinuousTailDetails(this.toString()); 1563 return tail.appendTo(masked); 1564 } 1565 1566 var details = new ChangeDetails(); 1567 1568 for (var ci = 0; ci < this.chunks.length && !details.skip; ++ci) { 1569 var chunk = this.chunks[ci]; 1570 1571 var lastBlockIter = masked._mapPosToBlock(masked.value.length); 1572 1573 var stop = chunk.stop; 1574 var chunkBlock = void 0; 1575 1576 if (stop != null && ( // if block not found or stop is behind lastBlock 1577 !lastBlockIter || lastBlockIter.index <= stop)) { 1578 if (chunk instanceof ChunksTailDetails || // for continuous block also check if stop is exist 1579 masked._stops.indexOf(stop) >= 0) { 1580 details.aggregate(masked._appendPlaceholder(stop)); 1581 } 1582 1583 chunkBlock = chunk instanceof ChunksTailDetails && masked._blocks[stop]; 1584 } 1585 1586 if (chunkBlock) { 1587 var tailDetails = chunkBlock.appendTail(chunk); 1588 tailDetails.skip = false; // always ignore skip, it will be set on last 1589 1590 details.aggregate(tailDetails); 1591 masked._value += tailDetails.inserted; // get not inserted chars 1592
1593 var remainChars = chunk.toString().slice(tailDetails.rawInserted.length); 1594 if (remainChars) details.aggregate(masked.append(remainChars, { 1595 tail: true 1596 })); 1597 } else { 1598 details.aggregate(masked.append(chunk.toString(), { 1599 tail: true 1600 })); 1601 } 1602 } 1603 return details; 1604 } 1605 }, { 1606 key: "state", 1607 get: function get() { 1608 return { 1609 chunks: this.chunks.map(function (c) { 1610 return c.state; 1611 }), 1612 from: this.from, 1613 stop: this.stop, 1614 blockIndex: this.blockIndex 1615 }; 1616 }, 1617 set: function set(state) { 1618 var chunks = state.chunks, 1619 props = _objectWithoutProperties(state, _excluded$3); 1620 1621 Object.assign(this, props); 1622 this.chunks = chunks.map(function (cstate) { 1623 var chunk = "chunks" in cstate ? new ChunksTailDetails() : new ContinuousTailDetails(); // $FlowFixMe already checked above 1624 1625 chunk.state = cstate; 1626 return chunk; 1627 }); 1628 } 1629 }, { 1630 key: "unshift", 1631 value: function unshift(beforePos) { 1632 if (!this.chunks.length || beforePos != null && this.from >= beforePos) return ''; 1633 var chunkShiftPos = beforePos != null ? beforePos - this.from : beforePos; 1634 var ci = 0; 1635 1636 while (ci < this.chunks.length) { 1637 var chunk = this.chunks[ci]; 1638 var shiftChar = chunk.unshift(chunkShiftPos); 1639 1640 if (chunk.toString()) { 1641 // chunk still contains value 1642 // but not shifted - means no more available chars to shift 1643 if (!shiftChar) break; 1644 ++ci; 1645 } else { 1646 // clean if chunk has no value 1647 this.chunks.splice(ci, 1); 1648 } 1649 1650 if (shiftChar) return shiftChar; 1651 } 1652 1653 return ''; 1654 } 1655 }, { 1656 key: "shift", 1657 value: function shift() { 1658 if (!this.chunks.length) return ''; 1659 var ci = this.chunks.length - 1; 1660 1661 while (0 <= ci) { 1662 var chunk = this.chunks[ci]; 1663 var shiftChar = chunk.shift(); 1664 1665 if (chunk.toString()) { 1666 // chunk still contains value 1667 // but not shifted - means no more available chars to shift 1668 if (!shiftChar) break; 1669 --ci; 1670 } else { 1671 // clean if chunk has no value 1672 this.chunks.splice(ci, 1); 1673 } 1674 1675 if (shiftChar) return shiftChar; 1676 } 1677 1678 return ''; 1679 } 1680 }]); 1681 1682 return ChunksTailDetails; 1683 }(); 1684 1685 var PatternCursor = /*#__PURE__*/function () { 1686 function PatternCursor(masked, pos) { 1687 _classCallCheck(this, PatternCursor); 1688 1689 this.masked = masked; 1690 this._log = []; 1691 1692 var _ref = masked._mapPosToBlock(pos) || (pos < 0 ? // first 1693 { 1694 index: 0, 1695 offset: 0 1696 } : // last 1697 { 1698 index: this.masked._blocks.length, 1699 offset: 0 1700 }), 1701 offset = _ref.offset, 1702 index = _ref.index; 1703 1704 this.offset = offset; 1705 this.index = index; 1706 this.ok = false; 1707 } 1708 1709 _createClass(PatternCursor, [{ 1710 key: "block", 1711 get: function get() { 1712 return this.masked._blocks[this.index]; 1713 } 1714 }, { 1715 key: "pos", 1716 get: function get() { 1717 return this.masked._blockStartPos(this.index) + this.offset; 1718 } 1719 }, { 1720 key: "state", 1721 get: function get() { 1722 return { 1723 index: this.index, 1724 offset: this.offset, 1725 ok: this.ok 1726 }; 1727 }, 1728 set: function set(s) { 1729 Object.assign(this, s); 1730 } 1731 }, { 1732 key: "pushState", 1733 value: function pushState() { 1734 this._log.push(this.state); 1735 } 1736 }, { 1737 key: "popState", 1738 value: function popState() { 1739 var s = this._log.pop(); 1740 1741 this.state = s; 1742 return s; 1743 } 1744 }, { 1745 key: "bindBlock", 1746 value: function bindBlock() { 1747 if (this.block) return; 1748 1749 if (this.index < 0) { 1750 this.index = 0; 1751 this.offset = 0; 1752 } 1753 1754 if (this.index >= this.masked._blocks.length) { 1755 this.index = this.masked._blocks.length - 1; 1756 this.offset = this.block.value.length; 1757 } 1758 } 1759 }, {
1760 key: "_pushLeft", 1761 value: function _pushLeft(fn) { 1762 this.pushState(); 1763 1764 for (this.bindBlock(); 0 <= this.index; --this.index, this.offset = ((_this$block = this.block) === null || _this$block === void 0 ? void 0 : _this$block.value.length) || 0) { 1765 var _this$block; 1766 1767 if (fn()) return this.ok = true; 1768 } 1769 1770 return this.ok = false; 1771 } 1772 }, { 1773 key: "_pushRight", 1774 value: function _pushRight(fn) { 1775 this.pushState(); 1776 1777 for (this.bindBlock(); this.index < this.masked._blocks.length; ++this.index, this.offset = 0) { 1778 if (fn()) return this.ok = true; 1779 } 1780 1781 return this.ok = false; 1782 } 1783 }, { 1784 key: "pushLeftBeforeFilled", 1785 value: function pushLeftBeforeFilled() { 1786 var _this = this; 1787 1788 return this._pushLeft(function () { 1789 if (_this.block.isFixed || !_this.block.value) return; 1790 _this.offset = _this.block.nearestInputPos(_this.offset, DIRECTION.FORCE_LEFT); 1791 if (_this.offset !== 0) return true; 1792 }); 1793 } 1794 }, { 1795 key: "pushLeftBeforeInput", 1796 value: function pushLeftBeforeInput() { 1797 var _this2 = this; 1798 1799 // cases: 1800 // filled input: 00| 1801 // optional empty input: 00[]| 1802 // nested block: XX<[]>| 1803 return this._pushLeft(function () { 1804 if (_this2.block.isFixed) return; 1805 _this2.offset = _this2.block.nearestInputPos(_this2.offset, DIRECTION.LEFT); 1806 return true; 1807 }); 1808 } 1809 }, { 1810 key: "pushLeftBeforeRequired", 1811 value: function pushLeftBeforeRequired() { 1812 var _this3 = this; 1813 1814 return this._pushLeft(function () { 1815 if (_this3.block.isFixed || _this3.block.isOptional && !_this3.block.value) return; 1816 _this3.offset = _this3.block.nearestInputPos(_this3.offset, DIRECTION.LEFT); 1817 return true; 1818 }); 1819 } 1820 }, { 1821 key: "pushRightBeforeFilled", 1822 value: function pushRightBeforeFilled() { 1823 var _this4 = this; 1824 1825 return this._pushRight(function () { 1826 if (_this4.block.isFixed || !_this4.block.value) return; 1827 _this4.offset = _this4.block.nearestInputPos(_this4.offset, DIRECTION.FORCE_RIGHT); 1828 if (_this4.offset !== _this4.block.value.length) return true; 1829 }); 1830 } 1831 }, { 1832 key: "pushRightBeforeInput", 1833 value: function pushRightBeforeInput() { 1834 var _this5 = this; 1835 1836 return this._pushRight(function () { 1837 if (_this5.block.isFixed) return; // const o = this.offset; 1838 1839 _this5.offset = _this5.block.nearestInputPos(_this5.offset, DIRECTION.NONE); // HACK cases like (STILL DOES NOT WORK FOR NESTED) 1840 // aa|X 1841 // aa<X|[]>X_ - this will not work 1842 // if (o && o === this.offset && this.block instanceof PatternInputDefinition) continue; 1843 1844 return true; 1845 }); 1846 } 1847 }, { 1848 key: "pushRightBeforeRequired", 1849 value: function pushRightBeforeRequired() { 1850 var _this6 = this; 1851 1852 return this._pushRight(function () { 1853 if (_this6.block.isFixed || _this6.block.isOptional && !_this6.block.value) return; // TODO check |[*]XX_ 1854 1855 _this6.offset = _this6.block.nearestInputPos(_this6.offset, DIRECTION.NONE); 1856 return true; 1857 }); 1858 } 1859 }]); 1860 1861 return PatternCursor; 1862 }(); 1863 1864 /** Masking by RegExp */ 1865 1866 var MaskedRegExp = /*#__PURE__*/function (_Masked) { 1867 _inherits(MaskedRegExp, _Masked); 1868 1869 var _super = _createSuper(MaskedRegExp); 1870 1871 function MaskedRegExp() { 1872 _classCallCheck(this, MaskedRegExp); 1873 1874 return _super.apply(this, arguments); 1875 } 1876 1877 _createClass(MaskedRegExp, [{ 1878 key: "_update", 1879 value: 1880 /** 1881 @override 1882 @param {Object} opts 1883 */ 1884 function _update(opts) { 1885 if (opts.mask) opts.validate = function (value) { 1886 return value.search(opts.mask) >= 0; 1887 }; 1888 1889 _get(_getPrototypeOf(MaskedRegExp.prototype), "_update", this).call(this, opts); 1890 } 1891 }]); 1892 1893 return MaskedRegExp; 1894 }(Masked); 1895 IMask.MaskedRegExp = MaskedRegExp; 1896 1897 var _excluded$2 = ["_blocks"]; 1898 1899 /** 1900 Pattern mask 1901 @param {Object} opts 1902 @param {Object} opts.blocks 1903 @param {Object} opts.definitions 1904 @param {string} opts.placeholderChar 1905 @param {boolean} opts.lazy 1906 */ 1907 var MaskedPattern = /*#__PURE__*/function (_Masked) { 1908 _inherits(MaskedPattern, _Masked); 1909 1910 var _super = _createSuper(MaskedPattern); 1911 1912 /** */ 1913 1914 /** */ 1915 1916 /** Single char for empty input */ 1917 1918 /** Show placeholder only when needed */ 1919 function MaskedPattern() { 1920 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 1921 1922 _classCallCheck(this, MaskedPattern); 1923 1924 // TODO type $Shape<MaskedPatternOptions>={} does not work 1925 opts.definitions = Object.assign({}, DEFAULT_INPUT_DEFINITIONS, opts.definitions); 1926 return _super.call(this, Object.assign({}, MaskedPattern.DEFAULTS, opts)); 1927 } 1928 /** 1929 @override 1930 @param {Object} opts 1931 */ 1932 1933 1934 _createClass(MaskedPattern, [{ 1935 key: "_update", 1936 value: function _update() { 1937 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 1938 opts.definitions = Object.assign({}, this.definitions, opts.definitions); 1939 1940 _get(_getPrototypeOf(MaskedPattern.prototype), "_update", this).call(this, opts); 1941 1942 this._rebuildMask(); 1943 } 1944 /** */ 1945 1946 }, {
1947 key: "_rebuildMask", 1948 value: function _rebuildMask() { 1949 var _this = this; 1950 1951 var defs = this.definitions; 1952 this._blocks = []; 1953 this._stops = []; 1954 this._maskedBlocks = {}; 1955 var pattern = this.mask; 1956 if (!pattern || !defs) return; 1957 var unmaskingBlock = false; 1958 var optionalBlock = false; 1959 1960 for (var i = 0; i < pattern.length; ++i) { 1961 if (this.blocks) { 1962 var _ret = function () { 1963 var p = pattern.slice(i); 1964 var bNames = Object.keys(_this.blocks).filter(function (bName) { 1965 return p.indexOf(bName) === 0; 1966 }); // order by key length 1967 1968 bNames.sort(function (a, b) { 1969 return b.length - a.length; 1970 }); // use block name with max length 1971 1972 var bName = bNames[0]; 1973 1974 if (bName) { 1975 // $FlowFixMe no ideas 1976 var maskedBlock = createMask(Object.assign({ 1977 parent: _this, 1978 lazy: _this.lazy, 1979 eager: _this.eager, 1980 placeholderChar: _this.placeholderChar, 1981 overwrite: _this.overwrite 1982 }, _this.blocks[bName])); 1983 1984 if (maskedBlock) { 1985 _this._blocks.push(maskedBlock); // store block index 1986 1987 1988 if (!_this._maskedBlocks[bName]) _this._maskedBlocks[bName] = []; 1989 1990 _this._maskedBlocks[bName].push(_this._blocks.length - 1); 1991 } 1992 1993 i += bName.length - 1; 1994 return "continue"; 1995 } 1996 }(); 1997 1998 if (_ret === "continue") continue; 1999 } 2000 2001 var char = pattern[i]; 2002 var isInput = (char in defs); 2003 2004 if (char === MaskedPattern.STOP_CHAR) { 2005 this._stops.push(this._blocks.length); 2006 2007 continue; 2008 } 2009 2010 if (char === '{' || char === '}') { 2011 unmaskingBlock = !unmaskingBlock; 2012 continue; 2013 } 2014 2015 if (char === '[' || char === ']') { 2016 optionalBlock = !optionalBlock; 2017 continue; 2018 } 2019 2020 if (char === MaskedPattern.ESCAPE_CHAR) { 2021 ++i; 2022 char = pattern[i]; 2023 if (!char) break; 2024 isInput = false; 2025 } 2026 2027 var def = isInput ? new PatternInputDefinition({ 2028 parent: this, 2029 lazy: this.lazy, 2030 eager: this.eager, 2031 placeholderChar: this.placeholderChar, 2032 mask: defs[char], 2033 isOptional: optionalBlock 2034 }) : new PatternFixedDefinition({ 2035 char: char, 2036 eager: this.eager, 2037 isUnmasking: unmaskingBlock 2038 }); 2039 2040 this._blocks.push(def); 2041 } 2042 } 2043 /** 2044 @override 2045 */ 2046 2047 }, { 2048 key: "state", 2049 get: function get() { 2050 return Object.assign({}, _get(_getPrototypeOf(MaskedPattern.prototype), "state", this), { 2051 _blocks: this._blocks.map(function (b) { 2052 return b.state; 2053 }) 2054 }); 2055 }, 2056 set: function set(state) { 2057 var _blocks = state._blocks, 2058 maskedState = _objectWithoutProperties(state, _excluded$2); 2059 2060 this._blocks.forEach(function (b, bi) { 2061 return b.state = _blocks[bi]; 2062 }); 2063 2064 _set(_getPrototypeOf(MaskedPattern.prototype), "state", maskedState, this, true); 2065 } 2066 /** 2067 @override 2068 */ 2069 2070 }, { 2071 key: "reset", 2072 value: function reset() { 2073 _get(_getPrototypeOf(MaskedPattern.prototype), "reset", this).call(this); 2074 2075 this._blocks.forEach(function (b) { 2076 return b.reset(); 2077 }); 2078 } 2079 /** 2080 @override 2081 */ 2082 2083 }, { 2084 key: "isComplete", 2085 get: function get() { 2086 return this._blocks.every(function (b) { 2087 return b.isComplete; 2088 }); 2089 } 2090 /** 2091 @override 2092 */ 2093 2094 }, { 2095 key: "isFilled", 2096 get: function get() { 2097 return this._blocks.every(function (b) { 2098 return b.isFilled; 2099 }); 2100 } 2101 }, {
2102 key: "isFixed", 2103 get: function get() { 2104 return this._blocks.every(function (b) { 2105 return b.isFixed; 2106 }); 2107 } 2108 }, { 2109 key: "isOptional", 2110 get: function get() { 2111 return this._blocks.every(function (b) { 2112 return b.isOptional; 2113 }); 2114 } 2115 /** 2116 @override 2117 */ 2118 2119 }, { 2120 key: "doCommit", 2121 value: function doCommit() { 2122 this._blocks.forEach(function (b) { 2123 return b.doCommit(); 2124 }); 2125 2126 _get(_getPrototypeOf(MaskedPattern.prototype), "doCommit", this).call(this); 2127 } 2128 /** 2129 @override 2130 */ 2131 2132 }, { 2133 key: "unmaskedValue", 2134 get: function get() { 2135 return this._blocks.reduce(function (str, b) { 2136 return str += b.unmaskedValue; 2137 }, ''); 2138 }, 2139 set: function set(unmaskedValue) { 2140 _set(_getPrototypeOf(MaskedPattern.prototype), "unmaskedValue", unmaskedValue, this, true); 2141 } 2142 /** 2143 @override 2144 */ 2145 2146 }, { 2147 key: "value", 2148 get: function get() { 2149 // TODO return _value when not in change? 2150 return this._blocks.reduce(function (str, b) { 2151 return str += b.value; 2152 }, ''); 2153 }, 2154 set: function set(value) { 2155 _set(_getPrototypeOf(MaskedPattern.prototype), "value", value, this, true); 2156 } 2157 /** 2158 @override 2159 */ 2160 2161 }, { 2162 key: "appendTail", 2163 value: function appendTail(tail) { 2164 return _get(_getPrototypeOf(MaskedPattern.prototype), "appendTail", this).call(this, tail).aggregate(this._appendPlaceholder()); 2165 } 2166 /** 2167 @override 2168 */ 2169 2170 }, { 2171 key: "_appendEager", 2172 value: function _appendEager() { 2173 var _this$_mapPosToBlock; 2174 2175 var details = new ChangeDetails(); 2176 var startBlockIndex = (_this$_mapPosToBlock = this._mapPosToBlock(this.value.length)) === null || _this$_mapPosToBlock === void 0 ? void 0 : _this$_mapPosToBlock.index; 2177 if (startBlockIndex == null) return details; // TODO test if it works for nested pattern masks 2178 2179 if (this._blocks[startBlockIndex].isFilled) ++startBlockIndex; 2180 2181 for (var bi = startBlockIndex; bi < this._blocks.length; ++bi) { 2182 var d = this._blocks[bi]._appendEager(); 2183 2184 if (!d.inserted) break; 2185 details.aggregate(d); 2186 } 2187 2188 return details; 2189 } 2190 /** 2191 @override 2192 */ 2193 2194 }, { 2195 key: "_appendCharRaw", 2196 value: function _appendCharRaw(ch) { 2197 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 2198 2199 var blockIter = this._mapPosToBlock(this.value.length); 2200 2201 var details = new ChangeDetails(); 2202 if (!blockIter) return details; 2203 2204 for (var bi = blockIter.index;; ++bi) { 2205 var _flags$_beforeTailSta, _flags$_beforeTailSta2; 2206 2207 var _block = this._blocks[bi]; 2208 if (!_block) break; 2209 2210 var blockDetails = _block._appendChar(ch, Object.assign({}, flags, { 2211 _beforeTailState: (_flags$_beforeTailSta = flags._beforeTailState) === null || _flags$_beforeTailSta === void 0 ? void 0 : (_flags$_beforeTailSta2 = _flags$_beforeTailSta._blocks) === null || _flags$_beforeTailSta2 === void 0 ? void 0 : _flags$_beforeTailSta2[bi] 2212 })); 2213 2214 var skip = blockDetails.skip; 2215 details.aggregate(blockDetails); 2216 if (skip || blockDetails.rawInserted) break; // go next char 2217 } 2218 2219 return details; 2220 } 2221 /** 2222 @override 2223 */ 2224 2225 }, { 2226 key: "extractTail", 2227 value: function extractTail() { 2228 var _this2 = this; 2229 2230 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 2231 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 2232 var chunkTail = new ChunksTailDetails(); 2233 if (fromPos === toPos) return chunkTail; 2234 2235 this._forEachBlocksInRange(fromPos, toPos, function (b, bi, bFromPos, bToPos) { 2236 var blockChunk = b.extractTail(bFromPos, bToPos); 2237 blockChunk.stop = _this2._findStopBefore(bi); 2238 blockChunk.from = _this2._blockStartPos(bi); 2239 if (blockChunk instanceof ChunksTailDetails) blockChunk.blockIndex = bi; 2240 chunkTail.extend(blockChunk); 2241 }); 2242 2243 return chunkTail; 2244 } 2245 /** 2246 @override 2247 */ 2248 2249 }, {
2250 key: "extractInput", 2251 value: function extractInput() { 2252 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 2253 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 2254 var flags = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; 2255 if (fromPos === toPos) return ''; 2256 var input = ''; 2257 2258 this._forEachBlocksInRange(fromPos, toPos, function (b, _, fromPos, toPos) { 2259 input += b.extractInput(fromPos, toPos, flags); 2260 }); 2261 2262 return input; 2263 } 2264 }, { 2265 key: "_findStopBefore", 2266 value: function _findStopBefore(blockIndex) { 2267 var stopBefore; 2268 2269 for (var si = 0; si < this._stops.length; ++si) { 2270 var stop = this._stops[si]; 2271 if (stop <= blockIndex) stopBefore = stop;else break; 2272 } 2273 2274 return stopBefore; 2275 } 2276 /** Appends placeholder depending on laziness */ 2277 2278 }, { 2279 key: "_appendPlaceholder", 2280 value: function _appendPlaceholder(toBlockIndex) { 2281 var _this3 = this; 2282 2283 var details = new ChangeDetails(); 2284 if (this.lazy && toBlockIndex == null) return details; 2285 2286 var startBlockIter = this._mapPosToBlock(this.value.length); 2287 2288 if (!startBlockIter) return details; 2289 var startBlockIndex = startBlockIter.index; 2290 var endBlockIndex = toBlockIndex != null ? toBlockIndex : this._blocks.length; 2291 2292 this._blocks.slice(startBlockIndex, endBlockIndex).forEach(function (b) { 2293 if (!b.lazy || toBlockIndex != null) { 2294 // $FlowFixMe `_blocks` may not be present 2295 var args = b._blocks != null ? [b._blocks.length] : []; 2296 2297 var bDetails = b._appendPlaceholder.apply(b, args); 2298 2299 _this3._value += bDetails.inserted; 2300 details.aggregate(bDetails); 2301 } 2302 }); 2303 2304 return details; 2305 } 2306 /** Finds block in pos */ 2307 2308 }, { 2309 key: "_mapPosToBlock", 2310 value: function _mapPosToBlock(pos) { 2311 var accVal = ''; 2312 2313 for (var bi = 0; bi < this._blocks.length; ++bi) { 2314 var _block2 = this._blocks[bi]; 2315 var blockStartPos = accVal.length; 2316 accVal += _block2.value; 2317 2318 if (pos <= accVal.length) { 2319 return { 2320 index: bi, 2321 offset: pos - blockStartPos 2322 }; 2323 } 2324 } 2325 } 2326 /** */ 2327 2328 }, { 2329 key: "_blockStartPos", 2330 value: function _blockStartPos(blockIndex) { 2331 return this._blocks.slice(0, blockIndex).reduce(function (pos, b) { 2332 return pos += b.value.length; 2333 }, 0); 2334 } 2335 /** */ 2336 2337 }, { 2338 key: "_forEachBlocksInRange", 2339 value: function _forEachBlocksInRange(fromPos) { 2340 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 2341 var fn = arguments.length > 2 ? arguments[2] : undefined; 2342 2343 var fromBlockIter = this._mapPosToBlock(fromPos); 2344 2345 if (fromBlockIter) { 2346 var toBlockIter = this._mapPosToBlock(toPos); // process first block 2347 2348 2349 var isSameBlock = toBlockIter && fromBlockIter.index === toBlockIter.index; 2350 var fromBlockStartPos = fromBlockIter.offset; 2351 var fromBlockEndPos = toBlockIter && isSameBlock ? toBlockIter.offset : this._blocks[fromBlockIter.index].value.length; 2352 fn(this._blocks[fromBlockIter.index], fromBlockIter.index, fromBlockStartPos, fromBlockEndPos); 2353 2354 if (toBlockIter && !isSameBlock) { 2355 // process intermediate blocks 2356 for (var bi = fromBlockIter.index + 1; bi < toBlockIter.index; ++bi) { 2357 fn(this._blocks[bi], bi, 0, this._blocks[bi].value.length); 2358 } // process last block 2359 2360 2361 fn(this._blocks[toBlockIter.index], toBlockIter.index, 0, toBlockIter.offset); 2362 } 2363 } 2364 } 2365 /** 2366 @override 2367 */ 2368 2369 }, { 2370 key: "remove", 2371 value: function remove() { 2372 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 2373 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 2374 2375 var removeDetails = _get(_getPrototypeOf(MaskedPattern.prototype), "remove", this).call(this, fromPos, toPos); 2376
2377 this._forEachBlocksInRange(fromPos, toPos, function (b, _, bFromPos, bToPos) { 2378 removeDetails.aggregate(b.remove(bFromPos, bToPos)); 2379 }); 2380 2381 return removeDetails; 2382 } 2383 /** 2384 @override 2385 */ 2386 2387 }, { 2388 key: "nearestInputPos", 2389 value: function nearestInputPos(cursorPos) { 2390 var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DIRECTION.NONE; 2391 if (!this._blocks.length) return 0; 2392 var cursor = new PatternCursor(this, cursorPos); 2393 2394 if (direction === DIRECTION.NONE) { 2395 // ------------------------------------------------- 2396 // NONE should only go out from fixed to the right! 2397 // ------------------------------------------------- 2398 if (cursor.pushRightBeforeInput()) return cursor.pos; 2399 cursor.popState(); 2400 if (cursor.pushLeftBeforeInput()) return cursor.pos; 2401 return this.value.length; 2402 } // FORCE is only about a|* otherwise is 0 2403 2404 2405 if (direction === DIRECTION.LEFT || direction === DIRECTION.FORCE_LEFT) { 2406 // try to break fast when *|a 2407 if (direction === DIRECTION.LEFT) { 2408 cursor.pushRightBeforeFilled(); 2409 if (cursor.ok && cursor.pos === cursorPos) return cursorPos; 2410 cursor.popState(); 2411 } // forward flow 2412 2413 2414 cursor.pushLeftBeforeInput(); 2415 cursor.pushLeftBeforeRequired(); 2416 cursor.pushLeftBeforeFilled(); // backward flow 2417 2418 if (direction === DIRECTION.LEFT) { 2419 cursor.pushRightBeforeInput(); 2420 cursor.pushRightBeforeRequired(); 2421 if (cursor.ok && cursor.pos <= cursorPos) return cursor.pos; 2422 cursor.popState(); 2423 if (cursor.ok && cursor.pos <= cursorPos) return cursor.pos; 2424 cursor.popState(); 2425 } 2426 2427 if (cursor.ok) return cursor.pos; 2428 if (direction === DIRECTION.FORCE_LEFT) return 0; 2429 cursor.popState(); 2430 if (cursor.ok) return cursor.pos; 2431 cursor.popState(); 2432 if (cursor.ok) return cursor.pos; // cursor.popState(); 2433 // if ( 2434 // cursor.pushRightBeforeInput() && 2435 // // TODO HACK for lazy if has aligned left inside fixed and has came to the start - use start position 2436 // (!this.lazy || this.extractInput()) 2437 // ) return cursor.pos; 2438 2439 return 0; 2440 } 2441 2442 if (direction === DIRECTION.RIGHT || direction === DIRECTION.FORCE_RIGHT) { 2443 // forward flow 2444 cursor.pushRightBeforeInput(); 2445 cursor.pushRightBeforeRequired(); 2446 if (cursor.pushRightBeforeFilled()) return cursor.pos; 2447 if (direction === DIRECTION.FORCE_RIGHT) return this.value.length; // backward flow 2448 2449 cursor.popState(); 2450 if (cursor.ok) return cursor.pos; 2451 cursor.popState(); 2452 if (cursor.ok) return cursor.pos; 2453 return this.nearestInputPos(cursorPos, DIRECTION.LEFT); 2454 } 2455 2456 return cursorPos; 2457 } 2458 /** Get block by name */ 2459 2460 }, { 2461 key: "maskedBlock", 2462 value: function maskedBlock(name) { 2463 return this.maskedBlocks(name)[0]; 2464 } 2465 /** Get all blocks by name */ 2466 2467 }, { 2468 key: "maskedBlocks", 2469 value: function maskedBlocks(name) { 2470 var _this4 = this; 2471 2472 var indices = this._maskedBlocks[name]; 2473 if (!indices) return []; 2474 return indices.map(function (gi) { 2475 return _this4._blocks[gi]; 2476 }); 2477 } 2478 }]); 2479 2480 return MaskedPattern; 2481 }(Masked); 2482 MaskedPattern.DEFAULTS = { 2483 lazy: true, 2484 placeholderChar: '_' 2485 }; 2486 MaskedPattern.STOP_CHAR = '`'; 2487 MaskedPattern.ESCAPE_CHAR = '\\'; 2488 MaskedPattern.InputDefinition = PatternInputDefinition; 2489 MaskedPattern.FixedDefinition = PatternFixedDefinition; 2490 IMask.MaskedPattern = MaskedPattern; 2491 2492 /** Pattern which accepts ranges */ 2493 2494 var MaskedRange = /*#__PURE__*/function (_MaskedPattern) { 2495 _inherits(MaskedRange, _MaskedPattern); 2496 2497 var _super = _createSuper(MaskedRange); 2498 2499 function MaskedRange() { 2500 _classCallCheck(this, MaskedRange); 2501 2502 return _super.apply(this, arguments); 2503 } 2504 2505 _createClass(MaskedRange, [{ 2506 key: "_matchFrom", 2507 get: 2508 /** 2509 Optionally sets max length of pattern. 2510 Used when pattern length is longer then `to` param length. Pads zeros at start in this case. 2511 */ 2512 2513 /** Min bound */ 2514 2515 /** Max bound */ 2516 2517 /** */ 2518 function get() { 2519 return this.maxLength - String(this.from).length; 2520 } 2521 /** 2522 @override 2523 */ 2524 2525 }, {
2526 key: "_update", 2527 value: function _update(opts) { 2528 // TODO type 2529 opts = Object.assign({ 2530 to: this.to || 0, 2531 from: this.from || 0, 2532 maxLength: this.maxLength || 0 2533 }, opts); 2534 var maxLength = String(opts.to).length; 2535 if (opts.maxLength != null) maxLength = Math.max(maxLength, opts.maxLength); 2536 opts.maxLength = maxLength; 2537 var fromStr = String(opts.from).padStart(maxLength, '0'); 2538 var toStr = String(opts.to).padStart(maxLength, '0'); 2539 var sameCharsCount = 0; 2540 2541 while (sameCharsCount < toStr.length && toStr[sameCharsCount] === fromStr[sameCharsCount]) { 2542 ++sameCharsCount; 2543 } 2544 2545 opts.mask = toStr.slice(0, sameCharsCount).replace(/0/g, '\\0') + '0'.repeat(maxLength - sameCharsCount); 2546 2547 _get(_getPrototypeOf(MaskedRange.prototype), "_update", this).call(this, opts); 2548 } 2549 /** 2550 @override 2551 */ 2552 2553 }, { 2554 key: "isComplete", 2555 get: function get() { 2556 return _get(_getPrototypeOf(MaskedRange.prototype), "isComplete", this) && Boolean(this.value); 2557 } 2558 }, { 2559 key: "boundaries", 2560 value: function boundaries(str) { 2561 var minstr = ''; 2562 var maxstr = ''; 2563 2564 var _ref = str.match(/^(\D*)(\d*)(\D*)/) || [], 2565 _ref2 = _slicedToArray(_ref, 3), 2566 placeholder = _ref2[1], 2567 num = _ref2[2]; 2568 2569 if (num) { 2570 minstr = '0'.repeat(placeholder.length) + num; 2571 maxstr = '9'.repeat(placeholder.length) + num; 2572 } 2573 2574 minstr = minstr.padEnd(this.maxLength, '0'); 2575 maxstr = maxstr.padEnd(this.maxLength, '9'); 2576 return [minstr, maxstr]; 2577 } // TODO str is a single char everytime 2578 2579 /** 2580 @override 2581 */ 2582 2583 }, { 2584 key: "doPrepare", 2585 value: function doPrepare(ch) { 2586 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 2587 var details; 2588 2589 var _normalizePrepare = normalizePrepare(_get(_getPrototypeOf(MaskedRange.prototype), "doPrepare", this).call(this, ch.replace(/\D/g, ''), flags)); 2590 2591 var _normalizePrepare2 = _slicedToArray(_normalizePrepare, 2); 2592 2593 ch = _normalizePrepare2[0]; 2594 details = _normalizePrepare2[1]; 2595 if (!this.autofix || !ch) return ch; 2596 var fromStr = String(this.from).padStart(this.maxLength, '0'); 2597 var toStr = String(this.to).padStart(this.maxLength, '0'); 2598 var nextVal = this.value + ch;
2599 if (nextVal.length > this.maxLength) return ''; 2600 2601 var _this$boundaries = this.boundaries(nextVal), 2602 _this$boundaries2 = _slicedToArray(_this$boundaries, 2), 2603 minstr = _this$boundaries2[0], 2604 maxstr = _this$boundaries2[1]; 2605 2606 if (Number(maxstr) < this.from) return fromStr[nextVal.length - 1]; 2607 2608 if (Number(minstr) > this.to) { 2609 if (this.autofix === 'pad' && nextVal.length < this.maxLength) { 2610 return ['', details.aggregate(this.append(fromStr[nextVal.length - 1] + ch, flags))]; 2611 } 2612 2613 return toStr[nextVal.length - 1]; 2614 } 2615 2616 return ch; 2617 } 2618 /** 2619 @override 2620 */ 2621 2622 }, { 2623 key: "doValidate", 2624 value: function doValidate() { 2625 var _get2; 2626 2627 var str = this.value; 2628 var firstNonZero = str.search(/[^0]/); 2629 if (firstNonZero === -1 && str.length <= this._matchFrom) return true; 2630 2631 var _this$boundaries3 = this.boundaries(str), 2632 _this$boundaries4 = _slicedToArray(_this$boundaries3, 2), 2633 minstr = _this$boundaries4[0], 2634 maxstr = _this$boundaries4[1]; 2635 2636 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { 2637 args[_key] = arguments[_key]; 2638 } 2639 2640 return this.from <= Number(maxstr) && Number(minstr) <= this.to && (_get2 = _get(_getPrototypeOf(MaskedRange.prototype), "doValidate", this)).call.apply(_get2, [this].concat(args)); 2641 } 2642 }]); 2643 2644 return MaskedRange; 2645 }(MaskedPattern); 2646 IMask.MaskedRange = MaskedRange; 2647 2648 /** Date mask */ 2649 2650 var MaskedDate = /*#__PURE__*/function (_MaskedPattern) { 2651 _inherits(MaskedDate, _MaskedPattern); 2652 2653 var _super = _createSuper(MaskedDate); 2654 2655 /** Pattern mask for date according to {@link MaskedDate#format} */ 2656 2657 /** Start date */ 2658 2659 /** End date */ 2660 2661 /** */ 2662 2663 /** 2664 @param {Object} opts 2665 */ 2666 function MaskedDate(opts) { 2667 _classCallCheck(this, MaskedDate); 2668 2669 return _super.call(this, Object.assign({}, MaskedDate.DEFAULTS, opts)); 2670 } 2671 /** 2672 @override 2673 */ 2674 2675 2676 _createClass(MaskedDate, [{ 2677 key: "_update", 2678 value: function _update(opts) { 2679 if (opts.mask === Date) delete opts.mask; 2680 if (opts.pattern) opts.mask = opts.pattern; 2681 var blocks = opts.blocks; 2682 opts.blocks = Object.assign({}, MaskedDate.GET_DEFAULT_BLOCKS()); // adjust year block 2683 2684 if (opts.min) opts.blocks.Y.from = opts.min.getFullYear(); 2685 if (opts.max) opts.blocks.Y.to = opts.max.getFullYear(); 2686 2687 if (opts.min && opts.max && opts.blocks.Y.from === opts.blocks.Y.to) { 2688 opts.blocks.m.from = opts.min.getMonth() + 1; 2689 opts.blocks.m.to = opts.max.getMonth() + 1; 2690 2691 if (opts.blocks.m.from === opts.blocks.m.to) { 2692 opts.blocks.d.from = opts.min.getDate(); 2693 opts.blocks.d.to = opts.max.getDate(); 2694 } 2695 } 2696 2697 Object.assign(opts.blocks, this.blocks, blocks); // add autofix 2698
2699 Object.keys(opts.blocks).forEach(function (bk) { 2700 var b = opts.blocks[bk]; 2701 if (!('autofix' in b) && 'autofix' in opts) b.autofix = opts.autofix; 2702 }); 2703 2704 _get(_getPrototypeOf(MaskedDate.prototype), "_update", this).call(this, opts); 2705 } 2706 /** 2707 @override 2708 */ 2709 2710 }, { 2711 key: "doValidate", 2712 value: function doValidate() { 2713 var _get2; 2714 2715 var date = this.date; 2716 2717 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { 2718 args[_key] = arguments[_key]; 2719 } 2720 2721 return (_get2 = _get(_getPrototypeOf(MaskedDate.prototype), "doValidate", this)).call.apply(_get2, [this].concat(args)) && (!this.isComplete || this.isDateExist(this.value) && date != null && (this.min == null || this.min <= date) && (this.max == null || date <= this.max)); 2722 } 2723 /** Checks if date is exists */ 2724 2725 }, { 2726 key: "isDateExist", 2727 value: function isDateExist(str) { 2728 return this.format(this.parse(str, this), this).indexOf(str) >= 0; 2729 } 2730 /** Parsed Date */ 2731 2732 }, { 2733 key: "date", 2734 get: function get() { 2735 return this.typedValue; 2736 }, 2737 set: function set(date) { 2738 this.typedValue = date; 2739 } 2740 /** 2741 @override 2742 */ 2743 2744 }, { 2745 key: "typedValue", 2746 get: function get() { 2747 return this.isComplete ? _get(_getPrototypeOf(MaskedDate.prototype), "typedValue", this) : null; 2748 }, 2749 set: function set(value) { 2750 _set(_getPrototypeOf(MaskedDate.prototype), "typedValue", value, this, true); 2751 } 2752 /** 2753 @override 2754 */ 2755 2756 }, { 2757 key: "maskEquals", 2758 value: function maskEquals(mask) { 2759 return mask === Date || _get(_getPrototypeOf(MaskedDate.prototype), "maskEquals", this).call(this, mask); 2760 } 2761 }]); 2762 2763 return MaskedDate; 2764 }(MaskedPattern); 2765 MaskedDate.DEFAULTS = { 2766 pattern: 'd{.}`m{.}`Y', 2767 format: function format(date) { 2768 if (!date) return ''; 2769 var day = String(date.getDate()).padStart(2, '0'); 2770 var month = String(date.getMonth() + 1).padStart(2, '0'); 2771 var year = date.getFullYear(); 2772 return [day, month, year].join('.'); 2773 }, 2774 parse: function parse(str) { 2775 var _str$split = str.split('.'), 2776 _str$split2 = _slicedToArray(_str$split, 3), 2777 day = _str$split2[0], 2778 month = _str$split2[1], 2779 year = _str$split2[2]; 2780 2781 return new Date(year, month - 1, day); 2782 } 2783 }; 2784 2785 MaskedDate.GET_DEFAULT_BLOCKS = function () { 2786 return { 2787 d: { 2788 mask: MaskedRange, 2789 from: 1, 2790 to: 31, 2791 maxLength: 2 2792 }, 2793 m: { 2794 mask: MaskedRange, 2795 from: 1, 2796 to: 12, 2797 maxLength: 2 2798 }, 2799 Y: { 2800 mask: MaskedRange, 2801 from: 1900, 2802 to: 9999 2803 } 2804 }; 2805 }; 2806 2807 IMask.MaskedDate = MaskedDate; 2808 2809 /** 2810 Generic element API to use with mask 2811 @interface 2812 */ 2813 var MaskElement = /*#__PURE__*/function () { 2814 function MaskElement() { 2815 _classCallCheck(this, MaskElement); 2816 } 2817 2818 _createClass(MaskElement, [{ 2819 key: "selectionStart", 2820 get: 2821 /** */ 2822 2823 /** */ 2824 2825 /** */ 2826 2827 /** Safely returns selection start */ 2828 function get() { 2829 var start; 2830 2831 try { 2832 start = this._unsafeSelectionStart; 2833 } catch (e) {} 2834 2835 return start != null ? start : this.value.length; 2836 } 2837 /** Safely returns selection end */ 2838 2839 }, { 2840 key: "selectionEnd", 2841 get: function get() { 2842 var end; 2843 2844 try { 2845 end = this._unsafeSelectionEnd; 2846 } catch (e) {} 2847 2848 return end != null ? end : this.value.length; 2849 } 2850 /** Safely sets element selection */ 2851 2852 }, { 2853 key: "select", 2854 value: function select(start, end) { 2855 if (start == null || end == null || start === this.selectionStart && end === this.selectionEnd) return; 2856 2857 try { 2858 this._unsafeSelect(start, end); 2859 } catch (e) {} 2860 } 2861 /** Should be overriden in subclasses */ 2862 2863 }, { 2864 key: "_unsafeSelect", 2865 value: function _unsafeSelect(start, end) {} 2866 /** Should be overriden in subclasses */ 2867 2868 }, {
2869 key: "isActive", 2870 get: function get() { 2871 return false; 2872 } 2873 /** Should be overriden in subclasses */ 2874 2875 }, { 2876 key: "bindEvents", 2877 value: function bindEvents(handlers) {} 2878 /** Should be overriden in subclasses */ 2879 2880 }, { 2881 key: "unbindEvents", 2882 value: function unbindEvents() {} 2883 }]); 2884 2885 return MaskElement; 2886 }(); 2887 IMask.MaskElement = MaskElement; 2888 2889 /** Bridge between HTMLElement and {@link Masked} */ 2890 2891 var HTMLMaskElement = /*#__PURE__*/function (_MaskElement) { 2892 _inherits(HTMLMaskElement, _MaskElement); 2893 2894 var _super = _createSuper(HTMLMaskElement); 2895 2896 /** Mapping between HTMLElement events and mask internal events */ 2897 2898 /** HTMLElement to use mask on */ 2899 2900 /** 2901 @param {HTMLInputElement|HTMLTextAreaElement} input 2902 */ 2903 function HTMLMaskElement(input) { 2904 var _this; 2905 2906 _classCallCheck(this, HTMLMaskElement); 2907 2908 _this = _super.call(this); 2909 _this.input = input; 2910 _this._handlers = {}; 2911 return _this; 2912 } 2913 /** */ 2914 // $FlowFixMe https://github.com/facebook/flow/issues/2839 2915 2916 2917 _createClass(HTMLMaskElement, [{ 2918 key: "rootElement", 2919 get: function get() { 2920 var _this$input$getRootNo, _this$input$getRootNo2, _this$input; 2921 2922 return (_this$input$getRootNo = (_this$input$getRootNo2 = (_this$input = this.input).getRootNode) === null || _this$input$getRootNo2 === void 0 ? void 0 : _this$input$getRootNo2.call(_this$input)) !== null && _this$input$getRootNo !== void 0 ? _this$input$getRootNo : document; 2923 } 2924 /** 2925 Is element in focus 2926 @readonly 2927 */ 2928 2929 }, { 2930 key: "isActive", 2931 get: function get() { 2932 //$FlowFixMe 2933 return this.input === this.rootElement.activeElement; 2934 } 2935 /** 2936 Returns HTMLElement selection start 2937 @override 2938 */ 2939 2940 }, { 2941 key: "_unsafeSelectionStart", 2942 get: function get() { 2943 return this.input.selectionStart; 2944 } 2945 /** 2946 Returns HTMLElement selection end 2947 @override 2948 */ 2949 2950 }, { 2951 key: "_unsafeSelectionEnd", 2952 get: function get() { 2953 return this.input.selectionEnd; 2954 } 2955 /** 2956 Sets HTMLElement selection 2957 @override 2958 */ 2959 2960 }, { 2961 key: "_unsafeSelect", 2962 value: function _unsafeSelect(start, end) { 2963 this.input.setSelectionRange(start, end); 2964 } 2965 /** 2966 HTMLElement value 2967 @override 2968 */ 2969 2970 }, { 2971 key: "value", 2972 get: function get() { 2973 return this.input.value; 2974 }, 2975 set: function set(value) { 2976 this.input.value = value; 2977 } 2978 /** 2979 Binds HTMLElement events to mask internal events 2980 @override 2981 */ 2982 2983 }, { 2984 key: "bindEvents", 2985 value: function bindEvents(handlers) { 2986 var _this2 = this; 2987 2988 Object.keys(handlers).forEach(function (event) { 2989 return _this2._toggleEventHandler(HTMLMaskElement.EVENTS_MAP[event], handlers[event]); 2990 }); 2991 } 2992 /** 2993 Unbinds HTMLElement events to mask internal events 2994 @override 2995 */ 2996 2997 }, { 2998 key: "unbindEvents", 2999 value: function unbindEvents() { 3000 var _this3 = this; 3001 3002 Object.keys(this._handlers).forEach(function (event) { 3003 return _this3._toggleEventHandler(event); 3004 }); 3005 } 3006 /** */ 3007 3008 }, { 3009 key: "_toggleEventHandler", 3010 value: function _toggleEventHandler(event, handler) { 3011 if (this._handlers[event]) { 3012 this.input.removeEventListener(event, this._handlers[event]); 3013 delete this._handlers[event]; 3014 } 3015 3016 if (handler) { 3017 this.input.addEventListener(event, handler); 3018 this._handlers[event] = handler; 3019 } 3020 } 3021 }]); 3022 3023 return HTMLMaskElement; 3024 }(MaskElement); 3025 HTMLMaskElement.EVENTS_MAP = { 3026 selectionChange: 'keydown', 3027 input: 'input', 3028 drop: 'drop', 3029 click: 'click', 3030 focus: 'focus', 3031 commit: 'blur' 3032 }; 3033 IMask.HTMLMaskElement = HTMLMaskElement; 3034 3035 var HTMLContenteditableMaskElement = /*#__PURE__*/function (_HTMLMaskElement) { 3036 _inherits(HTMLContenteditableMaskElement, _HTMLMaskElement); 3037 3038 var _super = _createSuper(HTMLContenteditableMaskElement); 3039 3040 function HTMLContenteditableMaskElement() { 3041 _classCallCheck(this, HTMLContenteditableMaskElement); 3042 3043 return _super.apply(this, arguments); 3044 } 3045 3046 _createClass(HTMLContenteditableMaskElement, [{ 3047 key: "_unsafeSelectionStart", 3048 get: 3049 /** 3050 Returns HTMLElement selection start 3051 @override 3052 */ 3053 function get() { 3054 var root = this.rootElement; 3055 var selection = root.getSelection && root.getSelection(); 3056 var anchorOffset = selection && selection.anchorOffset; 3057 var focusOffset = selection && selection.focusOffset; 3058 3059 if (focusOffset == null || anchorOffset == null || anchorOffset < focusOffset) { 3060 return anchorOffset; 3061 } 3062 3063 return focusOffset; 3064 } 3065 /** 3066 Returns HTMLElement selection end 3067 @override 3068 */ 3069 3070 }, {
3071 key: "_unsafeSelectionEnd", 3072 get: function get() { 3073 var root = this.rootElement; 3074 var selection = root.getSelection && root.getSelection(); 3075 var anchorOffset = selection && selection.anchorOffset; 3076 var focusOffset = selection && selection.focusOffset; 3077 3078 if (focusOffset == null || anchorOffset == null || anchorOffset > focusOffset) { 3079 return anchorOffset; 3080 } 3081 3082 return focusOffset; 3083 } 3084 /** 3085 Sets HTMLElement selection 3086 @override 3087 */ 3088 3089 }, { 3090 key: "_unsafeSelect", 3091 value: function _unsafeSelect(start, end) { 3092 if (!this.rootElement.createRange) return; 3093 var range = this.rootElement.createRange(); 3094 range.setStart(this.input.firstChild || this.input, start); 3095 range.setEnd(this.input.lastChild || this.input, end); 3096 var root = this.rootElement; 3097 var selection = root.getSelection && root.getSelection(); 3098 3099 if (selection) { 3100 selection.removeAllRanges(); 3101 selection.addRange(range); 3102 } 3103 } 3104 /** 3105 HTMLElement value 3106 @override 3107 */ 3108 3109 }, { 3110 key: "value", 3111 get: function get() { 3112 // $FlowFixMe 3113 return this.input.textContent; 3114 }, 3115 set: function set(value) { 3116 this.input.textContent = value; 3117 } 3118 }]); 3119 3120 return HTMLContenteditableMaskElement; 3121 }(HTMLMaskElement); 3122 IMask.HTMLContenteditableMaskElement = HTMLContenteditableMaskElement; 3123 3124 var _excluded$1 = ["mask"]; 3125 /** Listens to element events and controls changes between element and {@link Masked} */ 3126 3127 var InputMask = /*#__PURE__*/function () { 3128 /** 3129 View element 3130 @readonly 3131 */ 3132 3133 /** 3134 Internal {@link Masked} model 3135 @readonly 3136 */ 3137 3138 /** 3139 @param {MaskElement|HTMLInputElement|HTMLTextAreaElement} el 3140 @param {Object} opts 3141 */ 3142 function InputMask(el, opts) { 3143 _classCallCheck(this, InputMask); 3144 3145 this.el = el instanceof MaskElement ? el : el.isContentEditable && el.tagName !== 'INPUT' && el.tagName !== 'TEXTAREA' ? new HTMLContenteditableMaskElement(el) : new HTMLMaskElement(el); 3146 this.masked = createMask(opts); 3147 this._listeners = {}; 3148 this._value = ''; 3149 this._unmaskedValue = ''; 3150 this._saveSelection = this._saveSelection.bind(this); 3151 this._onInput = this._onInput.bind(this); 3152 this._onChange = this._onChange.bind(this); 3153 this._onDrop = this._onDrop.bind(this); 3154 this._onFocus = this._onFocus.bind(this); 3155 this._onClick = this._onClick.bind(this); 3156 this.alignCursor = this.alignCursor.bind(this); 3157 this.alignCursorFriendly = this.alignCursorFriendly.bind(this); 3158 3159 this._bindEvents(); // refresh 3160 3161 3162 this.updateValue(); 3163 3164 this._onChange(); 3165 } 3166 /** Read or update mask */ 3167 3168 3169 _createClass(InputMask, [{ 3170 key: "mask", 3171 get: function get() { 3172 return this.masked.mask; 3173 }, 3174 set: function set(mask) { 3175 if (this.maskEquals(mask)) return; // $FlowFixMe No ideas ... after update 3176 3177 if (!(mask instanceof IMask.Masked) && this.masked.constructor === maskedClass(mask)) { 3178 this.masked.updateOptions({ 3179 mask: mask 3180 }); 3181 return; 3182 } 3183 3184 var masked = createMask({ 3185 mask: mask 3186 }); 3187 masked.unmaskedValue = this.masked.unmaskedValue; 3188 this.masked = masked; 3189 } 3190 /** Raw value */ 3191 3192 }, { 3193 key: "maskEquals", 3194 value: function maskEquals(mask) { 3195 var _this$masked; 3196 3197 return mask == null || ((_this$masked = this.masked) === null || _this$masked === void 0 ? void 0 : _this$masked.maskEquals(mask)); 3198 } 3199 }, { 3200 key: "value", 3201 get: function get() { 3202 return this._value; 3203 }, 3204 set: function set(str) { 3205 if (this.value === str) return; 3206 this.masked.value = str; 3207 this.updateControl(); 3208 this.alignCursor(); 3209 } 3210 /** Unmasked value */ 3211 3212 }, { 3213 key: "unmaskedValue", 3214 get: function get() { 3215 return this._unmaskedValue; 3216 }, 3217 set: function set(str) { 3218 if (this.unmaskedValue === str) return; 3219 this.masked.unmaskedValue = str; 3220 this.updateControl(); 3221 this.alignCursor(); 3222 } 3223 /** Typed unmasked value */ 3224 3225 }, {
3226 key: "typedValue", 3227 get: function get() { 3228 return this.masked.typedValue; 3229 }, 3230 set: function set(val) { 3231 if (this.masked.typedValueEquals(val)) return; 3232 this.masked.typedValue = val; 3233 this.updateControl(); 3234 this.alignCursor(); 3235 } 3236 /** 3237 Starts listening to element events 3238 @protected 3239 */ 3240 3241 }, { 3242 key: "_bindEvents", 3243 value: function _bindEvents() { 3244 this.el.bindEvents({ 3245 selectionChange: this._saveSelection, 3246 input: this._onInput, 3247 drop: this._onDrop, 3248 click: this._onClick, 3249 focus: this._onFocus, 3250 commit: this._onChange 3251 }); 3252 } 3253 /** 3254 Stops listening to element events 3255 @protected 3256 */ 3257 3258 }, { 3259 key: "_unbindEvents", 3260 value: function _unbindEvents() { 3261 if (this.el) this.el.unbindEvents(); 3262 } 3263 /** 3264 Fires custom event 3265 @protected 3266 */ 3267 3268 }, { 3269 key: "_fireEvent", 3270 value: function _fireEvent(ev) { 3271 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 3272 args[_key - 1] = arguments[_key]; 3273 } 3274 3275 var listeners = this._listeners[ev]; 3276 if (!listeners) return; 3277 listeners.forEach(function (l) { 3278 return l.apply(void 0, args); 3279 }); 3280 } 3281 /** 3282 Current selection start 3283 @readonly 3284 */ 3285 3286 }, { 3287 key: "selectionStart", 3288 get: function get() { 3289 return this._cursorChanging ? this._changingCursorPos : this.el.selectionStart; 3290 } 3291 /** Current cursor position */ 3292 3293 }, { 3294 key: "cursorPos", 3295 get: function get() { 3296 return this._cursorChanging ? this._changingCursorPos : this.el.selectionEnd; 3297 }, 3298 set: function set(pos) { 3299 if (!this.el || !this.el.isActive) return; 3300 this.el.select(pos, pos); 3301 3302 this._saveSelection(); 3303 } 3304 /** 3305 Stores current selection 3306 @protected 3307 */ 3308 3309 }, { 3310 key: "_saveSelection", 3311 value: function 3312 /* ev */ 3313 _saveSelection() { 3314 if (this.value !== this.el.value) { 3315 console.warn('Element value was changed outside of mask. Syncronize mask using `mask.updateValue()` to work properly.'); // eslint-disable-line no-console 3316 } 3317 3318 this._selection = { 3319 start: this.selectionStart, 3320 end: this.cursorPos 3321 }; 3322 } 3323 /** Syncronizes model value from view */ 3324 3325 }, { 3326 key: "updateValue", 3327 value: function updateValue() { 3328 this.masked.value = this.el.value; 3329 this._value = this.masked.value; 3330 } 3331 /** Syncronizes view from model value, fires change events */ 3332 3333 }, { 3334 key: "updateControl", 3335 value: function updateControl() { 3336 var newUnmaskedValue = this.masked.unmaskedValue; 3337 var newValue = this.masked.value; 3338 var isChanged = this.unmaskedValue !== newUnmaskedValue || this.value !== newValue; 3339 this._unmaskedValue = newUnmaskedValue; 3340 this._value = newValue; 3341 if (this.el.value !== newValue) this.el.value = newValue; 3342 if (isChanged) this._fireChangeEvents(); 3343 } 3344 /** Updates options with deep equal check, recreates @{link Masked} model if mask type changes */ 3345 3346 }, { 3347 key: "updateOptions", 3348 value: function updateOptions(opts) { 3349 var mask = opts.mask, 3350 restOpts = _objectWithoutProperties(opts, _excluded$1); 3351 3352 var updateMask = !this.maskEquals(mask); 3353 var updateOpts = !objectIncludes(this.masked, restOpts); 3354 if (updateMask) this.mask = mask; 3355 if (updateOpts) this.masked.updateOptions(restOpts); 3356 if (updateMask || updateOpts) this.updateControl(); 3357 } 3358 /** Updates cursor */ 3359 3360 }, { 3361 key: "updateCursor", 3362 value: function updateCursor(cursorPos) { 3363 if (cursorPos == null) return;
3364 this.cursorPos = cursorPos; // also queue change cursor for mobile browsers 3365 3366 this._delayUpdateCursor(cursorPos); 3367 } 3368 /** 3369 Delays cursor update to support mobile browsers 3370 @private 3371 */ 3372 3373 }, { 3374 key: "_delayUpdateCursor", 3375 value: function _delayUpdateCursor(cursorPos) { 3376 var _this = this; 3377 3378 this._abortUpdateCursor(); 3379 3380 this._changingCursorPos = cursorPos; 3381 this._cursorChanging = setTimeout(function () { 3382 if (!_this.el) return; // if was destroyed 3383 3384 _this.cursorPos = _this._changingCursorPos; 3385 3386 _this._abortUpdateCursor(); 3387 }, 10); 3388 } 3389 /** 3390 Fires custom events 3391 @protected 3392 */ 3393 3394 }, { 3395 key: "_fireChangeEvents", 3396 value: function _fireChangeEvents() { 3397 this._fireEvent('accept', this._inputEvent); 3398 3399 if (this.masked.isComplete) this._fireEvent('complete', this._inputEvent); 3400 } 3401 /** 3402 Aborts delayed cursor update 3403 @private 3404 */ 3405 3406 }, { 3407 key: "_abortUpdateCursor", 3408 value: function _abortUpdateCursor() { 3409 if (this._cursorChanging) { 3410 clearTimeout(this._cursorChanging); 3411 delete this._cursorChanging; 3412 } 3413 } 3414 /** Aligns cursor to nearest available position */ 3415 3416 }, { 3417 key: "alignCursor", 3418 value: function alignCursor() {
3419 this.cursorPos = this.masked.nearestInputPos(this.masked.nearestInputPos(this.cursorPos, DIRECTION.LEFT)); 3420 } 3421 /** Aligns cursor only if selection is empty */ 3422 3423 }, { 3424 key: "alignCursorFriendly", 3425 value: function alignCursorFriendly() { 3426 if (this.selectionStart !== this.cursorPos) return; // skip if range is selected 3427 3428 this.alignCursor(); 3429 } 3430 /** Adds listener on custom event */ 3431 3432 }, { 3433 key: "on", 3434 value: function on(ev, handler) { 3435 if (!this._listeners[ev]) this._listeners[ev] = []; 3436 3437 this._listeners[ev].push(handler); 3438 3439 return this; 3440 } 3441 /** Removes custom event listener */ 3442 3443 }, { 3444 key: "off", 3445 value: function off(ev, handler) { 3446 if (!this._listeners[ev]) return this; 3447 3448 if (!handler) { 3449 delete this._listeners[ev]; 3450 return this; 3451 } 3452 3453 var hIndex = this._listeners[ev].indexOf(handler); 3454 3455 if (hIndex >= 0) this._listeners[ev].splice(hIndex, 1); 3456 return this; 3457 } 3458 /** Handles view input event */ 3459 3460 }, { 3461 key: "_onInput", 3462 value: function _onInput(e) { 3463 this._inputEvent = e; 3464 3465 this._abortUpdateCursor(); // fix strange IE behavior 3466 3467 3468 if (!this._selection) return this.updateValue(); 3469 var details = new ActionDetails( // new state 3470 this.el.value, this.cursorPos, // old state 3471 this.value, this._selection); 3472 var oldRawValue = this.masked.rawInputValue; 3473 var offset = this.masked.splice(details.startChangePos, details.removed.length, details.inserted, details.removeDirection, { 3474 input: true, 3475 raw: true 3476 }).offset; // force align in remove direction only if no input chars were removed 3477 // otherwise we still need to align with NONE (to get out from fixed symbols for instance) 3478 3479 var removeDirection = oldRawValue === this.masked.rawInputValue ? details.removeDirection : DIRECTION.NONE; 3480 var cursorPos = this.masked.nearestInputPos(details.startChangePos + offset, removeDirection); 3481 if (removeDirection !== DIRECTION.NONE) cursorPos = this.masked.nearestInputPos(cursorPos, DIRECTION.NONE); 3482 this.updateControl(); 3483 this.updateCursor(cursorPos); 3484 delete this._inputEvent; 3485 } 3486 /** Handles view change event and commits model value */ 3487 3488 }, { 3489 key: "_onChange", 3490 value: function _onChange() { 3491 if (this.value !== this.el.value) { 3492 this.updateValue(); 3493 } 3494 3495 this.masked.doCommit(); 3496 this.updateControl(); 3497 3498 this._saveSelection(); 3499 } 3500 /** Handles view drop event, prevents by default */ 3501 3502 }, { 3503 key: "_onDrop", 3504 value: function _onDrop(ev) { 3505 ev.preventDefault(); 3506 ev.stopPropagation(); 3507 } 3508 /** Restore last selection on focus */ 3509 3510 }, { 3511 key: "_onFocus", 3512 value: function _onFocus(ev) { 3513 this.alignCursorFriendly(); 3514 } 3515 /** Restore last selection on focus */ 3516 3517 }, { 3518 key: "_onClick", 3519 value: function _onClick(ev) { 3520 this.alignCursorFriendly(); 3521 } 3522 /** Unbind view events and removes element reference */ 3523 3524 }, { 3525 key: "destroy", 3526 value: function destroy() { 3527 this._unbindEvents(); // $FlowFixMe why not do so? 3528 3529 3530 this._listeners.length = 0; // $FlowFixMe 3531 3532 delete this.el; 3533 } 3534 }]); 3535 3536 return InputMask; 3537 }(); 3538 IMask.InputMask = InputMask; 3539 3540 /** Pattern which validates enum values */ 3541 3542 var MaskedEnum = /*#__PURE__*/function (_MaskedPattern) { 3543 _inherits(MaskedEnum, _MaskedPattern); 3544 3545 var _super = _createSuper(MaskedEnum); 3546 3547 function MaskedEnum() { 3548 _classCallCheck(this, MaskedEnum); 3549 3550 return _super.apply(this, arguments); 3551 } 3552 3553 _createClass(MaskedEnum, [{ 3554 key: "_update", 3555 value: 3556 /** 3557 @override 3558 @param {Object} opts 3559 */ 3560 function _update(opts) { 3561 // TODO type 3562 if (opts.enum) opts.mask = '*'.repeat(opts.enum[0].length); 3563 3564 _get(_getPrototypeOf(MaskedEnum.prototype), "_update", this).call(this, opts); 3565 } 3566 /** 3567 @override 3568 */ 3569 3570 }, {
3571 key: "doValidate", 3572 value: function doValidate() { 3573 var _this = this, 3574 _get2; 3575 3576 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { 3577 args[_key] = arguments[_key]; 3578 } 3579 3580 return this.enum.some(function (e) { 3581 return e.indexOf(_this.unmaskedValue) >= 0; 3582 }) && (_get2 = _get(_getPrototypeOf(MaskedEnum.prototype), "doValidate", this)).call.apply(_get2, [this].concat(args)); 3583 } 3584 }]); 3585 3586 return MaskedEnum; 3587 }(MaskedPattern); 3588 IMask.MaskedEnum = MaskedEnum; 3589 3590 /** 3591 Number mask 3592 @param {Object} opts 3593 @param {string} opts.radix - Single char 3594 @param {string} opts.thousandsSeparator - Single char 3595 @param {Array<string>} opts.mapToRadix - Array of single chars 3596 @param {number} opts.min 3597 @param {number} opts.max 3598 @param {number} opts.scale - Digits after point 3599 @param {boolean} opts.signed - Allow negative 3600 @param {boolean} opts.normalizeZeros - Flag to remove leading and trailing zeros in the end of editing 3601 @param {boolean} opts.padFractionalZeros - Flag to pad trailing zeros after point in the end of editing 3602 */ 3603 var MaskedNumber = /*#__PURE__*/function (_Masked) { 3604 _inherits(MaskedNumber, _Masked); 3605 3606 var _super = _createSuper(MaskedNumber); 3607 3608 /** Single char */ 3609 3610 /** Single char */ 3611 3612 /** Array of single chars */ 3613 3614 /** */ 3615 3616 /** */ 3617 3618 /** Digits after point */ 3619 3620 /** */ 3621 3622 /** Flag to remove leading and trailing zeros in the end of editing */ 3623 3624 /** Flag to pad trailing zeros after point in the end of editing */ 3625 function MaskedNumber(opts) { 3626 _classCallCheck(this, MaskedNumber); 3627 3628 return _super.call(this, Object.assign({}, MaskedNumber.DEFAULTS, opts)); 3629 } 3630 /** 3631 @override 3632 */ 3633 3634 3635 _createClass(MaskedNumber, [{ 3636 key: "_update", 3637 value: function _update(opts) { 3638 _get(_getPrototypeOf(MaskedNumber.prototype), "_update", this).call(this, opts); 3639 3640 this._updateRegExps(); 3641 } 3642 /** */ 3643 3644 }, { 3645 key: "_updateRegExps", 3646 value: function _updateRegExps() { 3647 // use different regexp to process user input (more strict, input suffix) and tail shifting 3648 var start = '^' + (this.allowNegative ? '[+|\\-]?' : ''); 3649 var midInput = '(0|([1-9]+\\d*))?'; 3650 var mid = '\\d*'; 3651 var end = (this.scale ? '(' + escapeRegExp(this.radix) + '\\d{0,' + this.scale + '})?' : '') + '$'; 3652 this._numberRegExpInput = new RegExp(start + midInput + end); 3653 this._numberRegExp = new RegExp(start + mid + end); 3654 this._mapToRadixRegExp = new RegExp('[' + this.mapToRadix.map(escapeRegExp).join('') + ']', 'g'); 3655 this._thousandsSeparatorRegExp = new RegExp(escapeRegExp(this.thousandsSeparator), 'g'); 3656 } 3657 /** */ 3658 3659 }, { 3660 key: "_removeThousandsSeparators", 3661 value: function _removeThousandsSeparators(value) { 3662 return value.replace(this._thousandsSeparatorRegExp, ''); 3663 } 3664 /** */ 3665 3666 }, { 3667 key: "_insertThousandsSeparators", 3668 value: function _insertThousandsSeparators(value) { 3669 // https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript 3670 var parts = value.split(this.radix); 3671 parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, this.thousandsSeparator); 3672 return parts.join(this.radix); 3673 } 3674 /** 3675 @override 3676 */ 3677 3678 }, { 3679 key: "doPrepare", 3680 value: function doPrepare(ch) { 3681 var _get2; 3682 3683 ch = ch.replace(this._mapToRadixRegExp, this.radix); 3684 3685 var noSepCh = this._removeThousandsSeparators(ch); 3686 3687 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 3688 args[_key - 1] = arguments[_key]; 3689 } 3690 3691 var _normalizePrepare = normalizePrepare((_get2 = _get(_getPrototypeOf(MaskedNumber.prototype), "doPrepare", this)).call.apply(_get2, [this, noSepCh].concat(args))), 3692 _normalizePrepare2 = _slicedToArray(_normalizePrepare, 2), 3693 prepCh = _normalizePrepare2[0], 3694 details = _normalizePrepare2[1]; 3695 3696 if (ch && !noSepCh) details.skip = true; 3697 return [prepCh, details]; 3698 } 3699 /** */ 3700 3701 }, {
3702 key: "_separatorsCount", 3703 value: function _separatorsCount(to) { 3704 var extendOnSeparators = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; 3705 var count = 0; 3706 3707 for (var pos = 0; pos < to; ++pos) { 3708 if (this._value.indexOf(this.thousandsSeparator, pos) === pos) { 3709 ++count; 3710 if (extendOnSeparators) to += this.thousandsSeparator.length; 3711 } 3712 } 3713 3714 return count; 3715 } 3716 /** */ 3717 3718 }, { 3719 key: "_separatorsCountFromSlice", 3720 value: function _separatorsCountFromSlice() { 3721 var slice = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._value; 3722 return this._separatorsCount(this._removeThousandsSeparators(slice).length, true); 3723 } 3724 /** 3725 @override 3726 */ 3727 3728 }, { 3729 key: "extractInput", 3730 value: function extractInput() { 3731 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 3732 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 3733 var flags = arguments.length > 2 ? arguments[2] : undefined; 3734 3735 var _this$_adjustRangeWit = this._adjustRangeWithSeparators(fromPos, toPos); 3736 3737 var _this$_adjustRangeWit2 = _slicedToArray(_this$_adjustRangeWit, 2); 3738 3739 fromPos = _this$_adjustRangeWit2[0]; 3740 toPos = _this$_adjustRangeWit2[1]; 3741 return this._removeThousandsSeparators(_get(_getPrototypeOf(MaskedNumber.prototype), "extractInput", this).call(this, fromPos, toPos, flags)); 3742 } 3743 /** 3744 @override 3745 */ 3746 3747 }, { 3748 key: "_appendCharRaw", 3749 value: function _appendCharRaw(ch) { 3750 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 3751 if (!this.thousandsSeparator) return _get(_getPrototypeOf(MaskedNumber.prototype), "_appendCharRaw", this).call(this, ch, flags); 3752 var prevBeforeTailValue = flags.tail && flags._beforeTailState ? flags._beforeTailState._value : this._value; 3753 3754 var prevBeforeTailSeparatorsCount = this._separatorsCountFromSlice(prevBeforeTailValue); 3755 3756 this._value = this._removeThousandsSeparators(this.value); 3757 3758 var appendDetails = _get(_getPrototypeOf(MaskedNumber.prototype), "_appendCharRaw", this).call(this, ch, flags); 3759 3760 this._value = this._insertThousandsSeparators(this._value); 3761 var beforeTailValue = flags.tail && flags._beforeTailState ? flags._beforeTailState._value : this._value; 3762 3763 var beforeTailSeparatorsCount = this._separatorsCountFromSlice(beforeTailValue); 3764 3765 appendDetails.tailShift += (beforeTailSeparatorsCount - prevBeforeTailSeparatorsCount) * this.thousandsSeparator.length; 3766 appendDetails.skip = !appendDetails.rawInserted && ch === this.thousandsSeparator; 3767 return appendDetails; 3768 } 3769 /** */ 3770 3771 }, { 3772 key: "_findSeparatorAround", 3773 value: function _findSeparatorAround(pos) { 3774 if (this.thousandsSeparator) { 3775 var searchFrom = pos - this.thousandsSeparator.length + 1; 3776 var separatorPos = this.value.indexOf(this.thousandsSeparator, searchFrom); 3777 if (separatorPos <= pos) return separatorPos; 3778 } 3779 3780 return -1; 3781 } 3782 }, { 3783 key: "_adjustRangeWithSeparators", 3784 value: function _adjustRangeWithSeparators(from, to) { 3785 var separatorAroundFromPos = this._findSeparatorAround(from); 3786 3787 if (separatorAroundFromPos >= 0) from = separatorAroundFromPos; 3788 3789 var separatorAroundToPos = this._findSeparatorAround(to); 3790 3791 if (separatorAroundToPos >= 0) to = separatorAroundToPos + this.thousandsSeparator.length; 3792 return [from, to]; 3793 } 3794 /** 3795 @override 3796 */ 3797 3798 }, { 3799 key: "remove", 3800 value: function remove() { 3801 var fromPos = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; 3802 var toPos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.value.length; 3803 3804 var _this$_adjustRangeWit3 = this._adjustRangeWithSeparators(fromPos, toPos); 3805 3806 var _this$_adjustRangeWit4 = _slicedToArray(_this$_adjustRangeWit3, 2); 3807 3808 fromPos = _this$_adjustRangeWit4[0]; 3809 toPos = _this$_adjustRangeWit4[1]; 3810 var valueBeforePos = this.value.slice(0, fromPos); 3811 var valueAfterPos = this.value.slice(toPos); 3812 3813 var prevBeforeTailSeparatorsCount = this._separatorsCount(valueBeforePos.length); 3814 3815 this._value = this._insertThousandsSeparators(this._removeThousandsSeparators(valueBeforePos + valueAfterPos)); 3816 3817 var beforeTailSeparatorsCount = this._separatorsCountFromSlice(valueBeforePos); 3818 3819 return new ChangeDetails({ 3820 tailShift: (beforeTailSeparatorsCount - prevBeforeTailSeparatorsCount) * this.thousandsSeparator.length 3821 }); 3822 } 3823 /** 3824 @override 3825 */ 3826 3827 }, {
3828 key: "nearestInputPos", 3829 value: function nearestInputPos(cursorPos, direction) { 3830 if (!this.thousandsSeparator) return cursorPos; 3831 3832 switch (direction) { 3833 case DIRECTION.NONE: 3834 case DIRECTION.LEFT: 3835 case DIRECTION.FORCE_LEFT: 3836 { 3837 var separatorAtLeftPos = this._findSeparatorAround(cursorPos - 1); 3838 3839 if (separatorAtLeftPos >= 0) { 3840 var separatorAtLeftEndPos = separatorAtLeftPos + this.thousandsSeparator.length; 3841 3842 if (cursorPos < separatorAtLeftEndPos || this.value.length <= separatorAtLeftEndPos || direction === DIRECTION.FORCE_LEFT) { 3843 return separatorAtLeftPos; 3844 } 3845 } 3846 3847 break; 3848 } 3849 3850 case DIRECTION.RIGHT: 3851 case DIRECTION.FORCE_RIGHT: 3852 { 3853 var separatorAtRightPos = this._findSeparatorAround(cursorPos); 3854 3855 if (separatorAtRightPos >= 0) { 3856 return separatorAtRightPos + this.thousandsSeparator.length; 3857 } 3858 } 3859 } 3860 3861 return cursorPos; 3862 } 3863 /** 3864 @override 3865 */ 3866 3867 }, { 3868 key: "doValidate", 3869 value: function doValidate(flags) { 3870 var regexp = flags.input ? this._numberRegExpInput : this._numberRegExp; // validate as string 3871 3872 var valid = regexp.test(this._removeThousandsSeparators(this.value)); 3873 3874 if (valid) { 3875 // validate as number 3876 var number = this.number; 3877 valid = valid && !isNaN(number) && ( // check min bound for negative values 3878 this.min == null || this.min >= 0 || this.min <= this.number) && ( // check max bound for positive values 3879 this.max == null || this.max <= 0 || this.number <= this.max); 3880 } 3881 3882 return valid && _get(_getPrototypeOf(MaskedNumber.prototype), "doValidate", this).call(this, flags); 3883 } 3884 /** 3885 @override 3886 */ 3887 3888 }, { 3889 key: "doCommit", 3890 value: function doCommit() { 3891 if (this.value) { 3892 var number = this.number; 3893 var validnum = number; // check bounds 3894 3895 if (this.min != null) validnum = Math.max(validnum, this.min); 3896 if (this.max != null) validnum = Math.min(validnum, this.max); 3897 if (validnum !== number) this.unmaskedValue = String(validnum); 3898 var formatted = this.value; 3899 if (this.normalizeZeros) formatted = this._normalizeZeros(formatted); 3900 if (this.padFractionalZeros && this.scale > 0) formatted = this._padFractionalZeros(formatted); 3901 this._value = formatted; 3902 } 3903 3904 _get(_getPrototypeOf(MaskedNumber.prototype), "doCommit", this).call(this); 3905 } 3906 /** */ 3907 3908 }, { 3909 key: "_normalizeZeros", 3910 value: function _normalizeZeros(value) { 3911 var parts = this._removeThousandsSeparators(value).split(this.radix); // remove leading zeros 3912 3913 3914 parts[0] = parts[0].replace(/^(\D*)(0*)(\d*)/, function (match, sign, zeros, num) { 3915 return sign + num; 3916 }); // add leading zero 3917 3918 if (value.length && !/\d$/.test(parts[0])) parts[0] = parts[0] + '0'; 3919 3920 if (parts.length > 1) { 3921 parts[1] = parts[1].replace(/0*$/, ''); // remove trailing zeros 3922 3923 if (!parts[1].length) parts.length = 1; // remove fractional 3924 } 3925 3926 return this._insertThousandsSeparators(parts.join(this.radix)); 3927 } 3928 /** */ 3929 3930 }, { 3931 key: "_padFractionalZeros", 3932 value: function _padFractionalZeros(value) { 3933 if (!value) return value; 3934 var parts = value.split(this.radix); 3935 if (parts.length < 2) parts.push(''); 3936 parts[1] = parts[1].padEnd(this.scale, '0'); 3937 return parts.join(this.radix); 3938 } 3939 /** 3940 @override 3941 */ 3942 3943 }, { 3944 key: "unmaskedValue", 3945 get: function get() { 3946 return this._removeThousandsSeparators(this._normalizeZeros(this.value)).replace(this.radix, '.'); 3947 }, 3948 set: function set(unmaskedValue) { 3949 _set(_getPrototypeOf(MaskedNumber.prototype), "unmaskedValue", unmaskedValue.replace('.', this.radix), this, true); 3950 } 3951 /** 3952 @override 3953 */ 3954 3955 }, {
3956 key: "typedValue", 3957 get: function get() { 3958 return Number(this.unmaskedValue); 3959 }, 3960 set: function set(n) { 3961 _set(_getPrototypeOf(MaskedNumber.prototype), "unmaskedValue", String(n), this, true); 3962 } 3963 /** Parsed Number */ 3964 3965 }, { 3966 key: "number", 3967 get: function get() { 3968 return this.typedValue; 3969 }, 3970 set: function set(number) { 3971 this.typedValue = number; 3972 } 3973 /** 3974 Is negative allowed 3975 @readonly 3976 */ 3977 3978 }, { 3979 key: "allowNegative", 3980 get: function get() { 3981 return this.signed || this.min != null && this.min < 0 || this.max != null && this.max < 0; 3982 } 3983 /** 3984 @override 3985 */ 3986 3987 }, { 3988 key: "typedValueEquals", 3989 value: function typedValueEquals(value) { 3990 // handle 0 -> '' case (typed = 0 even if value = '') 3991 // for details see https://github.com/uNmAnNeR/imaskjs/issues/134 3992 return (_get(_getPrototypeOf(MaskedNumber.prototype), "typedValueEquals", this).call(this, value) || MaskedNumber.EMPTY_VALUES.includes(value) && MaskedNumber.EMPTY_VALUES.includes(this.typedValue)) && !(value === 0 && this.value === ''); 3993 } 3994 }]); 3995 3996 return MaskedNumber; 3997 }(Masked); 3998 MaskedNumber.DEFAULTS = { 3999 radix: ',', 4000 thousandsSeparator: '', 4001 mapToRadix: ['.'], 4002 scale: 2, 4003 signed: false, 4004 normalizeZeros: true, 4005 padFractionalZeros: false 4006 }; 4007 MaskedNumber.EMPTY_VALUES = [].concat(_toConsumableArray(Masked.EMPTY_VALUES), [0]); 4008 IMask.MaskedNumber = MaskedNumber; 4009 4010 /** Masking by custom Function */ 4011 4012 var MaskedFunction = /*#__PURE__*/function (_Masked) { 4013 _inherits(MaskedFunction, _Masked); 4014 4015 var _super = _createSuper(MaskedFunction); 4016 4017 function MaskedFunction() { 4018 _classCallCheck(this, MaskedFunction); 4019 4020 return _super.apply(this, arguments); 4021 } 4022 4023 _createClass(MaskedFunction, [{ 4024 key: "_update", 4025 value: 4026 /** 4027 @override 4028 @param {Object} opts 4029 */ 4030 function _update(opts) { 4031 if (opts.mask) opts.validate = opts.mask; 4032 4033 _get(_getPrototypeOf(MaskedFunction.prototype), "_update", this).call(this, opts); 4034 } 4035 }]); 4036 4037 return MaskedFunction; 4038 }(Masked); 4039 IMask.MaskedFunction = MaskedFunction; 4040 4041 var _excluded = ["compiledMasks", "currentMaskRef", "currentMask"]; 4042 4043 /** Dynamic mask for choosing apropriate mask in run-time */ 4044 var MaskedDynamic = /*#__PURE__*/function (_Masked) { 4045 _inherits(MaskedDynamic, _Masked); 4046 4047 var _super = _createSuper(MaskedDynamic); 4048 4049 /** Currently chosen mask */ 4050 4051 /** Compliled {@link Masked} options */ 4052 4053 /** Chooses {@link Masked} depending on input value */ 4054 4055 /** 4056 @param {Object} opts 4057 */ 4058 function MaskedDynamic(opts) { 4059 var _this; 4060 4061 _classCallCheck(this, MaskedDynamic); 4062 4063 _this = _super.call(this, Object.assign({}, MaskedDynamic.DEFAULTS, opts)); 4064 _this.currentMask = null; 4065 return _this; 4066 } 4067 /** 4068 @override 4069 */ 4070 4071 4072 _createClass(MaskedDynamic, [{ 4073 key: "_update", 4074 value: function _update(opts) { 4075 _get(_getPrototypeOf(MaskedDynamic.prototype), "_update", this).call(this, opts); 4076 4077 if ('mask' in opts) { 4078 // mask could be totally dynamic with only `dispatch` option 4079 this.compiledMasks = Array.isArray(opts.mask) ? opts.mask.map(function (m) { 4080 return createMask(m); 4081 }) : []; 4082 } 4083 } 4084 /** 4085 @override 4086 */ 4087 4088 }, { 4089 key: "_appendCharRaw", 4090 value: function _appendCharRaw(ch) { 4091 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 4092 4093 var details = this._applyDispatch(ch, flags); 4094 4095 if (this.currentMask) { 4096 details.aggregate(this.currentMask._appendChar(ch, this.currentMaskFlags(flags))); 4097 } 4098 4099 return details; 4100 } 4101 }, { 4102 key: "_applyDispatch", 4103 value: function _applyDispatch() { 4104 var appended = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; 4105 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 4106 var prevValueBeforeTail = flags.tail && flags._beforeTailState != null ? flags._beforeTailState._value : this.value; 4107 var inputValue = this.rawInputValue; 4108 var insertValue = flags.tail && flags._beforeTailState != null ? // $FlowFixMe - tired to fight with type system 4109 flags._beforeTailState._rawInputValue : inputValue; 4110 var tailValue = inputValue.slice(insertValue.length); 4111 var prevMask = this.currentMask; 4112 var details = new ChangeDetails(); 4113 var prevMaskState = prevMask === null || prevMask === void 0 ? void 0 : prevMask.state;
4113 // clone flags to prevent overwriting `_beforeTailState` 4114 4115 this.currentMask = this.doDispatch(appended, Object.assign({}, flags)); // restore state after dispatch 4116 4117 if (this.currentMask) { 4118 if (this.currentMask !== prevMask) { 4119 // if mask changed reapply input 4120 this.currentMask.reset(); 4121 4122 if (insertValue) { 4123 // $FlowFixMe - it's ok, we don't change current mask above 4124 var d = this.currentMask.append(insertValue, { 4125 raw: true 4126 }); 4127 details.tailShift = d.inserted.length - prevValueBeforeTail.length; 4128 } 4129 4130 if (tailValue) { 4131 // $FlowFixMe - it's ok, we don't change current mask above 4132 details.tailShift += this.currentMask.append(tailValue, { 4133 raw: true, 4134 tail: true 4135 }).tailShift; 4136 } 4137 } else { 4138 // Dispatch can do something bad with state, so 4139 // restore prev mask state 4140 this.currentMask.state = prevMaskState; 4141 } 4142 } 4143 4144 return details; 4145 } 4146 }, { 4147 key: "_appendPlaceholder", 4148 value: function _appendPlaceholder() { 4149 var details = this._applyDispatch.apply(this, arguments); 4150 4151 if (this.currentMask) { 4152 details.aggregate(this.currentMask._appendPlaceholder()); 4153 } 4154 4155 return details; 4156 } 4157 /** 4158 @override 4159 */ 4160 4161 }, { 4162 key: "_appendEager", 4163 value: function _appendEager() { 4164 var details = this._applyDispatch.apply(this, arguments); 4165 4166 if (this.currentMask) { 4167 details.aggregate(this.currentMask._appendEager()); 4168 } 4169 4170 return details; 4171 } 4172 }, { 4173 key: "currentMaskFlags", 4174 value: function currentMaskFlags(flags) { 4175 var _flags$_beforeTailSta, _flags$_beforeTailSta2; 4176 4177 return Object.assign({}, flags, { 4178 _beforeTailState: ((_flags$_beforeTailSta = flags._beforeTailState) === null || _flags$_beforeTailSta === void 0 ? void 0 : _flags$_beforeTailSta.currentMaskRef) === this.currentMask && ((_flags$_beforeTailSta2 = flags._beforeTailState) === null || _flags$_beforeTailSta2 === void 0 ? void 0 : _flags$_beforeTailSta2.currentMask) || flags._beforeTailState 4179 }); 4180 } 4181 /** 4182 @override 4183 */ 4184 4185 }, { 4186 key: "doDispatch", 4187 value: function doDispatch(appended) { 4188 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 4189 return this.dispatch(appended, this, flags); 4190 } 4191 /** 4192 @override 4193 */ 4194 4195 }, { 4196 key: "doValidate", 4197 value: function doValidate(flags) { 4198 return _get(_getPrototypeOf(MaskedDynamic.prototype), "doValidate", this).call(this, flags) && (!this.currentMask || this.currentMask.doValidate(this.currentMaskFlags(flags))); 4199 } 4200 /** 4201 @override 4202 */ 4203 4204 }, { 4205 key: "doPrepare", 4206 value: function doPrepare(str) { 4207 var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 4208 4209 var _normalizePrepare = normalizePrepare(_get(_getPrototypeOf(MaskedDynamic.prototype), "doPrepare", this).call(this, str, flags)), 4210 _normalizePrepare2 = _slicedToArray(_normalizePrepare, 2), 4211 s = _normalizePrepare2[0], 4212 details = _normalizePrepare2[1]; 4213 4214 if (this.currentMask) { 4215 var currentDetails; 4216 4217 var _normalizePrepare3 = normalizePrepare(_get(_getPrototypeOf(MaskedDynamic.prototype), "doPrepare", this).call(this, s, this.currentMaskFlags(flags))); 4218 4219 var _normalizePrepare4 = _slicedToArray(_normalizePrepare3, 2); 4220 4221 s = _normalizePrepare4[0]; 4222 currentDetails = _normalizePrepare4[1]; 4223 details = details.aggregate(currentDetails); 4224 } 4225 4226 return [s, details]; 4227 } 4228 /** 4229 @override 4230 */ 4231 4232 }, { 4233 key: "reset", 4234 value: function reset() { 4235 var _this$currentMask; 4236 4237 (_this$currentMask = this.currentMask) === null || _this$currentMask === void 0 ? void 0 : _this$currentMask.reset(); 4238 this.compiledMasks.forEach(function (m) { 4239 return m.reset(); 4240 }); 4241 } 4242 /** 4243 @override 4244 */ 4245 4246 }, {
4247 key: "value", 4248 get: function get() { 4249 return this.currentMask ? this.currentMask.value : ''; 4250 }, 4251 set: function set(value) { 4252 _set(_getPrototypeOf(MaskedDynamic.prototype), "value", value, this, true); 4253 } 4254 /** 4255 @override 4256 */ 4257 4258 }, { 4259 key: "unmaskedValue", 4260 get: function get() { 4261 return this.currentMask ? this.currentMask.unmaskedValue : ''; 4262 }, 4263 set: function set(unmaskedValue) { 4264 _set(_getPrototypeOf(MaskedDynamic.prototype), "unmaskedValue", unmaskedValue, this, true); 4265 } 4266 /** 4267 @override 4268 */ 4269 4270 }, { 4271 key: "typedValue", 4272 get: function get() { 4273 return this.currentMask ? this.currentMask.typedValue : ''; 4274 } // probably typedValue should not be used with dynamic 4275 , 4276 set: function set(value) { 4277 var unmaskedValue = String(value); // double check it 4278 4279 if (this.currentMask) { 4280 this.currentMask.typedValue = value; 4281 unmaskedValue = this.currentMask.unmaskedValue; 4282 } 4283 4284 this.unmaskedValue = unmaskedValue; 4285 } 4286 /** 4287 @override 4288 */ 4289 4290 }, { 4291 key: "isComplete", 4292 get: function get() { 4293 var _this$currentMask2; 4294 4295 return Boolean((_this$currentMask2 = this.currentMask) === null || _this$currentMask2 === void 0 ? void 0 : _this$currentMask2.isComplete); 4296 } 4297 /** 4298 @override 4299 */ 4300 4301 }, { 4302 key: "isFilled", 4303 get: function get() { 4304 var _this$currentMask3; 4305 4306 return Boolean((_this$currentMask3 = this.currentMask) === null || _this$currentMask3 === void 0 ? void 0 : _this$currentMask3.isFilled); 4307 } 4308 /** 4309 @override 4310 */ 4311 4312 }, { 4313 key: "remove", 4314 value: function remove() { 4315 var details = new ChangeDetails(); 4316 4317 if (this.currentMask) { 4318 var _this$currentMask4; 4319 4320 details.aggregate((_this$currentMask4 = this.currentMask).remove.apply(_this$currentMask4, arguments)) // update with dispatch 4321 .aggregate(this._applyDispatch()); 4322 } 4323 4324 return details; 4325 } 4326 /** 4327 @override 4328 */ 4329 4330 }, { 4331 key: "state", 4332 get: function get() { 4333 var _this$currentMask5; 4334 4335 return Object.assign({}, _get(_getPrototypeOf(MaskedDynamic.prototype), "state", this), { 4336 _rawInputValue: this.rawInputValue, 4337 compiledMasks: this.compiledMasks.map(function (m) { 4338 return m.state; 4339 }), 4340 currentMaskRef: this.currentMask, 4341 currentMask: (_this$currentMask5 = this.currentMask) === null || _this$currentMask5 === void 0 ? void 0 : _this$currentMask5.state 4342 }); 4343 }, 4344 set: function set(state) { 4345 var compiledMasks = state.compiledMasks, 4346 currentMaskRef = state.currentMaskRef, 4347 currentMask = state.currentMask, 4348 maskedState = _objectWithoutProperties(state, _excluded); 4349 4350 this.compiledMasks.forEach(function (m, mi) { 4351 return m.state = compiledMasks[mi]; 4352 }); 4353 4354 if (currentMaskRef != null) { 4355 this.currentMask = currentMaskRef; 4356 this.currentMask.state = currentMask; 4357 } 4358 4359 _set(_getPrototypeOf(MaskedDynamic.prototype), "state", maskedState, this, true); 4360 } 4361 /** 4362 @override 4363 */ 4364 4365 }, { 4366 key: "extractInput", 4367 value: function extractInput() { 4368 var _this$currentMask6; 4369 4370 return this.currentMask ? (_this$currentMask6 = this.currentMask).extractInput.apply(_this$currentMask6, arguments) : ''; 4371 } 4372 /** 4373 @override 4374 */ 4375 4376 }, { 4377 key: "extractTail", 4378 value: function extractTail() { 4379 var _this$currentMask7, _get2; 4380 4381 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { 4382 args[_key] = arguments[_key]; 4383 } 4384 4385 return this.currentMask ? (_this$currentMask7 = this.currentMask).extractTail.apply(_this$currentMask7, args) : (_get2 = _get(_getPrototypeOf(MaskedDynamic.prototype), "extractTail", this)).call.apply(_get2, [this].concat(args)); 4386 } 4387 /** 4388 @override 4389 */ 4390 4391 }, {
4392 key: "doCommit", 4393 value: function doCommit() { 4394 if (this.currentMask) this.currentMask.doCommit(); 4395 4396 _get(_getPrototypeOf(MaskedDynamic.prototype), "doCommit", this).call(this); 4397 } 4398 /** 4399 @override 4400 */ 4401 4402 }, { 4403 key: "nearestInputPos", 4404 value: function nearestInputPos() { 4405 var _this$currentMask8, _get3; 4406 4407 for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { 4408 args[_key2] = arguments[_key2]; 4409 } 4410 4411 return this.currentMask ? (_this$currentMask8 = this.currentMask).nearestInputPos.apply(_this$currentMask8, args) : (_get3 = _get(_getPrototypeOf(MaskedDynamic.prototype), "nearestInputPos", this)).call.apply(_get3, [this].concat(args)); 4412 } 4413 }, { 4414 key: "overwrite", 4415 get: function get() { 4416 return this.currentMask ? this.currentMask.overwrite : _get(_getPrototypeOf(MaskedDynamic.prototype), "overwrite", this); 4417 }, 4418 set: function set(overwrite) { 4419 console.warn('"overwrite" option is not available in dynamic mask, use this option in siblings'); 4420 } 4421 }, { 4422 key: "eager", 4423 get: function get() { 4424 return this.currentMask ? this.currentMask.eager : _get(_getPrototypeOf(MaskedDynamic.prototype), "eager", this); 4425 }, 4426 set: function set(eager) { 4427 console.warn('"eager" option is not available in dynamic mask, use this option in siblings'); 4428 } 4429 /** 4430 @override 4431 */ 4432 4433 }, { 4434 key: "maskEquals", 4435 value: function maskEquals(mask) { 4436 return Array.isArray(mask) && this.compiledMasks.every(function (m, mi) { 4437 var _mask$mi; 4438 4439 return m.maskEquals((_mask$mi = mask[mi]) === null || _mask$mi === void 0 ? void 0 : _mask$mi.mask); 4440 }); 4441 } 4442 /** 4443 @override 4444 */ 4445 4446 }, { 4447 key: "typedValueEquals", 4448 value: function typedValueEquals(value) { 4449 var _this$currentMask9; 4450 4451 return Boolean((_this$currentMask9 = this.currentMask) === null || _this$currentMask9 === void 0 ? void 0 : _this$currentMask9.typedValueEquals(value)); 4452 } 4453 }]); 4454 4455 return MaskedDynamic; 4456 }(Masked); 4457 MaskedDynamic.DEFAULTS = { 4458 dispatch: function dispatch(appended, masked, flags) { 4459 if (!masked.compiledMasks.length) return; 4460 var inputValue = masked.rawInputValue; // simulate input 4461 4462 var inputs = masked.compiledMasks.map(function (m, index) { 4463 m.reset(); 4464 m.append(inputValue, { 4465 raw: true 4466 }); 4467 m.append(appended, masked.currentMaskFlags(flags)); 4468 var weight = m.rawInputValue.length; 4469 return { 4470 weight: weight, 4471 index: index 4472 }; 4473 }); // pop masks with longer values first 4474 4475 inputs.sort(function (i1, i2) { 4476 return i2.weight - i1.weight; 4477 }); 4478 return masked.compiledMasks[inputs[0].index]; 4479 } 4480 }; 4481 IMask.MaskedDynamic = MaskedDynamic; 4482 4483 /** Mask pipe source and destination types */ 4484 4485 var PIPE_TYPE = { 4486 MASKED: 'value', 4487 UNMASKED: 'unmaskedValue', 4488 TYPED: 'typedValue' 4489 }; 4490 /** Creates new pipe function depending on mask type, source and destination options */ 4491 4492 function createPipe(mask) { 4493 var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : PIPE_TYPE.MASKED; 4494 var to = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : PIPE_TYPE.MASKED; 4495 var masked = createMask(mask); 4496 return function (value) { 4497 return masked.runIsolated(function (m) { 4498 m[from] = value; 4499 return m[to]; 4500 }); 4501 }; 4502 } 4503 /** Pipes value through mask depending on mask type, source and destination options */ 4504 4505 function pipe(value) { 4506 for (var _len = arguments.length, pipeArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 4507 pipeArgs[_key - 1] = arguments[_key]; 4508 } 4509 4510 return createPipe.apply(void 0, pipeArgs)(value); 4511 } 4512 IMask.PIPE_TYPE = PIPE_TYPE; 4513 IMask.createPipe = createPipe; 4514 IMask.pipe = pipe; 4515 4516 try { 4517 globalThis.IMask = IMask; 4518 } catch (e) {} 4519 4520 exports.HTMLContenteditableMaskElement = HTMLContenteditableMaskElement; 4521 exports.HTMLMaskElement = HTMLMaskElement; 4522 exports.InputMask = InputMask; 4523 exports.MaskElement = MaskElement; 4524 exports.Masked = Masked; 4525 exports.MaskedDate = MaskedDate; 4526 exports.MaskedDynamic = MaskedDynamic; 4527 exports.MaskedEnum = MaskedEnum; 4528 exports.MaskedFunction = MaskedFunction; 4529 exports.MaskedNumber = MaskedNumber; 4530 exports.MaskedPattern = MaskedPattern; 4531 exports.MaskedRange = MaskedRange; 4532 exports.MaskedRegExp = MaskedRegExp; 4533 exports.PIPE_TYPE = PIPE_TYPE; 4534 exports.createMask = createMask; 4535 exports.createPipe = createPipe; 4536 exports["default"] = IMask; 4537 exports.pipe = pipe; 4538 4539 Object.defineProperty(exports, '__esModule', { value: true }); 4540 4541})); 4542//# sourceMappingURL=imask.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.