jquery.form.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * jQuery EasyUI 1.5.2
  3. *
  4. * Copyright (c) 2009-2017 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,_3){
  12. var _4=$.data(_2,"form").options;
  13. $.extend(_4,_3||{});
  14. var _5=$.extend({},_4.queryParams);
  15. if(_4.onSubmit.call(_2,_5)==false){
  16. return;
  17. }
  18. var _6=$(_2).find(".textbox-text:focus");
  19. _6.triggerHandler("blur");
  20. _6.focus();
  21. var _7=null;
  22. if(_4.dirty){
  23. var ff=[];
  24. $.map(_4.dirtyFields,function(f){
  25. if($(f).hasClass("textbox-f")){
  26. $(f).next().find(".textbox-value").each(function(){
  27. ff.push(this);
  28. });
  29. }else{
  30. ff.push(f);
  31. }
  32. });
  33. _7=$(_2).find("input[name]:enabled,textarea[name]:enabled,select[name]:enabled").filter(function(){
  34. return $.inArray(this,ff)==-1;
  35. });
  36. _7.attr("disabled","disabled");
  37. }
  38. if(_4.ajax){
  39. if(_4.iframe){
  40. _8(_2,_5);
  41. }else{
  42. if(window.FormData!==undefined){
  43. _9(_2,_5);
  44. }else{
  45. _8(_2,_5);
  46. }
  47. }
  48. }else{
  49. $(_2).submit();
  50. }
  51. if(_4.dirty){
  52. _7.removeAttr("disabled");
  53. }
  54. };
  55. function _8(_a,_b){
  56. var _c=$.data(_a,"form").options;
  57. var _d="easyui_frame_"+(new Date().getTime());
  58. var _e=$("<iframe id="+_d+" name="+_d+"></iframe>").appendTo("body");
  59. _e.attr("src",window.ActiveXObject?"javascript:false":"about:blank");
  60. _e.css({position:"absolute",top:-1000,left:-1000});
  61. _e.bind("load",cb);
  62. _f(_b);
  63. function _f(_10){
  64. var _11=$(_a);
  65. if(_c.url){
  66. _11.attr("action",_c.url);
  67. }
  68. var t=_11.attr("target"),a=_11.attr("action");
  69. _11.attr("target",_d);
  70. var _12=$();
  71. try{
  72. for(var n in _10){
  73. var _13=$("<input type=\"hidden\" name=\""+n+"\">").val(_10[n]).appendTo(_11);
  74. _12=_12.add(_13);
  75. }
  76. _14();
  77. _11[0].submit();
  78. }
  79. finally{
  80. _11.attr("action",a);
  81. t?_11.attr("target",t):_11.removeAttr("target");
  82. _12.remove();
  83. }
  84. };
  85. function _14(){
  86. var f=$("#"+_d);
  87. if(!f.length){
  88. return;
  89. }
  90. try{
  91. var s=f.contents()[0].readyState;
  92. if(s&&s.toLowerCase()=="uninitialized"){
  93. setTimeout(_14,100);
  94. }
  95. }
  96. catch(e){
  97. cb();
  98. }
  99. };
  100. var _15=10;
  101. function cb(){
  102. var f=$("#"+_d);
  103. if(!f.length){
  104. return;
  105. }
  106. f.unbind();
  107. var _16="";
  108. try{
  109. var _17=f.contents().find("body");
  110. _16=_17.html();
  111. if(_16==""){
  112. if(--_15){
  113. setTimeout(cb,100);
  114. return;
  115. }
  116. }
  117. var ta=_17.find(">textarea");
  118. if(ta.length){
  119. _16=ta.val();
  120. }else{
  121. var pre=_17.find(">pre");
  122. if(pre.length){
  123. _16=pre.html();
  124. }
  125. }
  126. }
  127. catch(e){
  128. }
  129. _c.success.call(_a,_16);
  130. setTimeout(function(){
  131. f.unbind();
  132. f.remove();
  133. },100);
  134. };
  135. };
  136. function _9(_18,_19){
  137. var _1a=$.data(_18,"form").options;
  138. var _1b=new FormData($(_18)[0]);
  139. for(var _1c in _19){
  140. _1b.append(_1c,_19[_1c]);
  141. }
  142. $.ajax({url:_1a.url,type:"post",xhr:function(){
  143. var xhr=$.ajaxSettings.xhr();
  144. if(xhr.upload){
  145. xhr.upload.addEventListener("progress",function(e){
  146. if(e.lengthComputable){
  147. var _1d=e.total;
  148. var _1e=e.loaded||e.position;
  149. var _1f=Math.ceil(_1e*100/_1d);
  150. _1a.onProgress.call(_18,_1f);
  151. }
  152. },false);
  153. }
  154. return xhr;
  155. },data:_1b,dataType:"html",cache:false,contentType:false,processData:false,complete:function(res){
  156. _1a.success.call(_18,res.responseText);
  157. }});
  158. };
  159. function _20(_21,_22){
  160. var _23=$.data(_21,"form").options;
  161. if(typeof _22=="string"){
  162. var _24={};
  163. if(_23.onBeforeLoad.call(_21,_24)==false){
  164. return;
  165. }
  166. $.ajax({url:_22,data:_24,dataType:"json",success:function(_25){
  167. _26(_25);
  168. },error:function(){
  169. _23.onLoadError.apply(_21,arguments);
  170. }});
  171. }else{
  172. _26(_22);
  173. }
  174. function _26(_27){
  175. var _28=$(_21);
  176. for(var _29 in _27){
  177. var val=_27[_29];
  178. if(!_2a(_29,val)){
  179. if(!_2b(_29,val)){
  180. _28.find("input[name=\""+_29+"\"]").val(val);
  181. _28.find("textarea[name=\""+_29+"\"]").val(val);
  182. _28.find("select[name=\""+_29+"\"]").val(val);
  183. }
  184. }
  185. }
  186. _23.onLoadSuccess.call(_21,_27);
  187. _28.form("validate");
  188. };
  189. function _2a(_2c,val){
  190. var cc=$(_21).find("[switchbuttonName=\""+_2c+"\"]");
  191. if(cc.length){
  192. cc.switchbutton("uncheck");
  193. cc.each(function(){
  194. if(_2d($(this).switchbutton("options").value,val)){
  195. $(this).switchbutton("check");
  196. }
  197. });
  198. return true;
  199. }
  200. cc=$(_21).find("input[name=\""+_2c+"\"][type=radio], input[name=\""+_2c+"\"][type=checkbox]");
  201. if(cc.length){
  202. cc._propAttr("checked",false);
  203. cc.each(function(){
  204. if(_2d($(this).val(),val)){
  205. $(this)._propAttr("checked",true);
  206. }
  207. });
  208. return true;
  209. }
  210. return false;
  211. };
  212. function _2d(v,val){
  213. if(v==String(val)||$.inArray(v,$.isArray(val)?val:[val])>=0){
  214. return true;
  215. }else{
  216. return false;
  217. }
  218. };
  219. function _2b(_2e,val){
  220. var _2f=$(_21).find("[textboxName=\""+_2e+"\"],[sliderName=\""+_2e+"\"]");
  221. if(_2f.length){
  222. for(var i=0;i<_23.fieldTypes.length;i++){
  223. var _30=_23.fieldTypes[i];
  224. var _31=_2f.data(_30);
  225. if(_31){
  226. if(_31.options.multiple||_31.options.range){
  227. _2f[_30]("setValues",val);
  228. }else{
  229. _2f[_30]("setValue",val);
  230. }
  231. return true;
  232. }
  233. }
  234. }
  235. return false;
  236. };
  237. };
  238. function _32(_33){
  239. $("input,select,textarea",_33).each(function(){
  240. if($(this).hasClass("textbox-value")){
  241. return;
  242. }
  243. var t=this.type,tag=this.tagName.toLowerCase();
  244. if(t=="text"||t=="hidden"||t=="password"||tag=="textarea"){
  245. this.value="";
  246. }else{
  247. if(t=="file"){
  248. var _34=$(this);
  249. if(!_34.hasClass("textbox-value")){
  250. var _35=_34.clone().val("");
  251. _35.insertAfter(_34);
  252. if(_34.data("validatebox")){
  253. _34.validatebox("destroy");
  254. _35.validatebox();
  255. }else{
  256. _34.remove();
  257. }
  258. }
  259. }else{
  260. if(t=="checkbox"||t=="radio"){
  261. this.checked=false;
  262. }else{
  263. if(tag=="select"){
  264. this.selectedIndex=-1;
  265. }
  266. }
  267. }
  268. }
  269. });
  270. var tmp=$();
  271. var _36=$(_33);
  272. var _37=$.data(_33,"form").options;
  273. for(var i=0;i<_37.fieldTypes.length;i++){
  274. var _38=_37.fieldTypes[i];
  275. var _39=_36.find("."+_38+"-f").not(tmp);
  276. if(_39.length&&_39[_38]){
  277. _39[_38]("clear");
  278. tmp=tmp.add(_39);
  279. }
  280. }
  281. _36.form("validate");
  282. };
  283. function _3a(_3b){
  284. _3b.reset();
  285. var _3c=$(_3b);
  286. var _3d=$.data(_3b,"form").options;
  287. for(var i=_3d.fieldTypes.length-1;i>=0;i--){
  288. var _3e=_3d.fieldTypes[i];
  289. var _3f=_3c.find("."+_3e+"-f");
  290. if(_3f.length&&_3f[_3e]){
  291. _3f[_3e]("reset");
  292. }
  293. }
  294. _3c.form("validate");
  295. };
  296. function _40(_41){
  297. var _42=$.data(_41,"form").options;
  298. $(_41).unbind(".form");
  299. if(_42.ajax){
  300. $(_41).bind("submit.form",function(){
  301. setTimeout(function(){
  302. _1(_41,_42);
  303. },0);
  304. return false;
  305. });
  306. }
  307. $(_41).bind("_change.form",function(e,t){
  308. if($.inArray(t,_42.dirtyFields)==-1){
  309. _42.dirtyFields.push(t);
  310. }
  311. _42.onChange.call(this,t);
  312. }).bind("change.form",function(e){
  313. var t=e.target;
  314. if(!$(t).hasClass("textbox-text")){
  315. if($.inArray(t,_42.dirtyFields)==-1){
  316. _42.dirtyFields.push(t);
  317. }
  318. _42.onChange.call(this,t);
  319. }
  320. });
  321. _43(_41,_42.novalidate);
  322. };
  323. function _44(_45,_46){
  324. _46=_46||{};
  325. var _47=$.data(_45,"form");
  326. if(_47){
  327. $.extend(_47.options,_46);
  328. }else{
  329. $.data(_45,"form",{options:$.extend({},$.fn.form.defaults,$.fn.form.parseOptions(_45),_46)});
  330. }
  331. };
  332. function _48(_49){
  333. if($.fn.validatebox){
  334. var t=$(_49);
  335. t.find(".validatebox-text:not(:disabled)").validatebox("validate");
  336. var _4a=t.find(".validatebox-invalid");
  337. _4a.filter(":not(:disabled):first").focus();
  338. return _4a.length==0;
  339. }
  340. return true;
  341. };
  342. function _43(_4b,_4c){
  343. var _4d=$.data(_4b,"form").options;
  344. _4d.novalidate=_4c;
  345. $(_4b).find(".validatebox-text:not(:disabled)").validatebox(_4c?"disableValidation":"enableValidation");
  346. };
  347. $.fn.form=function(_4e,_4f){
  348. if(typeof _4e=="string"){
  349. this.each(function(){
  350. _44(this);
  351. });
  352. return $.fn.form.methods[_4e](this,_4f);
  353. }
  354. return this.each(function(){
  355. _44(this,_4e);
  356. _40(this);
  357. });
  358. };
  359. $.fn.form.methods={options:function(jq){
  360. return $.data(jq[0],"form").options;
  361. },submit:function(jq,_50){
  362. return jq.each(function(){
  363. _1(this,_50);
  364. });
  365. },load:function(jq,_51){
  366. return jq.each(function(){
  367. _20(this,_51);
  368. });
  369. },clear:function(jq){
  370. return jq.each(function(){
  371. _32(this);
  372. });
  373. },reset:function(jq){
  374. return jq.each(function(){
  375. _3a(this);
  376. });
  377. },validate:function(jq){
  378. return _48(jq[0]);
  379. },disableValidation:function(jq){
  380. return jq.each(function(){
  381. _43(this,true);
  382. });
  383. },enableValidation:function(jq){
  384. return jq.each(function(){
  385. _43(this,false);
  386. });
  387. },resetValidation:function(jq){
  388. return jq.each(function(){
  389. $(this).find(".validatebox-text:not(:disabled)").validatebox("resetValidation");
  390. });
  391. },resetDirty:function(jq){
  392. return jq.each(function(){
  393. $(this).form("options").dirtyFields=[];
  394. });
  395. }};
  396. $.fn.form.parseOptions=function(_52){
  397. var t=$(_52);
  398. return $.extend({},$.parser.parseOptions(_52,[{ajax:"boolean",dirty:"boolean"}]),{url:(t.attr("action")?t.attr("action"):undefined)});
  399. };
  400. $.fn.form.defaults={fieldTypes:["combobox","combotree","combogrid","combotreegrid","datetimebox","datebox","combo","datetimespinner","timespinner","numberspinner","spinner","slider","searchbox","numberbox","passwordbox","filebox","textbox","switchbutton"],novalidate:false,ajax:true,iframe:true,dirty:false,dirtyFields:[],url:null,queryParams:{},onSubmit:function(_53){
  401. return $(this).form("validate");
  402. },onProgress:function(_54){
  403. },success:function(_55){
  404. },onBeforeLoad:function(_56){
  405. },onLoadSuccess:function(_57){
  406. },onLoadError:function(){
  407. },onChange:function(_58){
  408. }};
  409. })(jQuery);