Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <cfsilent> |
2 | |||
3 | <cfset sajax_version = "0.13"> |
||
4 | <cfset sajax_debug_mode = false> |
||
5 | <cfset sajax_export_array = arraynew(1)> |
||
6 | <cfset sajax_export_list = ""> |
||
7 | <cfset sajax_method_list = ""> |
||
8 | <cfset sajax_asynchronous_list = ""> |
||
9 | <cfset sajax_uri_list = ""> |
||
10 | <cfset sajax_remote_uri = ""> |
||
11 | <cfset sajax_failure_redirect = ""> |
||
12 | <cfset sajax_request_type = "GET"> |
||
13 | <cfset sajax_request_asynchronous = "true"> |
||
14 | |||
15 | <!--- Always call server if this is a Sajax GET call ---> |
||
16 | <cfif isdefined("URL.rs")> |
||
17 | <cfheader name="Cache-Control" value="max-age=0, must-revalidate"> |
||
18 | <cfheader name="Pragma" value="no-cache"> |
||
19 | </cfif> |
||
20 | |||
21 | <!--- emulate the php exit keyword ---> |
||
22 | <cffunction name="exit"> |
||
23 | <cfabort> |
||
24 | </cffunction> |
||
25 | |||
26 | <cffunction name="sajax_handle_client_request"> |
||
27 | <cftry> |
||
28 | <cfsavecontent variable="output"> |
||
29 | <cfscript> |
||
30 | if(not isdefined("URL.rs") and not isdefined("FORM.rs")) |
||
31 | return; |
||
32 | if(isdefined("URL.rs")) { |
||
33 | func_name = URL.rs; |
||
34 | if(isdefined("URL.rsargs")) |
||
35 | args = URL.rsargs; |
||
36 | } else { |
||
37 | func_name = FORM.rs; |
||
38 | if(isdefined("FORM.rsargs")) |
||
39 | args = FORM.rsargs; |
||
40 | } |
||
41 | |||
42 | if(isdefined("args")) { |
||
43 | args = DeserializeJSON(args); |
||
44 | } else { |
||
45 | args = arraynew(1); |
||
46 | } |
||
47 | if(not ListFindNoCase(sajax_export_list, func_name)) { |
||
48 | error = "#func_name# not callable"; |
||
49 | } else { |
||
50 | result = SerializeJSON(call_user_func_array(func_name, args)); |
||
51 | } |
||
52 | </cfscript> |
||
53 | </cfsavecontent> |
||
54 | <cfcatch> |
||
55 | <cfheader name="Content-Type" value="text/plain; charset=UTF-8"> |
||
56 | <cfoutput>-:#cfcatch.type# |
||
57 | #cfcatch.message# |
||
58 | #cfcatch.detail# |
||
59 | <cfif arraylen(cfcatch.tagcontext) gt 0> |
||
60 | in #cfcatch.tagcontext[1].template# at #cfcatch.tagcontext[1].line# |
||
61 | </cfif> |
||
62 | </cfoutput> |
||
63 | <cfabort> |
||
64 | </cfcatch> |
||
65 | </cftry> |
||
66 | <cfheader name="Content-Type" value="text/plain; charset=UTF-8"> |
||
67 | <cfscript> |
||
68 | //Remove start and end white space from output |
||
69 | output = REReplace(output,"^\s*|\s*$","","ALL"); |
||
70 | if(isdefined("result") and "#output#" eq "") |
||
71 | writeoutput("+:#result#"); |
||
72 | else if("#output#" eq "") |
||
73 | writeoutput("-:#error#"); |
||
74 | else |
||
75 | writeoutput("-:#output#"); |
||
76 | exit(); |
||
77 | </cfscript> |
||
78 | </cffunction> |
||
79 | |||
80 | <cfscript> |
||
81 | sajax_js_has_been_shown = false; |
||
82 | function sajax_show_javascript() |
||
83 | { |
||
84 | |||
85 | if (not sajax_js_has_been_shown) { |
||
86 | |||
87 | writeoutput(" |
||
88 | sajax_debug_mode = #iif(sajax_debug_mode, 'true', 'false')#; |
||
89 | sajax_failure_redirect = ""#sajax_failure_redirect#""; |
||
90 | "); |
||
91 | //TODO get from an array instead of list |
||
92 | size = listlen(sajax_export_list); |
||
93 | for(i = 1; i lte size; i=i+1) { |
||
94 | name = listgetat(sajax_export_list, i); |
||
95 | method = listgetat(sajax_method_list, i); |
||
96 | asynchronous = listgetat(sajax_asynchronous_list, i); |
||
97 | uri = listgetat(sajax_uri_list, i); |
||
98 | if(uri == "##") |
||
99 | uri = ""; |
||
100 | writeoutput(" |
||
101 | function x_#name#() { |
||
102 | return sajax_do_call(""#name#"", arguments, ""#method#"", #asynchronous#, ""#uri#""); |
||
103 | }"); |
||
104 | } |
||
105 | sajax_js_has_been_shown = true; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | function sajax_export() { |
||
110 | //TODO make it a multi dimentional array of options |
||
111 | //TODO prevent multiple instances of the same functions |
||
112 | //if(not isarray(function)) |
||
113 | var keys = structkeylist(arguments); |
||
114 | var size = listlen(keys); |
||
115 | var key = ""; |
||
116 | |||
117 | for(i=1; i lte size;i=i+1) { |
||
118 | key = listgetat(keys, i); |
||
119 | sajax_export_list = listappend(sajax_export_list, arguments[key]); |
||
120 | sajax_method_list = listappend(sajax_method_list, sajax_request_type); |
||
121 | sajax_asynchronous_list = listappend(sajax_asynchronous_list, sajax_request_asynchronous); |
||
122 | if(sajax_remote_uri == "") |
||
123 | sajax_remote_uri = "##"; |
||
124 | sajax_uri_list = listappend(sajax_uri_list, sajax_remote_uri); |
||
125 | if(sajax_remote_uri == "##") |
||
126 | sajax_remote_uri = ""; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | function call_user_func_array(user_func, arg_array) { |
||
131 | //TODO test with multi dimentional arrays |
||
132 | var func_call = ""; |
||
133 | var func_args = ""; |
||
134 | var size = ArrayLen(arg_array); |
||
135 | |||
136 | /* Loop though each of the args */ |
||
137 | for(i=1; i lte size; i = i + 1) { |
||
138 | func_args = func_args & "arg_array[#i#]"; |
||
139 | if(i lt size) |
||
140 | func_args = func_args & ","; |
||
141 | } |
||
142 | |||
143 | return evaluate("#user_func#(#func_args#)"); |
||
144 | } |
||
145 | |||
146 | SAJAX_INCLUDED = 1; |
||
147 | </cfscript> |
||
148 | </cfsilent> |