source: branches/gdl-frontend/src/ajax/javascripts/requests.js

Last change on this file was 427, checked in by lgiessmann, 14 years ago

JSON-Interface: all / that are not escaped will be escaped after calling prototypes toJSON method, because prototype does not escape /; if no topics for a player-constraint or other-player-constraint exist there is no error message thrown, instead the constraint is ignored as long as there are to few topics; the backend now escapes all /, too

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