jquery.switchbutton.js
5.72 KB
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
/**
* jQuery EasyUI 1.5
*
* Copyright (c) 2009-2016 www.jeasyui.com. All rights reserved.
*
* Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
* To use it on other terms please contact us: info@jeasyui.com
*
*/
(function($){
function _1(_2){
var _3=$("<span class=\"switchbutton\">"+"<span class=\"switchbutton-inner\">"+"<span class=\"switchbutton-on\"></span>"+"<span class=\"switchbutton-handle\"></span>"+"<span class=\"switchbutton-off\"></span>"+"<input class=\"switchbutton-value\" type=\"checkbox\">"+"</span>"+"</span>").insertAfter(_2);
var t=$(_2);
t.addClass("switchbutton-f").hide();
var _4=t.attr("name");
if(_4){
t.removeAttr("name").attr("switchbuttonName",_4);
_3.find(".switchbutton-value").attr("name",_4);
}
_3.bind("_resize",function(e,_5){
if($(this).hasClass("easyui-fluid")||_5){
_6(_2);
}
return false;
});
return _3;
};
function _6(_7,_8){
var _9=$.data(_7,"switchbutton");
var _a=_9.options;
var _b=_9.switchbutton;
if(_8){
$.extend(_a,_8);
}
var _c=_b.is(":visible");
if(!_c){
_b.appendTo("body");
}
_b._size(_a);
var w=_b.width();
var h=_b.height();
var w=_b.outerWidth();
var h=_b.outerHeight();
var _d=parseInt(_a.handleWidth)||_b.height();
var _e=w*2-_d;
_b.find(".switchbutton-inner").css({width:_e+"px",height:h+"px",lineHeight:h+"px"});
_b.find(".switchbutton-handle")._outerWidth(_d)._outerHeight(h).css({marginLeft:-_d/2+"px"});
_b.find(".switchbutton-on").css({width:(w-_d/2)+"px",textIndent:(_a.reversed?"":"-")+_d/2+"px"});
_b.find(".switchbutton-off").css({width:(w-_d/2)+"px",textIndent:(_a.reversed?"-":"")+_d/2+"px"});
_a.marginWidth=w-_d;
_f(_7,_a.checked,false);
if(!_c){
_b.insertAfter(_7);
}
};
function _10(_11){
var _12=$.data(_11,"switchbutton");
var _13=_12.options;
var _14=_12.switchbutton;
var _15=_14.find(".switchbutton-inner");
var on=_15.find(".switchbutton-on").html(_13.onText);
var off=_15.find(".switchbutton-off").html(_13.offText);
var _16=_15.find(".switchbutton-handle").html(_13.handleText);
if(_13.reversed){
off.prependTo(_15);
on.insertAfter(_16);
}else{
on.prependTo(_15);
off.insertAfter(_16);
}
_14.find(".switchbutton-value")._propAttr("checked",_13.checked);
_14.removeClass("switchbutton-disabled").addClass(_13.disabled?"switchbutton-disabled":"");
_14.removeClass("switchbutton-reversed").addClass(_13.reversed?"switchbutton-reversed":"");
_f(_11,_13.checked);
_17(_11,_13.readonly);
$(_11).switchbutton("setValue",_13.value);
};
function _f(_18,_19,_1a){
var _1b=$.data(_18,"switchbutton");
var _1c=_1b.options;
_1c.checked=_19;
var _1d=_1b.switchbutton.find(".switchbutton-inner");
var _1e=_1d.find(".switchbutton-on");
var _1f=_1c.reversed?(_1c.checked?_1c.marginWidth:0):(_1c.checked?0:_1c.marginWidth);
var dir=_1e.css("float").toLowerCase();
var css={};
css["margin-"+dir]=-_1f+"px";
_1a?_1d.animate(css,200):_1d.css(css);
var _20=_1d.find(".switchbutton-value");
var ck=_20.is(":checked");
$(_18).add(_20)._propAttr("checked",_1c.checked);
if(ck!=_1c.checked){
_1c.onChange.call(_18,_1c.checked);
}
};
function _21(_22,_23){
var _24=$.data(_22,"switchbutton");
var _25=_24.options;
var _26=_24.switchbutton;
var _27=_26.find(".switchbutton-value");
if(_23){
_25.disabled=true;
$(_22).add(_27).attr("disabled","disabled");
_26.addClass("switchbutton-disabled");
}else{
_25.disabled=false;
$(_22).add(_27).removeAttr("disabled");
_26.removeClass("switchbutton-disabled");
}
};
function _17(_28,_29){
var _2a=$.data(_28,"switchbutton");
var _2b=_2a.options;
_2b.readonly=_29==undefined?true:_29;
_2a.switchbutton.removeClass("switchbutton-readonly").addClass(_2b.readonly?"switchbutton-readonly":"");
};
function _2c(_2d){
var _2e=$.data(_2d,"switchbutton");
var _2f=_2e.options;
_2e.switchbutton.unbind(".switchbutton").bind("click.switchbutton",function(){
if(!_2f.disabled&&!_2f.readonly){
_f(_2d,_2f.checked?false:true,true);
}
});
};
$.fn.switchbutton=function(_30,_31){
if(typeof _30=="string"){
return $.fn.switchbutton.methods[_30](this,_31);
}
_30=_30||{};
return this.each(function(){
var _32=$.data(this,"switchbutton");
if(_32){
$.extend(_32.options,_30);
}else{
_32=$.data(this,"switchbutton",{options:$.extend({},$.fn.switchbutton.defaults,$.fn.switchbutton.parseOptions(this),_30),switchbutton:_1(this)});
}
_32.options.originalChecked=_32.options.checked;
_10(this);
_6(this);
_2c(this);
});
};
$.fn.switchbutton.methods={options:function(jq){
var _33=jq.data("switchbutton");
return $.extend(_33.options,{value:_33.switchbutton.find(".switchbutton-value").val()});
},resize:function(jq,_34){
return jq.each(function(){
_6(this,_34);
});
},enable:function(jq){
return jq.each(function(){
_21(this,false);
});
},disable:function(jq){
return jq.each(function(){
_21(this,true);
});
},readonly:function(jq,_35){
return jq.each(function(){
_17(this,_35);
});
},check:function(jq){
return jq.each(function(){
_f(this,true);
});
},uncheck:function(jq){
return jq.each(function(){
_f(this,false);
});
},clear:function(jq){
return jq.each(function(){
_f(this,false);
});
},reset:function(jq){
return jq.each(function(){
var _36=$(this).switchbutton("options");
_f(this,_36.originalChecked);
});
},setValue:function(jq,_37){
return jq.each(function(){
$(this).val(_37);
$.data(this,"switchbutton").switchbutton.find(".switchbutton-value").val(_37);
});
}};
$.fn.switchbutton.parseOptions=function(_38){
var t=$(_38);
return $.extend({},$.parser.parseOptions(_38,["onText","offText","handleText",{handleWidth:"number",reversed:"boolean"}]),{value:(t.val()||undefined),checked:(t.attr("checked")?true:undefined),disabled:(t.attr("disabled")?true:undefined),readonly:(t.attr("readonly")?true:undefined)});
};
$.fn.switchbutton.defaults={handleWidth:"auto",width:60,height:26,checked:false,disabled:false,readonly:false,reversed:false,onText:"ON",offText:"OFF",handleText:"",value:"on",onChange:function(_39){
}};
})(jQuery);