Wijmo覆盖方法

时间:2013-01-25 01:37:26

标签: javascript jquery oop override wijmo

这是 jquery.wijmo.wijgrid.js

的一部分
(function ($) {
    "use strict";
    $.extend($.wijmo.wijgrid, {
        cellEditorHelper: function () {
            this.currentCellEditStart = function (grid, e) {
                var result = false,
                    currentCell = grid.currentCell(),
                    view = grid._view(),
                    rowObj, args, $innerDiv, rowType;

                if (currentCell._isValid() && !currentCell._isEdit() && (currentCell.column().dataIndex >= 0)) {
                    rowObj = currentCell._row();

                    if (rowObj && rowObj.length) {
                        rowType = view._getRowInfo(rowObj).type;

                        if (rowType & $.wijmo.wijgrid.rowType.data) {

                            args = {
                                cell: currentCell,
                                event: e,
                                handled: false
                            };

                            if (result = grid._trigger("beforeCellEdit", null, args)) { // todo
                                if (!args.handled) {
                                    result = defaultBeforeCellEdit(grid, args);
                                }
                            }

                            if (result) {
                                currentCell._isEdit(true);

                                if (grid.options.showRowHeader) {
                                    $innerDiv = $(rowObj[0].cells[0]).children("div.wijmo-wijgrid-innercell");
                                    if ($innerDiv.length) {
                                        $innerDiv.empty();
                                        $innerDiv.append($("<div>&nbsp;</div>").addClass("ui-icon ui-icon-pencil"));
                                    }
                                }
                            }
                        }
                    }
                }

                return result;
            };

            this.currentCellEditEnd = function (grid, e) {
                var currentCell = grid.currentCell(),
                    result = false,
                    view = grid._view(),
                    rowObj, rowType, escPressed, args, a, b,
                    domCell;

                if (!currentCell._isValid() || !currentCell._isEdit()) {
                    return;
                }

                rowObj = currentCell._row();
                if (rowObj && rowObj.length) {
                    rowType = view._getRowInfo(rowObj).type;

                    if (!(rowType & $.wijmo.wijgrid.rowType.data)) {
                        return result;
                    }

                    escPressed = (e && e.which === $.ui.keyCode.ESCAPE);

                    if (!e || (!escPressed)) {
                        args = {
                            cell: currentCell,
                            value: undefined
                        };

                        if (result = grid._trigger("beforeCellUpdate", null, args)) {
                            if (args.value === undefined) {
                                args.value = getCellValue(grid, currentCell); // get raw value from editor using the default implementation.
                            }

                            a = args.value; // new value
                            try {
                                args.value = grid._parse(currentCell.column(), args.value); // try to parse raw value
                                a = args.value;
                            } catch (ex) {
                                args.value = a; // restore raw value
                            }

                            b = currentCell.value(); // old value

                            if (args.cell.column().dataType === "datetime") {
                                if (a instanceof Date) {
                                    a = a.getTime();
                                }

                                if (b instanceof Date) {
                                    b = b.getTime();
                                }
                            }

                            if (a !== b) { // value is changed
                                // update datasource
                                try {
                                    currentCell.value(args.value);
                                } catch (ex) {
                                    result = false;
                                    grid._trigger("invalidCellValue", null, { cell: currentCell, value: args.value });
                                }

                                if (result) {
                                    grid._trigger("afterCellUpdate", null, { cell: currentCell });
                                }
                            }
                        }
                    } else {
                        // ESC key
                        result = true;
                    }

                    if (result) {
                        args = {
                            cell: currentCell,
                            event: e,
                            handled: false
                        };

                        grid._trigger("afterCellEdit", null, args);

                        $(grid._view().focusableElement()).focus(); // move focus from editor to wijgrid before editor element will be deleted.

                        if (!args.handled) {
                            result = defaultAfterCellEdit(grid, args);
                        }

                        if (result) {
                            currentCell._isEdit(false);
                        }

                        if (grid.options.showRowHeader) {
                            $(rowObj[0].cells[0]).children("div.wijmo-wijgrid-innercell").html("&nbsp;"); // remove ui-icon-pencil
                        }

                        window.setTimeout(function () {
                            if (!grid.destroyed) {
                                //grid.element.focus();
                                currentCell = grid.currentCell();

                                if (args.cell.isEqual(currentCell) && (domCell = currentCell.tableCell())) { // same cell?
                                    $(domCell).focus(); // restore focus
                                }
                            }
                        }, 50);
                    }
                }

                return result;
            };

            // private

            function defaultBeforeCellEdit(grid, args) {
                var leafOpt = args.cell.column(),
                    result = false,
                    value, $container, $input, len, kbEvent;

                if (leafOpt.dataIndex >= 0) {
                    value = args.cell.value();
                    result = true;

                    try {
                        $container = args.cell.container();

                        if (leafOpt.dataType === "boolean") {
                            $input = $container.children("input");
                            $input.focus();
                            if (args.event && args.event.type === "keypress") {
                                $input.one("keyup", function (e) {
                                    if (e.which === $.ui.keyCode.SPACE) {
                                        e.preventDefault();
                                        $input[0].checked = !value;
                                    }
                                });
                            }
                        } else {
                            $input = $("<input />")
                                .attr("type", "text")
                                .addClass("wijgridinput wijmo-wijinput ui-state-focus")
                                .bind("keydown", grid, checkBoxOrInputKeyDown);

                            //the problem of inputing
                            $input.bind(($.support.selectstart ? "selectstart" : "mousedown"), function (event) {
                                event.stopPropagation();
                            });

                            if (args.event && args.event.type === "keypress" && args.event.which) {
                                $input.val(String.fromCharCode(args.event.which));
                            } else {
                                switch (args.cell.column().dataType) {
                                    case "currency":
                                    case "number":
                                        if (value !== null) {
                                            $input.val(value); // ignore formatting
                                            break;
                                        }
                                        // fall through
                                    default:
                                        $input.val(grid._toStr(args.cell.column(), value));
                                        break;
                                }
                            }

                            $container
                                .empty()
                                .append($input);

                            // move caret to the end of the text
                            len = $input.val().length;
                            new $.wijmo.wijgrid.domSelection($input[0]).setSelection({ start: len, end: len });

                            $input.focus();

                            if ($.browser.msie) {
                                setTimeout(function () {
                                    $input.focus();
                                }, 0);
                            }

                            // FF issue: text does not track to the new position of the caret
                            if ($.browser.mozilla && document.createEvent && $input[0].dispatchEvent) {
                                kbEvent = document.createEvent("KeyboardEvent");
                                kbEvent.initKeyEvent("keypress", false, true, null, false, false, false, false, 0, $.ui.keyCode.SPACE);
                                $input[0].dispatchEvent(kbEvent);
                                kbEvent = document.createEvent("KeyboardEvent");
                                kbEvent.initKeyEvent("keypress", false, true, null, false, false, false, false, $.ui.keyCode.BACKSPACE, 0);
                                $input[0].dispatchEvent(kbEvent);
                            }
                        }
                    }
                    catch (ex) {
                        alert(ex.message);
                        result = false;
                    }
                }

                return result;
            }

            function defaultAfterCellEdit(grid, args) {
                var leafOpt = args.cell.column(),
                    result = false,
                    $container, cellValue, input,
                    rowInfo, view;

                if (leafOpt.dataIndex >= 0) {
                    result = true;
                    view = grid._view();

                    try {
                        $container = args.cell.container();
                        cellValue = grid._toStr(leafOpt, args.cell.value());

                        rowInfo = view._getRowInfo(grid._rows().item(args.cell.rowIndex()));

                        if (leafOpt.dataType === "boolean") {
                            input = $container.children("input");

                            if (cellValue === "true") {
                                input.attr("checked", "checked");
                            }
                            else {
                                input.removeAttr("checked");
                            }
                        }
                        else {
                            grid.cellFormatter.format($container, leafOpt, cellValue, rowInfo);
                        }
                    }
                    catch (ex) {
                        console.warn("defaultAfterCellEdit: " + ex.message);
                        result = false;
                    }
                }

                return result;
            }

            function checkBoxOrInputKeyDown(args) {
                if (args.which === $.ui.keyCode.ENTER) { // stop editing when Enter key is pressed
                    var grid = args.data;

                    if (grid) {
                        grid._endEditInternal(args);
                        return false; // prevent submit behaviour.
                    }
                }
            }

            function getCellValue(gridView, currentCell) {
                var $input = currentCell.container().find(":input:first"),
                    result = null;

                if ($input.length) {
                    result = ($input.attr("type") === "checkbox")
                        ? $input[0].checked
                        : $input.val();
                }

                return result;
            }

            // private *
        }
    });
})(jQuery);

如何覆盖此私有函数function defaultAfterCellEdit(grid, args)?那是因为它嵌套在cellEditorHelper.currentCellEditStart

如何在不复制几乎整个原始文件的情况下覆盖该功能?

0 个答案:

没有答案