jquery.switchbutton.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * jQuery EasyUI 1.4.4
  3. *
  4. * Copyright (c) 2009-2015 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. function _1(_2){
  12. 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);
  13. var t=$(_2);
  14. t.addClass("switchbutton-f").hide();
  15. var _4=t.attr("name");
  16. if(_4){
  17. t.removeAttr("name").attr("switchbuttonName",_4);
  18. _3.find(".switchbutton-value").attr("name",_4);
  19. }
  20. _3.bind("_resize",function(e,_5){
  21. if($(this).hasClass("easyui-fluid")||_5){
  22. _6(_2);
  23. }
  24. return false;
  25. });
  26. return _3;
  27. };
  28. function _6(_7,_8){
  29. var _9=$.data(_7,"switchbutton");
  30. var _a=_9.options;
  31. var _b=_9.switchbutton;
  32. if(_8){
  33. $.extend(_a,_8);
  34. }
  35. var _c=_b.is(":visible");
  36. if(!_c){
  37. _b.appendTo("body");
  38. }
  39. _b._size(_a);
  40. var w=_b.width();
  41. var h=_b.height();
  42. var w=_b.outerWidth();
  43. var h=_b.outerHeight();
  44. var _d=parseInt(_a.handleWidth)||_b.height();
  45. var _e=w*2-_d;
  46. _b.find(".switchbutton-inner").css({width:_e+"px",height:h+"px",lineHeight:h+"px"});
  47. _b.find(".switchbutton-handle")._outerWidth(_d)._outerHeight(h).css({marginLeft:-_d/2+"px"});
  48. _b.find(".switchbutton-on").css({width:(w-_d/2)+"px",textIndent:(_a.reversed?"":"-")+_d/2+"px"});
  49. _b.find(".switchbutton-off").css({width:(w-_d/2)+"px",textIndent:(_a.reversed?"-":"")+_d/2+"px"});
  50. _a.marginWidth=w-_d;
  51. _f(_7,_a.checked,false);
  52. if(!_c){
  53. _b.insertAfter(_7);
  54. }
  55. };
  56. function _10(_11){
  57. var _12=$.data(_11,"switchbutton");
  58. var _13=_12.options;
  59. var _14=_12.switchbutton;
  60. var _15=_14.find(".switchbutton-inner");
  61. var on=_15.find(".switchbutton-on").html(_13.onText);
  62. var off=_15.find(".switchbutton-off").html(_13.offText);
  63. var _16=_15.find(".switchbutton-handle").html(_13.handleText);
  64. if(_13.reversed){
  65. off.prependTo(_15);
  66. on.insertAfter(_16);
  67. }else{
  68. on.prependTo(_15);
  69. off.insertAfter(_16);
  70. }
  71. _14.find(".switchbutton-value")._propAttr("checked",_13.checked);
  72. _14.removeClass("switchbutton-disabled").addClass(_13.disabled?"switchbutton-disabled":"");
  73. _14.removeClass("switchbutton-reversed").addClass(_13.reversed?"switchbutton-reversed":"");
  74. _f(_11,_13.checked);
  75. _17(_11,_13.readonly);
  76. $(_11).switchbutton("setValue",_13.value);
  77. };
  78. function _f(_18,_19,_1a){
  79. var _1b=$.data(_18,"switchbutton");
  80. var _1c=_1b.options;
  81. _1c.checked=_19;
  82. var _1d=_1b.switchbutton.find(".switchbutton-inner");
  83. var _1e=_1d.find(".switchbutton-on");
  84. var _1f=_1c.reversed?(_1c.checked?_1c.marginWidth:0):(_1c.checked?0:_1c.marginWidth);
  85. var dir=_1e.css("float").toLowerCase();
  86. var css={};
  87. css["margin-"+dir]=-_1f+"px";
  88. _1a?_1d.animate(css,200):_1d.css(css);
  89. var _20=_1d.find(".switchbutton-value");
  90. var ck=_20.is(":checked");
  91. $(_18).add(_20)._propAttr("checked",_1c.checked);
  92. if(ck!=_1c.checked){
  93. _1c.onChange.call(_18,_1c.checked);
  94. }
  95. };
  96. function _21(_22,_23){
  97. var _24=$.data(_22,"switchbutton");
  98. var _25=_24.options;
  99. var _26=_24.switchbutton;
  100. var _27=_26.find(".switchbutton-value");
  101. if(_23){
  102. _25.disabled=true;
  103. $(_22).add(_27).attr("disabled","disabled");
  104. _26.addClass("switchbutton-disabled");
  105. }else{
  106. _25.disabled=false;
  107. $(_22).add(_27).removeAttr("disabled");
  108. _26.removeClass("switchbutton-disabled");
  109. }
  110. };
  111. function _17(_28,_29){
  112. var _2a=$.data(_28,"switchbutton");
  113. var _2b=_2a.options;
  114. _2b.readonly=_29==undefined?true:_29;
  115. _2a.switchbutton.removeClass("switchbutton-readonly").addClass(_2b.readonly?"switchbutton-readonly":"");
  116. };
  117. function _2c(_2d){
  118. var _2e=$.data(_2d,"switchbutton");
  119. var _2f=_2e.options;
  120. _2e.switchbutton.unbind(".switchbutton").bind("click.switchbutton",function(){
  121. if(!_2f.disabled&&!_2f.readonly){
  122. _f(_2d,_2f.checked?false:true,true);
  123. }
  124. });
  125. };
  126. $.fn.switchbutton=function(_30,_31){
  127. if(typeof _30=="string"){
  128. return $.fn.switchbutton.methods[_30](this,_31);
  129. }
  130. _30=_30||{};
  131. return this.each(function(){
  132. var _32=$.data(this,"switchbutton");
  133. if(_32){
  134. $.extend(_32.options,_30);
  135. }else{
  136. _32=$.data(this,"switchbutton",{options:$.extend({},$.fn.switchbutton.defaults,$.fn.switchbutton.parseOptions(this),_30),switchbutton:_1(this)});
  137. }
  138. _32.options.originalChecked=_32.options.checked;
  139. _10(this);
  140. _6(this);
  141. _2c(this);
  142. });
  143. };
  144. $.fn.switchbutton.methods={options:function(jq){
  145. var _33=jq.data("switchbutton");
  146. return $.extend(_33.options,{value:_33.switchbutton.find(".switchbutton-value").val()});
  147. },resize:function(jq,_34){
  148. return jq.each(function(){
  149. _6(this,_34);
  150. });
  151. },enable:function(jq){
  152. return jq.each(function(){
  153. _21(this,false);
  154. });
  155. },disable:function(jq){
  156. return jq.each(function(){
  157. _21(this,true);
  158. });
  159. },readonly:function(jq,_35){
  160. return jq.each(function(){
  161. _17(this,_35);
  162. });
  163. },check:function(jq){
  164. return jq.each(function(){
  165. _f(this,true);
  166. });
  167. },uncheck:function(jq){
  168. return jq.each(function(){
  169. _f(this,false);
  170. });
  171. },clear:function(jq){
  172. return jq.each(function(){
  173. _f(this,false);
  174. });
  175. },reset:function(jq){
  176. return jq.each(function(){
  177. var _36=$(this).switchbutton("options");
  178. _f(this,_36.originalChecked);
  179. });
  180. },setValue:function(jq,_37){
  181. return jq.each(function(){
  182. $(this).val(_37);
  183. $.data(this,"switchbutton").switchbutton.find(".switchbutton-value").val(_37);
  184. });
  185. }};
  186. $.fn.switchbutton.parseOptions=function(_38){
  187. var t=$(_38);
  188. 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)});
  189. };
  190. $.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){
  191. }};
  192. })(jQuery);