~lss-team/lilsoftstats/trunk

« back to all changes in this revision

Viewing changes to js/jquery/ui.checkbox.js

  • Committer: Nick
  • Date: 2011-11-14 04:10:28 UTC
  • Revision ID: nick@little-apps.org-20111114041028-cvmpwq6z6hx3pkya
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @author alexander.farkas
 
3
 * @version 1.4.3
 
4
 */
 
5
(function($){
 
6
        
 
7
        var supportsValidity;
 
8
        (function(){
 
9
                if(!$.prop || supportsValidity){return;}
 
10
                var supportTest = function(){
 
11
                        supportsValidity = !!$('<input />').prop('validity');
 
12
                };
 
13
                supportTest();
 
14
                $(supportTest);
 
15
        })();
 
16
        
 
17
    $.widget('ui.checkBox', {
 
18
                options: {
 
19
                hideInput: true,
 
20
                        addVisualElement: true,
 
21
                        addLabel: true
 
22
            },
 
23
        _create: function(){
 
24
            var that = this, 
 
25
                                opts = this.options
 
26
                        ;
 
27
                        
 
28
                        if(!this.element.is(':radio,:checkbox')){
 
29
                                if(this.element[0].elements && $.nodeName(this.element[0], 'form')){
 
30
                                        $(this.element[0].elements).filter(':radio,:checkbox').checkBox(opts);
 
31
                                }
 
32
                                return false;
 
33
                        }
 
34
                        
 
35
                        this._proxiedReflectUI = $.proxy(this, 'reflectUI');
 
36
                        
 
37
            this.labels = $([]);
 
38
                        
 
39
            this.checkedStatus = false;
 
40
                        this.disabledStatus = false;
 
41
                        this.hoverStatus = false;
 
42
            
 
43
                        this.inputType = this.element[0].type;
 
44
            this.radio = this.inputType == 'radio';
 
45
                                        
 
46
            this.visualElement = $([]);
 
47
            if (opts.hideInput) {
 
48
                                this.element.addClass('ui-helper-hidden-accessible');
 
49
                                if(opts.addVisualElement){
 
50
                                        this.visualElement = $('<span />')
 
51
                                                .addClass('ui-'+this.inputType)
 
52
                                        ;
 
53
                                        this.element.after(this.visualElement[0]);
 
54
                                }
 
55
            }
 
56
                        
 
57
                        if(opts.addLabel){
 
58
                                var id = this.element[0].id;
 
59
                                if(id){
 
60
                                        this.labels = $('label[for="' + id + '"]', this.element[0].form || this.element[0].ownerDocument).add(this.element.parent('label'));
 
61
                                }
 
62
                                if(!this.labels[0]){
 
63
                                        this.labels = this.element.closest('label', this.element[0].form);
 
64
                                }
 
65
                                this.labels.addClass(this.radio ? 'ui-radio' : 'ui-checkbox');
 
66
                        }
 
67
                        
 
68
                        this.visualGroup = this.visualElement.add(this.labels);
 
69
                        
 
70
                        this._addEvents();
 
71
                        
 
72
                        this.initialized = true;
 
73
            this.reflectUI({type: 'initialreflect'});
 
74
                        return undefined;
 
75
        },
 
76
                _addEvents: function(){
 
77
                        var that                = this, 
 
78
                        
 
79
                                opts            = this.options,
 
80
                                        
 
81
                                toggleHover = function(e){
 
82
                                        if(that.disabledStatus){
 
83
                                                return false;
 
84
                                        }
 
85
                                        that.hover = (e.type == 'focus' || e.type == 'mouseenter');
 
86
                                        if(e.type == 'focus'){
 
87
                                                that.visualGroup.addClass(that.inputType +'-focused');
 
88
                                        } else if(e.type == 'blur'){
 
89
                                                that.visualGroup.removeClass(that.inputType +'-focused');
 
90
                                        }
 
91
                                        that._changeStateClassChain();
 
92
                                        return undefined;
 
93
                                }
 
94
                        ;
 
95
                        
 
96
                        this.element
 
97
                                .bind('click.checkBox invalid.checkBox', this._proxiedReflectUI)
 
98
                                .bind('focus.checkBox blur.checkBox', toggleHover)
 
99
                        ;
 
100
                        if (opts.hideInput){
 
101
                                this.element
 
102
                                        .bind('usermode', function(e){
 
103
                            (e.enabled &&
 
104
                                that.destroy.call(that, true));
 
105
                        })
 
106
                                ;
 
107
            }
 
108
                        if(opts.addVisualElement){
 
109
                                this.visualElement
 
110
                                        .bind('click.checkBox', function(e){
 
111
                                                that.element[0].click();
 
112
                                                return false;
 
113
                                        })
 
114
                                ;
 
115
                        }
 
116
                        
 
117
                        this.visualGroup.bind('mouseenter.checkBox mouseleave.checkBox', toggleHover);
 
118
                        
 
119
                },
 
120
                _changeStateClassChain: function(){
 
121
                        var allElements = this.labels.add(this.visualElement),
 
122
                                stateClass      = '',
 
123
                                baseClass       = 'ui-'+ this.inputType
 
124
                        ;
 
125
                                
 
126
                        if(this.checkedStatus){
 
127
                                stateClass += '-checked'; 
 
128
                                allElements.addClass(baseClass+'-checked');
 
129
                        } else {
 
130
                                allElements.removeClass(baseClass+'-checked');
 
131
                        }
 
132
                        
 
133
                        if(this.disabledStatus){
 
134
                                stateClass += '-disabled'; 
 
135
                                allElements.addClass(baseClass+'-disabled');
 
136
                        } else {
 
137
                                allElements.removeClass(baseClass+'-disabled');
 
138
                        }
 
139
                        if(this.hover){
 
140
                                stateClass += '-hover'; 
 
141
                                allElements.addClass(baseClass+'-hover');
 
142
                        } else {
 
143
                                allElements.removeClass(baseClass+'-hover');
 
144
                        }
 
145
                        
 
146
                        baseClass += '-state';
 
147
                        if(stateClass){
 
148
                                stateClass = baseClass + stateClass;
 
149
                        }
 
150
                        
 
151
                        function switchStateClass(){
 
152
                                var classes = this.className.split(' '),
 
153
                                        found = false;
 
154
                                $.each(classes, function(i, classN){
 
155
                                        if(classN.indexOf(baseClass) === 0){
 
156
                                                found = true;
 
157
                                                classes[i] = stateClass;
 
158
                                                return false;
 
159
                                        }
 
160
                                        return undefined;
 
161
                                });
 
162
                                if(!found){
 
163
                                        classes.push(stateClass);
 
164
                                }
 
165
                                this.className = classes.join(' ');
 
166
                        }
 
167
                        
 
168
                        this.visualGroup.each(switchStateClass);
 
169
                },
 
170
        destroy: function(onlyCss){
 
171
            this.element.removeClass('ui-helper-hidden-accessible');
 
172
                        this.visualElement.addClass('ui-helper-hidden');
 
173
            if (!onlyCss) {
 
174
                var o = this.options;
 
175
                this.element.unbind('.checkBox');
 
176
                                this.visualElement.remove();
 
177
                this.labels
 
178
                                        .unbind('.checkBox')
 
179
                                        .removeClass('ui-state-hover ui-state-checked ui-state-disabled')
 
180
                                ;
 
181
            }
 
182
        },
 
183
                
 
184
        disable: function(status){
 
185
                        if(status === undefined){
 
186
                                status = true;
 
187
                        }
 
188
            this.element[0].disabled = status;
 
189
            this.reflectUI({type: 'manuallydisabled'});
 
190
        },
 
191
                
 
192
        enable: function(){
 
193
            this.element[0].disabled = false;
 
194
            this.reflectUI({type: 'manuallyenabled'});
 
195
        },
 
196
                
 
197
        toggle: function(e){
 
198
            this.changeCheckStatus(!(this.element.is(':checked')), e);
 
199
        },
 
200
                
 
201
        changeCheckStatus: function(status, e){
 
202
            if(e && e.type == 'click' && this.element[0].disabled){
 
203
                                return false;
 
204
                        }
 
205
                        this.element[0].checked = !!status;
 
206
            this.reflectUI(e || {
 
207
                type: 'changecheckstatus'
 
208
            });
 
209
                        return undefined;
 
210
        },
 
211
        propagate: function(n, e, _noGroupReflect){
 
212
                        if(!e || e.type != 'initialreflect'){
 
213
                                if (this.radio && !_noGroupReflect) {
 
214
                                        var elem = this.element[0];
 
215
                                        //dynamic
 
216
                        $('[name="'+ elem.name +'"]', elem.form || elem.ownerDocument).checkBox('reflectUI', e, true);
 
217
                                                
 
218
                    }
 
219
                    return this._trigger(n, e, {
 
220
                        options: this.options,
 
221
                        checked: this.checkedStatus,
 
222
                        labels: this.labels,
 
223
                                        disabled: this.disabledStatus
 
224
                    });
 
225
                        }
 
226
                        return undefined;
 
227
        },
 
228
                changeValidityState: function(){
 
229
                        if(supportsValidity){
 
230
                                this.visualGroup[ !this.element.prop('willValidate') || (this.element.prop('validity') || {valid: true}).valid ? 'removeClass' : 'addClass' ](this.inputType +'-invalid');
 
231
                        }
 
232
                },
 
233
        reflectUI: function(e){
 
234
                        
 
235
            var oldChecked                      = this.checkedStatus, 
 
236
                                oldDisabledStatus       = this.disabledStatus
 
237
                        ;
 
238
                                                
 
239
                        this.disabledStatus = this.element.is(':disabled');
 
240
                        this.checkedStatus = this.element.is(':checked');
 
241
                        if(!e || e.type !== 'initialreflect'){
 
242
                                this.changeValidityState();
 
243
                        }
 
244
                        
 
245
                        if (this.disabledStatus != oldDisabledStatus || this.checkedStatus !== oldChecked) {
 
246
                                this._changeStateClassChain();
 
247
                                
 
248
                                (this.disabledStatus != oldDisabledStatus &&
 
249
                                        this.propagate('disabledchange', e));
 
250
                                
 
251
                                (this.checkedStatus !== oldChecked &&
 
252
                                        this.propagate('change', e));
 
253
                        }
 
254
            
 
255
        }
 
256
    });
 
257
                
 
258
        if($.propHooks){
 
259
                $.each({checked: 'changeCheckStatus', disabled: 'disable'}, function(name, fn){
 
260
                        //be hook friendly
 
261
                        if(!$.propHooks[name]){
 
262
                                $.propHooks[name] = {};
 
263
                        }
 
264
                        var oldSetHook = $.propHooks[name].set;
 
265
                        
 
266
                        $.propHooks[name].set = function(elem, value){
 
267
                                var widget = $.data(elem, 'checkBox');
 
268
                                if(widget){
 
269
                                        widget[fn](!!value);
 
270
                                }
 
271
                                return oldSetHook && oldSetHook(elem, value) ;
 
272
                        };
 
273
                        
 
274
                });
 
275
        }
 
276
})(jQuery);
 
277