all files / grid/actions/ toolbar.js

94.18% Statements 453/481
85.61% Branches 339/396
100% Functions 45/45
94.18% Lines 453/481
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   292× 292× 292×   292× 292× 292× 292× 292×   297× 297×   297× 297× 297× 2970× 2970× 2970× 2970×       2970× 160× 160×     297×         297× 297×     297×           292×           297× 11×         11×           297× 70×   11×         297× 297× 16× 13×   16× 16×       48× 48× 48× 153× 22×     26×   51×   605× 290× 15×   290× 290× 290× 290× 290× 290×       287×   290×   290× 286× 286×       299× 299× 299× 299× 299×     299× 38× 26×   26×                   12×                 299×         299×   297× 297×               297× 297× 297× 297× 297× 297× 297×       297× 297× 16×   297×                                       293×   297× 297× 297× 297× 297× 297× 297× 297× 297× 297× 297× 297×                       39× 39×   39× 34×           26× 13×     13×                             2154× 2154× 2154× 2154× 2154×   2154× 2154× 1864×     290×   2154× 1643×     511×   2154× 1620×     534×   2154× 863× 506× 506×     357× 357×       1291× 403×   403×       888× 888×     2154× 2154×   297× 297× 297×     297× 11×   11×     297× 1187× 1187×   16×   1171× 11×   1171×     1164× 1164×       297×   297×     1198×   4311× 10773× 10773× 10773× 9308×       239× 239× 239× 239× 239×     230× 230× 230× 230× 230× 230×     40× 40×   54× 54×   10× 10×   72× 72×   15× 15×                   16×       16× 16× 16× 16× 16× 16×   16×                                                       13× 13× 13× 72× 72×   16×             13×   16× 16×   13×   10×       16×   13× 13×     13× 13× 13×               13×                   347× 26×     26× 22×                     647× 81×     299× 26× 26× 26×   299× 299×   292× 24× 24×   292× 292×   53× 53× 13× 13×   53×   42× 42×                 263× 263× 263× 647× 628×     263×   123×                                 293×   292×                       292× 292× 292× 292× 292×   292×   290× 290× 290×   257× 257× 257×         1096×     20596×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "../base/constant", "../base/util", "../base/enum", "@syncfusion/ej2-navigations", "@syncfusion/ej2-popups", "../services/focus-strategy", "../base/string-literals"], function (require, exports, ej2_base_1, ej2_base_2, ej2_navigations_1, events, util_1, enum_1, ej2_navigations_2, ej2_popups_1, focus_strategy_1, literals) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Toolbar = (function () {
        function Toolbar(parent, serviceLocator) {
            this.predefinedItems = {};
            this.isSearched = false;
            this.items = ['Add', 'Edit', 'Update', 'Delete', 'Cancel', 'Print', 'Search',
                'ColumnChooser', 'PdfExport', 'ExcelExport', 'CsvExport', 'WordExport'];
            this.isRightToolbarMenu = false;
            this.parent = parent;
            this.gridID = parent.element.id;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
        }
        Toolbar.prototype.render = function () {
            this.l10n = this.serviceLocator.getService('localization');
            var preItems = ['Add', 'Edit', 'Update', 'Delete', 'Cancel', 'Print',
                'PdfExport', 'ExcelExport', 'WordExport', 'CsvExport'];
            var isAdaptive = this.parent.enableAdaptiveUI;
            var excludingItems = ['Edit', 'Delete', 'Update', 'Cancel'];
            for (var _i = 0, preItems_1 = preItems; _i < preItems_1.length; _i++) {
                var item = preItems_1[_i];
                var itemStr = item.toLowerCase();
                var localeName = itemStr[0].toUpperCase() + itemStr.slice(1);
                this.predefinedItems["" + item] = {
                    id: this.gridID + '_' + itemStr, prefixIcon: 'e-' + itemStr,
                    text: this.l10n.getConstant(localeName), tooltipText: this.l10n.getConstant(localeName)
                };
                if (isAdaptive) {
                    this.predefinedItems["" + item].text = '';
                    this.predefinedItems["" + item].visible = excludingItems.indexOf(item) === -1;
                }
            }
            this.predefinedItems.Search = {
                id: this.gridID + '_search',
                tooltipText: this.l10n.getConstant('Search'), align: 'Right', cssClass: 'e-search-wrapper',
                type: 'Input'
            };
            this.isRightToolbarMenu = false;
            if (this.parent.enableAdaptiveUI && this.isResponsiveToolbarMenuItems(true) && ((this.parent.rowRenderingMode === 'Horizontal') ||
                (this.parent.rowRenderingMode === 'Vertical' && !this.parent.allowFiltering && !this.parent.allowSorting))) {
                this.isRightToolbarMenu = true;
            }
            if (isAdaptive && this.isResponsiveToolbarMenuItems(false)) {
                this.predefinedItems.responsiveToolbarItems = {
                    id: this.gridID + '_' + 'responsivetoolbaritems', cssClass: 'e-responsive-toolbar-items e-menu-toolbar', suffixIcon: 'e-' + 'responsivetoolbaritems-btn',
                    align: this.isRightToolbarMenu ? 'Left' : 'Right'
                };
            }
            else {
                this.predefinedItems.ColumnChooser = {
                    id: this.gridID + '_' + 'columnchooser', cssClass: 'e-cc e-ccdiv e-cc-toolbar', suffixIcon: 'e-' + 'columnchooser-btn',
                    text: isAdaptive ? '' : this.l10n.getConstant('Columnchooser'),
                    tooltipText: this.l10n.getConstant('Columnchooser'), align: 'Right'
                };
            }
            if (this.parent.rowRenderingMode === 'Vertical') {
                if (this.parent.allowFiltering && this.parent.filterSettings.type !== 'FilterBar') {
                    this.predefinedItems.responsiveFilter = {
                        id: this.gridID + '_' + 'responsivefilter', cssClass: 'e-gridresponsiveicons e-icons',
                        suffixIcon: 'e-' + 'resfilter-icon', tooltipText: this.l10n.getConstant('FilterIcon')
                    };
                }
                if (this.parent.allowSorting) {
                    this.predefinedItems.responsiveSort = {
                        id: this.gridID + '_' + 'responsivesort', cssClass: 'e-gridresponsiveicons e-icons',
                        suffixIcon: 'e-' + 'ressort-icon', tooltipText: this.l10n.getConstant('SortIcon')
                    };
                }
            }
            if (this.parent.enableAdaptiveUI && this.parent.toolbar && this.parent.toolbar.some(function (item) {
                return (typeof item === 'object' && item.text === 'Search') || item === 'Search';
            })) {
                this.predefinedItems.responsiveBack = {
                    id: this.gridID + '_' + 'responsiveback', cssClass: 'e-gridresponsiveicons e-icons',
                    suffixIcon: 'e-' + 'resback-icon', visible: false
                };
            }
            this.createToolbar();
            if (this.parent.enableAdaptiveUI) {
                if (ej2_base_1.isNullOrUndefined(this.responsiveToolbarMenu)) {
                    this.renderResponsiveToolbarpopup();
                }
                Eif (this.toolbar.element) {
                    this.toolbar.refreshOverflow();
                }
            }
        };
        Toolbar.prototype.isResponsiveToolbarMenuItems = function (isRight) {
            var items = isRight ? ['Add', 'Edit', 'Delete', 'Search'] : ['Print', 'ColumnChooser', 'PdfExport', 'ExcelExport', 'CsvExport'];
            var toolbarItems = this.parent.toolbar || [];
            for (var i = 0; i < items.length; i++) {
                if (toolbarItems.indexOf(items[parseInt(i.toString(), 10)]) >= 0) {
                    return isRight ? false : true;
                }
            }
            return isRight ? true : false;
        };
        Toolbar.prototype.getToolbar = function () {
            return this.toolbar.element;
        };
        Toolbar.prototype.destroy = function () {
            if (this.toolbar && !this.toolbar.isDestroyed) {
                if (this.responsiveToolbarMenu) {
                    this.responsiveToolbarMenu.destroy();
                }
                this.toolbar.off('render-react-toolbar-template', this.addReactToolbarPortals);
                this.unWireEvent();
                this.removeEventListener();
                this.toolbar.created = null;
                this.toolbar.clicked = null;
                if (!this.toolbar.element) {
                    this.parent.destroyTemplate(['toolbarTemplate']);
                    if (this.parent.isReact) {
                        this.parent.renderTemplates();
                    }
                }
                else {
                    this.toolbar.destroy();
                }
                if (this.parent.isAngular) {
                    var viewStr = 'viewContainerRef';
                    var registerTemp = 'registeredTemplate';
                    this.toolbar["" + viewStr] = null;
                    this.toolbar["" + registerTemp] = null;
                }
                if (this.element.parentNode) {
                    ej2_base_2.remove(this.element);
                    this.toolbar = null;
                }
            }
        };
        Toolbar.prototype.bindSearchEvents = function () {
            this.searchElement = ej2_base_2.select('#' + this.gridID + '_searchbar', this.element);
            this.wireEvent();
            this.refreshToolbarItems();
            Eif (this.parent.searchSettings) {
                this.updateSearchBox();
            }
        };
        Toolbar.prototype.toolbarCreated = function (isNormal) {
            if (this.element.querySelector('.e-search-wrapper')) {
                if (!this.parent.enableAdaptiveUI || isNormal) {
                    var classList = this.parent.cssClass ? 'e-input-group e-search ' + this.parent.cssClass
                        : 'e-input-group e-search';
                    this.element.querySelector('.e-search-wrapper').innerHTML = '<div class="' + classList + '" role="search">\
                    <input id="' + this.gridID + '_searchbar" class="e-input e-search" name="input" type="search" \
                    placeholder= "' + this.l10n.getConstant('Search') + '"/>\
                    <span id="' + this.gridID + '_clearbutton" class="e-input-group-icon e-icons e-sicon" \
                    tabindex="-1" aria-label= "clear" role= "button" style="cursor: default"></span>\
                    <span id="' + this.gridID + '_searchbutton" class="e-input-group-icon e-search-icon e-icons" \
                    tabindex="-1" title="' + this.l10n.getConstant('Search') + '" role= "search"></span> \
                    </div>';
                }
                else {
                    this.element.querySelector('.e-search-wrapper').innerHTML = '<span id="' + this.gridID
                        + '_clearbutton" class="e-input-group-icon e-icons e-sicon" \
                    tabindex="-1" role= "button" aria-label= "clear" style="cursor: default"></span>\
                    <span id="' + this.gridID
                        + '_searchbutton" class="e-input-group-icon e-search-icon e-icons" \
                    tabindex="-1" role= "button" title="' + this.l10n.getConstant('Search') + '"></span> \
                    </div>';
                }
            }
            if (this.element.querySelector('.e-responsive-toolbar-items')) {
                this.element.querySelector('.e-responsive-toolbar-items').innerHTML = '<button id="' + this.gridID
                    + '_responsivetoolbaritems" class="e-tbar-btn e-control e-btn e-lib e-icon-btn" \
                    type="button" data-ripple="true" tabindex="-1" data-tabindex="-1" aria-label="responsivetoolbaritems" \
                     aria-disabled="false" style="width: auto;"><span class="e-btn-icon e-responsivetoolbaritems-btn e-icons"></span>';
            }
            this.bindSearchEvents();
        };
        Toolbar.prototype.createToolbar = function () {
            var items = this.getItems();
            this.toolbar = new ej2_navigations_1.Toolbar({
                items: items,
                clicked: this.toolbarClickHandler.bind(this),
                enablePersistence: this.parent.enablePersistence,
                enableRtl: this.parent.enableRtl,
                created: this.toolbarCreated.bind(this),
                cssClass: this.parent.cssClass ? this.parent.cssClass : ''
            });
            this.toolbar.isReact = this.parent.isReact;
            this.toolbar.on('render-react-toolbar-template', this.addReactToolbarPortals, this);
            var isStringTemplate = 'isStringTemplate';
            this.toolbar["" + isStringTemplate] = true;
            var viewStr = 'viewContainerRef';
            var registerTemp = 'registeredTemplate';
            Iif (this.parent["" + viewStr]) {
                this.toolbar["" + registerTemp] = {};
                this.toolbar["" + viewStr] = this.parent["" + viewStr];
            }
            this.element = this.parent.createElement('div', { id: this.gridID + '_toolbarItems' });
            if (this.parent.enableAdaptiveUI) {
                this.element.classList.add('e-res-toolbar');
            }
            if (this.parent.toolbarTemplate) {
                var isVue = this.parent.isVue
                    || (this.parent.parentDetails && this.parent.parentDetails.parentInstObj && this.parent.parentDetails.parentInstObj.isVue);
                Eif (typeof (this.parent.toolbarTemplate) === 'string'
                    && !(isVue && !document.querySelectorAll(this.parent.toolbarTemplate).length)) {
                    this.toolbar.appendTo(this.parent.toolbarTemplate);
                    this.element = this.toolbar.element;
                }
                else {
                    var isReactCompiler = this.parent.isReact && typeof (this.parent.toolbarTemplate) !== 'string' &&
                        !(this.parent.toolbarTemplate.prototype && this.parent.toolbarTemplate.prototype.CSPTemplate);
                    var isReactChild = this.parent.parentDetails && this.parent.parentDetails.parentInstObj &&
                        this.parent.parentDetails.parentInstObj.isReact;
                    var ID = this.parent.element.id + 'toolbarTemplate';
                    if (isReactCompiler || isReactChild) {
                        util_1.templateCompiler(this.parent.toolbarTemplate)({}, this.parent, 'toolbarTemplate', ID, null, null, this.element);
                        this.parent.renderTemplates();
                    }
                    else {
                        util_1.appendChildren(this.element, util_1.templateCompiler(this.parent.toolbarTemplate)({}, this.parent, 'toolbarTemplate', null, null, null, null, this.parent.root));
                    }
                }
                this.element.classList.add('e-temp-toolbar');
            }
            else {
                this.toolbar.appendTo(this.element);
            }
            this.parent.element.insertBefore(this.element, this.parent.getHeaderContent());
            var tlbrLeftElement = this.element.querySelector('.e-toolbar-left');
            var tlbrCenterElement = this.element.querySelector('.e-toolbar-center');
            var tlbrRightElement = this.element.querySelector('.e-toolbar-right');
            var tlbrItems = this.element.querySelector('.e-toolbar-items');
            var tlbrElement = this.element;
            var tlbrLeftWidth = tlbrLeftElement ? tlbrLeftElement.clientWidth : 0;
            var tlbrCenterWidth = tlbrCenterElement ? tlbrCenterElement.clientWidth : 0;
            var tlbrRightWidth = tlbrRightElement ? tlbrRightElement.clientWidth : 0;
            var tlbrItemsWidth = tlbrItems ? tlbrItems.clientWidth : 0;
            var tlbrWidth = tlbrElement ? tlbrElement.clientWidth : 0;
            Iif (tlbrLeftWidth > tlbrWidth || tlbrCenterWidth > tlbrWidth || tlbrRightWidth > tlbrWidth || tlbrItemsWidth > tlbrWidth) {
                this.toolbar.refreshOverflow();
            }
        };
        Toolbar.prototype.addReactToolbarPortals = function (args) {
            if (this.parent.isReact && args) {
                this.parent.portals = this.parent.portals.concat(args);
                this.parent.renderTemplates();
            }
        };
        Toolbar.prototype.renderResponsiveSearch = function (isRender) {
            if (isRender) {
                this.toolbarCreated(true);
                this.refreshResponsiveToolbarItems(enum_1.ResponsiveToolbarAction.isSearch);
                this.searchElement = ej2_base_2.select('#' + this.gridID + '_searchbar', this.element);
                var right = util_1.parentsUntil(this.searchElement, 'e-toolbar-right');
                Eif (right) {
                    right.classList.add('e-responsive-right');
                }
                Eif (this.parent.searchSettings) {
                    this.updateSearchBox();
                }
                this.searchBoxObj.searchFocus({ target: this.searchElement });
                this.searchElement.focus();
            }
            else {
                this.refreshResponsiveToolbarItems(enum_1.ResponsiveToolbarAction.isInitial);
            }
        };
        Toolbar.prototype.refreshResponsiveToolbarItems = function (action) {
            if (action === enum_1.ResponsiveToolbarAction.isInitial) {
                var id = this.parent.element.id;
                var items = [id + '_edit', id + '_delete'];
                var selectedRecords = this.parent.getSelectedRowIndexes();
                var excludingItems = [id + '_responsiveback', id + '_update', id + '_cancel'];
                for (var _i = 0, _a = this.toolbar.items; _i < _a.length; _i++) {
                    var item = _a[_i];
                    var toolbarEle = item.template && item.template.length ?
                        util_1.parentsUntil(this.toolbar.element.querySelector('#' + item.id), 'e-template').children[0] : this.toolbar.element.querySelector('#' + item.id);
                    if (toolbarEle) {
                        if (items.indexOf(item.id) > -1) {
                            if (selectedRecords.length) {
                                toolbarEle.parentElement.classList.remove('e-hidden');
                            }
                            else {
                                toolbarEle.parentElement.classList.add('e-hidden');
                            }
                        }
                        else {
                            if (excludingItems.indexOf(item.id) === -1) {
                                toolbarEle.parentElement.classList.remove('e-hidden');
                            }
                            else {
                                toolbarEle.parentElement.classList.add('e-hidden');
                            }
                        }
                    }
                }
                if (this.searchElement) {
                    var right = util_1.parentsUntil(this.searchElement, 'e-toolbar-right');
                    Eif (right) {
                        right.classList.remove('e-responsive-right');
                    }
                    this.toolbarCreated(false);
                    this.unWireEvent();
                    this.searchElement = undefined;
                }
            }
            if (action === enum_1.ResponsiveToolbarAction.isSearch) {
                var items = [this.parent.element.id + '_responsiveback', this.parent.element.id + '_search'];
                for (var _b = 0, _c = this.toolbar.items; _b < _c.length; _b++) {
                    var item = _c[_b];
                    var toolbarEle = this.toolbar.element.querySelector('#' + item.id);
                    if (toolbarEle) {
                        if (items.indexOf(item.id) > -1) {
                            toolbarEle.parentElement.classList.remove('e-hidden');
                        }
                        else {
                            toolbarEle.parentElement.classList.add('e-hidden');
                        }
                    }
                }
            }
        };
        Toolbar.prototype.refreshToolbarItems = function (args) {
            var gObj = this.parent;
            var enableItems = [];
            var disableItems = [];
            var edit = gObj.editSettings;
            var hasData = (gObj.currentViewData && gObj.currentViewData.length) ||
                (gObj.editSettings.mode === 'Batch' && !ej2_base_1.isNullOrUndefined(gObj.editModule) && gObj.editModule.getBatchChanges()[literals.addedRecords].length);
            var addRow = edit.showAddNewRow && !gObj.element.querySelector('.e-editedrow');
            if (edit.allowAdding) {
                enableItems.push(this.gridID + '_add');
            }
            else {
                disableItems.push(this.gridID + '_add');
            }
            if (edit.allowEditing && hasData) {
                enableItems.push(this.gridID + '_edit');
            }
            else {
                disableItems.push(this.gridID + '_edit');
            }
            if (edit.allowDeleting && hasData) {
                enableItems.push(this.gridID + '_delete');
            }
            else {
                disableItems.push(this.gridID + '_delete');
            }
            if (gObj.editSettings.mode === 'Batch') {
                if (gObj.element.getElementsByClassName('e-updatedtd').length && (edit.allowAdding || edit.allowEditing)) {
                    enableItems.push(this.gridID + '_update');
                    enableItems.push(this.gridID + '_cancel');
                }
                else {
                    disableItems.push(this.gridID + '_update');
                    disableItems.push(this.gridID + '_cancel');
                }
            }
            else {
                if ((gObj.isEdit || edit.showAddNewRow) && (edit.allowAdding || edit.allowEditing)) {
                    enableItems = addRow ? [this.gridID + '_update', this.gridID + '_cancel', this.gridID + '_edit', this.gridID + '_delete'] :
                        [this.gridID + '_update', this.gridID + '_cancel'];
                    disableItems = addRow ? [this.gridID + '_add'] :
                        [this.gridID + '_add', this.gridID + '_edit', this.gridID + '_delete'];
                }
                else {
                    disableItems.push(this.gridID + '_update');
                    disableItems.push(this.gridID + '_cancel');
                }
            }
            this.enableItems(enableItems, true);
            this.enableItems(disableItems, false);
        };
        Toolbar.prototype.getItems = function () {
            var items = [];
            var toolbarItems = this.parent.toolbar || [];
            Iif (typeof (this.parent.toolbar) === 'string') {
                return [];
            }
            if (this.parent.rowRenderingMode === 'Vertical') {
                if (this.parent.allowFiltering && this.parent.filterSettings.type !== 'FilterBar') {
                    items.push(this.getItemObject('responsiveFilter'));
                }
                if (this.parent.allowSorting) {
                    items.push(this.getItemObject('responsiveSort'));
                }
            }
            for (var _i = 0, toolbarItems_1 = toolbarItems; _i < toolbarItems_1.length; _i++) {
                var item = toolbarItems_1[_i];
                if (this.parent.enableAdaptiveUI && ['Print', 'ColumnChooser',
                    'PdfExport', 'ExcelExport', 'CsvExport'].indexOf(item) !== -1) {
                    continue;
                }
                if (this.parent.enableAdaptiveUI && ((typeof item === 'object' && item.text === 'Search') || item === 'Search')) {
                    items.push(this.getItemObject('responsiveBack'));
                }
                switch (typeof item) {
                    case 'number':
                        items.push(this.getItemObject(this.items[item]));
                        break;
                    case 'string':
                        items.push(this.getItemObject(item));
                        break;
                    default:
                        items.push(this.getItem(item));
                }
            }
            if (this.parent.enableAdaptiveUI && this.isResponsiveToolbarMenuItems(false)) {
                items.push(this.getItemObject('responsiveToolbarItems'));
            }
            return items;
        };
        Toolbar.prototype.getItem = function (itemObject) {
            var item = this.predefinedItems[itemObject.text];
            return item ? ej2_base_1.extend(item, item, itemObject) : itemObject;
        };
        Toolbar.prototype.getItemObject = function (itemName) {
            return this.predefinedItems["" + itemName] || { text: itemName, id: this.gridID + '_' + itemName };
        };
        Toolbar.prototype.enableItems = function (items, isEnable) {
            for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
                var item = items_1[_i];
                var element = ej2_base_2.select('#' + item, this.element);
                if (element) {
                    this.toolbar.enableItems(element.closest('.e-toolbar-item'), isEnable);
                }
            }
        };
        Toolbar.prototype.toolbarClickHandler = function (args) {
            var _this = this;
            var gObj = this.parent;
            var gID = this.gridID;
            var originalEvent = args.originalEvent;
            if (this.parent.enableAdaptiveUI && args.item.id === gID + '_responsivetoolbaritems') {
                this.openResponsiveToolbarMenuPopup(originalEvent, args.item.id);
            }
            else {
                ej2_base_1.extend(args, { cancel: false });
                var newArgs = args;
                gObj.trigger(events.toolbarClick, newArgs, function (toolbarargs) {
                    toolbarargs.originalEvent = toolbarargs.originalEvent ? toolbarargs.originalEvent : originalEvent;
                    Eif (!toolbarargs.cancel) {
                        switch (!ej2_base_1.isNullOrUndefined(toolbarargs.item) && toolbarargs.item.id) {
                            case gID + '_print':
                                gObj.print();
                                break;
                            case gID + '_edit':
                                gObj.startEdit();
                                break;
                            case gID + '_update':
                                gObj.endEdit();
                                break;
                            case gID + '_cancel':
                                gObj.closeEdit();
                                break;
                            case gID + '_add':
                                gObj.addRecord();
                                break;
                            case gID + '_delete':
                                gObj.deleteRecord();
                                break;
                            case gID + '_search':
                                if (toolbarargs.originalEvent.target.id === gID + '_searchbutton' && _this.searchElement) {
                                    _this.search();
                                }
                                else if (gObj.enableAdaptiveUI && !_this.searchElement
                                    && (toolbarargs.originalEvent.target.classList.contains('e-search-wrapper')
                                        || toolbarargs.originalEvent.target.id === gID + '_searchbutton')) {
                                    _this.renderResponsiveSearch(true);
                                }
                                else if (toolbarargs.originalEvent.target.classList.contains('e-clear-icon') && toolbarargs.originalEvent.target.id === gID + '_clearbutton' && _this.searchElement) {
                                    _this.searchElement.value = '';
                                    Eif (_this.searchElement) {
                                        _this.sIcon = _this.searchElement.parentElement.querySelector('.e-sicon');
                                        _this.sIcon.classList.remove('e-clear-icon');
                                        _this.sIcon.removeAttribute('title');
                                        _this.sIcon.style.cursor = 'default';
                                    }
                                    Eif (_this.isSearched || _this.parent.searchSettings.key.length) {
                                        _this.parent.search(_this.searchElement.value);
                                        _this.isSearched = false;
                                    }
                                }
                                break;
                            case gID + '_columnchooser':
                                Iif (_this.parent.enableAdaptiveUI) {
                                    gObj.showResponsiveCustomColumnChooser();
                                }
                                else {
                                    var tarElement = _this.parent.element.querySelector('.e-ccdiv');
                                    var y = tarElement.getBoundingClientRect().top;
                                    var x = tarElement.getBoundingClientRect().left;
                                    var targetEle = toolbarargs.originalEvent.target;
                                    y = tarElement.getBoundingClientRect().top + tarElement.offsetTop;
                                    gObj.createColumnchooser(x, y, targetEle);
                                }
                                break;
                            case gID + '_responsivefilter':
                                gObj.notify(events.renderResponsiveChangeAction, { action: 3 });
                                gObj.showResponsiveCustomFilter();
                                break;
                            case gID + '_responsivesort':
                                gObj.notify(events.renderResponsiveChangeAction, { action: 2 });
                                gObj.showResponsiveCustomSort();
                                break;
                            case gID + '_responsiveback':
                                _this.renderResponsiveSearch(false);
                                break;
                        }
                    }
                });
            }
        };
        Toolbar.prototype.openResponsiveToolbarMenuPopup = function (e, id) {
            var pos = { top: 0, left: 0 };
            this.toolbarMenuElement.style.cssText = 'display:block;visibility:hidden';
            var elePos = this.toolbarMenuElement.getBoundingClientRect();
            var gClient = this.parent.element.getBoundingClientRect();
            this.toolbarMenuElement.style.cssText = 'display:none;visibility:visible';
            var target;
            Eif (ej2_base_1.isNullOrUndefined(e)) {
                target = this.parent.element.querySelector('#' + id);
            }
            else {
                target = e.target;
            }
            Iif (this.parent.enableRtl) {
                pos = ej2_popups_1.calculatePosition(target, 'left', 'bottom');
                if (this.isRightToolbarMenu) {
                    pos.left -= elePos.width;
                }
            }
            else {
                pos = ej2_popups_1.calculatePosition(target, 'right', 'bottom');
                if (!this.isRightToolbarMenu) {
                    pos.left -= elePos.width;
                }
                if ((pos.left + elePos.width + 1) >= gClient.right) {
                    pos.left -= 35;
                }
            }
            this.responsiveToolbarMenu['open'](pos.top, pos.left);
            Iif (e && e.preventDefault) {
                e.preventDefault();
            }
            util_1.applyBiggerTheme(this.parent.element, this.responsiveToolbarMenu.element.parentElement);
        };
        Toolbar.prototype.getMenuItems = function () {
            var items = [];
            var toolbarItems = this.parent.toolbar || [];
            for (var _i = 0, toolbarItems_2 = toolbarItems; _i < toolbarItems_2.length; _i++) {
                var item = toolbarItems_2[_i];
                if (typeof item === 'string' && (item === 'ColumnChooser' || item === 'PdfExport' || item === 'ExcelExport' ||
                    item === 'CsvExport' || item === 'Print')) {
                    items.push({
                        text: this.getLocaleText(item),
                        id: this.gridID + '_' + item.toLowerCase(),
                        iconCss: 'e-btn-icon e-' + item.toLowerCase() + ' e-icons'
                    });
                }
            }
            return items;
        };
        Toolbar.prototype.getLocaleText = function (item) {
            var title;
            if (item === 'ColumnChooser') {
                title = this.l10n.getConstant('Columnchooser');
            }
            else if (item === 'PdfExport') {
                title = this.l10n.getConstant('Pdfexport');
            }
            else if (item === 'ExcelExport') {
                title = this.l10n.getConstant('Excelexport');
            }
            else if (item === 'CsvExport') {
                title = this.l10n.getConstant('Csvexport');
            }
            else Eif (item === 'Print') {
                title = this.l10n.getConstant('Print');
            }
            return title;
        };
        Toolbar.prototype.renderResponsiveToolbarpopup = function () {
            this.l10n = this.serviceLocator.getService('localization');
            this.toolbarMenuElement = this.parent.createElement('ul', {
                id: this.gridID + '__toolbaritemsmenu', className: 'e-responsivetoolbar-menu'
            });
            this.element.setAttribute('aria-label', this.l10n.getConstant('ToolbarMenuDialogARIA'));
            this.parent.element.appendChild(this.toolbarMenuElement);
            this.responsiveToolbarMenu = new ej2_navigations_2.ContextMenu({
                cssClass: this.parent.cssClass ? 'e-grid-toolbarmenu' + ' ' + this.parent.cssClass : 'e-grid-toolbarmenu',
                enableRtl: this.parent.enableRtl,
                enablePersistence: this.parent.enablePersistence,
                locale: this.parent.locale,
                items: this.getMenuItems(),
                select: this.ResponsiveToolbarMenuItemClick.bind(this)
            });
            this.responsiveToolbarMenu.appendTo(this.toolbarMenuElement);
        };
        Toolbar.prototype.ResponsiveToolbarMenuItemClick = function (args) {
            var _this = this;
            var gObj = this.parent;
            var element = args.element;
            args.item.id = element.id;
            var newArgs = { cancel: false, name: 'clicked', item: args.item, originalEvent: args.event };
            gObj.trigger(events.toolbarClick, newArgs, function (toolbarargs) {
                Eif (!toolbarargs.cancel) {
                    switch (!ej2_base_1.isNullOrUndefined(toolbarargs.item) && toolbarargs.item.id) {
                        case _this.gridID + '_columnchooser':
                            gObj.notify(events.renderResponsiveChangeAction, { action: 5 });
                            gObj.showResponsiveCustomColumnChooser();
                            break;
                        case _this.gridID + '_print':
                            gObj.print();
                            break;
                    }
                }
            });
        };
        Toolbar.prototype.modelChanged = function (e) {
            if (e.module === 'edit') {
                this.refreshToolbarItems();
            }
        };
        Toolbar.prototype.onPropertyChanged = function (e) {
            if (e.module !== this.getModuleName() || !this.parent.toolbar) {
                return;
            }
            if (this.element) {
                ej2_base_2.remove(this.element);
            }
            this.render();
        };
        Toolbar.prototype.keyUpHandler = function (e) {
            if (e.keyCode === 13) {
                this.search();
            }
            Eif (this.searchElement) {
                this.sIcon = this.searchElement.parentElement.querySelector('.e-sicon');
                if (this.searchElement.value.length && !ej2_base_1.isNullOrUndefined(this.sIcon)) {
                    this.sIcon.classList.add('e-clear-icon');
                    this.sIcon.setAttribute('title', this.l10n.getConstant('Clear'));
                    this.sIcon.style.cursor = 'pointer';
                }
                else {
                    this.sIcon.classList.remove('e-clear-icon');
                    this.sIcon.removeAttribute('title');
                    this.sIcon.style.cursor = 'default';
                }
            }
        };
        Toolbar.prototype.search = function () {
            this.isSearched = true;
            this.parent.search(this.searchElement.value);
        };
        Toolbar.prototype.updateSearchBox = function () {
            if (this.searchElement) {
                this.searchElement.value = this.parent.searchSettings.key;
            }
        };
        Toolbar.prototype.wireEvent = function () {
            if (this.searchElement) {
                this.searchBoxObj = new focus_strategy_1.SearchBox(this.searchElement, this.serviceLocator);
                ej2_base_1.EventHandler.add(this.searchElement, 'keyup', this.keyUpHandler, this);
                this.searchBoxObj.wireEvent();
            }
            ej2_base_1.EventHandler.add(this.element, 'focusin', this.onFocusIn, this);
            ej2_base_1.EventHandler.add(this.element, 'focusout', this.onFocusOut, this);
        };
        Toolbar.prototype.unWireEvent = function () {
            if (this.searchElement) {
                ej2_base_1.EventHandler.remove(this.searchElement, 'keyup', this.keyUpHandler);
                this.searchBoxObj.unWireEvent();
            }
            ej2_base_1.EventHandler.remove(this.element, 'focusin', this.onFocusIn);
            ej2_base_1.EventHandler.remove(this.element, 'focusout', this.onFocusOut);
        };
        Toolbar.prototype.onFocusIn = function (e) {
            var currentInfo = this.parent.focusModule.currentInfo;
            if (currentInfo && currentInfo.element) {
                ej2_base_2.removeClass([currentInfo.element, currentInfo.elementToFocus], ['e-focused', 'e-focus']);
                currentInfo.element.tabIndex = -1;
            }
            e.target.tabIndex = 0;
        };
        Toolbar.prototype.onFocusOut = function (e) {
            e.target.tabIndex = -1;
            if (e.target && e.target.id === this.parent.element.id + '_searchbar' &&
                !(e.relatedTarget && (e.relatedTarget.id === this.parent.element.id + '_clearbutton' ||
                    e.relatedTarget.id === this.parent.element.id + '_searchbutton'))) {
                this.search();
            }
        };
        Toolbar.prototype.setFocusToolbarItem = function (element) {
            var elementToFocus = element.querySelector('.e-btn,.e-input,.e-toolbar-item-focus');
            Iif (!elementToFocus && this.parent.enableAdaptiveUI && !this.searchElement
                && element.classList.contains('e-search-wrapper')) {
                elementToFocus = element.querySelector('#' + this.gridID + '_searchbutton');
            }
            elementToFocus.focus();
        };
        Toolbar.prototype.getFocusableToolbarItems = function () {
            var getFocusToolbarElements = [].slice.call(this.element.querySelectorAll('.e-toolbar-item:not(.e-overlay):not(.e-hidden)'));
            var getFocusToolbarItems = [];
            for (var i = 0; i < getFocusToolbarElements.length; i++) {
                if (!ej2_base_1.isNullOrUndefined(getFocusToolbarElements[parseInt(i.toString(), 10)].querySelector('.e-btn,.e-input,.e-toolbar-item-focus'))) {
                    getFocusToolbarItems.push(getFocusToolbarElements[parseInt(i.toString(), 10)]);
                }
            }
            return getFocusToolbarItems;
        };
        Toolbar.prototype.keyPressedHandler = function (e) {
            if (e.target && util_1.parentsUntil(e.target, 'e-toolbar-item')) {
                var targetParent = util_1.parentsUntil(e.target, 'e-toolbar-item');
                var focusableToolbarItems = this.getFocusableToolbarItems();
                Eif (e.action === 'tab' || e.action === 'shiftTab') {
                    Iif ((e.action === 'tab' && targetParent === focusableToolbarItems[focusableToolbarItems.length - 1])
                        || (e.action === 'shiftTab' && targetParent === focusableToolbarItems[0])) {
                        return;
                    }
                    for (var i = 0; i < focusableToolbarItems.length; i++) {
                        if (targetParent === focusableToolbarItems[parseInt(i.toString(), 10)]) {
                            e.preventDefault();
                            var index = e.action === 'tab' ? i + 1 : i - 1;
                            this.setFocusToolbarItem(focusableToolbarItems[parseInt(index.toString(), 10)]);
                            return;
                        }
                    }
                }
                if (e.action === 'enter') {
                    if (this.parent.enableAdaptiveUI && !this.searchElement
                        && e.target.id === this.gridID + '_searchbutton') {
                        this.renderResponsiveSearch(true);
                    }
                }
            }
        };
        Toolbar.prototype.reRenderToolbar = function () {
            Eif (this.element) {
                ej2_base_2.remove(this.element);
            }
            this.render();
        };
        Toolbar.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.evtHandlers = [{ event: events.setFullScreenDialog, handler: this.reRenderToolbar },
                { event: events.initialEnd, handler: this.render },
                { event: events.uiUpdate, handler: this.onPropertyChanged },
                { event: events.inBoundModelChanged, handler: this.updateSearchBox.bind(this) },
                { event: events.modelChanged, handler: this.refreshToolbarItems },
                { event: events.toolbarRefresh, handler: this.refreshToolbarItems },
                { event: events.inBoundModelChanged, handler: this.modelChanged },
                { event: events.dataBound, handler: this.refreshToolbarItems },
                { event: events.click, handler: this.removeResponsiveSearch },
                { event: events.rowModeChange, handler: this.reRenderToolbar },
                { event: events.destroy, handler: this.destroy },
                { event: events.keyPressed, handler: this.keyPressedHandler }];
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, true, this);
            this.rowSelectedFunction = this.rowSelected.bind(this);
            this.rowDeSelectedFunction = this.rowSelected.bind(this);
            this.parent.addEventListener(events.rowSelected, this.rowSelectedFunction);
            this.parent.addEventListener(events.rowDeselected, this.rowDeSelectedFunction);
        };
        Toolbar.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            util_1.addRemoveEventListener(this.parent, this.evtHandlers, false);
            this.parent.removeEventListener(events.rowSelected, this.rowSelectedFunction);
            this.parent.removeEventListener(events.rowDeselected, this.rowDeSelectedFunction);
        };
        Toolbar.prototype.removeResponsiveSearch = function (e) {
            var target = e.target;
            var isSearch = target.classList.contains('e-search-icon') || target.classList.contains('e-search-wrapper');
            Iif (this.parent.enableAdaptiveUI && !isSearch && this.searchElement
                && !util_1.parentsUntil(e.target, 'e-res-toolbar')) {
                this.renderResponsiveSearch(false);
            }
        };
        Toolbar.prototype.rowSelected = function () {
            if (this.parent.enableAdaptiveUI && this.toolbar.element) {
                this.refreshResponsiveToolbarItems(enum_1.ResponsiveToolbarAction.isInitial);
                this.toolbar.refreshOverflow();
            }
        };
        Toolbar.prototype.getModuleName = function () {
            return 'toolbar';
        };
        return Toolbar;
    }());
    exports.Toolbar = Toolbar;
});