utils_grid_setup-better-input-grid.js

/**
 * 인수로 지정한 그리드에 입력 편의 기능을 추가합니다.
 *
 * - Enter: 다음 행으로 이동합니다.
 * - Space: 현재 셀이 콤보 혹은 캘린더일 경우 이를 엽니다.
 * - Left: 현재 셀의 변경사항을 데이터셋에 반영 후 쓰기 가능한 이전 셀로 포커스를 이동합니다.
 * - Right: 현재 셀의 변경사항을 데이터셋에 반영 후 쓰기 가능한 다음 셀로 포커스를 이동합니다.
 *
 * @function setupBetterInputGrid
 * @param {nexacro.Grid} grid  대상 그리드
 * @memberof $f
 * @example
 * $f.setupBetterInputGrid(this.grd_main);
 *
 */
export function setupBetterInputGrid(grid) {
    grid.addEventHandler("onkeydown", (obj, e) => {
        let editType;
        const ds = obj.getBindDataset();
        const curRow = ds.rowposition;

        switch (e.keycode) {
            case 13: // Enter
                const nextRow =
                    curRow === ds.getRowCount() - 1 ? curRow : curRow + 1;

                if (nextRow !== curRow) {
                    ds.rowposition = nextRow;
                }
                break;
            case 32: // Space
                editType = obj.getCellPropertyValue(
                    curRow,
                    obj.getCellPos(),
                    "edittype",
                );

                if (editType === "combo") {
                    obj.dropdownCombo();
                } else if (editType === "date") {
                    obj.dropdownCalendar();
                }
                break;
            case 37: // Left
                e.preventDefault();
                obj.updateToDataset();

                for (var i = obj.getCellPos() - 1; i >= 0; i--) {
                    var cellType = obj.getCellPropertyValue(
                        curRow,
                        i,
                        "edittype",
                    );

                    if (cellType !== "none") {
                        obj.setCellPos(i);
                        break;
                    }
                }
                break;
            case 38: // Up
                break;
            case 39: // Right
                e.preventDefault();
                obj.updateToDataset();

                for (
                    let i = obj.getCellPos() + 1;
                    i < obj.getCellCount("body");
                    i++
                ) {
                    const cellType = obj.getCellPropertyValue(
                        curRow,
                        i,
                        "edittype",
                    );

                    if (cellType !== "none") {
                        obj.setCellPos(i);
                        break;
                    }
                }

                break;
            case 40: // Bottom
                break;
        }
    });
}