all files / grid/actions/ column-menu.js

98.09% Statements 308/314
91.47% Branches 193/211
100% Functions 46/46
98.09% Lines 308/314
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     10× 10× 43×     31×                                   18×                                                       34×           32×                       51×     36× 36× 36× 36× 35×   30×         45× 45× 45×           10× 10×                             45×   11× 11×                       11× 11×         36×   31× 31× 30×     24×             31× 31×             31×       31×   31×   60×   134×       7970×                               33× 33× 27×                                                                     12× 12×   19×   23×          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "../base/util", "../base/constant", "@syncfusion/ej2-popups", "@syncfusion/ej2-buttons", "../actions/group", "../actions/sort", "../actions/filter", "../actions/resize"], function (require, exports, ej2_base_1, ej2_base_2, ej2_navigations_1, util_1, events, ej2_popups_1, ej2_buttons_1, group_1, sort_1, filter_1, resize_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ColumnMenu = (function () {
        function ColumnMenu(parent, serviceLocator) {
            this.defaultItems = {};
            this.localeText = this.setLocaleKey();
            this.disableItems = [];
            this.hiddenItems = [];
            this.isOpen = false;
            this.GROUP = 'e-icon-group';
            this.UNGROUP = 'e-icon-ungroup';
            this.ASCENDING = 'e-icon-ascending';
            this.DESCENDING = 'e-icon-descending';
            this.ROOT = 'e-columnmenu';
            this.FILTER = 'e-icon-filter';
            this.POP = 'e-filter-popup';
            this.WRAP = 'e-col-menu';
            this.CHOOSER = '_chooser_';
            this.parent = parent;
            this.gridID = parent.element.id;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
        }
        ColumnMenu.prototype.wireEvents = function () {
            var _this = this;
            this.getColumnMenuHandlers().forEach(function (ele) {
                ej2_base_1.EventHandler.add(ele, 'mousedown', _this.columnMenuHandlerDown, _this);
            });
        };
        ColumnMenu.prototype.unwireEvents = function () {
            var _this = this;
            this.getColumnMenuHandlers().forEach(function (ele) {
                ej2_base_1.EventHandler.remove(ele, 'mousedown', _this.columnMenuHandlerDown);
            });
        };
        ColumnMenu.prototype.destroy = function () {
            this.columnMenu.destroy();
            this.removeEventListener();
            this.unwireFilterEvents();
            this.unwireEvents();
            ej2_base_2.remove(this.element);
        };
        ColumnMenu.prototype.columnMenuHandlerClick = function (e) {
            Eif (e.target.classList.contains('e-columnmenu')) {
                if ((this.isOpen && this.headerCell !== this.getHeaderCell(e)) || document.querySelector('.e-grid-menu .e-menu-parent.e-ul')) {
                    this.columnMenu.close();
                    this.openColumnMenu(e);
                }
                else if (!this.isOpen) {
                    this.openColumnMenu(e);
                }
                else {
                    this.columnMenu.close();
                }
            }
        };
        ColumnMenu.prototype.openColumnMenu = function (e) {
            var pos = { top: 0, left: 0 };
            this.element.style.cssText = 'display:block;visibility:hidden';
            var elePos = this.element.getBoundingClientRect();
            this.element.style.cssText = 'display:none;visibility:visible';
            this.headerCell = this.getHeaderCell(e);
            if (ej2_base_1.Browser.isDevice) {
                pos.top = ((window.innerHeight / 2) - (elePos.height / 2));
                pos.left = ((window.innerWidth / 2) - (elePos.width / 2));
            }
            else {
                if (this.parent.enableRtl) {
                    pos = ej2_popups_1.calculatePosition(this.headerCell, 'left', 'bottom');
                }
                else {
                    pos = ej2_popups_1.calculatePosition(this.headerCell, 'right', 'bottom');
                    pos.left -= elePos.width;
                }
            }
            this.columnMenu.open(pos.top, pos.left);
            e.preventDefault();
        };
        ColumnMenu.prototype.columnMenuHandlerDown = function (e) {
            this.isOpen = !(this.element.style.display === 'none' || this.element.style.display === '');
        };
        ColumnMenu.prototype.getColumnMenuHandlers = function () {
            return [].slice.call(this.parent.getHeaderTable().querySelectorAll('.' + this.ROOT));
        };
        ColumnMenu.prototype.addEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.headerRefreshed, this.wireEvents, this);
            this.parent.on(events.uiUpdate, this.enableAfterRenderMenu, this);
            this.parent.on(events.initialEnd, this.render, this);
            if (this.isFilterItemAdded()) {
                this.parent.on(events.filterDialogCreated, this.filterPosition, this);
            }
            this.parent.on(events.click, this.columnMenuHandlerClick, this);
        };
        ColumnMenu.prototype.removeEventListener = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.headerRefreshed, this.unwireEvents);
            this.parent.off(events.uiUpdate, this.enableAfterRenderMenu);
            this.parent.off(events.initialEnd, this.render);
            Iif (this.isFilterItemAdded()) {
                this.parent.off(events.filterDialogCreated, this.filterPosition);
            }
            this.parent.off(events.click, this.columnMenuHandlerClick);
        };
        ColumnMenu.prototype.enableAfterRenderMenu = function (e) {
            if (e.module === this.getModuleName() && e.enable) {
                if (this.columnMenu) {
                    this.columnMenu.destroy();
                    ej2_base_2.remove(this.element);
                }
                this.render();
            }
        };
        ColumnMenu.prototype.render = function () {
            this.l10n = this.serviceLocator.getService('localization');
            this.element = ej2_base_2.createElement('ul', { id: this.gridID + '_columnmenu', className: 'e-colmenu' });
            this.parent.element.appendChild(this.element);
            this.columnMenu = new ej2_navigations_1.ContextMenu({
                cssClass: 'e-grid-menu',
                enableRtl: this.parent.enableRtl,
                enablePersistence: this.parent.enablePersistence,
                locale: this.parent.locale,
                items: this.getItems(),
                select: this.columnMenuItemClick.bind(this),
                beforeOpen: this.columnMenuBeforeOpen.bind(this),
                onClose: this.columnMenuOnClose.bind(this),
                beforeItemRender: this.beforeMenuItemRender.bind(this),
                beforeClose: this.columnMenuBeforeClose.bind(this)
            });
            this.columnMenu.appendTo(this.element);
            this.wireFilterEvents();
        };
        ColumnMenu.prototype.wireFilterEvents = function () {
            if (!ej2_base_1.Browser.isDevice && this.isFilterItemAdded()) {
                ej2_base_1.EventHandler.add(this.element, 'mouseover', this.appendFilter, this);
            }
        };
        ColumnMenu.prototype.unwireFilterEvents = function () {
            if (!ej2_base_1.Browser.isDevice && this.isFilterItemAdded()) {
                ej2_base_1.EventHandler.remove(this.element, 'mouseover', this.appendFilter);
            }
        };
        ColumnMenu.prototype.beforeMenuItemRender = function (args) {
            if (this.isChooserItem(args.item)) {
                var field = this.getKeyFromId(args.item.id, this.CHOOSER);
                var column = this.parent.getColumnByField(field);
                var check = ej2_buttons_1.createCheckBox(false, {
                    label: args.item.text,
                    checked: column.visible
                });
                if (this.parent.enableRtl) {
                    check.classList.add('e-rtl');
                }
                args.element.innerHTML = '';
                args.element.appendChild(check);
            }
            else if (args.item.id && this.getKeyFromId(args.item.id) === 'Filter') {
                args.element.appendChild(ej2_base_2.createElement('span', { className: 'e-icons e-caret' }));
                args.element.className += 'e-filter-item e-menu-caret-icon';
            }
        };
        ColumnMenu.prototype.columnMenuBeforeClose = function (args) {
            var colChooser = args.event ? ej2_base_1.closest(args.event.target, '.e-menu-item') : null;
            if (!ej2_base_1.isNullOrUndefined(args.parentItem) &&
                this.getKeyFromId(args.parentItem.id) === 'ColumnChooser' &&
                colChooser && this.isChooserItem(colChooser)) {
                args.cancel = true;
            }
            else if (args.event && (ej2_base_1.closest(args.event.target, '.' + this.POP)
                || (args.event.currentTarget && args.event.currentTarget.activeElement &&
                    util_1.parentsUntil(args.event.currentTarget.activeElement, 'e-filter-popup'))
                || util_1.parentsUntil(args.event.target, 'e-popup') ||
                (util_1.parentsUntil(args.event.target, 'e-popup-wrapper'))) && !ej2_base_1.Browser.isDevice) {
                args.cancel = true;
            }
        };
        ColumnMenu.prototype.isChooserItem = function (item) {
            return item.id && item.id.indexOf('_colmenu_') >= 0 &&
                this.getKeyFromId(item.id, this.CHOOSER).indexOf('_colmenu_') === -1;
        };
        ColumnMenu.prototype.columnMenuBeforeOpen = function (args) {
            args.column = this.targetColumn = this.getColumn();
            this.parent.trigger(events.columnMenuOpen, args);
            for (var _i = 0, _a = args.items; _i < _a.length; _i++) {
                var item = _a[_i];
                var key = this.getKeyFromId(item.id);
                var dItem = this.defaultItems[key];
                if (this.getDefaultItems().indexOf(key) !== -1) {
                    if (this.ensureDisabledStatus(key) && !dItem.hide) {
                        this.disableItems.push(item.text);
                    }
                    else if (item.hide) {
                        this.hiddenItems.push(item.text);
                    }
                }
            }
            this.columnMenu.enableItems(this.disableItems, false);
            this.columnMenu.hideItems(this.hiddenItems);
        };
        ColumnMenu.prototype.ensureDisabledStatus = function (item) {
            var _this = this;
            var status = false;
            switch (item) {
                case 'Group':
                    if (!this.parent.allowGrouping || (this.parent.ensureModuleInjected(group_1.Group) && this.targetColumn
                        && this.parent.groupSettings.columns.indexOf(this.targetColumn.field) >= 0)) {
                        status = true;
                    }
                    break;
                case 'AutoFitAll':
                case 'AutoFit':
                    status = !this.parent.ensureModuleInjected(resize_1.Resize);
                    break;
                case 'Ungroup':
                    Eif (!this.parent.ensureModuleInjected(group_1.Group) || (this.parent.ensureModuleInjected(group_1.Group) && this.targetColumn
                        && this.parent.groupSettings.columns.indexOf(this.targetColumn.field) < 0)) {
                        status = true;
                    }
                    break;
                case 'SortDescending':
                case 'SortAscending':
                    if (this.parent.allowSorting && this.parent.ensureModuleInjected(sort_1.Sort)
                        && this.parent.sortSettings.columns.length > 0 && this.targetColumn) {
                        this.parent.sortSettings.columns.forEach(function (ele) {
                            if (ele.field === _this.targetColumn.field
                                && ele.direction.toLocaleLowerCase() === item.toLocaleLowerCase().replace('sort', '')) {
                                status = true;
                            }
                        });
                    }
                    else if (!this.parent.allowSorting || !this.parent.ensureModuleInjected(sort_1.Sort)) {
                        status = true;
                    }
                    break;
                case 'Filter':
                    status = !(this.parent.allowFiltering && (this.parent.filterSettings.type !== 'FilterBar')
                        && this.parent.ensureModuleInjected(filter_1.Filter));
            }
            return status;
        };
        ColumnMenu.prototype.columnMenuItemClick = function (args) {
            var item = this.isChooserItem(args.item) ? 'ColumnChooser' : this.getKeyFromId(args.item.id);
            switch (item) {
                case 'AutoFit':
                    this.parent.autoFitColumns(this.targetColumn.field);
                    break;
                case 'AutoFitAll':
                    this.parent.autoFitColumns([]);
                    break;
                case 'Ungroup':
                    this.parent.ungroupColumn(this.targetColumn.field);
                    break;
                case 'Group':
                    this.parent.groupColumn(this.targetColumn.field);
                    break;
                case 'SortAscending':
                    this.parent.sortColumn(this.targetColumn.field, 'Ascending');
                    break;
                case 'SortDescending':
                    this.parent.sortColumn(this.targetColumn.field, 'Descending');
                    break;
                case 'ColumnChooser':
                    var key = this.getKeyFromId(args.item.id, this.CHOOSER);
                    var checkbox = args.element.querySelector('.e-checkbox-wrapper .e-frame');
                    if (checkbox && checkbox.classList.contains('e-check')) {
                        checkbox.classList.remove('e-check');
                        this.parent.hideColumns(key, 'field');
                    }
                    else if (checkbox) {
                        this.parent.showColumns(key, 'field');
                        checkbox.classList.add('e-check');
                    }
                    break;
                case 'Filter':
                    this.getFilter(args.element, args.item.id);
                    break;
            }
            args.column = this.targetColumn;
            this.parent.trigger(events.columnMenuClick, args);
        };
        ColumnMenu.prototype.columnMenuOnClose = function (args) {
            var parent = 'parentObj';
            Eif (args.items.length > 0 && args.items[0][parent] instanceof ej2_navigations_1.ContextMenu) {
                this.columnMenu.enableItems(this.disableItems);
                this.disableItems = [];
                this.columnMenu.showItems(this.hiddenItems);
                this.hiddenItems = [];
                if (this.isFilterPopupOpen()) {
                    this.getFilter(args.element, args.element.id, true);
                }
            }
        };
        ColumnMenu.prototype.getDefaultItems = function () {
            return ['AutoFitAll', 'AutoFit', 'SortAscending', 'SortDescending', 'Group', 'Ungroup', 'ColumnChooser', 'Filter'];
        };
        ColumnMenu.prototype.getItems = function () {
            var items = [];
            var defultItems = this.parent.columnMenuItems ? this.parent.columnMenuItems : this.getDefault();
            for (var _i = 0, defultItems_1 = defultItems; _i < defultItems_1.length; _i++) {
                var item = defultItems_1[_i];
                if (typeof item === 'string') {
                    if (item === 'ColumnChooser') {
                        var col = this.getDefaultItem(item);
                        col.items = this.createChooserItems();
                        items.push(col);
                    }
                    else {
                        items.push(this.getDefaultItem(item));
                    }
                }
                else {
                    items.push(item);
                }
            }
            return items;
        };
        ColumnMenu.prototype.getDefaultItem = function (item) {
            var menuItem = {};
            switch (item) {
                case 'SortAscending':
                    menuItem = { iconCss: this.ASCENDING };
                    break;
                case 'SortDescending':
                    menuItem = { iconCss: this.DESCENDING };
                    break;
                case 'Group':
                    menuItem = { iconCss: this.GROUP };
                    break;
                case 'Ungroup':
                    menuItem = { iconCss: this.UNGROUP };
                    break;
                case 'Filter':
                    menuItem = { iconCss: this.FILTER };
                    break;
            }
            this.defaultItems[item] = {
                text: this.getLocaleText(item), id: this.generateID(item),
                iconCss: menuItem.iconCss ? 'e-icons ' + menuItem.iconCss : null
            };
            return this.defaultItems[item];
        };
        ColumnMenu.prototype.getLocaleText = function (item) {
            return this.l10n.getConstant(this.localeText[item]);
        };
        ColumnMenu.prototype.generateID = function (item, append) {
            return this.gridID + '_colmenu_' + (append ? append + item : item);
        };
        ColumnMenu.prototype.getKeyFromId = function (id, append) {
            return id.indexOf('_colmenu_') > 0 &&
                id.replace(this.gridID + '_colmenu_' + (append ? append : ''), '');
        };
        ColumnMenu.prototype.getColumnMenu = function () {
            return this.element;
        };
        ColumnMenu.prototype.getModuleName = function () {
            return 'columnMenu';
        };
        ColumnMenu.prototype.setLocaleKey = function () {
            return {
                'AutoFitAll': 'autoFitAll',
                'AutoFit': 'autoFit',
                'Group': 'Group',
                'Ungroup': 'Ungroup',
                'SortAscending': 'SortAscending',
                'SortDescending': 'SortDescending',
                'ColumnChooser': 'Columnchooser',
                'Filter': 'FilterMenu'
            };
        };
        ColumnMenu.prototype.getHeaderCell = function (e) {
            return ej2_base_1.closest(e.target, 'th.e-headercell');
        };
        ColumnMenu.prototype.getColumn = function () {
            Eif (this.headerCell) {
                var uid = this.headerCell.querySelector('.e-headercelldiv').getAttribute('e-mappinguid');
                return this.parent.getColumnByUid(uid);
            }
            return null;
        };
        ColumnMenu.prototype.createChooserItems = function () {
            var items = [];
            for (var _i = 0, _a = this.parent.getColumns(); _i < _a.length; _i++) {
                var col = _a[_i];
                if (col.showInColumnChooser && col.field) {
                    items.push({ id: this.generateID(col.field, this.CHOOSER), text: col.headerText ? col.headerText : col.field });
                }
            }
            return items;
        };
        ColumnMenu.prototype.appendFilter = function (e) {
            var filter = 'Filter';
            if (!this.defaultItems[filter]) {
                return;
            }
            else {
                var key = this.defaultItems[filter].id;
                if (ej2_base_1.closest(e.target, '#' + key) && !this.isFilterPopupOpen()) {
                    this.getFilter(e.target, key);
                }
                else Eif (!ej2_base_1.closest(e.target, '#' + key) && this.isFilterPopupOpen()) {
                    this.getFilter(e.target, key, true);
                }
            }
        };
        ColumnMenu.prototype.getFilter = function (target, id, isClose) {
            var filterPopup = this.getFilterPop();
            if (filterPopup) {
                filterPopup.style.display = !ej2_base_1.Browser.isDevice && isClose ? 'none' : 'block';
            }
            else {
                this.parent.notify(events.filterOpen, {
                    col: this.targetColumn, target: target, isClose: isClose, id: id
                });
            }
        };
        ColumnMenu.prototype.setPosition = function (li, ul) {
            var gridPos = this.parent.element.getBoundingClientRect();
            var liPos = li.getBoundingClientRect();
            var left = liPos.left - gridPos.left;
            var top = liPos.top - gridPos.top;
            Iif (gridPos.height < top) {
                top = top - ul.offsetHeight + liPos.height;
            }
            else Iif (gridPos.height < top + ul.offsetHeight) {
                top = gridPos.height - ul.offsetHeight;
            }
            Iif (window.innerHeight < ul.offsetHeight + top + gridPos.top) {
                top = window.innerHeight - ul.offsetHeight - gridPos.top;
            }
            left += (this.parent.enableRtl ? -ul.offsetWidth : liPos.width);
            Iif (gridPos.width <= left + ul.offsetWidth) {
                left -= liPos.width + ul.offsetWidth;
            }
            else Eif (left < 0) {
                left += ul.offsetWidth + liPos.width;
            }
            ul.style.top = top + 'px';
            ul.style.left = left + 'px';
        };
        ColumnMenu.prototype.filterPosition = function (e) {
            var filterPopup = this.getFilterPop();
            filterPopup.classList.add(this.WRAP);
            Eif (!ej2_base_1.Browser.isDevice) {
                var disp = filterPopup.style.display;
                filterPopup.style.cssText += 'display:block;visibility:hidden';
                var li = this.element.querySelector('.' + this.FILTER);
                Eif (li) {
                    this.setPosition(li.parentElement, filterPopup);
                    filterPopup.style.cssText += 'display:' + disp + ';visibility:visible';
                }
            }
        };
        ColumnMenu.prototype.getDefault = function () {
            var items = [];
            Eif (this.parent.ensureModuleInjected(resize_1.Resize)) {
                items.push('AutoFitAll');
                items.push('AutoFit');
            }
            if (this.parent.allowGrouping && this.parent.ensureModuleInjected(group_1.Group)) {
                items.push('Group');
                items.push('Ungroup');
            }
            if (this.parent.allowSorting && this.parent.ensureModuleInjected(sort_1.Sort)) {
                items.push('SortAscending');
                items.push('SortDescending');
            }
            items.push('ColumnChooser');
            if (this.parent.allowFiltering && (this.parent.filterSettings.type !== 'FilterBar') &&
                this.parent.ensureModuleInjected(filter_1.Filter)) {
                items.push('Filter');
            }
            return items;
        };
        ColumnMenu.prototype.isFilterPopupOpen = function () {
            var filterPopup = this.getFilterPop();
            return filterPopup && filterPopup.style.display !== 'none';
        };
        ColumnMenu.prototype.getFilterPop = function () {
            return this.parent.element.querySelector('.' + this.POP);
        };
        ColumnMenu.prototype.isFilterItemAdded = function () {
            return (this.parent.columnMenuItems &&
                this.parent.columnMenuItems.indexOf('Filter') >= 0) || !this.parent.columnMenuItems;
        };
        return ColumnMenu;
    }());
    exports.ColumnMenu = ColumnMenu;
});