File size: 5,055 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * ESUI (Enterprise Simple UI)
 * Copyright 2010 Baidu Inc. All rights reserved.
 * 
 * path:    esui/BoxControl.js
 * desc:    选择框控件基类
 * author:  zhaolei, erik
 */

///import esui.InputControl;
///import esui.BoxGroup;
///import baidu.lang.inherits;
///import baidu.string.encodeHTML;
///import baidu.dom.insertAfter;
///import baidu.array.contains;

/**
 * 选择框控件基类
 * 
 * @description 不直接使用,供CheckBox和Radio继承
 * @param {Object} options 控件初始化参数
 */
esui.BoxControl = function ( options ) {
    // 标识鼠标事件触发自动状态转换
    this._autoState = 1;

    esui.InputControl.call( this, options );
};

esui.BoxControl.prototype = {
    onclick: new Function(),
    
    /**
     * 获取控件类型,checkbox|radio
     * 
     * @public
     * @return {string}
     */
    getType: function () {
        return this._type;
    },

    /**
     * 设置选中状态
     * 
     * @public
     * @param {boolean} checked 状态
     */
    setChecked: function ( checked ) {
        this.main.checked = !!checked;
    },
    
    /**
     * 获取选中状态
     * 
     * @public
     * @return {boolean}
     */
    isChecked: function() {
        return this.main.checked;
    },
    
    /**
     * 设置box为不可用状
     * 
     * @public
     */
    disable: function () {
        this.main.disabled = true;
        this.disabled = true;

        esui.InputControl.prototype.disable.call( this );
    },

    /**
     * 设置box为不可用状
     * 
     * @public
     */
    enable: function () {
        this.main.disabled = false;
        this.disabled = false;

        esui.InputControl.prototype.enable.call( this );
    },
    
    /**
     * 设置box的只读状态
     * 
     * @public
     */
    setReadOnly: function ( readOnly ) {
        this.main.disabled = readOnly;
        readOnly ? this.addState( 'readonly' ) : this.removeState( 'readonly' );
    },
    
    /**
     * 获取分组
     * 
     * @public
     * @return {esui.BoxGroup}
     */
    getGroup: function() {
        return new esui.BoxGroup( {
            name    : this.name, 
            type    : this._type,
            control : this
        } );
    },
    
    /**
     * 设置值
     * 
     * @public
     * @param {string} value
     */
    setValue: function( value ) {
        this.main.setAttribute( 'value', value );
    },
    
    /**
     * 获取值
     * 
     * @public
     * @return {string}
     */
    getValue: function() {
        return this.main.getAttribute( 'value' ) || 'on';
    },
    
    /**
     * 渲染控件
     *
     * @public
     */
    render: function () {
        var me   = this,
            main = me.main,
            data = me.datasource,
            title,
            label,
            value;
        
        esui.InputControl.prototype.render.call( me );
        
        // 初始化click事件
        if ( !me._mainClick ) {
            me._mainClick = me.__getClickHandler();
            main.onclick  = me._mainClick;
        }

        // 插入点击相关的label元素
        if ( !me._label ) {
            label = document.createElement( 'label' );
            label.className = me.__getClass( 'label' );
            baidu.setAttr( label, 'for', main.id );

            baidu.dom.insertAfter( label, main );
            me._label = label;
        } else {
            label = me._label;
        }

        // 初始化label的内容
        title = me.title || main.title || me.getValue();
        label.innerHTML = baidu.encodeHTML( title );
        
        // 初始化disabled
        me.setDisabled ( !!me.disabled );

        // 初始化value
        me.value && me.setValue( me.value );
        value = me.getValue();
        
        // 初始化checked
        switch ( typeof data ) {
        case 'string':
        case 'number':
            me.setChecked( data == value );
            break;

        default:
            if ( data instanceof Array ) {
                me.setChecked( baidu.array.contains( data, value ) );
            }
            break;
        }
    },
    
    /**
     * 获取click事件handler
     *
     * @protected
     */
    __getClickHandler: function() {
        var me = this;
        return function ( e ) {
            if ( !me.isDisabled() ) {
                me.onclick( e );
            }
        };
    },

    /**
     * 释放控件
     * 
     * @protected
     */
    __dispose: function () {
        this.onclick    = null;
        this._mainClick = null;
        this._label     = null;

        esui.InputControl.prototype.__dispose.call( this );
    },

    /**
     * 创建控件主元素
     *
     * @protected
     * @return {HTMLInputElement}
     */
    __createMain: function () {
        return esui.InputControl.prototype.__createInput.call( this, {
            tagName : 'input',
            name    : this.name,
            type    : this.type
        } );
    }
};

baidu.inherits( esui.BoxControl, esui.InputControl );