jquery.switchbutton.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * EasyUI for jQuery 1.9.4
  3. *
  4. * Copyright (c) 2009-2020 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the freeware license: http://www.jeasyui.com/license_freeware.php
  7. * To use it on other terms please contact us: info@jeasyui.com
  8. *
  9. */
  10. (function($){
  11. var _1=1;
  12. function _2(_3){
  13. var _4=$("<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\" tabindex=\"-1\">"+"</span>"+"</span>").insertAfter(_3);
  14. var t=$(_3);
  15. t.addClass("switchbutton-f").hide();
  16. var _5=t.attr("name");
  17. if(_5){
  18. t.removeAttr("name").attr("switchbuttonName",_5);
  19. _4.find(".switchbutton-value").attr("name",_5);
  20. }
  21. _4._bind("_resize",function(e,_6){
  22. if($(this).hasClass("easyui-fluid")||_6){
  23. _7(_3);
  24. }
  25. return false;
  26. });
  27. return _4;
  28. };
  29. function _7(_8,_9){
  30. var _a=$.data(_8,"switchbutton");
  31. var _b=_a.options;
  32. var _c=_a.switchbutton;
  33. if(_9){
  34. $.extend(_b,_9);
  35. }
  36. var _d=_c.is(":visible");
  37. if(!_d){
  38. _c.appendTo("body");
  39. }
  40. _c._size(_b);
  41. if(_b.label&&_b.labelPosition){
  42. if(_b.labelPosition=="top"){
  43. _a.label._size({width:_b.labelWidth},_c);
  44. }else{
  45. _a.label._size({width:_b.labelWidth,height:_c.outerHeight()},_c);
  46. _a.label.css("lineHeight",_c.outerHeight()+"px");
  47. }
  48. }
  49. var w=_c.width();
  50. var h=_c.height();
  51. var w=_c.outerWidth();
  52. var h=_c.outerHeight();
  53. var _e=parseInt(_b.handleWidth)||_c.height();
  54. var _f=w*2-_e;
  55. _c.find(".switchbutton-inner").css({width:_f+"px",height:h+"px",lineHeight:h+"px"});
  56. _c.find(".switchbutton-handle")._outerWidth(_e)._outerHeight(h).css({marginLeft:-_e/2+"px"});
  57. _c.find(".switchbutton-on").css({width:(w-_e/2)+"px",textIndent:(_b.reversed?"":"-")+_e/2+"px"});
  58. _c.find(".switchbutton-off").css({width:(w-_e/2)+"px",textIndent:(_b.reversed?"-":"")+_e/2+"px"});
  59. _b.marginWidth=w-_e;
  60. _10(_8,_b.checked,false);
  61. if(!_d){
  62. _c.insertAfter(_8);
  63. }
  64. };
  65. function _11(_12){
  66. var _13=$.data(_12,"switchbutton");
  67. var _14=_13.options;
  68. var _15=_13.switchbutton;
  69. var _16=_15.find(".switchbutton-inner");
  70. var on=_16.find(".switchbutton-on").html(_14.onText);
  71. var off=_16.find(".switchbutton-off").html(_14.offText);
  72. var _17=_16.find(".switchbutton-handle").html(_14.handleText);
  73. if(_14.reversed){
  74. off.prependTo(_16);
  75. on.insertAfter(_17);
  76. }else{
  77. on.prependTo(_16);
  78. off.insertAfter(_17);
  79. }
  80. var _18="_easyui_switchbutton_"+(++_1);
  81. var _19=_15.find(".switchbutton-value")._propAttr("checked",_14.checked).attr("id",_18);
  82. _19._unbind(".switchbutton")._bind("change.switchbutton",function(e){
  83. return false;
  84. });
  85. _15.removeClass("switchbutton-reversed").addClass(_14.reversed?"switchbutton-reversed":"");
  86. if(_14.label){
  87. if(typeof _14.label=="object"){
  88. _13.label=$(_14.label);
  89. _13.label.attr("for",_18);
  90. }else{
  91. $(_13.label).remove();
  92. _13.label=$("<label class=\"textbox-label\"></label>").html(_14.label);
  93. _13.label.css("textAlign",_14.labelAlign).attr("for",_18);
  94. if(_14.labelPosition=="after"){
  95. _13.label.insertAfter(_15);
  96. }else{
  97. _13.label.insertBefore(_12);
  98. }
  99. _13.label.removeClass("textbox-label-left textbox-label-right textbox-label-top");
  100. _13.label.addClass("textbox-label-"+_14.labelPosition);
  101. }
  102. }else{
  103. $(_13.label).remove();
  104. }
  105. _10(_12,_14.checked);
  106. _1a(_12,_14.readonly);
  107. _1b(_12,_14.disabled);
  108. $(_12).switchbutton("setValue",_14.value);
  109. };
  110. function _10(_1c,_1d,_1e){
  111. var _1f=$.data(_1c,"switchbutton");
  112. var _20=_1f.options;
  113. var _21=_1f.switchbutton.find(".switchbutton-inner");
  114. var _22=_21.find(".switchbutton-on");
  115. var _23=_20.reversed?(_1d?_20.marginWidth:0):(_1d?0:_20.marginWidth);
  116. var dir=_22.css("float").toLowerCase();
  117. var css={};
  118. css["margin-"+dir]=-_23+"px";
  119. _1e?_21.animate(css,200):_21.css(css);
  120. var _24=_21.find(".switchbutton-value");
  121. $(_1c).add(_24)._propAttr("checked",_1d);
  122. if(_20.checked!=_1d){
  123. _20.checked=_1d;
  124. _20.onChange.call(_1c,_20.checked);
  125. $(_1c).closest("form").trigger("_change",[_1c]);
  126. }
  127. };
  128. function _1b(_25,_26){
  129. var _27=$.data(_25,"switchbutton");
  130. var _28=_27.options;
  131. var _29=_27.switchbutton;
  132. var _2a=_29.find(".switchbutton-value");
  133. if(_26){
  134. _28.disabled=true;
  135. $(_25).add(_2a)._propAttr("disabled",true);
  136. _29.addClass("switchbutton-disabled");
  137. _29.removeAttr("tabindex");
  138. }else{
  139. _28.disabled=false;
  140. $(_25).add(_2a)._propAttr("disabled",false);
  141. _29.removeClass("switchbutton-disabled");
  142. _29.attr("tabindex",$(_25).attr("tabindex")||"");
  143. }
  144. };
  145. function _1a(_2b,_2c){
  146. var _2d=$.data(_2b,"switchbutton");
  147. var _2e=_2d.options;
  148. _2e.readonly=_2c==undefined?true:_2c;
  149. _2d.switchbutton.removeClass("switchbutton-readonly").addClass(_2e.readonly?"switchbutton-readonly":"");
  150. };
  151. function _2f(_30){
  152. var _31=$.data(_30,"switchbutton");
  153. var _32=_31.options;
  154. _31.switchbutton._unbind(".switchbutton")._bind("click.switchbutton",function(){
  155. if(!_32.disabled&&!_32.readonly){
  156. _10(_30,_32.checked?false:true,true);
  157. }
  158. })._bind("keydown.switchbutton",function(e){
  159. if(e.which==13||e.which==32){
  160. if(!_32.disabled&&!_32.readonly){
  161. _10(_30,_32.checked?false:true,true);
  162. return false;
  163. }
  164. }
  165. });
  166. };
  167. $.fn.switchbutton=function(_33,_34){
  168. if(typeof _33=="string"){
  169. return $.fn.switchbutton.methods[_33](this,_34);
  170. }
  171. _33=_33||{};
  172. return this.each(function(){
  173. var _35=$.data(this,"switchbutton");
  174. if(_35){
  175. $.extend(_35.options,_33);
  176. }else{
  177. _35=$.data(this,"switchbutton",{options:$.extend({},$.fn.switchbutton.defaults,$.fn.switchbutton.parseOptions(this),_33),switchbutton:_2(this)});
  178. }
  179. _35.options.originalChecked=_35.options.checked;
  180. _11(this);
  181. _7(this);
  182. _2f(this);
  183. });
  184. };
  185. $.fn.switchbutton.methods={options:function(jq){
  186. var _36=jq.data("switchbutton");
  187. return $.extend(_36.options,{value:_36.switchbutton.find(".switchbutton-value").val()});
  188. },resize:function(jq,_37){
  189. return jq.each(function(){
  190. _7(this,_37);
  191. });
  192. },enable:function(jq){
  193. return jq.each(function(){
  194. _1b(this,false);
  195. });
  196. },disable:function(jq){
  197. return jq.each(function(){
  198. _1b(this,true);
  199. });
  200. },readonly:function(jq,_38){
  201. return jq.each(function(){
  202. _1a(this,_38);
  203. });
  204. },check:function(jq){
  205. return jq.each(function(){
  206. _10(this,true);
  207. });
  208. },uncheck:function(jq){
  209. return jq.each(function(){
  210. _10(this,false);
  211. });
  212. },clear:function(jq){
  213. return jq.each(function(){
  214. _10(this,false);
  215. });
  216. },reset:function(jq){
  217. return jq.each(function(){
  218. var _39=$(this).switchbutton("options");
  219. _10(this,_39.originalChecked);
  220. });
  221. },setValue:function(jq,_3a){
  222. return jq.each(function(){
  223. $(this).val(_3a);
  224. $.data(this,"switchbutton").switchbutton.find(".switchbutton-value").val(_3a);
  225. });
  226. }};
  227. $.fn.switchbutton.parseOptions=function(_3b){
  228. var t=$(_3b);
  229. return $.extend({},$.parser.parseOptions(_3b,["onText","offText","handleText",{handleWidth:"number",reversed:"boolean"},"label","labelPosition","labelAlign",{labelWidth:"number"}]),{value:(t.val()||undefined),checked:(t.attr("checked")?true:undefined),disabled:(t.attr("disabled")?true:undefined),readonly:(t.attr("readonly")?true:undefined)});
  230. };
  231. $.fn.switchbutton.defaults={handleWidth:"auto",width:60,height:30,checked:false,disabled:false,readonly:false,reversed:false,onText:"ON",offText:"OFF",handleText:"",value:"on",label:null,labelWidth:"auto",labelPosition:"before",labelAlign:"left",onChange:function(_3c){
  232. }};
  233. })(jQuery);