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

30.19% Statements 16/53
16.67% Branches 3/18
20% Functions 2/10
30.19% Lines 16/53
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                                                                                                                                                                                                                                                                           
/**
 * 第一行数据上方表头下方的提示框
 *
 * @file 第一行数据上方表头下方的提示框
 * @author Brian Li(lbxxlht@163.com)
 */
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');
    var CLOSE_BUTTON_CMD = 'close-information-bar';
 
    // public /////////////////////////////////////////////////////////////////
 
    /**
     * 构造函数
     *
     * @constructor
     * @param {Object} param 初始化对象
     * @param {string} param.content 显示的内容,支持html
     * @param {boolean} param.close 初始是否处于关闭状态
     */
    function TableInformationBar(param) {
        if (!(this instanceof TableInformationBar)) {
            return new TableInformationBar(param);
        }
        param = param || {};
        this.content = param.content || '';
        this.isClose = param.close;
        this.required = ['TableHeader'];
        Plugin.call(this, param);
    }
 
    util.inherit(TableInformationBar, Plugin);
 
    /**
     * @override
     *
     * @param {Object} param 宿主UI初始化时的参数引用,***操作请小心***
     * @return {boolean} 是否初始化成功
     */
    TableInformationBar.prototype.load = function (param) {
        param.pluginSkin = util.joinClassName(param.pluginSkin, 'table-information-bar');
        param.tpl = extendTPL(param.tpl, this.content, this.isClose);
        util.appendHandler(param, 'click', mouseHandler);
        return true;
    };
 
    /**
     * 修改提示的内容
     *
     * @param {string} content 要重新写的内容
     */
    TableInformationBar.prototype.setContent = function (content) {
        // 获取信息栏模板
        var bar = produceTPL(content, this.isClose);
        // 修改宿主模板
        var picker = new util.Picker(this.host.tpl);
        picker.find('.information-bar').remove();
        picker.find('.table-head').after(bar);
        this.host.tpl = picker.export();
        // 动态修改页面
        content += '<div class="iconfont icon-guanbi" data-ui-cmd="' + CLOSE_BUTTON_CMD + '"></div>';
        $(this.host.container).find('.information-bar td')
            .css('opacity', 0.5).html(content).animate({opacity: 1});
    };
 
    /**
     * 弹出信息栏
     *
     * @param {?string} content 如果指定同时更新内容
     */
    TableInformationBar.prototype.pop = function (content) {
        this.isClose = false;
        if (typeof content === 'string') {
            var picker = new util.Picker(this.host.tpl);
            var bar = produceTPL(content, false);
            picker.find('.information-bar').remove();
            picker.find('.table-head').after(bar);
            this.host.tpl = picker.export();
            content += '<div class="iconfont icon-guanbi" data-ui-cmd="' + CLOSE_BUTTON_CMD + '"></div>';
            $(this.host.container).find('.information-bar').removeClass('disable').find('td')
                .css('opacity', 0.5).html(content).animate({opacity: 1});
        }
        else {
            this.host.tpl = new util.Picker(this.host.tpl).find('.information-bar').removeClass('disable').export();
            $(this.host.container).find('.information-bar').removeClass('disable');
        }
    };
 
    /**
     * 关闭提示框
     */
    TableInformationBar.prototype.close = function () {
        this.isClose = true;
        this.host.tpl = new util.Picker(this.host.tpl).find('.information-bar').addClass('disable').export();
        $(this.host.container).find('.information-bar').addClass('disable');
    };
 
    // private /////////////////////////////////////////////////////////////////
 
    /**
     * 生成模板
     *
     * @param {string} content 信息栏的内容
     * @param {boolean} close 是否是关闭的
     * @return {Array.<string>} 信息框模板
     */
    function produceTPL(content, close) {
        return [
            '<tr class="information-bar' + (close ? ' disable' : '') + '">',
            '<td colspan="999">',
            content,
            '<div class="iconfont icon-guanbi" data-ui-cmd="' + CLOSE_BUTTON_CMD + '"></div>',
            '</td>',
            '</tr>'
        ];
    }
 
    /**
     * 对模版进行扩充
     *
     * @param {Array.<string>} tpl 原始模板
     * @param {string} content 信息栏的内容
     * @param {boolean} close 是否是关闭的
     * @return {Array.<string>} 扩展后的模板
     */
    function extendTPL(tpl, content, close) {
        return new util.Picker(tpl).find('.table-head').after(produceTPL(content, close)).export();
    }
 
    /**
     * 处理一切鼠标事件
     *
     * @param {Event} evt 鼠标事件句柄
     */
    function mouseHandler(evt) {
        var dataset = util.getDataset(evt.target);
        if (dataset.uiCmd === CLOSE_BUTTON_CMD) {
            this.plugin.TableInformationBar.close();
        }
    }
 
    return TableInformationBar;
});