all files / grid/actions/ group.js

84.81% Statements 430/507
74.55% Branches 208/279
84.13% Functions 53/63
84.69% Lines 426/503
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   120× 120× 120× 120×       120×                               120×     120×                 120×             120×                           120× 120× 120× 120× 120× 120× 120× 120× 120×                                                             120×     120× 120× 120× 120× 120× 120× 120× 120× 120× 120× 120× 120× 120× 120× 120×   86× 84×     118× 118× 118× 118× 27× 27× 34× 34×   27×     84× 84×   73×   11× 11×     17×                                   94× 94× 94× 94×   94×     94×                     94× 94× 94×               113×   113× 15× 15× 15× 15× 15× 15× 15× 15× 15× 15×     10× 10× 10× 10×   15× 15× 49×   34× 19×     15× 11× 11× 11×   11×                 15×     15×       80× 32× 32× 28× 28×     32×           48×           120× 120× 120× 120×   120× 120×     120× 120× 120× 120×     206× 100× 100× 100×     106×   25×   106×     120× 120×   120×                 120× 120×         66× 66× 66×       66×   64× 64× 64× 64× 30×     34×   64× 64×   21× 21×   21×     21×   19× 19× 19× 19× 19× 14× 18× 14× 14×       19× 19× 19× 19×     30× 30× 30× 30× 30× 30× 30×   47× 47× 28×     19×   47×     47× 47×   33× 33× 15×     33× 27×         62× 62× 62× 62× 62× 62×     62× 62×             61×         62×         62× 62×   62×         62×         62× 62×   464× 45× 45× 273× 253× 253× 253× 99×   253× 208×                     19× 17× 17×   19×   194× 194× 136×   58× 58×   52× 52× 52× 33×         33×       19×   52× 37× 21× 13×       52×   52×                   58× 58× 58× 58×                 33×     86×   86× 86× 86×         8193×   258× 258× 258× 258× 1534× 258× 258× 197×   195× 195×   66× 66× 15×     129× 56× 41×     15×       258× 158×   156×     278× 202×   76×   80× 83× 168× 36× 36×     80×   80×   48× 48× 48× 239× 239×   48×       12× 21× 21× 21× 21×              
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/util", "../base/constant", "../services/aria-service", "../services/group-model-generator", "@syncfusion/ej2-data"], function (require, exports, ej2_base_1, ej2_base_2, ej2_base_3, util_1, events, aria_service_1, group_model_generator_1, ej2_data_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Group = (function () {
        function Group(parent, groupSettings, sortedColumns, serviceLocator) {
            var _this = this;
            this.isAppliedGroup = false;
            this.isAppliedUnGroup = false;
            this.visualElement = ej2_base_2.createElement('div', {
                className: 'e-cloneproperties e-dragclone e-gdclone',
                styles: 'line-height:23px', attrs: { action: 'grouping' }
            });
            this.helper = function (e) {
                var gObj = _this.parent;
                var target = e.sender.target;
                var element = target.classList.contains('e-groupheadercell') ? target :
                    util_1.parentsUntil(target, 'e-groupheadercell');
                if (!element) {
                    return false;
                }
                _this.column = gObj.getColumnByField(element.firstElementChild.getAttribute('ej-mappingname'));
                _this.visualElement.textContent = element.textContent;
                _this.visualElement.style.width = element.offsetWidth + 2 + 'px';
                _this.visualElement.style.height = element.offsetHeight + 2 + 'px';
                _this.visualElement.setAttribute('e-mappinguid', _this.column.uid);
                gObj.element.appendChild(_this.visualElement);
                return _this.visualElement;
            };
            this.dragStart = function () {
                _this.parent.element.classList.add('e-ungroupdrag');
            };
            this.drag = function (e) {
                var target = e.target;
                var cloneElement = _this.parent.element.querySelector('.e-cloneproperties');
                _this.parent.trigger(events.columnDrag, { target: target, draggableType: 'headercell', column: _this.column });
                ej2_base_2.classList(cloneElement, ['e-defaultcur'], ['e-notallowedcur']);
                if (!(util_1.parentsUntil(target, 'e-gridcontent') || util_1.parentsUntil(target, 'e-headercell'))) {
                    ej2_base_2.classList(cloneElement, ['e-notallowedcur'], ['e-defaultcur']);
                }
            };
            this.dragStop = function (e) {
                _this.parent.element.classList.remove('e-ungroupdrag');
                if (!(util_1.parentsUntil(e.target, 'e-gridcontent') || util_1.parentsUntil(e.target, 'e-gridheader'))) {
                    ej2_base_2.remove(e.helper);
                    return;
                }
            };
            this.drop = function (e) {
                var gObj = _this.parent;
                var column = gObj.getColumnByUid(e.droppedElement.getAttribute('e-mappinguid'));
                _this.element.classList.remove('e-hover');
                ej2_base_2.remove(e.droppedElement);
                _this.aria.setDropTarget(_this.parent.element.querySelector('.e-groupdroparea'), false);
                _this.aria.setGrabbed(_this.parent.getHeaderTable().querySelector('[aria-grabbed=true]'), false);
                if (ej2_base_3.isNullOrUndefined(column) || column.allowGrouping === false ||
                    util_1.parentsUntil(gObj.getColumnHeaderByUid(column.uid), 'e-grid').getAttribute('id') !==
                        gObj.element.getAttribute('id')) {
                    return;
                }
                _this.groupColumn(column.field);
            };
            this.contentRefresh = true;
            this.aria = new aria_service_1.AriaService();
            this.parent = parent;
            this.groupSettings = groupSettings;
            this.serviceLocator = serviceLocator;
            this.sortedColumns = sortedColumns;
            this.focus = serviceLocator.getService('focus');
            this.addEventListener();
            this.groupGenerator = new group_model_generator_1.GroupModelGenerator(this.parent);
        }
        Group.prototype.columnDrag = function (e) {
            var gObj = this.parent;
            var cloneElement = this.parent.element.querySelector('.e-cloneproperties');
            ej2_base_2.classList(cloneElement, ['e-defaultcur'], ['e-notallowedcur']);
            if (!util_1.parentsUntil(e.target, 'e-groupdroparea') &&
                !(this.parent.allowReordering && util_1.parentsUntil(e.target, 'e-headercell'))) {
                ej2_base_2.classList(cloneElement, ['e-notallowedcur'], ['e-defaultcur']);
            }
            e.target.classList.contains('e-groupdroparea') ? this.element.classList.add('e-hover') : this.element.classList.remove('e-hover');
        };
        Group.prototype.columnDragStart = function (e) {
            if (e.target.classList.contains('e-stackedheadercell')) {
                return;
            }
            var gObj = this.parent;
            var dropArea = this.parent.element.querySelector('.e-groupdroparea');
            this.aria.setDropTarget(dropArea, e.column.allowGrouping);
            var element = e.target.classList.contains('e-headercell') ? e.target : util_1.parentsUntil(e.target, 'e-headercell');
            this.aria.setGrabbed(element, true, !e.column.allowGrouping);
        };
        Group.prototype.columnDrop = function (e) {
            var gObj = this.parent;
            if (e.droppedElement.getAttribute('action') === 'grouping') {
                var column = gObj.getColumnByUid(e.droppedElement.getAttribute('e-mappinguid'));
                if (ej2_base_3.isNullOrUndefined(column) || column.allowGrouping === false ||
                    util_1.parentsUntil(gObj.getColumnHeaderByUid(column.uid), 'e-grid').getAttribute('id') !==
                        gObj.element.getAttribute('id')) {
                    return;
                }
                this.ungroupColumn(column.field);
            }
        };
        Group.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.uiUpdate, this.enableAfterRender, this);
            this.parent.on(events.groupComplete, this.onActionComplete, this);
            this.parent.on(events.ungroupComplete, this.onActionComplete, this);
            this.parent.on(events.inBoundModelChanged, this.onPropertyChanged, this);
            this.parent.on(events.click, this.clickHandler, this);
            this.parent.on(events.columnDrag, this.columnDrag, this);
            this.parent.on(events.columnDragStart, this.columnDragStart, this);
            this.parent.on(events.columnDrop, this.columnDrop, this);
            this.parent.on(events.headerRefreshed, this.refreshSortIcons, this);
            this.parent.on(events.sortComplete, this.refreshSortIcons, this);
            this.parent.on(events.keyPressed, this.keyPressHandler, this);
            this.parent.on(events.contentReady, this.initialEnd, this);
            this.parent.on(events.onEmpty, this.initialEnd, this);
            this.parent.on(events.initialEnd, this.render, this);
            this.parent.on(events.groupAggregates, this.onGroupAggregates, this);
        };
        Group.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.initialEnd, this.render);
            this.parent.off(events.uiUpdate, this.enableAfterRender);
            this.parent.off(events.groupComplete, this.onActionComplete);
            this.parent.off(events.ungroupComplete, this.onActionComplete);
            this.parent.off(events.inBoundModelChanged, this.onPropertyChanged);
            this.parent.off(events.click, this.clickHandler);
            this.parent.off(events.columnDrag, this.columnDrag);
            this.parent.off(events.columnDragStart, this.columnDragStart);
            this.parent.off(events.columnDrop, this.columnDrop);
            this.parent.off(events.headerRefreshed, this.refreshSortIcons);
            this.parent.off(events.sortComplete, this.refreshSortIcons);
            this.parent.off(events.keyPressed, this.keyPressHandler);
            this.parent.off(events.groupAggregates, this.onGroupAggregates);
        };
        Group.prototype.initialEnd = function () {
            var gObj = this.parent;
            this.parent.off(events.contentReady, this.initialEnd);
            this.parent.off(events.onEmpty, this.initialEnd);
            if (this.parent.getColumns().length && this.groupSettings.columns.length) {
                this.contentRefresh = false;
                for (var _i = 0, _a = gObj.groupSettings.columns; _i < _a.length; _i++) {
                    var col = _a[_i];
                    this.groupColumn(col);
                }
                this.contentRefresh = true;
            }
        };
        Group.prototype.keyPressHandler = function (e) {
            var gObj = this.parent;
            if ((!this.groupSettings.columns.length ||
                ['altDownArrow', 'altUpArrow', 'ctrlDownArrow', 'ctrlUpArrow', 'enter'].indexOf(e.action) === -1)) {
                return;
            }
            e.preventDefault();
            switch (e.action) {
                case 'altDownArrow':
                case 'altUpArrow':
                    var selected = gObj.allowSelection ? gObj.getSelectedRowIndexes() : [];
                    if (selected.length) {
                        var rows = gObj.getContentTable().querySelector('tbody').children;
                        var dataRow = gObj.getDataRows()[selected[selected.length - 1]];
                        var grpRow = void 0;
                        for (var i = dataRow.rowIndex; i >= 0; i--) {
                            if (!rows[i].classList.contains('e-row') && !rows[i].classList.contains('e-detailrow')) {
                                grpRow = rows[i];
                                break;
                            }
                        }
                        this.expandCollapseRows(grpRow.querySelector(e.action === 'altUpArrow' ?
                            '.e-recordplusexpand' : '.e-recordpluscollapse'));
                    }
                    break;
                case 'ctrlDownArrow':
                    this.expandAll();
                    break;
                case 'ctrlUpArrow':
                    this.collapseAll();
                    break;
                case 'enter':
                    Eif (this.parent.isEdit || (ej2_base_2.closest(e.target, '#' + this.parent.element.id + '_searchbar') !== null)) {
                        return;
                    }
                    var element = this.focus.getFocusedElement();
                    var row = element ? element.parentElement.querySelector('[class^="e-record"]') : null;
                    if (!row) {
                        break;
                    }
                    this.expandCollapseRows(row);
                    break;
            }
        };
        Group.prototype.clickHandler = function (e) {
            this.expandCollapseRows(e.target);
            this.applySortFromTarget(e.target);
            this.unGroupFromTarget(e.target);
            this.toogleGroupFromHeader(e.target);
        };
        Group.prototype.unGroupFromTarget = function (target) {
            if (target.classList.contains('e-ungroupbutton')) {
                this.ungroupColumn(target.parentElement.getAttribute('ej-mappingname'));
            }
        };
        Group.prototype.toogleGroupFromHeader = function (target) {
            if (this.groupSettings.showToggleButton) {
                if (target.classList.contains('e-grptogglebtn')) {
                    if (target.classList.contains('e-toggleungroup')) {
                        this.ungroupColumn(this.parent.getColumnByUid(target.parentElement.getAttribute('e-mappinguid')).field);
                    }
                    else {
                        this.groupColumn(this.parent.getColumnByUid(target.parentElement.getAttribute('e-mappinguid')).field);
                    }
                }
                else {
                    Iif (target.classList.contains('e-toggleungroup')) {
                        this.ungroupColumn(target.parentElement.getAttribute('ej-mappingname'));
                    }
                }
            }
        };
        Group.prototype.applySortFromTarget = function (target) {
            var gObj = this.parent;
            var gHeader = ej2_base_2.closest(target, '.e-groupheadercell');
            if (gObj.allowSorting && gHeader && !target.classList.contains('e-ungroupbutton') &&
                !target.classList.contains('e-toggleungroup')) {
                var field = gHeader.firstElementChild.getAttribute('ej-mappingname');
                Eif (gObj.getColumnHeaderByField(field).querySelectorAll('.e-ascending').length) {
                    gObj.sortColumn(field, 'Descending', true);
                }
                else {
                    gObj.sortColumn(field, 'Ascending', true);
                }
            }
        };
        Group.prototype.expandCollapseRows = function (target) {
            var trgt = util_1.parentsUntil(target, 'e-recordplusexpand') ||
                util_1.parentsUntil(target, 'e-recordpluscollapse');
            if (trgt) {
                var cellIdx = trgt.cellIndex;
                var rowIdx = trgt.parentElement.rowIndex;
                var rowNodes = this.parent.getContentTable().querySelector('tbody').children;
                var rows = [].slice.call(rowNodes).slice(rowIdx + 1, rowNodes.length);
                var isHide = void 0;
                var expandElem = void 0;
                var toExpand = [];
                var indent = trgt.parentElement.querySelectorAll('.e-indentcell').length;
                var expand = false;
                if (trgt.classList.contains('e-recordpluscollapse')) {
                    ej2_base_2.addClass([trgt], 'e-recordplusexpand');
                    ej2_base_2.removeClass([trgt], 'e-recordpluscollapse');
                    trgt.firstElementChild.className = 'e-icons e-gdiagonaldown e-icon-gdownarrow';
                    expand = true;
                }
                else {
                    isHide = true;
                    ej2_base_2.removeClass([trgt], 'e-recordplusexpand');
                    ej2_base_2.addClass([trgt], 'e-recordpluscollapse');
                    trgt.firstElementChild.className = 'e-icons e-gnextforward e-icon-grightarrow';
                }
                this.aria.setExpand(trgt, expand);
                for (var i = 0, len = rows.length; i < len; i++) {
                    if (rows[i].querySelectorAll('td')[cellIdx] &&
                        rows[i].querySelectorAll('td')[cellIdx].classList.contains('e-indentcell') && rows) {
                        if (isHide) {
                            rows[i].style.display = 'none';
                        }
                        else {
                            if (rows[i].querySelectorAll('.e-indentcell').length === indent + 1) {
                                rows[i].style.display = '';
                                expandElem = rows[i].querySelector('.e-recordplusexpand');
                                if (expandElem) {
                                    toExpand.push(expandElem);
                                }
                                Iif (rows[i].classList.contains('e-detailrow')) {
                                    if (rows[i - 1].querySelectorAll('.e-detailrowcollapse').length) {
                                        rows[i].style.display = 'none';
                                    }
                                }
                            }
                        }
                    }
                    else {
                        break;
                    }
                }
                for (var i = 0, len = toExpand.length; i < len; i++) {
                    ej2_base_2.removeClass([toExpand[i]], 'e-recordplusexpand');
                    ej2_base_2.addClass([toExpand[i]], 'e-recordpluscollapse');
                    toExpand[i].firstElementChild.className = 'e-icons e-gnextforward e-icon-grightarrow';
                    this.expandCollapseRows(toExpand[i]);
                }
            }
        };
        Group.prototype.expandCollapse = function (isExpand) {
            var rowNodes = this.parent.getContentTable().querySelector('tbody').children;
            var row;
            for (var i = 0, len = rowNodes.length; i < len; i++) {
                if (rowNodes[i].querySelectorAll('.e-recordplusexpand, .e-recordpluscollapse').length) {
                    row = rowNodes[i].querySelector(isExpand ? '.e-recordpluscollapse' : '.e-recordplusexpand');
                    if (row) {
                        row.className = isExpand ? 'e-recordplusexpand' : 'e-recordpluscollapse';
                        row.firstElementChild.className = isExpand ? 'e-icons e-gdiagonaldown e-icon-gdownarrow' :
                            'e-icons e-gnextforward e-icon-grightarrow';
                    }
                    Iif (!(rowNodes[i].firstElementChild.classList.contains('e-recordplusexpand') ||
                        rowNodes[i].firstElementChild.classList.contains('e-recordpluscollapse'))) {
                        rowNodes[i].style.display = isExpand ? '' : 'none';
                    }
                }
                else {
                    rowNodes[i].style.display = isExpand ? '' : 'none';
                }
            }
        };
        Group.prototype.expandAll = function () {
            this.expandCollapse(true);
        };
        Group.prototype.collapseAll = function () {
            this.expandCollapse(false);
        };
        Group.prototype.render = function () {
            this.l10n = this.serviceLocator.getService('localization');
            this.renderGroupDropArea();
            this.initDragAndDrop();
            this.refreshToggleBtn();
        };
        Group.prototype.renderGroupDropArea = function () {
            var groupElem = this.parent.element.querySelector('.e-groupdroparea');
            Iif (groupElem) {
                ej2_base_2.remove(groupElem);
            }
            this.element = ej2_base_2.createElement('div', { className: 'e-groupdroparea', attrs: { 'tabindex': '-1' } });
            this.updateGroupDropArea();
            this.parent.element.insertBefore(this.element, this.parent.element.firstChild);
            if (!this.groupSettings.showDropArea) {
                this.element.style.display = 'none';
            }
        };
        Group.prototype.updateGroupDropArea = function (clear) {
            if (this.groupSettings.showDropArea && !this.groupSettings.columns.length) {
                var dragLabel = this.l10n.getConstant('GroupDropArea');
                this.element.innerHTML = dragLabel;
                this.element.classList.remove('e-grouped');
            }
            else {
                if ((this.element.innerHTML === this.l10n.getConstant('GroupDropArea') && (this.groupSettings.columns.length === 1
                    || !this.isAppliedGroup && !this.isAppliedUnGroup)) || clear) {
                    this.element.innerHTML = '';
                }
                this.element.classList.add('e-grouped');
            }
        };
        Group.prototype.initDragAndDrop = function () {
            this.initializeGHeaderDrop();
            this.initializeGHeaderDrag();
        };
        Group.prototype.initializeGHeaderDrag = function () {
            var drag = new ej2_base_1.Draggable(this.element, {
                dragTarget: '.e-groupheadercell',
                distance: 5,
                helper: this.helper,
                dragStart: this.dragStart,
                drag: this.drag,
                dragStop: this.dragStop
            });
        };
        Group.prototype.initializeGHeaderDrop = function () {
            var gObj = this.parent;
            var drop = new ej2_base_1.Droppable(this.element, {
                accept: '.e-dragclone',
                drop: this.drop
            });
        };
        Group.prototype.groupColumn = function (columnName) {
            var gObj = this.parent;
            var column = gObj.getColumnByField(columnName);
            Iif (ej2_base_3.isNullOrUndefined(column) || column.allowGrouping === false ||
                (this.contentRefresh && this.groupSettings.columns.indexOf(columnName) > -1)) {
                return;
            }
            if (util_1.isActionPrevent(gObj)) {
                gObj.notify(events.preventBatch, { instance: this, handler: this.groupColumn, arg1: columnName });
                return;
            }
            column.visible = gObj.groupSettings.showGroupedColumn;
            this.colName = columnName;
            this.isAppliedGroup = true;
            if (this.contentRefresh) {
                this.updateModel();
            }
            else {
                this.addColToGroupDrop(columnName);
            }
            this.updateGroupDropArea();
            this.isAppliedGroup = false;
        };
        Group.prototype.ungroupColumn = function (columnName) {
            var gObj = this.parent;
            var column = this.parent.enableColumnVirtualization ?
                this.parent.columns.filter(function (c) { return c.field === columnName; })[0] : gObj.getColumnByField(columnName);
            Iif (ej2_base_3.isNullOrUndefined(column) || column.allowGrouping === false || this.groupSettings.columns.indexOf(columnName) < 0) {
                return;
            }
            if (util_1.isActionPrevent(gObj)) {
                gObj.notify(events.preventBatch, { instance: this, handler: this.ungroupColumn, arg1: columnName });
                return;
            }
            column.visible = true;
            this.colName = column.field;
            var columns = JSON.parse(JSON.stringify(this.groupSettings.columns));
            columns.splice(columns.indexOf(this.colName), 1);
            if (this.sortedColumns.indexOf(columnName) < 0) {
                for (var i = 0, len = gObj.sortSettings.columns.length; i < len; i++) {
                    if (columnName === gObj.sortSettings.columns[i].field) {
                        gObj.sortSettings.columns.splice(i, 1);
                        break;
                    }
                }
            }
            this.groupSettings.columns = columns;
            Eif (gObj.allowGrouping) {
                this.isAppliedUnGroup = true;
                this.parent.dataBind();
            }
        };
        Group.prototype.updateModel = function () {
            var gObj = this.parent;
            var i = 0;
            var columns = JSON.parse(JSON.stringify(this.groupSettings.columns));
            columns.push(this.colName);
            this.groupSettings.columns = columns;
            this.groupAddSortingQuery(this.colName);
            this.parent.dataBind();
        };
        Group.prototype.onActionComplete = function (e) {
            var gObj = this.parent;
            if (e.requestType === 'grouping') {
                this.addColToGroupDrop(this.colName);
            }
            else {
                this.removeColFromGroupDrop(this.colName);
            }
            var args = this.groupSettings.columns.indexOf(this.colName) > -1 ? {
                columnName: this.colName, requestType: 'grouping', type: events.actionComplete
            } : { requestType: 'ungrouping', type: events.actionComplete };
            this.parent.trigger(events.actionComplete, ej2_base_3.extend(e, args));
            this.colName = null;
        };
        Group.prototype.groupAddSortingQuery = function (colName) {
            var i = 0;
            while (i < this.parent.sortSettings.columns.length) {
                if (this.parent.sortSettings.columns[i].field === colName) {
                    break;
                }
                i++;
            }
            if (this.parent.sortSettings.columns.length === i) {
                this.parent.sortSettings.columns.push({ field: colName, direction: 'Ascending' });
            }
            else Iif (!this.parent.allowSorting) {
                this.parent.sortSettings.columns[i].direction = 'Ascending';
            }
        };
        Group.prototype.addColToGroupDrop = function (field) {
            var gObj = this.parent;
            var direction = 'Ascending';
            var groupedColumn = ej2_base_2.createElement('div', { className: 'e-grid-icon e-groupheadercell' });
            var childDiv = ej2_base_2.createElement('div', { attrs: { 'ej-mappingname': field } });
            var column = this.parent.getColumnByField(field);
            Iif (ej2_base_3.isNullOrUndefined(column)) {
                return;
            }
            var headerCell = gObj.getColumnHeaderByUid(column.uid);
            if (!ej2_base_3.isNullOrUndefined(column.headerTemplate)) {
                Eif (column.headerTemplate.indexOf('#') !== -1) {
                    childDiv.innerHTML = document.querySelector(column.headerTemplate).innerHTML.trim();
                }
                else {
                    childDiv.innerHTML = column.headerTemplate;
                }
                childDiv.firstElementChild.classList.add('e-grouptext');
            }
            else {
                childDiv.appendChild(ej2_base_2.createElement('span', {
                    className: 'e-grouptext', innerHTML: column.headerText,
                    attrs: { tabindex: '-1', 'aria-label': 'sort the grouped column' }
                }));
            }
            if (this.groupSettings.showToggleButton) {
                childDiv.appendChild(ej2_base_2.createElement('span', {
                    className: 'e-togglegroupbutton e-icons e-icon-ungroup e-toggleungroup', innerHTML: '&nbsp;',
                    attrs: { tabindex: '-1', 'aria-label': 'ungroup button' }
                }));
            }
            Eif (headerCell.querySelectorAll('.e-ascending,.e-descending').length) {
                direction = headerCell.querySelector('.e-ascending') ? 'Ascending' : 'Descending';
            }
            childDiv.appendChild(ej2_base_2.createElement('span', {
                className: 'e-groupsort e-icons ' +
                    ('e-' + direction.toLowerCase() + ' e-icon-' + direction.toLowerCase()), innerHTML: '&nbsp;',
                attrs: { tabindex: '-1', 'aria-label': 'sort the grouped column' }
            }));
            childDiv.appendChild(ej2_base_2.createElement('span', {
                className: 'e-ungroupbutton e-icons e-icon-hide', innerHTML: '&nbsp;',
                attrs: { title: this.l10n.getConstant('UnGroup'), tabindex: '-1', 'aria-label': 'ungroup the grouped column' },
                styles: this.groupSettings.showUngroupButton ? '' : 'display:none'
            }));
            groupedColumn.appendChild(childDiv);
            this.element.appendChild(groupedColumn);
        };
        Group.prototype.refreshToggleBtn = function (isRemove) {
            if (this.groupSettings.showToggleButton) {
                var headers = [].slice.call(this.parent.element.getElementsByClassName('e-headercelldiv'));
                for (var i = 0, len = headers.length; i < len; i++) {
                    if (!((headers[i].classList.contains('e-emptycell')) || (headers[i].classList.contains('e-headerchkcelldiv')))) {
                        var column = this.parent.getColumnByUid(headers[i].getAttribute('e-mappinguid'));
                        Eif (!this.parent.showColumnMenu || (this.parent.showColumnMenu && !column.showColumnMenu)) {
                            if (headers[i].querySelectorAll('.e-grptogglebtn').length) {
                                ej2_base_2.remove(headers[i].querySelectorAll('.e-grptogglebtn')[0]);
                            }
                            if (!isRemove) {
                                headers[i].appendChild(ej2_base_2.createElement('span', {
                                    className: 'e-grptogglebtn e-icons ' +
                                        (this.groupSettings.columns.indexOf(column.field) > -1 ? 'e-toggleungroup e-icon-ungroup'
                                            : 'e-togglegroup e-icon-group'), attrs: { tabindex: '-1', 'aria-label': 'Group button' }
                                }));
                            }
                        }
                    }
                }
            }
        };
        Group.prototype.removeColFromGroupDrop = function (field) {
            if (!ej2_base_3.isNullOrUndefined(this.getGHeaderCell(field))) {
                ej2_base_2.remove(this.getGHeaderCell(field));
                this.updateGroupDropArea();
            }
            this.isAppliedUnGroup = false;
        };
        Group.prototype.onPropertyChanged = function (e) {
            var _this = this;
            if (e.module !== this.getModuleName()) {
                return;
            }
            var _loop_1 = function (prop) {
                switch (prop) {
                    case 'columns':
                        var args = void 0;
                        Eif (this_1.contentRefresh) {
                            if (!this_1.isAppliedUnGroup) {
                                if (!this_1.isAppliedGroup) {
                                    this_1.updateGroupDropArea(true);
                                    for (var i = 0; i < this_1.groupSettings.columns.length; i++) {
                                        this_1.colName = this_1.groupSettings.columns[i];
                                        var col = this_1.parent.getColumnByField(this_1.colName);
                                        col.visible = this_1.parent.groupSettings.showGroupedColumn;
                                        this_1.groupAddSortingQuery(this_1.colName);
                                        Iif (i < this_1.groupSettings.columns.length - 1) {
                                            this_1.addColToGroupDrop(this_1.groupSettings.columns[i]);
                                        }
                                    }
                                }
                                args = { columnName: this_1.colName, requestType: e.properties[prop].length ? 'grouping' : 'ungrouping',
                                    type: events.actionBegin };
                            }
                            else {
                                args = { requestType: 'ungrouping', type: events.actionBegin };
                            }
                            if (!this_1.groupSettings.showGroupedColumn) {
                                e.oldProperties[prop].forEach(function (column) {
                                    if (e.properties[prop].indexOf(column) === -1) {
                                        _this.parent.getColumnByField(column).visible = true;
                                    }
                                });
                            }
                            this_1.parent.notify(events.modelChanged, args);
                        }
                        break;
                    case 'showDropArea':
                        this_1.updateGroupDropArea();
                        this_1.groupSettings.showDropArea ? this_1.element.style.display = '' : this_1.element.style.display = 'none';
                        break;
                    case 'showGroupedColumn':
                        this_1.updateGroupedColumn(this_1.groupSettings.showGroupedColumn);
                        this_1.parent.notify(events.modelChanged, { requestType: 'refresh' });
                        break;
                    case 'showUngroupButton':
                        this_1.updateButtonVisibility(this_1.groupSettings.showUngroupButton, 'e-ungroupbutton');
                        break;
                    case 'showToggleButton':
                        this_1.updateButtonVisibility(this_1.groupSettings.showToggleButton, 'e-togglegroupbutton ');
                        this_1.parent.refreshHeader();
                        break;
                }
            };
            var this_1 = this;
            for (var _i = 0, _a = Object.keys(e.properties); _i < _a.length; _i++) {
                var prop = _a[_i];
                _loop_1(prop);
            }
        };
        Group.prototype.updateGroupedColumn = function (isVisible) {
            for (var i = 0; i < this.groupSettings.columns.length; i++) {
                this.parent.getColumnByField(this.groupSettings.columns[i]).visible = isVisible;
            }
        };
        Group.prototype.updateButtonVisibility = function (isVisible, className) {
            var gHeader = [].slice.call(this.element.querySelectorAll('.' + className));
            for (var i = 0; i < gHeader.length; i++) {
                gHeader[i].style.display = isVisible ? '' : 'none';
            }
        };
        Group.prototype.enableAfterRender = function (e) {
            if (e.module === this.getModuleName() && e.enable) {
                this.render();
            }
        };
        Group.prototype.destroy = function () {
            if (!this.parent.isDestroyed) {
                this.clearGrouping();
            }
            this.removeEventListener();
            this.refreshToggleBtn(true);
            ej2_base_2.remove(this.element);
        };
        Group.prototype.clearGrouping = function () {
            var cols = JSON.parse(JSON.stringify(this.groupSettings.columns));
            this.contentRefresh = false;
            for (var i = 0, len = cols.length; i < len; i++) {
                this.ungroupColumn(cols[i]);
            }
            this.contentRefresh = true;
        };
        Group.prototype.getModuleName = function () {
            return 'group';
        };
        Group.prototype.refreshSortIcons = function (e) {
            var gObj = this.parent;
            var header;
            var cols = gObj.sortSettings.columns;
            var gCols = gObj.groupSettings.columns;
            var fieldNames = this.parent.getColumns().map(function (c) { return c.field; });
            this.refreshToggleBtn();
            for (var i = 0, len = cols.length; i < len; i++) {
                if (fieldNames.indexOf(cols[i].field) === -1) {
                    continue;
                }
                header = gObj.getColumnHeaderByField(cols[i].field);
                if (!gObj.allowSorting && (this.sortedColumns.indexOf(cols[i].field) > -1 ||
                    this.groupSettings.columns.indexOf(cols[i].field) > -1)) {
                    ej2_base_2.classList(header.querySelector('.e-sortfilterdiv'), ['e-ascending', 'e-icon-ascending'], []);
                    if (cols.length > 1) {
                        header.querySelector('.e-headercelldiv').appendChild(ej2_base_2.createElement('span', { className: 'e-sortnumber', innerHTML: (i + 1).toString() }));
                    }
                }
                else if (this.getGHeaderCell(cols[i].field) && this.getGHeaderCell(cols[i].field).querySelectorAll('.e-groupsort').length) {
                    if (cols[i].direction === 'Ascending') {
                        ej2_base_2.classList(this.getGHeaderCell(cols[i].field).querySelector('.e-groupsort'), ['e-ascending', 'e-icon-ascending'], ['e-descending', 'e-icon-descending']);
                    }
                    else {
                        ej2_base_2.classList(this.getGHeaderCell(cols[i].field).querySelector('.e-groupsort'), ['e-descending', 'e-icon-descending'], ['e-ascending', 'e-icon-ascending']);
                    }
                }
            }
            for (var i = 0, len = gCols.length; i < len; i++) {
                if (fieldNames.indexOf(gCols[i]) === -1) {
                    continue;
                }
                gObj.getColumnHeaderByField(gCols[i]).setAttribute('aria-grouped', 'true');
            }
        };
        Group.prototype.getGHeaderCell = function (field) {
            if (this.element && this.element.querySelector('[ej-mappingname="' + field + '"]')) {
                return this.element.querySelector('[ej-mappingname="' + field + '"]').parentElement;
            }
            return null;
        };
        Group.prototype.onGroupAggregates = function (editedData) {
            var _this = this;
            var aggregates = this.iterateGroupAggregates(editedData);
            var rowData = this.groupGenerator.generateRows(aggregates, {});
            var summaryRows = this.parent.getRowsObject().filter(function (row) { return !row.isDataRow; });
            var updateSummaryRows = rowData.filter(function (data) { return !data.isDataRow; });
            updateSummaryRows.forEach(function (row, indx) {
                var cells = row.cells.filter(function (cell) { return cell.isDataCell; });
                var args = { cells: cells, data: row.data, dataUid: summaryRows[indx] ? summaryRows[indx].uid : '' };
                _this.parent.notify(events.refreshAggregateCell, args);
            });
        };
        Group.prototype.iterateGroupAggregates = function (editedData) {
            var updatedData = editedData instanceof Array ? editedData : [];
            var rows = this.parent.getRowsObject();
            var initData = this.parent.getCurrentViewRecords();
            var deletedCols = [];
            var changeds = rows.map(function (row) {
                if (row.edit === 'delete') {
                    deletedCols.push(row.data);
                }
                return row.changes instanceof Object ? row.changes : row.data;
            });
            var field = this.parent.getPrimaryKeyFieldNames()[0];
            changeds = updatedData.length === 0 ? changeds : updatedData;
            var mergeData = initData.map(function (item) {
                var pKeyVal = ej2_data_1.DataUtil.getObject(field, item);
                var value;
                var hasVal = changeds.some(function (cItem) {
                    value = cItem;
                    return pKeyVal === ej2_data_1.DataUtil.getObject(field, cItem);
                });
                return hasVal ? value : item;
            });
            if (deletedCols.length > 0) {
                deletedCols.forEach(function (row) {
                    var index = mergeData.indexOf(row);
                    mergeData.splice(index, 1);
                });
            }
            var aggregates = [];
            var aggregateRows = this.parent.aggregates;
            aggregateRows.forEach(function (row) {
                row.columns.forEach(function (col) {
                    var aggr = {};
                    var type = col.type.toString();
                    aggr = { type: type.toLowerCase(), field: col.field };
                    aggregates.push(aggr);
                });
            });
            var result;
            var aggrds;
            var groupedCols = this.parent.groupSettings.columns;
            groupedCols.forEach(function (field) {
                aggrds = result ? result : mergeData;
                result = ej2_data_1.DataUtil.group(aggrds, field, aggregates, null, null);
            });
            return result;
        };
        return Group;
    }());
    exports.Group = Group;
});