all files / maskbase/ masked-date-time.js

88.38% Statements 525/594
79.39% Branches 339/427
100% Functions 27/27
88.38% Lines 525/594
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847   70× 70×                 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 64× 64× 64× 64× 64× 64× 64× 64×   138×   64×     64× 64× 64× 64× 64×   79× 13×   66× 66× 66× 66× 66×   211× 211× 211× 208×   211× 208×   211× 208×   211× 206×   211× 206×   211× 206×   211× 199×   211× 211× 211× 211× 211× 211× 211× 211× 211× 119× 119× 119×     212× 212× 212× 1484×       268× 268× 221× 13×     268× 282× 216× 216×   66× 51× 51×       120× 120× 120× 120× 75×     326× 326× 326× 4031× 1062× 1062× 326×       326×     326×   31× 31×   29×     26× 26× 26× 26×       26×   26×   29×   16×         38× 38×   142× 142× 142× 142× 142× 142× 142×   24× 53× 53×     24× 22×     120×     26×   26× 26× 26× 26×   24×     24× 22× 22× 22× 22×     22× 22×       24×       27× 27× 19×       27× 27× 27× 27× 22×     22× 20× 20× 14×         14×         20×   20× 20× 20× 20×     20× 20× 20× 20×           40× 40×   35×       22×       40×   40× 40× 40× 40× 40×     40×     40×       40× 40× 10×   40× 40×   40×                                                                             113×   955× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260× 3260×       124× 124× 124×       124×   628× 628× 628×   628×       10× 10×       124× 124× 124×       124×   628× 628× 628×   628×   36× 36×     52× 52× 52×       730× 730× 730×   58× 58×       58×   216× 216×       216×           22× 22×   22×           294× 294×   294×           198× 198×       198×                               118× 118×                       3260× 3260× 1630× 1630× 5551×   1630×     1630×     955×   141× 141× 141× 137×   141× 141× 141× 141× 141× 141× 141× 141× 141× 141× 141× 704× 137× 137×     141× 141× 141× 51× 51× 51×   141×   141×   78× 78× 78× 78× 78× 116× 59× 59×   57×     1278× 1278× 1278× 1278× 944×   1278×   2287× 2287×   2287×   57×     18× 18× 18× 18×   18×   21× 21×               56×                                                                                           20071× 20065× 20065× 19837×     228×   20065×   282× 276× 121× 121× 109×     276× 101× 101× 98×     276× 54×   276×     122× 122× 122× 122× 122× 122× 122× 122×   15×        
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ARROWLEFT = 'ArrowLeft';
    var ARROWRIGHT = 'ArrowRight';
    var ARROWUP = 'ArrowUp';
    var ARROWDOWN = 'ArrowDown';
    var TAB = 'Tab';
    var SHIFTTAB = 'shiftTab';
    var END = 'End';
    var HOME = 'Home';
    var MaskedDateTime = (function () {
        function MaskedDateTime(parent) {
            this.mask = '';
            this.defaultConstant = {
                day: 'day',
                month: 'month',
                year: 'year',
                hour: 'hour',
                minute: 'minute',
                second: 'second',
                dayOfTheWeek: 'day of the week'
            };
            this.hiddenMask = '';
            this.validCharacters = 'dMyhmHfasz';
            this.isDayPart = false;
            this.isMonthPart = false;
            this.isYearPart = false;
            this.isHourPart = false;
            this.isMinutePart = false;
            this.isSecondsPart = false;
            this.isMilliSecondsPart = false;
            this.monthCharacter = '';
            this.periodCharacter = '';
            this.isHiddenMask = false;
            this.isComplete = false;
            this.isNavigate = false;
            this.navigated = false;
            this.isBlur = false;
            this.formatRegex = /EEEEE|EEEE|EEE|EE|E|dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yyy|yy|y|HH|H|hh|h|mm|m|fff|ff|f|aa|a|ss|s|zzzz|zzz|zz|z|'[^']*'|'[^']*'/g;
            this.isDeletion = false;
            this.isShortYear = false;
            this.isDeleteKey = false;
            this.isDateZero = false;
            this.isMonthZero = false;
            this.isYearZero = false;
            this.isLeadingZero = false;
            this.dayTypeCount = 0;
            this.monthTypeCount = 0;
            this.hourTypeCount = 0;
            this.minuteTypeCount = 0;
            this.secondTypeCount = 0;
            this.parent = parent;
            this.dateformat = this.getCulturedFormat();
            this.maskDateValue = this.parent.value != null ? new Date(+this.parent.value) : new Date();
            this.maskDateValue.setMonth(0);
            this.maskDateValue.setHours(0);
            this.maskDateValue.setMinutes(0);
            this.maskDateValue.setSeconds(0);
            this.previousDate = new Date(this.maskDateValue.getFullYear(), this.maskDateValue.getMonth(), this.maskDateValue.getDate(), this.maskDateValue.getHours(), this.maskDateValue.getMinutes(), this.maskDateValue.getSeconds());
            this.removeEventListener();
            this.addEventListener();
        }
        MaskedDateTime.prototype.getModuleName = function () {
            return 'MaskedDateTime';
        };
        MaskedDateTime.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on('createMask', this.createMask, this);
            this.parent.on('setMaskSelection', this.validCharacterCheck, this);
            this.parent.on('inputHandler', this.maskInputHandler, this);
            this.parent.on('keyDownHandler', this.maskKeydownHandler, this);
            this.parent.on('clearHandler', this.clearHandler, this);
        };
        MaskedDateTime.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off('createMask', this.createMask);
            this.parent.off('setMaskSelection', this.validCharacterCheck);
            this.parent.off('inputHandler', this.maskInputHandler);
            this.parent.off('keyDownHandler', this.maskKeydownHandler);
            this.parent.off('clearHandler', this.clearHandler);
        };
        MaskedDateTime.prototype.createMask = function (mask) {
            this.isDayPart = this.isMonthPart = this.isYearPart = this.isHourPart = this.isMinutePart = this.isSecondsPart = false;
            this.dateformat = this.getCulturedFormat();
            if (this.parent.maskPlaceholder.day) {
                this.defaultConstant['day'] = this.parent.maskPlaceholder.day;
            }
            if (this.parent.maskPlaceholder.month) {
                this.defaultConstant['month'] = this.parent.maskPlaceholder.month;
            }
            if (this.parent.maskPlaceholder.year) {
                this.defaultConstant['year'] = this.parent.maskPlaceholder.year;
            }
            if (this.parent.maskPlaceholder.hour) {
                this.defaultConstant['hour'] = this.parent.maskPlaceholder.hour;
            }
            if (this.parent.maskPlaceholder.minute) {
                this.defaultConstant['minute'] = this.parent.maskPlaceholder.minute;
            }
            if (this.parent.maskPlaceholder.second) {
                this.defaultConstant['second'] = this.parent.maskPlaceholder.second;
            }
            if (this.parent.maskPlaceholder.dayOfTheWeek) {
                this.defaultConstant['dayOfTheWeek'] = this.parent.maskPlaceholder.dayOfTheWeek.toString();
            }
            this.getCUltureMaskFormat();
            var inputValue = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isHiddenMask = true;
            this.hiddenMask = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isHiddenMask = false;
            this.previousHiddenMask = this.hiddenMask;
            this.mask = this.previousValue = inputValue;
            this.parent.maskedDateValue = this.mask;
            if (this.parent.value) {
                this.navigated = true;
                this.isBlur = mask.isBlur;
                this.setDynamicValue();
            }
        };
        MaskedDateTime.prototype.getCUltureMaskFormat = function () {
            this.l10n = new ej2_base_1.L10n(this.parent.moduleName, this.defaultConstant, this.parent.locale);
            this.objectString = Object.keys(this.defaultConstant);
            for (var i = 0; i < this.objectString.length; i++) {
                this.defaultConstant[this.objectString[i].toString()] =
                    this.l10n.getConstant(this.objectString[i].toString());
            }
        };
        MaskedDateTime.prototype.validCharacterCheck = function () {
            var start = this.parent.inputElement.selectionStart;
            if (this.parent.moduleName !== 'timepicker') {
                if (start === this.hiddenMask.length && this.mask === this.parent.inputElement.value) {
                    start = 0;
                }
            }
            for (var i = start, j = start - 1; i < this.hiddenMask.length || j >= 0; i++, j--) {
                if (i < this.hiddenMask.length && this.validCharacters.indexOf(this.hiddenMask[i]) !== -1) {
                    this.setSelection(this.hiddenMask[i]);
                    return;
                }
                if (j >= 0 && this.validCharacters.indexOf(this.hiddenMask[j]) !== -1) {
                    this.setSelection(this.hiddenMask[j]);
                    return;
                }
            }
        };
        MaskedDateTime.prototype.setDynamicValue = function () {
            this.maskDateValue = new Date(+this.parent.value);
            this.isDayPart = this.isMonthPart = this.isYearPart = this.isHourPart = this.isMinutePart = this.isSecondsPart = true;
            this.updateValue();
            if (!this.isBlur) {
                this.validCharacterCheck();
            }
        };
        MaskedDateTime.prototype.setSelection = function (validChar) {
            var start = -1;
            var end = 0;
            for (var i = 0; i < this.hiddenMask.length; i++) {
                if (this.hiddenMask[i] === validChar) {
                    end = i + 1;
                    if (start === -1) {
                        start = i;
                    }
                }
            }
            Iif (start < 0) {
                start = 0;
            }
            this.parent.inputElement.setSelectionRange(start, end);
        };
        MaskedDateTime.prototype.maskKeydownHandler = function (args) {
            this.dayTypeCount = this.monthTypeCount = this.hourTypeCount = this.minuteTypeCount = this.secondTypeCount = 0;
            if (args.e.key === 'Delete') {
                this.isDeleteKey = true;
                return;
            }
            if ((!args.e.altKey && !args.e.ctrlKey) && (args.e.key === ARROWLEFT || args.e.key === ARROWRIGHT
                || args.e.key === SHIFTTAB || args.e.key === TAB || args.e.action === SHIFTTAB ||
                args.e.key === END || args.e.key === HOME)) {
                var start = this.parent.inputElement.selectionStart;
                var end = this.parent.inputElement.selectionEnd;
                var length_1 = this.parent.inputElement.value.length;
                Iif ((start === 0 && end === length_1) && (args.e.key === TAB || args.e.action === SHIFTTAB)) {
                    var index = args.e.action === SHIFTTAB ? end : 0;
                    this.parent.inputElement.selectionStart = this.parent.inputElement.selectionEnd = index;
                }
                if (args.e.key === END || args.e.key === HOME) {
                    var range = args.e.key === END ? length_1 : 0;
                    this.parent.inputElement.selectionStart = this.parent.inputElement.selectionEnd = range;
                }
                this.navigateSelection(args.e.key === ARROWLEFT || args.e.action === SHIFTTAB || args.e.key === END ? true : false);
            }
            if ((!args.e.altKey && !args.e.ctrlKey) && (args.e.key === ARROWUP || args.e.key === ARROWDOWN)) {
                var start = this.parent.inputElement.selectionStart;
                var formatText = '';
                if (this.validCharacters.indexOf(this.hiddenMask[start]) !== -1) {
                    formatText = this.hiddenMask[start];
                }
                this.dateAlteration(args.e.key === ARROWDOWN ? true : false);
                var inputValue = this.dateformat.replace(this.formatRegex, this.formatCheck());
                this.isHiddenMask = true;
                this.hiddenMask = this.dateformat.replace(this.formatRegex, this.formatCheck());
                this.isHiddenMask = false;
                this.previousHiddenMask = this.hiddenMask;
                this.previousValue = inputValue;
                this.parent.inputElement.value = inputValue;
                for (var i = 0; i < this.hiddenMask.length; i++) {
                    if (formatText === this.hiddenMask[i]) {
                        start = i;
                        break;
                    }
                }
                this.parent.inputElement.selectionStart = start;
                this.validCharacterCheck();
            }
        };
        MaskedDateTime.prototype.isPersist = function () {
            var isPersist = this.parent.isFocused || this.navigated;
            return isPersist;
        };
        MaskedDateTime.prototype.differenceCheck = function () {
            var start = this.parent.inputElement.selectionStart;
            var inputValue = this.parent.inputElement.value;
            var previousVal = this.previousValue.substring(0, start + this.previousValue.length - inputValue.length);
            var newVal = inputValue.substring(0, start);
            var newDateValue = new Date(+this.maskDateValue);
            var maxDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth() + 1, 0).getDate();
            if (previousVal.indexOf(newVal) === 0 && (newVal.length === 0 ||
                this.previousHiddenMask[newVal.length - 1] !== this.previousHiddenMask[newVal.length])) {
                for (var i = newVal.length; i < previousVal.length; i++) {
                    Eif (this.previousHiddenMask[i] !== '' && this.validCharacters.indexOf(this.previousHiddenMask[i]) >= 0) {
                        this.isDeletion = this.handleDeletion(this.previousHiddenMask[i], false);
                    }
                }
                if (this.isDeletion) {
                    return;
                }
            }
            switch (this.previousHiddenMask[start - 1]) {
                case 'd':
                    {
                        var date = (this.isDayPart && newDateValue.getDate().toString().length < 2 &&
                            !this.isPersist() ? newDateValue.getDate() * 10 : 0) + parseInt(newVal[start - 1], 10);
                        this.isDateZero = (newVal[start - 1] === '0');
                        this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                        this.navigated = this.navigated ? false : this.navigated;
                        if (isNaN(date)) {
                            return;
                        }
                        for (var i = 0; date > maxDate; i++) {
                            date = parseInt(date.toString().slice(1), 10);
                        }
                        if (date >= 1) {
                            newDateValue.setDate(date);
                            this.isNavigate = date.toString().length === 2;
                            this.previousDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth(), newDateValue.getDate());
                            Iif (newDateValue.getMonth() !== this.maskDateValue.getMonth()) {
                                return;
                            }
                            this.isDayPart = true;
                            this.dayTypeCount = this.dayTypeCount + 1;
                        }
                        else {
                            this.isDayPart = false;
                            this.dayTypeCount = this.isDateZero ? this.dayTypeCount + 1 : this.dayTypeCount;
                        }
                        break;
                    }
                case 'M':
                    {
                        var month = void 0;
                        if (newDateValue.getMonth().toString().length < 2 && !this.isPersist()) {
                            month = (this.isMonthPart ? (newDateValue.getMonth() + 1) * 10 : 0) + parseInt(newVal[start - 1], 10);
                        }
                        else {
                            month = parseInt(newVal[start - 1], 10);
                        }
                        this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                        this.navigated = this.navigated ? false : this.navigated;
                        this.isMonthZero = (newVal[start - 1] === '0');
                        if (!isNaN(month)) {
                            while (month > 12) {
                                month = parseInt(month.toString().slice(1), 10);
                            }
                            if (month >= 1) {
                                newDateValue.setMonth(month - 1);
                                if (month >= 10 || month === 1) {
                                    Iif (this.isLeadingZero && month === 1) {
                                        this.isNavigate = month.toString().length === 1;
                                        this.isLeadingZero = false;
                                    }
                                    else {
                                        this.isNavigate = month.toString().length === 2;
                                    }
                                }
                                else {
                                    this.isNavigate = month.toString().length === 1;
                                }
                                if (newDateValue.getMonth() !== month - 1) {
                                    newDateValue.setDate(1);
                                    newDateValue.setMonth(month - 1);
                                }
                                Eif (this.isDayPart) {
                                    var previousMaxDate = new Date(this.previousDate.getFullYear(), this.previousDate.getMonth() + 1, 0).getDate();
                                    var currentMaxDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth() + 1, 0).getDate();
                                    if (this.previousDate.getDate() === previousMaxDate && currentMaxDate <= previousMaxDate) {
                                        newDateValue.setDate(currentMaxDate);
                                    }
                                }
                                this.previousDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth(), newDateValue.getDate());
                                this.isMonthPart = true;
                                this.monthTypeCount = this.monthTypeCount + 1;
                                this.isLeadingZero = false;
                            }
                            else {
                                newDateValue.setMonth(0);
                                this.isLeadingZero = true;
                                this.isMonthPart = false;
                                this.monthTypeCount = this.isMonthZero ? this.monthTypeCount + 1 : this.monthTypeCount;
                            }
                        }
                        else {
                            var monthString = (this.getCulturedValue('months[stand-alone].wide'));
                            var monthValue = Object.keys(monthString);
                            this.monthCharacter += newVal[start - 1].toLowerCase();
                            while (this.monthCharacter.length > 0) {
                                var i = 1;
                                for (var _i = 0, monthValue_1 = monthValue; _i < monthValue_1.length; _i++) {
                                    var months = monthValue_1[_i];
                                    if (monthString[i].toLowerCase().indexOf(this.monthCharacter) === 0) {
                                        newDateValue.setMonth(i - 1);
                                        this.isMonthPart = true;
                                        this.maskDateValue = newDateValue;
                                        return;
                                    }
                                    i++;
                                }
                                this.monthCharacter = this.monthCharacter.substring(1, this.monthCharacter.length);
                            }
                        }
                        break;
                    }
                case 'y':
                    {
                        var year = (this.isYearPart && (newDateValue.getFullYear().toString().length < 4
                            && !this.isShortYear) ? newDateValue.getFullYear() * 10 : 0) + parseInt(newVal[start - 1], 10);
                        var yearValue = (this.dateformat.match(/y/g) || []).length;
                        yearValue = yearValue !== 2 ? 4 : yearValue;
                        this.isShortYear = false;
                        this.isYearZero = (newVal[start - 1] === '0');
                        Iif (isNaN(year)) {
                            return;
                        }
                        while (year > 9999) {
                            year = parseInt(year.toString().slice(1), 10);
                        }
                        Iif (year < 1) {
                            this.isYearPart = false;
                        }
                        else {
                            newDateValue.setFullYear(year);
                            if (year.toString().length === yearValue) {
                                this.isNavigate = true;
                            }
                            this.previousDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth(), newDateValue.getDate());
                            this.isYearPart = true;
                        }
                        break;
                    }
                case 'h':
                    this.hour = (this.isHourPart && (newDateValue.getHours() % 12 || 12).toString().length < 2
                        && !this.isPersist() ? (newDateValue.getHours() % 12 || 12) * 10 : 0) + parseInt(newVal[start - 1], 10);
                    this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                    this.navigated = this.navigated ? false : this.navigated;
                    Iif (isNaN(this.hour)) {
                        return;
                    }
                    while (this.hour > 12) {
                        this.hour = parseInt(this.hour.toString().slice(1), 10);
                    }
                    newDateValue.setHours(Math.floor(newDateValue.getHours() / 12) * 12 + (this.hour % 12));
                    this.isNavigate = this.hour.toString().length === 2;
                    this.isHourPart = true;
                    this.hourTypeCount = this.hourTypeCount + 1;
                    break;
                case 'H':
                    this.hour = (this.isHourPart && newDateValue.getHours().toString().length < 2 &&
                        !this.isPersist() ? newDateValue.getHours() * 10 : 0) + parseInt(newVal[start - 1], 10);
                    this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                    this.navigated = this.navigated ? false : this.navigated;
                    Iif (isNaN(this.hour)) {
                        return;
                    }
                    for (var i = 0; this.hour > 23; i++) {
                        this.hour = parseInt(this.hour.toString().slice(1), 10);
                    }
                    newDateValue.setHours(this.hour);
                    this.isNavigate = this.hour.toString().length === 2;
                    this.isHourPart = true;
                    this.hourTypeCount = this.hourTypeCount + 1;
                    break;
                case 'm':
                    {
                        var minutes = (this.isMinutePart && newDateValue.getMinutes().toString().length < 2
                            && !this.isPersist() ? newDateValue.getMinutes() * 10 : 0) + parseInt(newVal[start - 1], 10);
                        this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                        this.navigated = this.navigated ? false : this.navigated;
                        Iif (isNaN(minutes)) {
                            return;
                        }
                        for (var i = 0; minutes > 59; i++) {
                            minutes = parseInt(minutes.toString().slice(1), 10);
                        }
                        newDateValue.setMinutes(minutes);
                        this.isNavigate = minutes.toString().length === 2;
                        this.isMinutePart = true;
                        this.minuteTypeCount = this.minuteTypeCount + 1;
                        break;
                    }
                case 's':
                    {
                        var seconds = (this.isSecondsPart && newDateValue.getSeconds().toString().length < 2 &&
                            !this.isPersist() ? newDateValue.getSeconds() * 10 : 0) + parseInt(newVal[start - 1], 10);
                        this.parent.isFocused = this.parent.isFocused ? false : this.parent.isFocused;
                        this.navigated = this.navigated ? false : this.navigated;
                        Iif (isNaN(seconds)) {
                            return;
                        }
                        for (var i = 0; seconds > 59; i++) {
                            seconds = parseInt(seconds.toString().slice(1), 10);
                        }
                        newDateValue.setSeconds(seconds);
                        this.isNavigate = seconds.toString().length === 2;
                        this.isSecondsPart = true;
                        this.secondTypeCount = this.secondTypeCount + 1;
                        break;
                    }
                case 'a':
                    {
                        this.periodCharacter += newVal[start - 1].toLowerCase();
                        var periodString = (this.getCulturedValue('dayPeriods.format.wide'));
                        var periodkeys = Object.keys(periodString);
                        for (var i = 0; this.periodCharacter.length > 0; i++) {
                            Eif ((periodString[periodkeys[0]].toLowerCase().indexOf(this.periodCharacter) === 0
                                && newDateValue.getHours() >= 12) || (periodString[periodkeys[1]].toLowerCase().
                                indexOf(this.periodCharacter) === 0 && newDateValue.getHours() < 12)) {
                                newDateValue.setHours((newDateValue.getHours() + 12) % 24);
                                this.maskDateValue = newDateValue;
                            }
                            this.periodCharacter = this.periodCharacter.substring(1, this.periodCharacter.length);
                        }
                        break;
                    }
                default:
                    break;
            }
            this.maskDateValue = newDateValue;
        };
        MaskedDateTime.prototype.formatCheck = function () {
            var proxy = null || this;
            function formatValueSpecifier(formattext) {
                var result;
                var daysAbbreviated = proxy.getCulturedValue('days[stand-alone].abbreviated');
                var dayKeyAbbreviated = Object.keys(daysAbbreviated);
                var daysWide = (proxy.getCulturedValue('days[stand-alone].wide'));
                var dayKeyWide = Object.keys(daysWide);
                var daysNarrow = (proxy.getCulturedValue('days[stand-alone].narrow'));
                var dayKeyNarrow = Object.keys(daysNarrow);
                var monthAbbreviated = (proxy.getCulturedValue('months[stand-alone].abbreviated'));
                var monthWide = (proxy.getCulturedValue('months[stand-alone].wide'));
                var periodString = (proxy.getCulturedValue('dayPeriods.format.wide'));
                var periodkeys = Object.keys(periodString);
                var milliseconds;
                var dateOptions;
                switch (formattext) {
                    case 'ddd':
                    case 'dddd':
                    case 'd':
                        result = proxy.isDayPart ? proxy.maskDateValue.getDate().toString() : proxy.defaultConstant['day'].toString();
                        result = proxy.zeroCheck(proxy.isDateZero, proxy.isDayPart, result);
                        Iif (proxy.dayTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.dayTypeCount = 0;
                        }
                        break;
                    case 'dd':
                        result = proxy.isDayPart ? proxy.roundOff(proxy.maskDateValue.getDate(), 2) : proxy.defaultConstant['day'].toString();
                        result = proxy.zeroCheck(proxy.isDateZero, proxy.isDayPart, result);
                        if (proxy.dayTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.dayTypeCount = 0;
                        }
                        break;
                    case 'E':
                    case 'EE':
                    case 'EEE':
                        result = proxy.isDayPart && proxy.isMonthPart && proxy.isYearPart ? daysAbbreviated[dayKeyAbbreviated[proxy.maskDateValue.getDay()]].toString() : proxy.defaultConstant['dayOfTheWeek'].toString();
                        break;
                    case 'EEEE':
                        result = proxy.isDayPart && proxy.isMonthPart && proxy.isYearPart ? daysWide[dayKeyWide[proxy.maskDateValue.getDay()]].toString() : proxy.defaultConstant['dayOfTheWeek'].toString();
                        break;
                    case 'EEEEE':
                        result = proxy.isDayPart && proxy.isMonthPart && proxy.isYearPart ? daysNarrow[dayKeyNarrow[proxy.maskDateValue.getDay()]].toString() : proxy.defaultConstant['dayOfTheWeek'].toString();
                        break;
                    case 'M':
                        result = proxy.isMonthPart ? (proxy.maskDateValue.getMonth() + 1).toString() : proxy.defaultConstant['month'].toString();
                        result = proxy.zeroCheck(proxy.isMonthZero, proxy.isMonthPart, result);
                        Iif (proxy.monthTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.monthTypeCount = 0;
                        }
                        break;
                    case 'MM':
                        result = proxy.isMonthPart ? proxy.roundOff(proxy.maskDateValue.getMonth() + 1, 2) : proxy.defaultConstant['month'].toString();
                        result = proxy.zeroCheck(proxy.isMonthZero, proxy.isMonthPart, result);
                        if (proxy.monthTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.monthTypeCount = 0;
                        }
                        break;
                    case 'MMM':
                        result = proxy.isMonthPart ? monthAbbreviated[proxy.maskDateValue.getMonth() + 1] : proxy.defaultConstant['month'].toString();
                        break;
                    case 'MMMM':
                        result = proxy.isMonthPart ? monthWide[proxy.maskDateValue.getMonth() + 1] : proxy.defaultConstant['month'].toString();
                        break;
                    case 'yy':
                        result = proxy.isYearPart ? proxy.roundOff(proxy.maskDateValue.getFullYear() % 100, 2) : proxy.defaultConstant['year'].toString();
                        result = proxy.zeroCheck(proxy.isYearZero, proxy.isYearPart, result);
                        break;
                    case 'y':
                    case 'yyy':
                    case 'yyyy':
                        result = proxy.isYearPart ? proxy.roundOff(proxy.maskDateValue.getFullYear(), 4) : proxy.defaultConstant['year'].toString();
                        result = proxy.zeroCheck(proxy.isYearZero, proxy.isYearPart, result);
                        break;
                    case 'h':
                        result = proxy.isHourPart ? (proxy.maskDateValue.getHours() % 12 || 12).toString() : proxy.defaultConstant['hour'].toString();
                        Iif (proxy.hourTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.hourTypeCount = 0;
                        }
                        break;
                    case 'hh':
                        result = proxy.isHourPart ? proxy.roundOff(proxy.maskDateValue.getHours() % 12 || 12, 2) : proxy.defaultConstant['hour'].toString();
                        Iif (proxy.hourTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.hourTypeCount = 0;
                        }
                        break;
                    case 'H':
                        result = proxy.isHourPart ? proxy.maskDateValue.getHours().toString() : proxy.defaultConstant['hour'].toString();
                        Iif (proxy.hourTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.hourTypeCount = 0;
                        }
                        break;
                    case 'HH':
                        result = proxy.isHourPart ? proxy.roundOff(proxy.maskDateValue.getHours(), 2) : proxy.defaultConstant['hour'].toString();
                        if (proxy.hourTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.hourTypeCount = 0;
                        }
                        break;
                    case 'm':
                        result = proxy.isMinutePart ? proxy.maskDateValue.getMinutes().toString() : proxy.defaultConstant['minute'].toString();
                        Iif (proxy.minuteTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.minuteTypeCount = 0;
                        }
                        break;
                    case 'mm':
                        result = proxy.isMinutePart ? proxy.roundOff(proxy.maskDateValue.getMinutes(), 2) : proxy.defaultConstant['minute'].toString();
                        if (proxy.minuteTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.minuteTypeCount = 0;
                        }
                        break;
                    case 's':
                        result = proxy.isSecondsPart ? proxy.maskDateValue.getSeconds().toString() : proxy.defaultConstant['second'].toString();
                        Iif (proxy.secondTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.secondTypeCount = 0;
                        }
                        break;
                    case 'ss':
                        result = proxy.isSecondsPart ? proxy.roundOff(proxy.maskDateValue.getSeconds(), 2) : proxy.defaultConstant['second'].toString();
                        Iif (proxy.secondTypeCount === 2) {
                            proxy.isNavigate = true;
                            proxy.secondTypeCount = 0;
                        }
                        break;
                    case 'f':
                        result = Math.floor(proxy.maskDateValue.getMilliseconds() / 100).toString();
                        break;
                    case 'ff':
                        milliseconds = proxy.maskDateValue.getMilliseconds();
                        if (proxy.maskDateValue.getMilliseconds() > 99) {
                            milliseconds = Math.floor(proxy.maskDateValue.getMilliseconds() / 10);
                        }
                        result = proxy.roundOff(milliseconds, 2);
                        break;
                    case 'fff':
                        result = proxy.roundOff(proxy.maskDateValue.getMilliseconds(), 3);
                        break;
                    case 'a':
                    case 'aa':
                        result = proxy.maskDateValue.getHours() < 12 ? periodString['am'] : periodString['pm'];
                        break;
                    case 'z':
                    case 'zz':
                    case 'zzz':
                    case 'zzzz':
                        dateOptions = {
                            format: formattext,
                            type: 'dateTime', skeleton: 'yMd', calendar: proxy.parent.calendarMode
                        };
                        result = proxy.parent.globalize.formatDate(proxy.maskDateValue, dateOptions);
                        break;
                }
                result = result !== undefined ? result : formattext.slice(1, formattext.length - 1);
                if (proxy.isHiddenMask) {
                    var hiddenChar = '';
                    for (var i = 0; i < result.length; i++) {
                        hiddenChar += formattext[0];
                    }
                    return hiddenChar;
                }
                else {
                    return result;
                }
            }
            return formatValueSpecifier;
        };
        MaskedDateTime.prototype.maskInputHandler = function () {
            var start = this.parent.inputElement.selectionStart;
            var formatText = '';
            if (this.validCharacters.indexOf(this.hiddenMask[start]) !== -1) {
                formatText = this.hiddenMask[start];
            }
            this.differenceCheck();
            var inputValue = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isHiddenMask = true;
            this.hiddenMask = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isDateZero = this.isMonthZero = this.isYearZero = false;
            this.isHiddenMask = false;
            this.previousHiddenMask = this.hiddenMask;
            this.previousValue = inputValue;
            this.parent.inputElement.value = inputValue;
            this.parent.inputElement.value = inputValue;
            for (var i = 0; i < this.hiddenMask.length; i++) {
                if (formatText === this.hiddenMask[i]) {
                    start = i;
                    break;
                }
            }
            this.parent.inputElement.selectionStart = start;
            this.validCharacterCheck();
            if ((this.isNavigate || this.isDeletion) && !this.isDeleteKey) {
                var isbackward = this.isNavigate ? false : true;
                this.isNavigate = this.isDeletion = false;
                this.navigateSelection(isbackward);
            }
            if (this.isDeleteKey) {
                this.isDeletion = false;
            }
            this.isDeleteKey = false;
        };
        MaskedDateTime.prototype.navigateSelection = function (isbackward) {
            var start = this.parent.inputElement.selectionStart;
            var end = this.parent.inputElement.selectionEnd;
            var formatIndex = isbackward ? start - 1 : end;
            this.navigated = true;
            while (formatIndex < this.hiddenMask.length && formatIndex >= 0) {
                if (this.validCharacters.indexOf(this.hiddenMask[formatIndex]) >= 0) {
                    this.setSelection(this.hiddenMask[formatIndex]);
                    break;
                }
                formatIndex = formatIndex + (isbackward ? -1 : 1);
            }
        };
        MaskedDateTime.prototype.roundOff = function (val, count) {
            var valueText = val.toString();
            var length = count - valueText.length;
            var result = '';
            for (var i = 0; i < length; i++) {
                result += '0';
            }
            return result + valueText;
        };
        MaskedDateTime.prototype.zeroCheck = function (isZero, isDayPart, resultValue) {
            var result = resultValue;
            if (isZero && !isDayPart) {
                result = '0';
            }
            return result;
        };
        MaskedDateTime.prototype.handleDeletion = function (format, isSegment) {
            switch (format) {
                case 'd':
                    this.isDayPart = isSegment;
                    break;
                case 'M':
                    this.isMonthPart = isSegment;
                    Eif (!isSegment) {
                        this.maskDateValue.setMonth(0);
                        this.monthCharacter = '';
                    }
                    break;
                case 'y':
                    this.isYearPart = isSegment;
                    break;
                case 'H':
                case 'h':
                    this.isHourPart = isSegment;
                    if (!isSegment) {
                        this.periodCharacter = '';
                    }
                    break;
                case 'm':
                    this.isMinutePart = isSegment;
                    break;
                case 's':
                    this.isSecondsPart = isSegment;
                    break;
                default:
                    return false;
            }
            return true;
        };
        MaskedDateTime.prototype.dateAlteration = function (isDecrement) {
            var start = this.parent.inputElement.selectionStart;
            var formatText = '';
            if (this.validCharacters.indexOf(this.hiddenMask[start]) !== -1) {
                formatText = this.hiddenMask[start];
            }
            else {
                return;
            }
            var newDateValue = new Date(this.maskDateValue.getFullYear(), this.maskDateValue.getMonth(), this.maskDateValue.getDate(), this.maskDateValue.getHours(), this.maskDateValue.getMinutes(), this.maskDateValue.getSeconds());
            this.previousDate = new Date(this.maskDateValue.getFullYear(), this.maskDateValue.getMonth(), this.maskDateValue.getDate(), this.maskDateValue.getHours(), this.maskDateValue.getMinutes(), this.maskDateValue.getSeconds());
            var incrementValue = isDecrement ? -1 : 1;
            switch (formatText) {
                case 'd':
                    newDateValue.setDate(newDateValue.getDate() + incrementValue);
                    break;
                case 'M':
                    {
                        var newMonth = newDateValue.getMonth() + incrementValue;
                        newDateValue.setDate(1);
                        newDateValue.setMonth(newMonth);
                        if (this.isDayPart) {
                            var previousMaxDate = new Date(this.previousDate.getFullYear(), this.previousDate.getMonth() + 1, 0).getDate();
                            var currentMaxDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth() + 1, 0).getDate();
                            if (this.previousDate.getDate() === previousMaxDate && currentMaxDate <= previousMaxDate) {
                                newDateValue.setDate(currentMaxDate);
                            }
                            else {
                                newDateValue.setDate(this.previousDate.getDate());
                            }
                        }
                        else {
                            newDateValue.setDate(this.previousDate.getDate());
                        }
                        this.previousDate = new Date(newDateValue.getFullYear(), newDateValue.getMonth(), newDateValue.getDate());
                        break;
                    }
                case 'y':
                    newDateValue.setFullYear(newDateValue.getFullYear() + incrementValue);
                    break;
                case 'H':
                case 'h':
                    newDateValue.setHours(newDateValue.getHours() + incrementValue);
                    break;
                case 'm':
                    newDateValue.setMinutes(newDateValue.getMinutes() + incrementValue);
                    break;
                case 's':
                    newDateValue.setSeconds(newDateValue.getSeconds() + incrementValue);
                    break;
                case 'a':
                    newDateValue.setHours((newDateValue.getHours() + 12) % 24);
                    break;
                default:
                    break;
            }
            this.maskDateValue = newDateValue.getFullYear() > 0 ? newDateValue : this.maskDateValue;
            Eif (this.validCharacters.indexOf(this.hiddenMask[start]) !== -1) {
                this.handleDeletion(this.hiddenMask[start], true);
            }
        };
        MaskedDateTime.prototype.getCulturedValue = function (format) {
            var locale = this.parent.locale;
            var result;
            if (locale === 'en' || locale === 'en-US') {
                result = ej2_base_1.getValue(format, ej2_base_1.getDefaultDateObject());
            }
            else {
                result = ej2_base_1.getValue('main.' + '' + locale + ('.dates.calendars.gregorian.' + format), ej2_base_1.cldrData);
            }
            return result;
        };
        MaskedDateTime.prototype.getCulturedFormat = function () {
            var formatString = (this.getCulturedValue('dateTimeFormats[availableFormats].yMd')).toString();
            if (this.parent.moduleName === 'datepicker') {
                formatString = (this.getCulturedValue('dateTimeFormats[availableFormats].yMd')).toString();
                if (this.parent.format && this.parent.formatString) {
                    formatString = this.parent.formatString;
                }
            }
            if (this.parent.moduleName === 'datetimepicker') {
                formatString = (this.getCulturedValue('dateTimeFormats[availableFormats].yMd')).toString();
                if (this.parent.dateTimeFormat) {
                    formatString = this.parent.dateTimeFormat;
                }
            }
            if (this.parent.moduleName === 'timepicker') {
                formatString = this.parent.cldrTimeFormat();
            }
            return formatString;
        };
        MaskedDateTime.prototype.clearHandler = function () {
            this.isDayPart = this.isMonthPart = this.isYearPart = this.isHourPart = this.isMinutePart = this.isSecondsPart = false;
            this.updateValue();
        };
        MaskedDateTime.prototype.updateValue = function () {
            this.monthCharacter = this.periodCharacter = '';
            var inputValue = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isHiddenMask = true;
            this.hiddenMask = this.dateformat.replace(this.formatRegex, this.formatCheck());
            this.isHiddenMask = false;
            this.previousHiddenMask = this.hiddenMask;
            this.previousValue = inputValue;
            this.parent.updateInputValue(inputValue);
        };
        MaskedDateTime.prototype.destroy = function () {
            this.removeEventListener();
        };
        return MaskedDateTime;
    }());
    exports.MaskedDateTime = MaskedDateTime;
});