root / trunk / web / dojo / dojox / rails.js
History | View | Annotate | Download (3.43 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.rails"]){ |
||
9 | dojo._hasResource["dojox.rails"]=true; |
||
10 | dojo.provide("dojox.rails");
|
||
11 | dojo.require("dojo.NodeList-traverse");
|
||
12 | dojox.rails.live=function(_1,_2,fn){ |
||
13 | if(dojo.isIE&&_2.match(/^(on)?submit$/i)){ |
||
14 | dojox.rails.live(_1,"click",function(_3){ |
||
15 | var _4=_3.target,_5=_4.tagName.toLowerCase();
|
||
16 | if((_5=="input"||_5=="button")&&dojo.attr(_4,"type").toLowerCase()=="submit"){ |
||
17 | var _6=dojo.query(_4).closest("form"); |
||
18 | if(_6.length){
|
||
19 | var h=dojo.connect(_6[0],"submit",function(_7){ |
||
20 | dojo.disconnect(h); |
||
21 | fn.call(_7.target,_7); |
||
22 | }); |
||
23 | } |
||
24 | } |
||
25 | }); |
||
26 | }else{
|
||
27 | dojo.connect(dojo.body(),_2,function(_8){
|
||
28 | var nl=dojo.query(_8.target).closest(_1);
|
||
29 | if(nl.length){
|
||
30 | fn.call(nl[0],_8);
|
||
31 | } |
||
32 | }); |
||
33 | } |
||
34 | }; |
||
35 | dojo.ready((function(d,dr,dg){
|
||
36 | return function(){ |
||
37 | var q=d.query,_9=dr.live,_a=q("meta[name=csrf-token]").attr("content"),_b=q("meta[name=csrf-param]").attr("content"); |
||
38 | var _c=function(_d,_e){ |
||
39 | var _f="<form style=\"display:none\" method=\"post\" action=\""+_d+"\">"+"<input type=\"hidden\" name=\"_method\" value=\""+_e+"\" />"+"<input type=\"hidden\" name=\""+_b+"\" value=\""+_a+"\" />"+"</form>"; |
||
40 | return dojo.place(_f,dojo.body());
|
||
41 | }; |
||
42 | var _10=function(_11){ |
||
43 | d.forEach(_11,function(_12){
|
||
44 | if(!d.attr(_12,"disabled")){ |
||
45 | var _13=_12.tagName.toLowerCase()=="input"?"value":"innerHTML"; |
||
46 | var _14=d.attr(_12,"data-disable-with"); |
||
47 | var _15=d.attr(_12,_13);
|
||
48 | d.attr(_12,"disabled",true); |
||
49 | d.attr(_12,"data-original-value",_15);
|
||
50 | d.attr(_12,_13,_14); |
||
51 | } |
||
52 | }); |
||
53 | }; |
||
54 | var _16={"text":"text","json":"application/json","json-comment-optional":"text","json-comment-filtered":"text","javascript":"application/javascript","xml":"text/xml"}; |
||
55 | var _17=function(evt){ |
||
56 | var el=evt.target,tag=el.tagName.toLowerCase();
|
||
57 | var _18=tag.toLowerCase()=="form"?d.formToObject(el):{},_19=d.attr(el,"data-type")||"javascript",_1a=(d.attr(el,"method")||d.attr(el,"data-method")||"get").toLowerCase(),url=d.attr(el,"action")||d.attr(el,"href"); |
||
58 | if(tag!="form"&&_1a!="get"){ |
||
59 | el=_c(url,_1a); |
||
60 | _1a="POST";
|
||
61 | } |
||
62 | evt.preventDefault(); |
||
63 | d.publish("ajax:before",[el]);
|
||
64 | var _1b=d.xhr(_1a,{url:url,headers:{"Accept":_16[_19]},content:_18,handleAs:_19,load:function(_1c,_1d){ |
||
65 | d.publish("ajax:success",[el,_1c,_1d]);
|
||
66 | },error:function(_1e,_1f){ |
||
67 | d.publish("ajax:failure",[el,_1e,_1f]);
|
||
68 | },handle:function(_20,_21){ |
||
69 | d.publish("ajax:complete",[el,_20,_21]);
|
||
70 | }}); |
||
71 | d.publish("ajax:after",[el]);
|
||
72 | }; |
||
73 | var _22=function(el){ |
||
74 | q("*[data-disable-with][disabled]",el).forEach(function(_23){ |
||
75 | var _24=_23.tagName.toLowerCase()=="input"?"value":"innerHTML"; |
||
76 | var _25=d.attr(_23,"data-original-value"); |
||
77 | d.attr(_23,"disabled",false); |
||
78 | d.attr(_23,"data-original-value",null); |
||
79 | d.attr(_23,_24,_25); |
||
80 | }); |
||
81 | }; |
||
82 | var _26=function(evt){ |
||
83 | var el=evt.target,_27=_c(el.href,dojo.attr(el,"data-method")); |
||
84 | evt.preventDefault(); |
||
85 | _27.submit(); |
||
86 | }; |
||
87 | var _28=function(evt){ |
||
88 | var el=evt.target,_29=q("*[data-disable-with]",el); |
||
89 | if(_29.length){
|
||
90 | _10(_29); |
||
91 | } |
||
92 | if(d.attr(el,"data-remote")){ |
||
93 | evt.preventDefault(); |
||
94 | _17(evt); |
||
95 | } |
||
96 | }; |
||
97 | var _2a=function(evt){ |
||
98 | var _2b=dg.confirm(d.attr(evt.target,"data-confirm")); |
||
99 | if(!_2b){
|
||
100 | evt.preventDefault(); |
||
101 | }else{
|
||
102 | if(d.attr(evt.target,"data-remote")){ |
||
103 | _17(evt); |
||
104 | } |
||
105 | } |
||
106 | }; |
||
107 | _9("*[data-confirm]","click",_2a); |
||
108 | d.subscribe("ajax:complete",_22);
|
||
109 | _9("a[data-remote]:not([data-confirm])","click",_17); |
||
110 | _9("a[data-method]:not([data-remote])","click",_26); |
||
111 | _9("form","submit",_28); |
||
112 | }; |
||
113 | })(dojo,dojox.rails,dojo.global)); |
||
114 | } |