1 | //+----------------------------------------------------------------------------- |
---|
2 | //+ Isidorus |
---|
3 | //+ (c) 2008-2009 Marc Kuester, Christoph Ludwig, Lukas Giessmann |
---|
4 | //+ |
---|
5 | //+ Isidorus is freely distributable under the LGPL license. |
---|
6 | //+ This ajax module uses the frameworks PrototypeJs and Scriptaculous, both |
---|
7 | //+ are distributed under the MIT license. |
---|
8 | //+ You can find a detailed description in trunk/docs/LGPL-LICENSE.txt and |
---|
9 | //+ in trunk/src/ajax/javascripts/external/MIT-LICENSE.txt. |
---|
10 | //+----------------------------------------------------------------------------- |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | // --- Sets a timeout function which alerts a message. |
---|
15 | function setAjaxTimeout(time, url) |
---|
16 | { |
---|
17 | return setTimeout(function(){ |
---|
18 | alert("The AJAX request for \"" + url + "\" timed out. Please check your network connection!"); |
---|
19 | hideLoad(); |
---|
20 | }, time); |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | // --- Returns a function whihc can be used as an XHR-Handler. |
---|
25 | // --- The returned function is the passed handler wrapped in |
---|
26 | // --- a lambda-function which additionally clears the passed timeout |
---|
27 | // --- function and call onLoad. |
---|
28 | function createXHRHandler(handler, timeFun) |
---|
29 | { |
---|
30 | function fun(xhr){ |
---|
31 | clearTimeout(timeFun); |
---|
32 | hideLoad(); |
---|
33 | handler(xhr); |
---|
34 | } |
---|
35 | return fun; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | // --- Removes all divs with the class ajaxLoader. The inner image with the |
---|
40 | // --- class ajaxLoader will be moved to the top of div.content and the |
---|
41 | // --- display attribute will be set to none; |
---|
42 | function hideLoad() |
---|
43 | { |
---|
44 | var img = $$("img." + CLASSES.ajaxLoader()); |
---|
45 | if(img.length === 1){ |
---|
46 | img[0].setStyle({"display" : "none"}) |
---|
47 | $("page").insert({"top" : img[0]}); |
---|
48 | } |
---|
49 | |
---|
50 | var loading = $$("div." + CLASSES.load()); |
---|
51 | if(loading.length === 1) loading[0].remove(); |
---|
52 | var content = $$("div." + CLASSES.content()); |
---|
53 | if(content.length === 1) content[0].show(); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | // --- The hidden image with the class ajaxLoader will be moved to the new created |
---|
58 | // --- div with the given message. The div with the class content will be hidden and instaed |
---|
59 | // --- of the hidden div there will be shown the new created element. |
---|
60 | function onLoad(text) |
---|
61 | { |
---|
62 | var div = new Element("div", {"class" : CLASSES.load()}).update(content); |
---|
63 | var content = $$("div." + CLASSES.content()); |
---|
64 | if(content.length === 1){ |
---|
65 | content[0].hide(); |
---|
66 | var load = new Element("div", {"class" : CLASSES.load()}).update("<br/><br/>" + text); |
---|
67 | content[0].insert({"before" : load}); |
---|
68 | var img = $$("img." + CLASSES.ajaxLoader()); |
---|
69 | if(img.length === 1){ |
---|
70 | img[0].setStyle({"display" : "block"}) |
---|
71 | load.insert({"top" : img[0]}) |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | // --- This is the default error handler of the used ajax.requests. |
---|
78 | function defaultFailureHandler(xhr) |
---|
79 | { |
---|
80 | window.alert("Something went wrong by calling \"" + xhr.request.url + "\"\n" + xhr.status + |
---|
81 | ": " + xhr.statusText + "\n" + xhr.responseText); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | // --- Gets all psis from the server. If typePsis is set to true |
---|
86 | // --- there will be requested only TopicType's psis. |
---|
87 | function getPsis(onSuccessHandler, onFailureHandler, what) |
---|
88 | { |
---|
89 | try{ |
---|
90 | var onFailure = onFailureHandler ? onFailureHandler : defaultFailureHandler; |
---|
91 | var timeFun = setAjaxTimeout(TIMEOUT, TYPE_PSIS_URL); |
---|
92 | |
---|
93 | var url = ALL_PSIS_URL; |
---|
94 | var message = "Requesting all type PSIs"; |
---|
95 | if(what && what.types && what.types === true) url = TYPE_PSIS_URL; |
---|
96 | else if(what && what.instances && what.instances === true){ |
---|
97 | url = INSTANCE_PSIS_URL; |
---|
98 | message = "Requesting all instance PSIs"; |
---|
99 | } |
---|
100 | else if(what && what.all && what.all === true){ |
---|
101 | url = ALL_PSIS_URL; |
---|
102 | message = "Requesting all PSIs"; |
---|
103 | } |
---|
104 | |
---|
105 | onLoad(message); |
---|
106 | |
---|
107 | new Ajax.Request(url, { |
---|
108 | "method" : "get", |
---|
109 | "requestHeaders" : INIT_DATE, |
---|
110 | "onSuccess" : createXHRHandler(onSuccessHandler, timeFun), |
---|
111 | "onFailure" : createXHRHandler(onFailure, timeFun)}); |
---|
112 | } |
---|
113 | catch(err){ |
---|
114 | alert("From getTypePsis(): could not request all type PSIs, please try again!\n\n" + err); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | // --- Sends a post-request to the server with the passed psis as postBody. |
---|
120 | // --- Gets a constraint-object. |
---|
121 | function requestConstraints(psis, onSuccessHandler, onFailureHandler, typeConstraints) |
---|
122 | { |
---|
123 | try{ |
---|
124 | var onFailure = onFailureHandler ? onFailureHandler : defaultFailureHandler; |
---|
125 | var timeFun = setAjaxTimeout(TIMEOUT, TMCL_TYPE_URL); |
---|
126 | onLoad("Requesting all constraints for the psis:\<br/>" + psis.gsub("\\[", "").gsub("\\]", "")); |
---|
127 | |
---|
128 | url = TMCL_INSTANCE_URL; |
---|
129 | if(typeConstraints === true) url = TMCL_TYPE_URL; |
---|
130 | |
---|
131 | new Ajax.Request(url, { |
---|
132 | "method" : "post", |
---|
133 | "postBody" : psis, |
---|
134 | "onSuccess" : createXHRHandler(onSuccessHandler, timeFun), |
---|
135 | "onFailure" : createXHRHandler(onFailure, timeFun)}); |
---|
136 | } |
---|
137 | catch(err){ |
---|
138 | alert("Could not request contraints, please try again!\n\n" + err); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | // --- gets all topicStubs information for the passed psis and |
---|
144 | // --- executes the onSuccessHandler or the on FailureHandler |
---|
145 | // --- if all stubs are requested or one request fails. |
---|
146 | function getTopicStubs(psis, onSuccessHandler, onFailureHandler) |
---|
147 | { |
---|
148 | if(!onSuccessHandler || !onFailureHandler) throw "From getTopicStubs(): onsuccessHandler and onFailureHandler must be set!"; |
---|
149 | try{ |
---|
150 | var topicStubs = new Array(); |
---|
151 | |
---|
152 | if(psis && psis.length !== 0){ |
---|
153 | onLoad("Requesting topicStubs information for<br/>" + psis); |
---|
154 | for(var i = 0; i !== psis.length; ++i){ |
---|
155 | var url = GET_STUB_PREFIX + psis[i].gsub("#", "%23"); |
---|
156 | new Ajax.Request(url, { |
---|
157 | "method" : "get", |
---|
158 | "requestHeaders" : INIT_DATE, |
---|
159 | "onSuccess" : function(xhr){ |
---|
160 | if(xhr.responseText.length === 0 || xhr.responseText.isJSON() === false) errorHandler("Got bad JSON-Data for \"" + psis[i] + "\"!"); |
---|
161 | else topicStubs.push(xhr.responseText); |
---|
162 | }, |
---|
163 | "onFailure" : function(xhr){ |
---|
164 | alert("From getTopicStubs(): Could not request topicStub information for \"" + xhr.request.url + "\"!!!"); |
---|
165 | onFailureHandler(); |
---|
166 | }}); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | // --- Checks the requested value. If there are all values requested, there will be called the |
---|
171 | // --- onSuccessHandler - otherwise (after the maximum time out or an faild request) there will |
---|
172 | // --- be called the onErrorHandler. |
---|
173 | var maxTimeout = psis.length * TIMEOUT; |
---|
174 | var neededTime = 0; |
---|
175 | function checkRequests(){ |
---|
176 | var delta = 100; |
---|
177 | neededTime += delta; |
---|
178 | if(delta > maxTimeout && psis && psis.length !== 0){ |
---|
179 | alert("From getTopicStubs(): Please check your network-connection - the request timed out!!!"); |
---|
180 | hideLoad(); |
---|
181 | onFailureHandler(); |
---|
182 | return; |
---|
183 | } |
---|
184 | |
---|
185 | if(topicStubs.length === psis.length){ |
---|
186 | hideLoad(); |
---|
187 | onSuccessHandler(topicStubs); |
---|
188 | |
---|
189 | } |
---|
190 | else setTimeout(checkRequests, delta); |
---|
191 | } |
---|
192 | |
---|
193 | checkRequests(); |
---|
194 | } |
---|
195 | catch(err){ |
---|
196 | alert("From getTopicStubs(): Could not request topicStubs information for: " + psis + "\n\n" + err); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | // --- Sends a POST-Message to the server with the fragment data which hast to be committed. |
---|
202 | function commitFragment(json, onSuccessHandler, onFailureHandler) |
---|
203 | { |
---|
204 | if(!json || !onSuccessHandler) throw "From commitFragment(): json and onSuccessHandler must be set!"; |
---|
205 | try{ |
---|
206 | var onFailure = onFailureHandler ? onFailureHandler : defaultFailureHandler; |
---|
207 | var timeFun = setAjaxTimeout(TIMEOUT, COMMIT_URL); |
---|
208 | onLoad("Committing current fragment to " + COMMIT_URL); |
---|
209 | |
---|
210 | new Ajax.Request(COMMIT_URL, { |
---|
211 | "method" : "post", |
---|
212 | "postBody" : json, |
---|
213 | "onSuccess" : createXHRHandler(onSuccessHandler, timeFun), |
---|
214 | "onFailure" : createXHRHandler(onFailure, timeFun)}); |
---|
215 | } |
---|
216 | catch(err){ |
---|
217 | alert("From commitFragment(): " + err); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | // --- Requests a JSON-Fragment for the passed psi and calls the onSuccessHandler function |
---|
223 | // --- after a succeeded request. |
---|
224 | function requestFragment(psi, onSuccessHandler, onFailureHandler) |
---|
225 | { |
---|
226 | if(!psi || !onSuccessHandler) throw "From requestFragment(): psi and onSuccessHandler must be set!"; |
---|
227 | |
---|
228 | try{ |
---|
229 | var onFailure = onFailureHandler ? onFailureHandler : defaultFailureHandler; |
---|
230 | var timeFun = setAjaxTimeout(TIMEOUT, COMMIT_URL); |
---|
231 | onLoad("Requesting fragment for the topic " + psi); |
---|
232 | |
---|
233 | var url = GET_PREFIX + psi.gsub("#", "%23"); |
---|
234 | |
---|
235 | new Ajax.Request(url, { |
---|
236 | "method" : "get", |
---|
237 | "requestHeaders" : INIT_DATE, |
---|
238 | "onSuccess" : createXHRHandler(onSuccessHandler, timeFun), |
---|
239 | "onFailure" : createXHRHandler(onFailure, timeFun)}); |
---|
240 | } |
---|
241 | catch(err){ |
---|
242 | alert("From requestFragment(): " + err); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | // --- Request a topic map overview object from the server and calls |
---|
248 | // --- onSuccessHandler or OnFailureHandler. |
---|
249 | function requestTreeView(onSuccessHandler, onFailureHandler) |
---|
250 | { |
---|
251 | if(!onSuccessHandler) throw "From requestTreeView(): onSuccessHandler must be set!"; |
---|
252 | |
---|
253 | try{ |
---|
254 | var onFailure = onFailureHandler ? onFailureHandler : defaultFailureHandler; |
---|
255 | var timeFun = setAjaxTimeout(6 * TIMEOUT, COMMIT_URL); |
---|
256 | onLoad("Requesting a topic map overview from " + TM_OVERVIEW); |
---|
257 | |
---|
258 | new Ajax.Request(TM_OVERVIEW, { |
---|
259 | "method" : "get", |
---|
260 | "requestHeaders" : INIT_DATE, |
---|
261 | "onSuccess" : createXHRHandler(onSuccessHandler, timeFun), |
---|
262 | "onFailure" : createXHRHandler(onFailure, timeFun)}); |
---|
263 | } |
---|
264 | catch(err){ |
---|
265 | alert("From requestFragment(): " + err); |
---|
266 | } |
---|
267 | } |
---|