@syncfusion/ej2-grids
Version: 
Feature-rich JavaScript datagrid (datatable) control with built-in support for editing, filtering, grouping, paging, sorting, and exporting to Excel.
154 lines (153 loc) • 6.47 kB
JavaScript
import { closest, isNullOrUndefined } from '@syncfusion/ej2-base';
import { click, keyPressed, commandClick, initialEnd, destroy } from '../base/constant';
import { CellType } from '../base/enum';
import { CommandColumnRenderer } from '../renderer/command-column-renderer';
import { getUid } from '../base/util';
import * as literals from '../base/string-literals';
/**
 * `CommandColumn` used to handle the command column actions.
 *
 * @hidden
 */
var CommandColumn = /** @class */ (function () {
    function CommandColumn(parent, locator) {
        this.parent = parent;
        this.locator = locator;
        this.initiateRender();
        this.addEventListener();
    }
    CommandColumn.prototype.initiateRender = function () {
        var cellFac = this.locator.getService('cellRendererFactory');
        cellFac.addCellRenderer(CellType.CommandColumn, new CommandColumnRenderer(this.parent, this.locator));
    };
    CommandColumn.prototype.commandClickHandler = function (e) {
        var gObj = this.parent;
        var target = closest(e.target, 'button');
        if (!target || !closest(e.target, '.e-unboundcell')) {
            return;
        }
        var buttonObj = target.ej2_instances[0];
        var type = buttonObj.commandType;
        var uid = target.getAttribute('data-uid');
        var commandColumn;
        var row = gObj.getRowObjectFromUID(closest(target, '.' + literals.row).getAttribute('data-uid'));
        var cols = this.parent.columnModel;
        for (var i = 0; i < cols.length; i++) {
            if (cols[parseInt(i.toString(), 10)].commands) {
                var commandCols = cols[parseInt(i.toString(), 10)].commands;
                for (var j = 0; j < commandCols.length; j++) {
                    var idInString = 'uid';
                    var typeInString = 'type';
                    if (commandCols[parseInt(j.toString(), 10)]["" + idInString] === uid && commandCols[parseInt(j.toString(), 10)]["" + typeInString] === type) {
                        commandColumn = commandCols[parseInt(j.toString(), 10)];
                    }
                    else {
                        var buttons = [].slice.call(closest(target, '.e-unboundcell').querySelectorAll('button'));
                        var index = buttons.findIndex(function (ele) { return ele === target; });
                        if (index < commandCols.length && commandCols[parseInt(index.toString(), 10)]["" + typeInString] === type &&
                            String(commandCols[parseInt(j.toString(), 10)]["" + idInString]) === uid) {
                            commandColumn = commandCols[parseInt(index.toString(), 10)];
                        }
                    }
                }
            }
        }
        var args = {
            cancel: false,
            target: target,
            commandColumn: commandColumn,
            rowData: isNullOrUndefined(row) ? undefined : row.data
        };
        this.parent.trigger(commandClick, args, function (commandclickargs) {
            if (buttonObj.disabled || !gObj.editModule || commandclickargs.cancel) {
                return;
            }
            switch (type) {
                case 'Edit':
                    gObj.editModule.endEdit();
                    gObj.editModule.startEdit(closest(target, 'tr'));
                    break;
                case 'Cancel':
                    gObj.isFocusFirstCell = true;
                    gObj.editModule.closeEdit();
                    break;
                case 'Save':
                    gObj.isFocusFirstCell = true;
                    gObj.editModule.endEdit();
                    break;
                case 'Delete':
                    if (gObj.editSettings.mode !== 'Batch') {
                        gObj.editModule.endEdit();
                    }
                    gObj.commandDelIndex = parseInt(closest(target, 'tr').getAttribute(literals.ariaRowIndex), 10) - 1;
                    gObj.clearSelection();
                    //for toogle issue when dbl click
                    gObj.selectRow(gObj.commandDelIndex, false);
                    gObj.isFocusFirstCell = true;
                    gObj.editModule.deleteRecord();
                    if (!(gObj.editSettings.showDeleteConfirmDialog && !gObj.allowSelection)) {
                        gObj.commandDelIndex = undefined;
                    }
                    break;
            }
        });
    };
    /**
     * For internal use only - Get the module name.
     *
     * @returns {string} returns the module name
     */
    CommandColumn.prototype.getModuleName = function () {
        return 'commandColumn';
    };
    /**
     * To destroy CommandColumn.
     *
     * @function destroy
     * @returns {void}
     */
    CommandColumn.prototype.destroy = function () {
        if (this.parent.isDestroyed) {
            return;
        }
        this.removeEventListener();
    };
    CommandColumn.prototype.removeEventListener = function () {
        if (this.parent.isDestroyed) {
            return;
        }
        this.parent.off(click, this.commandClickHandler);
        this.parent.off(keyPressed, this.keyPressHandler);
        this.parent.off(initialEnd, this.load);
        this.parent.off(destroy, this.destroy);
    };
    CommandColumn.prototype.addEventListener = function () {
        if (this.parent.isDestroyed) {
            return;
        }
        this.parent.on(click, this.commandClickHandler, this);
        this.parent.on(keyPressed, this.keyPressHandler, this);
        this.parent.on(initialEnd, this.load, this);
        this.parent.on(destroy, this.destroy, this);
    };
    CommandColumn.prototype.keyPressHandler = function (e) {
        if ((e.action === 'enter' || e.action === 'space') && closest(e.target, '.e-unboundcelldiv')) {
            this.commandClickHandler(e);
            e.preventDefault();
        }
    };
    CommandColumn.prototype.load = function () {
        var uid = 'uid';
        var col = this.parent.columnModel;
        for (var i = 0; i < col.length; i++) {
            if (col[parseInt(i.toString(), 10)].commands) {
                var commandCol = col[parseInt(i.toString(), 10)].commands;
                for (var j = 0; j < commandCol.length; j++) {
                    commandCol[parseInt(j.toString(), 10)]["" + uid] = getUid('gridcommand');
                }
            }
        }
    };
    return CommandColumn;
}());
export { CommandColumn };