all files / spreadsheet/integrations/ sheet-tabs.js

95.77% Statements 475/496
86.46% Branches 198/229
98.31% Functions 58/59
95.76% Lines 474/495
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   882× 882× 882× 882×   126650×   882× 882×       882× 882×     882×       882× 882× 882× 882× 882× 882× 882× 882× 882× 882×         69× 69×   66×         882× 882× 882× 882× 882×           195× 88×   107×       107× 107× 107×       175× 161×   175×     882× 882× 882×     882× 882× 882× 882× 882× 882× 882× 1073× 1073× 1073×   882× 882×   178× 178× 83×   95×           95× 95×           95× 95× 95× 95×       95× 95× 92×     92×     103× 95×   103× 103×   14× 14×   14×   14× 14×     14× 14× 14× 14× 11× 11× 11×   11× 11×   14× 14×   939× 939× 939× 939× 939× 1223× 1223× 1223×   939×   57× 57× 57× 57× 57× 57×     30× 30× 14×   30× 45× 45× 45×   30× 14×   30× 30× 19× 16×       11×     102× 84×     18×       101× 101× 90×   101×       404× 404× 373×   31× 31× 31× 31× 55× 31×   31×     31×   31×   31×   31×       31×       31×   41× 41× 180×     41×   18× 18× 18× 18× 18×           18× 18×   17×           17× 17× 17× 17× 17× 17× 17× 17×                         18×   17×   15× 15×     16× 16×       16× 16× 16× 16× 15× 15× 14× 14× 65×         13× 13× 13× 13×   13× 12×                   15×   19×   15× 15× 15× 15× 15× 15× 15× 15× 15×     14×   15× 15× 15× 15× 15×                       15× 15× 15× 15× 15× 15× 15× 15× 15×                         242× 242×     10× 10×     10× 10×                                                                                         10855× 10855× 9651× 9651×   1204× 1204× 1204× 802× 802× 156× 156× 156× 156× 156×   802×   802× 802× 802× 802× 509× 509× 509×                     509× 509×     293×       402×     806× 806× 806× 806× 806× 806× 4208× 4208× 3406× 3406× 3406×     806×   297× 297×   297× 297× 297×   18851× 509× 509× 509×     882× 882× 882× 882× 882× 882× 882× 882× 882× 882× 882× 882× 882× 882× 882×   882× 882× 882×   882× 882× 882×   882× 882× 882× 882× 882×   882× 882× 882× 882× 882×   882× 882×   882×   882× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16×          
define(["require", "exports", "@syncfusion/ej2-navigations", "../common/index", "../common/index", "../common/index", "../common/index", "../../workbook/index", "../../workbook/index", "@syncfusion/ej2-splitbuttons", "@syncfusion/ej2-popups", "@syncfusion/ej2-base", "../../workbook/common/index", "./../../workbook/common/index"], function (require, exports, ej2_navigations_1, index_1, index_2, index_3, index_4, index_5, index_6, ej2_splitbuttons_1, ej2_popups_1, ej2_base_1, index_7, index_8) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SheetTabs = (function () {
        function SheetTabs(parent) {
            this.aggregateContent = '';
            this.selaggregateCnt = 'Sum';
            this.parent = parent;
            this.addEventListener();
        }
        SheetTabs.prototype.getModuleName = function () {
            return 'sheetTabs';
        };
        SheetTabs.prototype.createSheetTabs = function () {
            var _this = this;
            Iif (!this.parent.showSheetTabs && this.tabInstance) {
                this.destroy();
                return;
            }
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            var panel = this.parent.createElement('div', {
                className: 'e-sheet-tab-panel', id: this.parent.element.id + '_sheet_tab_panel'
            });
            var addBtn = this.parent.createElement('button', {
                className: 'e-add-sheet-tab e-btn e-css e-flat e-icon-btn' + (this.parent.allowInsert ? '' : ' e-disabled'),
                attrs: { 'title': l10n.getConstant('AddSheet'), 'type': 'button' }
            });
            addBtn.appendChild(this.parent.createElement('span', { className: 'e-btn-icon e-icons e-add-icon' }));
            addBtn.addEventListener('click', this.addSheetTab.bind(this));
            addBtn.disabled = !this.parent.allowInsert;
            panel.appendChild(addBtn);
            this.addBtnRipple = ej2_base_1.rippleEffect(panel, { selector: '.e-add-sheet-tab' });
            var ddb = this.parent.createElement('button', { attrs: { 'title': l10n.getConstant('ListAllSheets'), 'type': 'button' } });
            panel.appendChild(ddb);
            this.parent.element.appendChild(panel);
            var items = this.getSheetTabItems();
            this.dropDownInstance = new ej2_splitbuttons_1.DropDownButton({
                iconCss: 'e-icons',
                items: items.ddbItems,
                createPopupOnClick: true,
                beforeItemRender: function (args) {
                    var sheet = _this.parent.sheets[_this.dropDownInstance.items.indexOf(args.item)];
                    if (sheet.state === 'Hidden') {
                        args.element.classList.add('e-hide');
                    }
                    else if (sheet.state === 'VeryHidden') {
                        args.element.style.display = 'none';
                    }
                },
                select: function (args) { return _this.updateSheetTab({ idx: _this.dropDownInstance.items.indexOf(args.item) }); },
                beforeOpen: function (args) { return _this.beforeOpenHandler(_this.dropDownInstance, args.element, l10n.getConstant('ListAllSheets')); },
                open: function (args) { return _this.openHandler(_this.dropDownInstance, args.element, 'left'); },
                cssClass: 'e-sheets-list e-flat e-caret-hide',
                close: function () { return _this.focusTab(_this.tabInstance.element); }
            });
            this.dropDownInstance.createElement = this.parent.createElement;
            this.dropDownInstance.appendTo(ddb);
            var sheetTab = this.parent.createElement('div', { className: 'e-sheet-tab' });
            var cancelSelect;
            this.tabInstance = new ej2_navigations_1.Tab({
                selectedItem: this.parent.activeSheetIndex,
                overflowMode: 'Scrollable',
                items: items.tabItems,
                scrollStep: 250,
                selecting: function (args) {
                    if (args.selectingIndex === args.selectedIndex) {
                        return;
                    }
                    Iif (cancelSelect) {
                        cancelSelect = false;
                    }
                    else {
                        var beginEventArgs = { currentSheetIndex: args.selectingIndex, previousSheetIndex: args.selectedIndex, cancel: false };
                        _this.parent.notify(index_6.beginAction, { eventArgs: beginEventArgs, action: 'gotoSheet' });
                        cancelSelect = beginEventArgs.cancel;
                    }
                },
                selected: function (args) {
                    if (!args.isInteracted) {
                        args.preventFocus = true;
                    }
                    _this.goToSheet(args, cancelSelect, true);
                },
                created: function () {
                    var tBarItems = _this.tabInstance.element.querySelector('.e-toolbar-items');
                    tBarItems.classList.add('e-sheet-tabs-items');
                    ej2_base_1.EventHandler.add(tBarItems, 'dblclick', _this.renameSheetTab, _this);
                }
            });
            panel.appendChild(sheetTab);
            this.tabInstance.createElement = this.parent.createElement;
            this.tabInstance.appendTo(sheetTab);
            ej2_base_1.EventHandler.remove(this.tabInstance.element, 'keydown', this.tabInstance.spaceKeyDown);
            var sheetCount = items.tabItems.length;
            var sheet;
            for (var i = 0; i < sheetCount; i++) {
                sheet = index_6.getSheet(this.parent, i);
                var arg = { action: 'addSheet', sheetName: 'Sheet' + sheet.id, sheetId: sheet.id, visibleName: sheet.name };
                this.parent.notify(index_7.workbookFormulaOperation, arg);
            }
            this.parent.notify(index_7.workbookFormulaOperation, { action: 'initiateDefinedNames' });
            this.parent.notify(index_4.protectSheet, null);
        };
        SheetTabs.prototype.goToSheet = function (args, cancelSelect, triggerEvent) {
            var _this = this;
            if (args.selectedIndex === args.previousIndex) {
                return;
            }
            Iif (cancelSelect) {
                this.tabInstance.selectedItem = args.previousIndex;
                this.tabInstance.dataBind();
                index_2.focus(this.parent.element);
                return;
            }
            this.parent.notify(index_1.removeDesignChart, {});
            if (this.parent.isEdit) {
                var selection = window.getSelection();
                var editArgs = { action: 'getCurrentEditValue', editedValue: '' };
                this.parent.notify(index_4.editOperation, editArgs);
                var formula = editArgs.editedValue ? index_7.checkIsFormula(editArgs.editedValue, true) : false;
                Iif (!formula && selection && selection.focusNode && selection.focusNode.classList &&
                    selection.focusNode.classList.contains('e-formula-bar-panel')) {
                    formula = index_7.checkIsFormula(this.parent.element.querySelector('.e-formula-bar').value, true);
                }
                if (!formula) {
                    this.parent.endEdit();
                }
            }
            this.parent.activeSheetIndex = args.selectedIndex;
            this.parent.dataBind();
            this.updateDropDownItems(args.selectedIndex, args.previousIndex);
            var eventArgs = {
                action: 'registerGridInCalc',
                sheetID: (args.selectedIndex + 1).toString()
            };
            this.parent.notify(index_7.workbookFormulaOperation, eventArgs);
            if (triggerEvent) {
                this.parent.notify(index_2.completeAction, {
                    eventArgs: { previousSheetIndex: args.previousIndex, currentSheetIndex: args.selectedIndex }, action: 'gotoSheet'
                });
                index_2.getUpdateUsingRaf(function () { return index_2.focus(_this.parent.element); });
            }
        };
        SheetTabs.prototype.updateDropDownItems = function (curIdx, prevIdx) {
            if (prevIdx > -1) {
                this.dropDownInstance.items[prevIdx].iconCss = '';
            }
            this.dropDownInstance.items[curIdx].iconCss = 'e-selected-icon e-icons';
            this.dropDownInstance.setProperties({ 'items': this.dropDownInstance.items }, true);
        };
        SheetTabs.prototype.beforeOpenHandler = function (instance, element, localeText) {
            var viewportHeight = this.parent.viewport.height;
            var actualHeight = (parseInt(getComputedStyle(element.firstElementChild).height, 10) *
                instance.items.length) + (parseInt(getComputedStyle(element).paddingTop, 10) * 2);
            if (actualHeight > viewportHeight) {
                element.style.height = viewportHeight + "px";
                element.style.overflowY = 'auto';
            }
            element.parentElement.style.visibility = 'hidden';
            if (localeText) {
                element.setAttribute('aria-label', localeText);
            }
        };
        SheetTabs.prototype.openHandler = function (instance, element, positionX) {
            var wrapper = element.parentElement;
            var height;
            var collide = ej2_popups_1.isCollide(wrapper);
            if (collide.indexOf('bottom') === -1) {
                height = element.style.overflowY === 'auto' ? this.parent.viewport.height : wrapper.getBoundingClientRect().height;
                var offset = ej2_popups_1.calculatePosition(instance.element, positionX, 'top');
                if (positionX === 'right') {
                    offset.left -= wrapper.getBoundingClientRect().width;
                }
                wrapper.style.left = offset.left + "px";
                wrapper.style.top = offset.top - height + "px";
            }
            wrapper.style.visibility = '';
            index_2.focus(element);
        };
        SheetTabs.prototype.getSheetTabItems = function () {
            var _this = this;
            var tabItems = [];
            var ddbItems = [];
            var sheetName;
            this.parent.sheets.forEach(function (sheet, index) {
                sheetName = index_5.getSheetName(_this.parent, index).replace(/</g, '&lt;').replace(/>/g, '&gt;');
                tabItems.push({ header: { 'text': sheetName }, visible: sheet.state === 'Visible' });
                ddbItems.push({ text: sheetName, iconCss: index === _this.parent.activeSheetIndex ? 'e-selected-icon e-icons' : '' });
            });
            return { tabItems: tabItems, ddbItems: ddbItems };
        };
        SheetTabs.prototype.refreshSheetTab = function () {
            var items = this.getSheetTabItems();
            this.dropDownInstance.items = items.ddbItems;
            this.dropDownInstance.setProperties({ 'items': this.dropDownInstance.items }, true);
            this.tabInstance.items = items.tabItems;
            this.tabInstance.selectedItem = this.parent.activeSheetIndex;
            this.tabInstance.dataBind();
        };
        SheetTabs.prototype.addSheetTab = function () {
            this.parent.notify(index_8.insertModel, { model: this.parent, start: this.parent.activeSheetIndex + 1, end: this.parent.activeSheetIndex + 1, modelType: 'Sheet', isAction: true, activeSheetIndex: this.parent.activeSheetIndex + 1 });
        };
        SheetTabs.prototype.insertSheetTab = function (args) {
            this.parent.notify(index_1.removeDesignChart, {});
            if (!args.preventUpdate || args.startIdx === this.tabInstance.selectedItem) {
                this.dropDownInstance.items[this.tabInstance.selectedItem].iconCss = '';
            }
            for (var i = args.startIdx; i <= args.endIdx; i++) {
                var sheetName = this.parent.sheets[i].name.replace(/</g, '&lt;').replace(/>/g, '&gt;');
                this.dropDownInstance.items.splice(i, 0, { text: sheetName });
                this.tabInstance.addTab([{ header: { text: sheetName }, content: '' }], i);
            }
            if (!args.preventUpdate || args.startIdx === this.tabInstance.selectedItem) {
                this.dropDownInstance.items[args.startIdx].iconCss = 'e-selected-icon e-icons';
            }
            this.dropDownInstance.setProperties({ 'items': this.dropDownInstance.items }, true);
            if (args.preventUpdate) {
                if (args.startIdx !== this.tabInstance.selectedItem) {
                    this.refreshSheetTab();
                }
            }
            else {
                this.updateSheetTab({ idx: args.startIdx, preventDataBind: true });
            }
        };
        SheetTabs.prototype.updateSheetTab = function (args) {
            if (args.name === 'activeSheetChanged') {
                args.idx = this.parent.skipHiddenSheets(args.idx);
            }
            else {
                if (this.parent.sheets[args.idx].state === 'Hidden') {
                    if (this.parent.isProtected) {
                        return;
                    }
                    this.showSheet({ sheetIndex: args.idx, triggerEvent: true });
                }
            }
            this.tabInstance.selectedItem = args.idx;
            if (!args.preventDataBind) {
                this.tabInstance.dataBind();
            }
            this.parent.notify(index_4.protectSheet, null);
        };
        SheetTabs.prototype.showSheet = function (args) {
            this.parent.setSheetPropertyOnMute(this.parent.sheets[args.sheetIndex], 'state', 'Visible');
            this.tabInstance.hideTab(args.sheetIndex, false);
            if (args.triggerEvent) {
                this.parent.notify(index_2.completeAction, { action: 'showSheet', eventArgs: { sheetIndex: args.sheetIndex } });
            }
        };
        SheetTabs.prototype.switchSheetTab = function (args) {
            var target = ej2_base_1.closest(args.event.target, '.e-toolbar-item');
            if (!target) {
                return;
            }
            var name = target.querySelector('.e-tab-text').textContent;
            var disableItems = [];
            var id = this.parent.element.id + "_cmenu";
            for (var i = 0, len = this.parent.sheets.length; i < len; i++) {
                if (this.parent.sheets[i].name === name) {
                    if (this.parent.activeSheetIndex !== i) {
                        this.updateSheetTab({ idx: i });
                    }
                    break;
                }
            }
            Eif (args.element.classList.contains('e-contextmenu') && args.items[0] &&
                args.items[0].id === this.parent.element.id + "_cmenu_insert_sheet") {
                if (this.skipHiddenSheets() === 1) {
                    disableItems.push(id + "_hide_sheet", id + "_delete_sheet");
                }
                if (!this.parent.allowInsert || this.parent.isProtected) {
                    disableItems.push(id + "_insert_sheet");
                }
                Iif (!this.parent.allowDelete && disableItems.indexOf(id + "_delete_sheet") > -1) {
                    disableItems.push(id + "_delete_sheet");
                }
            }
            if (this.parent.password.length > 0 || this.parent.isProtected) {
                Eif (disableItems.indexOf(id + "_insert_sheet") > -1) {
                    disableItems.push(id + "_insert_sheet");
                }
                if (disableItems.indexOf(id + "_delete_sheet") > -1) {
                    disableItems.push(id + "_delete_sheet");
                }
                disableItems.push(id + "_duplicate", id + "_rename", id + "_hide_sheet", id + "_delete_sheet", id + "_insert_sheet", id + "_move_left", id + "_move_right");
            }
            this.parent.enableContextMenuItems(disableItems, false, true);
        };
        SheetTabs.prototype.skipHiddenSheets = function () {
            var count = this.parent.sheets.length;
            this.parent.sheets.forEach(function (sheet) {
                if (sheet.state !== 'Visible') {
                    --count;
                }
            });
            return count;
        };
        SheetTabs.prototype.renameSheetTab = function () {
            var target = this.tabInstance.element.querySelector('.e-toolbar-item.e-active');
            Eif (target) {
                target = target.querySelector('.e-text-wrap');
                var value = target.querySelector('.e-tab-text').textContent;
                var args = {
                    eventArgs: {
                        name: value, index: this.parent.getActiveSheet().id
                    },
                    action: 'renameSheet', cancel: false
                };
                this.parent.trigger('actionBegin', args);
                if (args.cancel || this.parent.isProtected) {
                    return;
                }
                var input = this.parent.createElement('input', {
                    id: this.parent.element.id + '_rename_input',
                    className: 'e-input e-sheet-rename', styles: "width: " + target.getBoundingClientRect().width + "px", attrs: {
                        'type': 'text', 'name': 'Rename', 'required': '', 'value': value, 'spellcheck': 'false', 'maxlength': '31'
                    }
                });
                target.firstElementChild.style.display = 'none';
                target.appendChild(input);
                ej2_base_1.EventHandler.add(document, 'mousedown touchstart', this.renameInputFocusOut, this);
                ej2_base_1.EventHandler.add(input, 'keydown', this.renameKeyDown, this);
                ej2_base_1.EventHandler.add(input, 'input', this.updateWidth, this);
                input.focus();
                input.setSelectionRange(0, value.length);
                ej2_base_1.EventHandler.remove(ej2_base_1.closest(target, '.e-toolbar-items'), 'dblclick', this.renameSheetTab);
            }
        };
        SheetTabs.prototype.updateWidth = function (e) {
            var target = e.target;
            var len = target.value.length;
            var value = target.value.split(' ');
            if (value.length) {
                var spaceLen = value.length - 1;
                len -= spaceLen;
                len += (spaceLen * 0.5);
            }
            target.style.width = len + "ch";
        };
        SheetTabs.prototype.renameKeyDown = function (e) {
            if (e.keyCode === 32) {
                e.stopPropagation();
            }
            else if (e.keyCode === 27) {
                this.removeRenameInput(e.target);
                this.focusTab(this.tabInstance.element);
            }
            else Eif (e.keyCode === 13) {
                this.renameInputFocusOut(e);
            }
        };
        SheetTabs.prototype.renameInputFocusOut = function (e) {
            var target = e.target;
            Iif ((e.type === 'mousedown' || e.type === 'touchstart') && (target.classList.contains('e-sheet-rename') ||
                ej2_base_1.closest(target, '.e-dlg-container'))) {
                return;
            }
            target = document.getElementById(this.parent.element.id + '_rename_input');
            var value = target.value;
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            if (value) {
                var idx = this.tabInstance.selectedItem;
                if (!value.match(new RegExp('.*[\\[\\]\\*\\\\\/\\?].*'))) {
                    Eif (this.parent.sheets[idx].name !== value) {
                        for (var i = 0, len = this.parent.sheets.length; i < len; i++) {
                            if (i !== this.parent.activeSheetIndex && this.parent.sheets[i].name.toLowerCase() ===
                                value.toLowerCase()) {
                                this.showRenameDialog(target, l10n.getConstant('SheetRenameAlreadyExistsAlert'));
                                return;
                            }
                        }
                    }
                    var items = this.removeRenameInput(target);
                    Eif (this.parent.sheets[idx].name !== value) {
                        this.parent.setSheetPropertyOnMute(this.parent.sheets[idx], 'name', value);
                        this.updateSheetName({ value: value, idx: idx, items: items });
                    }
                    if (e.type === 'keydown') {
                        this.focusTab(items);
                    }
                    else Iif ((ej2_base_1.closest(e.target, '.e-spreadsheet'))) {
                        index_2.focus(this.parent.element);
                    }
                }
                else {
                    this.showRenameDialog(target, l10n.getConstant('SheetRenameInvalidAlert'));
                }
            }
            else {
                this.showRenameDialog(target, l10n.getConstant('SheetRenameEmptyAlert'));
            }
            this.parent.notify(index_2.completeAction, { eventArgs: { index: this.parent.getActiveSheet().id, value: value }, action: 'renameSheet' });
        };
        SheetTabs.prototype.focusTab = function (context) {
            index_2.focus(ej2_base_1.select('.e-toolbar-item.e-active .e-tab-wrap', context));
        };
        SheetTabs.prototype.updateSheetName = function (args) {
            var pName = this.tabInstance.items[args.idx].header.text.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
            var name = args.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');
            this.tabInstance.items[args.idx].header.text = name;
            this.dropDownInstance.items[args.idx].text = name;
            this.dropDownInstance.setProperties({ 'items': this.dropDownInstance.items }, true);
            var sheetTabText = args.items.querySelectorAll('.e-toolbar-item')[args.idx].querySelector('.e-tab-text');
            sheetTabText.textContent = '';
            sheetTabText.appendChild(document.createTextNode(args.value));
            if (args.value.indexOf('  ') > -1) {
                this.tabInstance.setProperties({ 'items': this.tabInstance.items }, true);
            }
            else {
                this.tabInstance.dataBind();
            }
            this.parent.notify(index_7.workbookFormulaOperation, { action: 'renameUpdation', value: args.value, pName: pName });
            Eif (this.parent.allowChart) {
                var range_1 = [];
                var lastIndex_1;
                this.parent.chartColl.forEach(function (chart) {
                    Eif (chart.range.includes('!')) {
                        lastIndex_1 = chart.range.lastIndexOf('!');
                        range_1[0] = chart.range.substring(0, lastIndex_1);
                        range_1[1] = chart.range.substring(lastIndex_1 + 1);
                        if (range_1[0].toLowerCase() === pName.toLowerCase()) {
                            range_1[0] = args.value;
                            chart.range = range_1.join('!');
                        }
                    }
                });
            }
        };
        SheetTabs.prototype.hideSheet = function (args) {
            var actionArgs = {
                action: 'hideSheet', eventArgs: { sheetIndex: args.sheetIndex, cancel: false }
            };
            if (args.triggerEvent) {
                this.parent.notify(index_6.beginAction, actionArgs);
                if (actionArgs.eventArgs.cancel) {
                    return;
                }
            }
            this.parent.setSheetPropertyOnMute(index_6.getSheet(this.parent, args.sheetIndex), 'state', 'Hidden');
            this.tabInstance.hideTab(args.sheetIndex);
            if (args.triggerEvent) {
                delete actionArgs.eventArgs.cancel;
                this.parent.notify(index_2.completeAction, actionArgs);
            }
        };
        SheetTabs.prototype.removeRenameInput = function (target) {
            var textEle = target.parentElement.querySelector('.e-tab-text');
            var sheetItems = ej2_base_1.closest(target, '.e-toolbar-items');
            ej2_base_1.EventHandler.add(sheetItems, 'dblclick', this.renameSheetTab, this);
            ej2_base_1.EventHandler.remove(document, 'mousedown touchstart', this.renameInputFocusOut);
            ej2_base_1.EventHandler.remove(target, 'keydown', this.renameKeyDown);
            ej2_base_1.EventHandler.remove(target, 'input', this.updateWidth);
            ej2_base_1.remove(target);
            textEle.style.display = '';
            return sheetItems;
        };
        SheetTabs.prototype.showRenameDialog = function (target, content) {
            var _this = this;
            var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
            dialogInst.show({
                height: 180, width: 400, isModal: true, showCloseIcon: true,
                content: content,
                beforeOpen: function (args) {
                    var dlgArgs = {
                        dialogName: 'SheetRenameDialog',
                        element: args.element, target: args.target, cancel: args.cancel
                    };
                    _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                    Iif (dlgArgs.cancel) {
                        args.cancel = true;
                    }
                    args.element.querySelector('.e-footer-content .e-primary').setAttribute('aria-label', content + " " + _this.parent.serviceLocator.getService(index_1.locale).getConstant('Ok'));
                    target.focus();
                },
                close: function () { return target.setSelectionRange(0, target.value.length); }
            });
        };
        SheetTabs.prototype.focusRenameInput = function () {
            var input = document.getElementById(this.parent.element.id + '_rename_input');
            if (input) {
                input.focus();
            }
        };
        SheetTabs.prototype.removeSheetTab = function (args) {
            var _this = this;
            Iif (args.count && (args.count === this.parent.sheets.length)) {
                return;
            }
            var l10n = this.parent.serviceLocator.getService(index_1.locale);
            if (this.skipHiddenSheets() > 1) {
                var sheet = args.sheetName ?
                    index_6.getSheet(this.parent, index_6.getSheetIndex(this.parent, args.sheetName)) :
                    this.parent.getActiveSheet();
                var sheetIndex_1 = ej2_base_1.isNullOrUndefined(args.index) ? index_6.getSheetIndex(this.parent, sheet.name) : args.index;
                var eventArgs_1 = {
                    index: sheetIndex_1,
                    sheetCount: this.parent.sheets.length,
                    sheetName: sheet.name
                };
                var isDataAvail = sheet.rows && sheet.rows.length ?
                    (sheet.rows.length === 1 ? (sheet.rows[0].cells && sheet.rows[0].cells.length ? true : false) : true) : false;
                if (isDataAvail) {
                    var dialogInst_1 = this.parent.serviceLocator.getService(index_1.dialog);
                    if (args.clicked) {
                        this.forceDelete(sheetIndex_1);
                    }
                    else {
                        dialogInst_1.show({
                            height: 200, width: 400, isModal: true, showCloseIcon: true, cssClass: 'e-delete-sheet-dlg',
                            content: l10n.getConstant('DeleteSheetAlert'),
                            beforeOpen: function (args) {
                                var dlgArgs = {
                                    dialogName: 'DeleteSheetDialog',
                                    element: args.element, target: args.target, cancel: args.cancel
                                };
                                _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                                if (dlgArgs.cancel) {
                                    args.cancel = true;
                                }
                                args.element.querySelector('.e-footer-content .e-primary').setAttribute('aria-label', l10n.getConstant('DeleteSheetAlert') + " " + l10n.getConstant('Ok'));
                                index_2.focus(_this.parent.element);
                            },
                            buttons: [{
                                    buttonModel: { content: l10n.getConstant('Ok'), isPrimary: true },
                                    click: function () {
                                        dialogInst_1.hide();
                                        _this.forceDelete(sheetIndex_1);
                                        _this.parent.notify(index_2.clearUndoRedoCollection, null);
                                        Eif (args && !args.isAction) {
                                            eventArgs_1.sheetCount = _this.parent.sheets.length;
                                            _this.parent.notify(index_2.completeAction, { eventArgs: eventArgs_1, action: 'removeSheet' });
                                        }
                                    }
                                }]
                        });
                    }
                }
                else {
                    this.parent.notify(index_7.workbookFormulaOperation, { action: 'deleteSheetTab', sheetId: index_6.getSheet(this.parent, sheetIndex_1).id });
                    this.destroySheet(sheetIndex_1);
                    this.parent.notify(index_2.clearUndoRedoCollection, null);
                    Eif (args && !args.isAction) {
                        eventArgs_1.sheetCount = this.parent.sheets.length;
                        this.parent.notify(index_2.completeAction, { eventArgs: eventArgs_1, action: 'removeSheet' });
                    }
                }
            }
            else {
                this.parent.serviceLocator.getService(index_1.dialog).show({
                    height: 180, width: 400, isModal: true, showCloseIcon: true,
                    content: l10n.getConstant('DeleteSingleLastSheetAlert'),
                    beforeOpen: function (args) {
                        var dlgArgs = {
                            dialogName: 'DeleteSingleSheetDialog',
                            element: args.element, target: args.target, cancel: args.cancel
                        };
                        _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                        if (dlgArgs.cancel) {
                            args.cancel = true;
                        }
                    }
                });
            }
        };
        SheetTabs.prototype.forceDelete = function (sheetIndex) {
            this.parent.notify(index_1.removeDesignChart, {});
            this.parent.notify(index_7.workbookFormulaOperation, { action: 'deleteSheetTab', sheetId: index_6.getSheet(this.parent, sheetIndex).id });
            this.destroySheet(sheetIndex);
        };
        SheetTabs.prototype.destroySheet = function (sheetIndex) {
            var activeSheetIdx = ej2_base_1.isNullOrUndefined(sheetIndex) ? this.parent.activeSheetIndex : sheetIndex;
            this.parent.removeSheet(activeSheetIdx);
            this.parent.notify(index_7.sheetsDestroyed, { sheetIndex: activeSheetIdx });
            this.dropDownInstance.items.splice(activeSheetIdx, 1);
            this.dropDownInstance.setProperties({ 'items': this.dropDownInstance.items }, true);
            this.tabInstance.removeTab(activeSheetIdx);
            var activeIndex = this.parent.skipHiddenSheets(this.tabInstance.selectedItem);
            this.parent.activeSheetIndex = activeIndex;
            this.parent.setProperties({ activeSheetIndex: activeIndex }, true);
            this.parent.renderModule.refreshSheet();
            this.tabInstance.selectedItem = activeIndex;
            this.tabInstance.dataBind();
            this.updateDropDownItems(activeIndex);
            this.parent.notify(index_4.protectSheet, null);
        };
        SheetTabs.prototype.showAggregate = function (args) {
            var _this = this;
            if (index_6.isSingleCell(index_6.getRangeIndexes(this.parent.getActiveSheet().selectedRange)) || (args && args.remove)) {
                this.removeAggregate();
                return;
            }
            var eventArgs = { Count: 0, Sum: '0', Avg: '0', Min: '0', Max: '0', countOnly: true };
            this.parent.notify(index_5.aggregateComputation, eventArgs);
            if (eventArgs.Count > 1) {
                this.aggregateContent = eventArgs.countOnly ? 'Count' : this.selaggregateCnt;
                if (eventArgs.countOnly) {
                    this.aggregateContent = 'Count';
                    delete eventArgs.Sum;
                    delete eventArgs.Avg;
                    delete eventArgs.Min;
                    delete eventArgs.Max;
                }
                var btnClass = eventArgs.countOnly ? 'e-aggregate-list e-flat e-aggregate-list-countonly e-caret-hide'
                    : 'e-aggregate-list e-flat';
                delete eventArgs.countOnly;
                var key = this.aggregateContent;
                var content = key + ": " + eventArgs[key.toString()];
                if (!this.aggregateDropDown) {
                    var aggregateEle = this.parent.createElement('button', { id: this.parent.element.id + '_aggregate', attrs: { 'type': 'button' } });
                    document.getElementById(this.parent.element.id + "_sheet_tab_panel").appendChild(aggregateEle);
                    this.aggregateDropDown = new ej2_splitbuttons_1.DropDownButton({
                        content: content,
                        items: this.getAggregateItems(eventArgs),
                        createPopupOnClick: true,
                        select: function (args) {
                            _this.parent.notify(index_5.aggregateComputation, eventArgs);
                            _this.updateAggregateContent(args.item.text, { Count: eventArgs.Count, Sum: eventArgs.Sum, Avg: eventArgs.Avg, Min: eventArgs.Min,
                                Max: eventArgs.Max }, true);
                        },
                        beforeOpen: function (args) {
                            return _this.beforeOpenHandler(_this.aggregateDropDown, args.element);
                        },
                        open: function (args) { return _this.openHandler(_this.aggregateDropDown, args.element, 'right'); },
                        close: function () { return index_2.focus(_this.parent.element); },
                        cssClass: btnClass
                    });
                    this.aggregateDropDown.createElement = this.parent.createElement;
                    this.aggregateDropDown.appendTo(aggregateEle);
                }
                else {
                    this.updateAggregateContent(content, eventArgs);
                }
            }
            else {
                this.removeAggregate();
            }
        };
        SheetTabs.prototype.getAggregateItems = function (args) {
            var _this = this;
            var items = [];
            var text;
            var iconCss;
            var argsKey;
            Object.keys(args).forEach(function (key) {
                argsKey = args["" + key];
                if (argsKey !== index_5.aggregateComputation) {
                    text = key + ": " + argsKey;
                    iconCss = key === _this.aggregateContent ? 'e-selected-icon e-icons' : '';
                    items.push({ text: text, iconCss: iconCss });
                }
            });
            return items;
        };
        SheetTabs.prototype.updateAggregateContent = function (text, eventArgs, isSelect) {
            this.aggregateContent = text.split(': ')[0];
            if (isSelect) {
                this.selaggregateCnt = text.split(': ')[0];
            }
            this.aggregateDropDown.content = text;
            this.aggregateDropDown.dataBind();
            this.aggregateDropDown.setProperties({ 'items': this.getAggregateItems(eventArgs) }, true);
        };
        SheetTabs.prototype.removeAggregate = function () {
            if (!ej2_base_1.isNullOrUndefined(this.aggregateDropDown)) {
                this.aggregateDropDown.destroy();
                ej2_base_1.remove(this.aggregateDropDown.element);
                this.aggregateDropDown = null;
            }
        };
        SheetTabs.prototype.addEventListener = function () {
            this.parent.on(index_3.sheetTabs, this.createSheetTabs, this);
            this.parent.on(index_1.refreshSheetTabs, this.refreshSheetTab, this);
            this.parent.on(index_1.insertSheetTab, this.insertSheetTab, this);
            this.parent.on(index_3.removeSheetTab, this.removeSheetTab, this);
            this.parent.on(index_3.renameSheetTab, this.renameSheetTab, this);
            this.parent.on(index_1.cMenuBeforeOpen, this.switchSheetTab, this);
            this.parent.on(index_3.activeSheetChanged, this.updateSheetTab, this);
            this.parent.on(index_7.activeCellChanged, this.removeAggregate, this);
            this.parent.on(index_3.onVerticalScroll, this.focusRenameInput, this);
            this.parent.on(index_3.onHorizontalScroll, this.focusRenameInput, this);
            this.parent.on(index_2.sheetNameUpdate, this.updateSheetName, this);
            this.parent.on(index_1.hideSheet, this.hideSheet, this);
            this.parent.on(index_2.showAggregate, this.showAggregate, this);
            this.parent.on(index_1.goToSheet, this.goToSheet, this);
            this.parent.on(index_1.showSheet, this.showSheet, this);
        };
        SheetTabs.prototype.destroy = function () {
            this.removeEventListener();
            Eif (this.dropDownInstance) {
                this.dropDownInstance.destroy();
            }
            this.dropDownInstance = null;
            Eif (this.tabInstance) {
                this.tabInstance.destroy();
            }
            this.tabInstance = null;
            this.removeAggregate();
            this.aggregateContent = null;
            Eif (this.addBtnRipple) {
                this.addBtnRipple();
            }
            this.addBtnRipple = null;
            ej2_base_1.EventHandler.remove(document, 'mousedown touchstart', this.renameInputFocusOut);
            var ele = document.getElementById(this.parent.element.id + '_sheet_tab_panel');
            Eif (ele) {
                ej2_base_1.remove(ele);
            }
            Eif (this.selaggregateCnt) {
                this.selaggregateCnt = null;
            }
            this.parent = null;
        };
        SheetTabs.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_3.sheetTabs, this.createSheetTabs);
                this.parent.off(index_1.refreshSheetTabs, this.refreshSheetTab);
                this.parent.off(index_1.insertSheetTab, this.insertSheetTab);
                this.parent.off(index_3.removeSheetTab, this.removeSheetTab);
                this.parent.off(index_3.renameSheetTab, this.renameSheetTab);
                this.parent.off(index_1.cMenuBeforeOpen, this.switchSheetTab);
                this.parent.off(index_3.activeSheetChanged, this.updateSheetTab);
                this.parent.off(index_7.activeCellChanged, this.removeAggregate);
                this.parent.off(index_3.onVerticalScroll, this.focusRenameInput);
                this.parent.off(index_3.onHorizontalScroll, this.focusRenameInput);
                this.parent.off(index_2.sheetNameUpdate, this.updateSheetName);
                this.parent.off(index_1.hideSheet, this.hideSheet);
                this.parent.off(index_2.showAggregate, this.showAggregate);
                this.parent.off(index_1.goToSheet, this.goToSheet);
                this.parent.off(index_1.showSheet, this.showSheet);
            }
        };
        return SheetTabs;
    }());
    exports.SheetTabs = SheetTabs;
});