all files / dui/src/plugin/ TableCellEditor.js

17.82% Statements 18/101
5.66% Branches 3/53
15.38% Functions 2/13
17.82% Lines 18/101
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * 表格单元格编辑器
 *
 * @file 表格单元格编辑器
 * @author Brian Li(lbxxlht@163.com)
 *
 * 数据结构
 * <Editor>
 *  {
 *      index: <number>, 列索引号
 *      value: <Function>, 初始值获取函数,有些列渲染时可能含有html,用这个函数从item中直接获取值
 *      check: <Array.<Function>>, 校验方法队列,
 *      onPass: <Function> 通过校验后的回调,形参:newValue(单元格修改后的值), item(行数据源), rowIndex(行号)
 *  }
 */
var define = typeof define === 'function' && define.amd ? define : function (factory) {
    typeof module === 'object' ? (module.exports = factory(require)) : '';
};
 
define(function (require) {
 
    var util = require('../core/util');
    var Plugin = require('../core/plugin');
 
    // public /////////////////////////////////////////////////////////////////
 
    /**
     * 构造函数
     *
     * @constructor
     * @param {Object} param 初始化对象
     * @param {Array.<Editor>} param.editors 行编辑配置
     * @param {string} param.enterButtonLabel 确定按钮标签
     * @param {string} param.cancelButtonLabel 取消按钮标签
     */
    function TableCellEditor(param) {
        if (!(this instanceof TableCellEditor)) {
            return new TableCellEditor(param);
        }
        param = param || {};
        this.editors = param.editors instanceof Array ? param.editors : [];
        this.enterButtonLabel = param.enterButtonLabel || 'enter';
        this.cancelButtonLabel = param.cancelButtonLabel || 'cancel';
        this.processor = {};
        this.activeCell = null;
        for (var i = 0; i < this.editors.length; i++) {
            this.processor[this.editors[i].index] = this.editors[i];
        }
        Plugin.call(this, param);
    }
 
    util.inherit(TableCellEditor, Plugin);
 
    /**
     * @override
     *
     * @param {Object} param 宿主UI初始化时的参数引用,***操作请小心***
     * @return {boolean} 是否初始化成功
     */
    TableCellEditor.prototype.load = function (param) {
        param.pluginSkin = util.joinClassName(param.pluginSkin, 'table-cell-editor');
        param.tpl = extendTPL(param.tpl, this.editors);
        util.appendHandler(param, 'click', mouseHandler);
        util.appendHandler(param, 'input', changeHandler);
        util.appendHandler(param, 'keyup', keyHandler);
        return true;
    };
 
    /**
     * 开启某单元格的编辑
     *
     * @param {number} rowIndex 行索引
     * @param {number} columnIndex 列索引
     */
    TableCellEditor.prototype.setEditor = function (rowIndex, columnIndex) {
        if (!this.processor.hasOwnProperty(columnIndex)) {
            return;
        }
        this.removeEditor();
        var processor = this.processor[columnIndex];
        var item = this.host.datasource[rowIndex];
        var data = typeof processor.value === 'function'
            ? processor.value(item) : this.host._temp.renderData[rowIndex][columnIndex];
        var html = [
            '<div class="editor-box">',
            '<input data-ui-cmd="cellinput" type="text" value="' + data + '"/>',
            '<div class="result-alert"></div>',
            '<div data-ui-cmd="cellenter" class="button important-button">' + this.enterButtonLabel + '</div>',
            '<div data-ui-cmd="cellcancel" class="button default-button">' + this.cancelButtonLabel + '</div>',
            '</div>'
        ];
        getCell(this.host.container, rowIndex, columnIndex).find('.cell-editor-border').prepend(html.join(''));
        this.activeCell = rowIndex + ';' + columnIndex;
    };
    
    /**
     * 移除editor
     */
    TableCellEditor.prototype.removeEditor = function () {
        this.activeCell = null;
        $(this.host.container).find('.editor-box').remove();
    };
 
    // private /////////////////////////////////////////////////////////////////
 
    /**
     * 对模版进行扩充
     *
     * @param {Array.<string>} tpl 原始模板
     * @param {Array.<Editor>} editors 编辑器配置
     * @return {Array.<string>} 扩展后的模板
     */
    function extendTPL(tpl, editors) {
        var picker = new util.Picker(tpl);
        for (var i = 0; i < editors.length; i++) {
            picker.find('.tr [data-ui-column="' + editors[i].index + '"]').head([
                '<div class="cell-editor-border">',
                '<div class="font-icon font-icon-edit" data-ui-cmd="celleditor"></div>'
            ]).foot('</div>');
        }
        return picker.export();
    }
 
    /**
     * 获取单元格操作jquery
     *
     * @param <HtmlElement> container 宿主容器
     * @param {number} rowIndex 行索引
     * @param {number} columnIndex 列索引
     * @param {Object} 单元格jquery对象
     */
    function getCell(container, rowIndex, columnIndex) {
        return $(container).find('[data-ui-row="' + rowIndex + '"] [data-ui-column="' + columnIndex + '"]');
    }
 
    /**
     * 对输入进行校验
     *
     * @param {TableCellEditor} me editor实例
     * @param {Object} processor 校验器
     * @param {string} value 等待校验的数据
     * @param {Object} item 处于编辑状态的行对应的数据源
     * @return {boolean} 是否通过了所有校验
     */
    function checking(me, processor, value, item) {
        var checkers = processor.check instanceof Array ? processor.check : [processor.check];
        var pass = true;
        for (var i = 0; i < checkers.length; i++) {
            if (typeof checkers[i] !== 'function') {
                continue;
            }
            var alertMsg = '';
            var result = checkers[i](value, item);
            if (typeof result === 'string') {
                alertMsg = result;
                pass = false;
            }
            $(me.host.container).find('.editor-box .result-alert').text(alertMsg);
            if (!pass) {
                break;
            }
        }
        return pass;
    }
 
    /**
     * 点击确定回传数据
     *
     * @param {Table} me table实例
     * @param {Array.<number>} axis 单元格坐标
     */
    function onEnter(me, axis) {
        var dataItem = me.datasource[axis[0]];
        var cellEditor = me.plugin.TableCellEditor;
        var processor = cellEditor.processor[axis[1]];
        var value = $(me.container).find('.editor-box input').val();
        if (
            !processor
            || typeof processor.onPass !== 'function'
            || !checking(cellEditor, processor, value, dataItem)
        ) {
            return;
        }
        cellEditor.removeEditor();
        processor.onPass.apply(me, [value, dataItem, axis[0]]);
    }
 
    /**
     * 处理一切鼠标事件
     *
     * @param {Event} evt 鼠标事件句柄
     */
    function mouseHandler(evt) {
        var dataset = util.getDataset(evt.target);
        var axis = this.getMouseAxis(evt.target);
        var cellEditor = this.plugin.TableCellEditor;
        if (dataset.uiCmd !== 'celleditor' && cellEditor.activeCell !== axis.join(';')){
            cellEditor.removeEditor();
            return;
        }
        if (axis[0] < 0 || axis[1] < 0) {
            return;
        }
        switch (dataset.uiCmd) {
            case 'celleditor':
                cellEditor.setEditor(axis[0], axis[1]);
                break;
            case 'cellcancel':
                cellEditor.removeEditor();
                break;
            case 'cellenter':
                onEnter(this, axis);
            default:
                break;
        }
    }
 
    /**
     * 处理输入框change事件
     */
    function changeHandler(evt) {
        var dataset = util.getDataset(evt.target);
        var axis = this.getMouseAxis(evt.target);
        var cellEditor = this.plugin.TableCellEditor;
        var processor = cellEditor.processor[axis[1]];
        if (!processor|| cellEditor.activeCell !== axis.join(';')) {
            return;
        }
        checking(cellEditor, processor, evt.target.value, this.datasource[axis[0]]);
    }
 
    /**
     * 输入框key事件
     */
    function keyHandler(evt) {
        var dataset = util.getDataset(evt.target);
        var axis = this.getMouseAxis(evt.target);
        var cellEditor = this.plugin.TableCellEditor;
        if (evt.keyCode !== 13 || cellEditor.activeCell !== axis.join(';')) {
            return;
        }
        onEnter(this, axis);
    }
 
    return TableCellEditor;
});