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

94.41% Statements 270/286
87.76% Branches 208/237
96.67% Functions 29/30
94.41% Lines 270/286
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                                                   10× 10× 10× 10× 10× 10× 10× 10×   10×     10× 10×   10× 10×                   11× 11× 11× 11× 11× 11× 11×                       11×         11× 11× 11× 108× 108× 107× 15×     92×         11×   11×   24× 24×   24×   16×   16× 16×                                                                         16× 16×   14× 14× 14×     112×   17× 17× 17× 17× 17×   27× 27× 27× 27× 27×   26×             21× 21× 21× 233× 233× 233× 233× 231× 81×   231×   16× 14×     215×         213×   149×           21× 21× 21× 21× 21× 21×       661× 661×     661× 137×     524×   661×   663×     232× 232× 232×     30× 30×   13×     13×   13×       13×         57× 13×   57×   16×   11×   16×   13×     13×               28×   20×         28×     28×     18×   28×     28×     14×   28×   232×     10× 10× 10× 10×   7969×   112×   249×   112× 112×     15× 15×                                     112×       112×   340×         10×                                           21× 21×   15×   22× 22× 22×          
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "@syncfusion/ej2-navigations", "../base/constant", "../actions/resize", "../actions/page", "../base/util", "../actions/group", "../actions/sort", "../actions/pdf-export", "../actions/excel-export"], function (require, exports, ej2_base_1, ej2_base_2, ej2_navigations_1, events, resize_1, page_1, util_1, group_1, sort_1, pdf_export_1, excel_export_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.menuClass = {
        header: '.e-gridheader',
        content: '.e-gridcontent',
        edit: '.e-inline-edit',
        batchEdit: '.e-editedbatchcell',
        editIcon: 'e-edit',
        pager: '.e-gridpager',
        delete: 'e-delete',
        save: 'e-save',
        cancel: 'e-cancel',
        copy: 'e-copy',
        pdf: 'e-pdfexport',
        group: 'e-icon-group',
        ungroup: 'e-icon-ungroup',
        csv: 'e-csvexport',
        excel: 'e-excelexport',
        fPage: 'e-icon-first',
        nPage: 'e-icon-next',
        lPage: 'e-icon-last',
        pPage: 'e-icon-prev',
        ascending: 'e-icon-ascending',
        descending: 'e-icon-descending',
        groupHeader: 'e-groupdroparea',
        touchPop: 'e-gridpopup'
    };
    var ContextMenu = (function () {
        function ContextMenu(parent, serviceLocator) {
            this.defaultItems = {};
            this.disableItems = [];
            this.hiddenItems = [];
            this.localeText = this.setLocaleKey();
            this.parent = parent;
            this.gridID = parent.element.id;
            this.serviceLocator = serviceLocator;
            this.addEventListener();
        }
        ContextMenu.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.uiUpdate, this.enableAfterRenderMenu, this);
            this.parent.on(events.initialLoad, this.render, this);
        };
        ContextMenu.prototype.removeEventListener = function () {
            Eif (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.initialLoad, this.render);
            this.parent.off(events.uiUpdate, this.enableAfterRenderMenu);
            ej2_base_1.EventHandler.remove(this.element, 'keydown', this.keyDownHandler.bind(this));
        };
        ContextMenu.prototype.keyDownHandler = function (e) {
            if (e.code === 'Tab' || e.which === 9) {
                this.contextMenu.close();
            }
        };
        ContextMenu.prototype.render = function () {
            this.parent.element.classList.add('e-noselect');
            this.l10n = this.serviceLocator.getService('localization');
            this.element = ej2_base_2.createElement('ul', { id: this.gridID + '_cmenu' });
            ej2_base_1.EventHandler.add(this.element, 'keydown', this.keyDownHandler.bind(this));
            this.parent.element.appendChild(this.element);
            var target = '#' + this.gridID;
            this.contextMenu = new ej2_navigations_1.ContextMenu({
                items: this.getMenuItems(),
                enableRtl: this.parent.enableRtl,
                enablePersistence: this.parent.enablePersistence,
                locale: this.parent.locale,
                target: target,
                select: this.contextMenuItemClick.bind(this),
                beforeOpen: this.contextMenuBeforeOpen.bind(this),
                onOpen: this.contextMenuOpen.bind(this),
                onClose: this.contextMenuOnClose.bind(this),
                cssClass: 'e-grid-menu'
            });
            this.contextMenu.appendTo(this.element);
        };
        ContextMenu.prototype.enableAfterRenderMenu = function (e) {
            Eif (e.module === this.getModuleName() && e.enable) {
                if (this.contextMenu) {
                    this.contextMenu.destroy();
                    ej2_base_2.remove(this.element);
                    this.parent.element.classList.remove('e-noselect');
                }
                this.render();
            }
        };
        ContextMenu.prototype.getMenuItems = function () {
            var menuItems = [];
            var exportItems = [];
            for (var _i = 0, _a = this.parent.contextMenuItems; _i < _a.length; _i++) {
                var item = _a[_i];
                if (typeof item === 'string' && this.getDefaultItems().indexOf(item) !== -1) {
                    if (item.toLocaleLowerCase().indexOf('export') !== -1) {
                        exportItems.push(this.buildDefaultItems(item));
                    }
                    else {
                        menuItems.push(this.buildDefaultItems(item));
                    }
                }
                else Eif (typeof item !== 'string') {
                    menuItems.push(item);
                }
            }
            if (exportItems.length > 0) {
                var exportGroup = this.buildDefaultItems('export');
                exportGroup.items = exportItems;
                menuItems.push(exportGroup);
            }
            return menuItems;
        };
        ContextMenu.prototype.getLastPage = function () {
            var totalpage = Math.floor(this.parent.pageSettings.totalRecordsCount / this.parent.pageSettings.pageSize);
            if (this.parent.pageSettings.totalRecordsCount % this.parent.pageSettings.pageSize) {
                totalpage += 1;
            }
            return totalpage;
        };
        ContextMenu.prototype.contextMenuOpen = function () {
            this.isOpen = true;
        };
        ContextMenu.prototype.contextMenuItemClick = function (args) {
            var item = this.getKeyFromId(args.item.id);
            switch (item) {
                case 'AutoFitAll':
                    this.parent.autoFitColumns([]);
                    break;
                case 'AutoFit':
                    this.parent.autoFitColumns(this.targetColumn.field);
                    break;
                case 'Group':
                    this.parent.groupColumn(this.targetColumn.field);
                    break;
                case 'Ungroup':
                    this.parent.ungroupColumn(this.targetColumn.field);
                    break;
                case 'Edit':
                    Iif (this.parent.editSettings.mode === 'Batch') {
                        if (this.row && this.cell && !isNaN(parseInt(this.cell.getAttribute('aria-colindex'), 10))) {
                            this.parent.editModule.editCell(parseInt(this.row.getAttribute('aria-rowindex'), 10), this.parent.getColumns()[parseInt(this.cell.getAttribute('aria-colindex'), 10)].field);
                        }
                    }
                    else {
                        this.parent.editModule.endEdit();
                        this.parent.editModule.startEdit(this.row);
                    }
                    break;
                case 'Delete':
                    Eif (this.parent.editSettings.mode !== 'Batch') {
                        this.parent.editModule.endEdit();
                    }
                    Iif (this.parent.getSelectedRecords().length === 1) {
                        this.parent.editModule.deleteRow(this.row);
                    }
                    else {
                        this.parent.deleteRecord();
                    }
                    break;
                case 'Save':
                    this.parent.editModule.endEdit();
                    break;
                case 'Cancel':
                    this.parent.editModule.closeEdit();
                    break;
                case 'Copy':
                    this.parent.copy();
                    break;
                case 'PdfExport':
                    this.parent.pdfExport();
                    break;
                case 'ExcelExport':
                    this.parent.excelExport();
                    break;
                case 'CsvExport':
                    this.parent.csvExport();
                    break;
                case 'SortAscending':
                    this.isOpen = false;
                    this.parent.sortColumn(this.targetColumn.field, 'Ascending');
                    break;
                case 'SortDescending':
                    this.isOpen = false;
                    this.parent.sortColumn(this.targetColumn.field, 'Descending');
                    break;
                case 'FirstPage':
                    this.parent.goToPage(1);
                    break;
                case 'PrevPage':
                    this.parent.goToPage(this.parent.pageSettings.currentPage - 1);
                    break;
                case 'LastPage':
                    this.parent.goToPage(this.getLastPage());
                    break;
                case 'NextPage':
                    this.parent.goToPage(this.parent.pageSettings.currentPage + 1);
                    break;
            }
            args.column = this.targetColumn;
            this.parent.trigger(events.contextMenuClick, args);
        };
        ContextMenu.prototype.contextMenuOnClose = function (args) {
            var parent = 'parentObj';
            Eif (args.items.length > 0 && args.items[0][parent] instanceof ej2_navigations_1.ContextMenu) {
                this.updateItemStatus();
            }
        };
        ContextMenu.prototype.getLocaleText = function (item) {
            return this.l10n.getConstant(this.localeText[item]);
        };
        ContextMenu.prototype.updateItemStatus = function () {
            this.contextMenu.showItems(this.hiddenItems);
            this.contextMenu.enableItems(this.disableItems);
            this.hiddenItems = [];
            this.disableItems = [];
            this.isOpen = false;
        };
        ContextMenu.prototype.contextMenuBeforeOpen = function (args) {
            var changedRecords = 'changedRecords';
            var addedRecords = 'addedRecords';
            var deletedRecords = 'deletedRecords';
            var closestGrid = ej2_base_1.closest(args.event.target, '.e-grid');
            if (args.event && closestGrid && closestGrid !== this.parent.element) {
                args.cancel = true;
            }
            else if (args.event && (ej2_base_1.closest(args.event.target, '.' + exports.menuClass.groupHeader)
                || ej2_base_1.closest(args.event.target, '.' + exports.menuClass.touchPop) ||
                ej2_base_1.closest(args.event.target, '.e-summarycell') ||
                ej2_base_1.closest(args.event.target, '.e-groupcaption') ||
                ej2_base_1.closest(args.event.target, '.e-filterbarcell'))) {
                args.cancel = true;
            }
            else {
                this.targetColumn = this.getColumn(args.event);
                this.selectRow(args.event, this.parent.selectionSettings.type !== 'Multiple');
                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)) {
                            this.disableItems.push(item.text);
                        }
                        if (args.event && (this.ensureTarget(args.event.target, exports.menuClass.edit) ||
                            this.ensureTarget(args.event.target, exports.menuClass.batchEdit))) {
                            if (key !== 'Save' && key !== 'Cancel') {
                                this.hiddenItems.push(item.text);
                            }
                        }
                        else if (this.parent.editSettings.mode === 'Batch' && ((ej2_base_1.closest(args.event.target, '.e-gridform')) ||
                            this.parent.editModule.getBatchChanges()[changedRecords].length ||
                            this.parent.editModule.getBatchChanges()[addedRecords].length ||
                            this.parent.editModule.getBatchChanges()[deletedRecords].length) && (key === 'Save' || key === 'Cancel')) {
                            continue;
                        }
                        else if (ej2_base_1.isNullOrUndefined(args.parentItem) && args.event
                            && !this.ensureTarget(args.event.target, dItem.target)) {
                            this.hiddenItems.push(item.text);
                        }
                    }
                    else Eif (item.target && args.event &&
                        !this.ensureTarget(args.event.target, item.target)) {
                        this.hiddenItems.push(item.text);
                    }
                }
                this.contextMenu.enableItems(this.disableItems, false);
                this.contextMenu.hideItems(this.hiddenItems);
                this.eventArgs = args.event;
                args.column = this.targetColumn;
                this.parent.trigger(events.contextMenuOpen, args);
                if (this.hiddenItems.length === args.items.length) {
                    this.updateItemStatus();
                    args.cancel = true;
                }
            }
        };
        ContextMenu.prototype.ensureTarget = function (targetElement, selector) {
            var target = targetElement;
            Iif (this.ensureFrozenHeader(targetElement) && (selector === exports.menuClass.header || selector === exports.menuClass.content)) {
                target = ej2_base_1.closest(targetElement, selector === exports.menuClass.header ? 'thead' : 'tbody');
            }
            else if (selector === exports.menuClass.content || selector === exports.menuClass.header) {
                target = util_1.parentsUntil(ej2_base_1.closest(targetElement, '.e-table'), selector.substr(1, selector.length));
            }
            else {
                target = ej2_base_1.closest(targetElement, selector);
            }
            return target && util_1.parentsUntil(target, 'e-grid') === this.parent.element;
        };
        ContextMenu.prototype.ensureFrozenHeader = function (targetElement) {
            return (this.parent.getFrozenColumns() || this.parent.frozenRows)
                && ej2_base_1.closest(targetElement, exports.menuClass.header) ? true : false;
        };
        ContextMenu.prototype.ensureDisabledStatus = function (item) {
            var _this = this;
            var status = false;
            switch (item) {
                case 'AutoFitAll':
                case 'AutoFit':
                    status = !(this.parent.ensureModuleInjected(resize_1.Resize) && !this.parent.isEdit);
                    break;
                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 'Ungroup':
                    if (!this.parent.allowGrouping || !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 'Edit':
                case 'Delete':
                case 'Save':
                case 'Cancel':
                    if (!this.parent.editModule || (this.parent.getDataRows().length === 0)) {
                        status = true;
                    }
                    break;
                case 'Copy':
                    if (this.parent.getSelectedRowIndexes().length === 0 ||
                        this.parent.getCurrentViewRecords().length === 0) {
                        status = true;
                    }
                    break;
                case 'export':
                    if ((!this.parent.allowExcelExport || !this.parent.excelExport) ||
                        !this.parent.ensureModuleInjected(pdf_export_1.PdfExport) && !this.parent.ensureModuleInjected(excel_export_1.ExcelExport)) {
                        status = true;
                    }
                    break;
                case 'PdfExport':
                    if (!(this.parent.allowPdfExport) || !this.parent.ensureModuleInjected(pdf_export_1.PdfExport)) {
                        status = true;
                    }
                    break;
                case 'ExcelExport':
                case 'CsvExport':
                    if (!(this.parent.allowExcelExport) || !this.parent.ensureModuleInjected(excel_export_1.ExcelExport)) {
                        status = true;
                    }
                    break;
                case 'SortAscending':
                case 'SortDescending':
                    if ((!this.parent.allowSorting) || !this.parent.ensureModuleInjected(sort_1.Sort)) {
                        status = true;
                    }
                    else if (this.parent.ensureModuleInjected(sort_1.Sort) && this.parent.sortSettings.columns.length > 0 && this.targetColumn) {
                        this.parent.sortSettings.columns.forEach(function (element) {
                            if (element.field === _this.targetColumn.field
                                && element.direction.toLowerCase() === item.toLowerCase().replace('sort', '').toLocaleLowerCase()) {
                                status = true;
                            }
                        });
                    }
                    break;
                case 'FirstPage':
                case 'PrevPage':
                    if (!this.parent.allowPaging || !this.parent.ensureModuleInjected(page_1.Page) ||
                        this.parent.getCurrentViewRecords().length === 0 ||
                        (this.parent.ensureModuleInjected(page_1.Page) && this.parent.pageSettings.currentPage === 1)) {
                        status = true;
                    }
                    break;
                case 'LastPage':
                case 'NextPage':
                    if (!this.parent.allowPaging || !this.parent.ensureModuleInjected(page_1.Page) ||
                        this.parent.getCurrentViewRecords().length === 0 ||
                        (this.parent.ensureModuleInjected(page_1.Page) && this.parent.pageSettings.currentPage === this.getLastPage())) {
                        status = true;
                    }
                    break;
            }
            return status;
        };
        ContextMenu.prototype.getContextMenu = function () {
            return this.element;
        };
        ContextMenu.prototype.destroy = function () {
            this.contextMenu.destroy();
            ej2_base_2.remove(this.element);
            this.removeEventListener();
            this.parent.element.classList.remove('e-noselect');
        };
        ContextMenu.prototype.getModuleName = function () {
            return 'contextMenu';
        };
        ContextMenu.prototype.generateID = function (item) {
            return this.gridID + '_cmenu_' + item;
        };
        ContextMenu.prototype.getKeyFromId = function (id) {
            return id.replace(this.gridID + '_cmenu_', '');
        };
        ContextMenu.prototype.buildDefaultItems = function (item) {
            var menuItem;
            switch (item) {
                case 'AutoFitAll':
                case 'AutoFit':
                    menuItem = { target: exports.menuClass.header };
                    break;
                case 'Group':
                    menuItem = { target: exports.menuClass.header, iconCss: exports.menuClass.group };
                    break;
                case 'Ungroup':
                    menuItem = { target: exports.menuClass.header, iconCss: exports.menuClass.ungroup };
                    break;
                case 'Edit':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.editIcon };
                    break;
                case 'Delete':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.delete };
                    break;
                case 'Save':
                    menuItem = { target: exports.menuClass.edit, iconCss: exports.menuClass.save };
                    break;
                case 'Cancel':
                    menuItem = { target: exports.menuClass.edit, iconCss: exports.menuClass.cancel };
                    break;
                case 'Copy':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.copy };
                    break;
                case 'export':
                    menuItem = { target: exports.menuClass.content };
                    break;
                case 'PdfExport':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.pdf };
                    break;
                case 'ExcelExport':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.excel };
                    break;
                case 'CsvExport':
                    menuItem = { target: exports.menuClass.content, iconCss: exports.menuClass.csv };
                    break;
                case 'SortAscending':
                    menuItem = { target: exports.menuClass.header, iconCss: exports.menuClass.ascending };
                    break;
                case 'SortDescending':
                    menuItem = { target: exports.menuClass.header, iconCss: exports.menuClass.descending };
                    break;
                case 'FirstPage':
                    menuItem = { target: exports.menuClass.pager, iconCss: exports.menuClass.fPage };
                    break;
                case 'PrevPage':
                    menuItem = { target: exports.menuClass.pager, iconCss: exports.menuClass.pPage };
                    break;
                case 'LastPage':
                    menuItem = { target: exports.menuClass.pager, iconCss: exports.menuClass.lPage };
                    break;
                case 'NextPage':
                    menuItem = { target: exports.menuClass.pager, iconCss: exports.menuClass.nPage };
                    break;
            }
            this.defaultItems[item] = {
                text: this.getLocaleText(item), id: this.generateID(item),
                target: menuItem.target, iconCss: menuItem.iconCss ? 'e-icons ' + menuItem.iconCss : ''
            };
            return this.defaultItems[item];
        };
        ContextMenu.prototype.getDefaultItems = function () {
            return ['AutoFitAll', 'AutoFit',
                'Group', 'Ungroup', 'Edit', 'Delete', 'Save', 'Cancel', 'Copy', 'export',
                'PdfExport', 'ExcelExport', 'CsvExport', 'SortAscending', 'SortDescending',
                'FirstPage', 'PrevPage', 'LastPage', 'NextPage'];
        };
        ContextMenu.prototype.setLocaleKey = function () {
            return {
                'AutoFitAll': 'autoFitAll',
                'AutoFit': 'autoFit',
                'Copy': 'Copy',
                'Group': 'Group',
                'Ungroup': 'Ungroup',
                'Edit': 'EditRecord',
                'Delete': 'DeleteRecord',
                'Save': 'Save',
                'Cancel': 'CancelButton',
                'PdfExport': 'Pdfexport',
                'ExcelExport': 'Excelexport',
                'CsvExport': 'Csvexport',
                'export': 'Export',
                'SortAscending': 'SortAscending',
                'SortDescending': 'SortDescending',
                'FirstPage': 'FirstPage',
                'LastPage': 'LastPage',
                'PrevPage': 'PreviousPage',
                'NextPage': 'NextPage'
            };
        };
        ContextMenu.prototype.getColumn = function (e) {
            var cell = ej2_base_1.closest(e.target, 'th.e-headercell');
            if (cell) {
                var uid = cell.querySelector('.e-headercelldiv').getAttribute('e-mappinguid');
                return this.parent.getColumnByUid(uid);
            }
            return null;
        };
        ContextMenu.prototype.selectRow = function (e, isSelectable) {
            this.cell = e.target;
            this.row = ej2_base_1.closest(e.target, 'tr.e-row') || this.row;
            if (this.row && isSelectable && !util_1.parentsUntil(e.target, 'e-gridpager')) {
                this.parent.selectRow(parseInt(this.row.getAttribute('aria-rowindex'), 10));
            }
        };
        return ContextMenu;
    }());
    exports.ContextMenu = ContextMenu;
});