author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 11:22:31 +0200 | |
branch | tls-sprint |
changeset 1498 | 2c6eec0b46b9 |
parent 1419 | 7ff24154351d |
child 1512 | c51ca5f49a78 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* :organization: Logilab |
|
1419 | 3 |
* :copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 4 |
* :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 |
*/ |
|
6 |
||
7 |
CubicWeb.require('python.js'); |
|
8 |
CubicWeb.require('htmlhelpers.js'); |
|
9 |
||
10 |
var JSON_BASE_URL = baseuri() + 'json?'; |
|
11 |
||
642
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
12 |
/* |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
13 |
* inspect dom response, search for a <div class="ajaxHtmlHead"> node and |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
14 |
* put its content into the real document's head. |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
15 |
* This enables dynamic css and js loading and is used by replacePageChunk |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
16 |
*/ |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
17 |
function loadAjaxHtmlHead(node) { |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
18 |
jQuery(node).find('div.ajaxHtmlHead').appendTo(jQuery('head')); |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
19 |
} |
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
20 |
|
1407
75863d3ffd9b
cleanup (undue req param)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1320
diff
changeset
|
21 |
function postAjaxLoad(node) { |
642
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
22 |
loadAjaxHtmlHead(node); |
540
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
23 |
// find sortable tables if there are some |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
24 |
if (typeof(Sortable) != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
25 |
Sortable.sortTables(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
26 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
27 |
// find textareas and wrap them if there are some |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
28 |
if (typeof(FCKeditor) != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
29 |
buildWysiwygEditors(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
30 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
31 |
if (typeof initFacetBoxEvents != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
32 |
initFacetBoxEvents(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
33 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
34 |
if (typeof buildWidgets != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
35 |
buildWidgets(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
36 |
} |
955 | 37 |
if (typeof roundedCornersOnLoad != 'undefined') { |
38 |
roundedCornersOnLoad(); |
|
39 |
} |
|
1419 | 40 |
jQuery(CubicWeb).trigger('ajax-loaded'); |
540
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
41 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
42 |
|
0 | 43 |
// cubicweb loadxhtml plugin to make jquery handle xhtml response |
44 |
jQuery.fn.loadxhtml = function(url, data, reqtype, mode) { |
|
45 |
var ajax = null; |
|
46 |
if (reqtype == 'post') { |
|
47 |
ajax = jQuery.post; |
|
48 |
} else { |
|
49 |
ajax = jQuery.get; |
|
50 |
} |
|
51 |
if (this.size() > 1) { |
|
52 |
log('loadxhtml was called with more than one element'); |
|
53 |
} |
|
642
2cf7b79d8e77
[javascript] rewrote loadxhtml() plugin in order to be able to pass the XHR object to postAjaxLoad
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
540
diff
changeset
|
54 |
var node = this.get(0); // only consider the first element |
0 | 55 |
mode = mode || 'replace'; |
56 |
var callback = null; |
|
57 |
if (data && data.callback) { |
|
58 |
callback = data.callback; |
|
59 |
delete data.callback; |
|
60 |
} |
|
61 |
ajax(url, data, function(response) { |
|
62 |
var domnode = getDomFromResponse(response); |
|
63 |
if (mode == 'swap') { |
|
64 |
var origId = node.id; |
|
65 |
node = swapDOM(node, domnode); |
|
66 |
if (!node.id) { |
|
67 |
node.id = origId; |
|
68 |
} |
|
69 |
} else if (mode == 'replace') { |
|
70 |
jQuery(node).empty().append(domnode); |
|
71 |
} else if (mode == 'append') { |
|
72 |
jQuery(node).append(domnode); |
|
73 |
} |
|
1407
75863d3ffd9b
cleanup (undue req param)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1320
diff
changeset
|
74 |
postAjaxLoad(node); |
0 | 75 |
while (jQuery.isFunction(callback)) { |
76 |
callback = callback.apply(this, [domnode]); |
|
77 |
} |
|
78 |
}); |
|
540
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
79 |
}; |
0 | 80 |
|
81 |
||
82 |
||
83 |
/* finds each dynamic fragment in the page and executes the |
|
84 |
* the associated RQL to build them (Async call) |
|
85 |
*/ |
|
86 |
function loadDynamicFragments() { |
|
492
cd792cfda071
[javascript] minor lints
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
227
diff
changeset
|
87 |
var fragments = jQuery('div.dynamicFragment'); |
0 | 88 |
if (fragments.length == 0) { |
89 |
return; |
|
90 |
} |
|
91 |
if (typeof LOADING_MSG == 'undefined') { |
|
92 |
LOADING_MSG = 'loading'; // this is only a safety belt, it should not happen |
|
93 |
} |
|
94 |
for(var i=0; i<fragments.length; i++) { |
|
95 |
var fragment = fragments[i]; |
|
96 |
fragment.innerHTML = '<h3>' + LOADING_MSG + ' ... <img src="data/loading.gif" /></h3>'; |
|
97 |
var rql = getNodeAttribute(fragment, 'cubicweb:rql'); |
|
98 |
var vid = getNodeAttribute(fragment, 'cubicweb:vid'); |
|
99 |
var extraparams = {}; |
|
100 |
var actrql = getNodeAttribute(fragment, 'cubicweb:actualrql'); |
|
101 |
if (actrql) { extraparams['actualrql'] = actrql; } |
|
102 |
var fbvid = getNodeAttribute(fragment, 'cubicweb:fallbackvid'); |
|
103 |
if (fbvid) { extraparams['fallbackvid'] = fbvid; } |
|
104 |
||
105 |
replacePageChunk(fragment.id, rql, vid, extraparams); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
jQuery(document).ready(loadDynamicFragments); |
|
110 |
||
111 |
//============= base AJAX functions to make remote calls =====================// |
|
112 |
||
113 |
function remoteCallFailed(err, req) { |
|
114 |
if (req.status == 500) { |
|
115 |
updateMessage(err); |
|
116 |
} else { |
|
117 |
updateMessage(_("an error occured while processing your request")); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/* |
|
122 |
* This function is the equivalent of MochiKit's loadJSONDoc but |
|
123 |
* uses POST instead of GET |
|
124 |
*/ |
|
1419 | 125 |
function loadJSONDocUsingPOST(url, data) { |
0 | 126 |
setProgressCursor(); |
1419 | 127 |
var deferred = loadJSON(url, data, 'POST'); |
0 | 128 |
deferred = deferred.addErrback(remoteCallFailed); |
129 |
deferred = deferred.addCallback(resetCursor); |
|
130 |
return deferred; |
|
131 |
} |
|
132 |
||
133 |
||
134 |
/* |
|
1419 | 135 |
* This function will call **synchronously** a remote method on the cubicweb server |
136 |
* @param fname: the function name to call (as exposed by the JSONController) |
|
137 |
* |
|
138 |
* additional arguments will be directly passed to the specified function |
|
0 | 139 |
* |
1419 | 140 |
* It looks at http headers to guess the response type. |
0 | 141 |
*/ |
1419 | 142 |
function remoteExec(fname /* ... */) { |
143 |
setProgressCursor(); |
|
144 |
var props = {'fname' : fname, 'pageid' : pageid, |
|
145 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
146 |
var result = jQuery.ajax({url: JSON_BASE_URL, data: props, async: false}).responseText; |
|
147 |
if (result) { |
|
148 |
result = evalJSON(result); |
|
149 |
} |
|
150 |
resetCursor(); |
|
151 |
return result; |
|
0 | 152 |
} |
153 |
||
154 |
/* |
|
1419 | 155 |
* This function will call **asynchronously** a remote method on the json |
156 |
* controller of the cubicweb http server |
|
157 |
* |
|
0 | 158 |
* @param fname: the function name to call (as exposed by the JSONController) |
1419 | 159 |
* |
0 | 160 |
* additional arguments will be directly passed to the specified function |
1419 | 161 |
* |
0 | 162 |
* It looks at http headers to guess the response type. |
163 |
*/ |
|
1419 | 164 |
function asyncRemoteExec(fname /* ... */) { |
165 |
var props = {'fname' : fname, 'pageid' : pageid, |
|
166 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
167 |
return loadJSONDocUsingPOST(JSON_BASE_URL, props); |
|
0 | 168 |
} |
169 |
||
170 |
||
171 |
/* emulation of gettext's _ shortcut |
|
172 |
*/ |
|
173 |
function _(message) { |
|
1419 | 174 |
return remoteExec('i18n', [message])[0]; |
0 | 175 |
} |
176 |
||
177 |
function userCallback(cbname) { |
|
1419 | 178 |
asyncRemoteExec('user_callback', cbname); |
0 | 179 |
} |
180 |
||
181 |
function unloadPageData() { |
|
182 |
// NOTE: do not make async calls on unload if you want to avoid |
|
183 |
// strange bugs |
|
1419 | 184 |
remoteExec('unload_page_data'); |
0 | 185 |
} |
186 |
||
187 |
function openHash() { |
|
188 |
if (document.location.hash) { |
|
189 |
var nid = document.location.hash.replace('#', ''); |
|
190 |
var node = jQuery('#' + nid); |
|
191 |
if (node) { removeElementClass(node, "hidden"); } |
|
192 |
}; |
|
193 |
} |
|
194 |
jQuery(document).ready(openHash); |
|
195 |
||
196 |
function reloadComponent(compid, rql, registry, nodeid, extraargs) { |
|
197 |
registry = registry || 'components'; |
|
198 |
rql = rql || ''; |
|
199 |
nodeid = nodeid || (compid + 'Component'); |
|
200 |
extraargs = extraargs || {}; |
|
201 |
var node = getNode(nodeid); |
|
1419 | 202 |
var d = asyncRemoteExec('component', compid, rql, registry, extraargs); |
0 | 203 |
d.addCallback(function(result, req) { |
204 |
var domnode = getDomFromResponse(result); |
|
205 |
if (node) { |
|
206 |
// make sure the component is visible |
|
207 |
removeElementClass(node, "hidden"); |
|
208 |
swapDOM(node, domnode); |
|
209 |
} |
|
210 |
}); |
|
211 |
d.addCallback(resetCursor); |
|
212 |
d.addErrback(function(xxx) { |
|
213 |
updateMessage(_("an error occured")); |
|
214 |
log(xxx); |
|
215 |
}); |
|
216 |
return d; |
|
217 |
} |
|
218 |
||
219 |
/* XXX: HTML architecture of cubicweb boxes is a bit strange */ |
|
220 |
function reloadBox(boxid, rql) { |
|
225
b90904dc3069
[javascript] reloadBox: return deferred for further callback chaining
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
12
diff
changeset
|
221 |
return reloadComponent(boxid, rql, 'boxes', boxid); |
0 | 222 |
} |
223 |
||
224 |
function userCallbackThenUpdateUI(cbname, compid, rql, msg, registry, nodeid) { |
|
1419 | 225 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 226 |
d.addCallback(function() { |
227 |
reloadComponent(compid, rql, registry, nodeid); |
|
228 |
if (msg) { updateMessage(msg); } |
|
229 |
}); |
|
230 |
d.addCallback(resetCursor); |
|
231 |
d.addErrback(function(xxx) { |
|
232 |
updateMessage(_("an error occured")); |
|
233 |
log(xxx); |
|
234 |
return resetCursor(); |
|
235 |
}); |
|
236 |
} |
|
237 |
||
238 |
function userCallbackThenReloadPage(cbname, msg) { |
|
1419 | 239 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 240 |
d.addCallback(function() { |
241 |
window.location.reload(); |
|
242 |
if (msg) { updateMessage(msg); } |
|
243 |
}); |
|
244 |
d.addCallback(resetCursor); |
|
245 |
d.addErrback(function(xxx) { |
|
246 |
updateMessage(_("an error occured")); |
|
247 |
log(xxx); |
|
248 |
return resetCursor(); |
|
249 |
}); |
|
250 |
} |
|
251 |
||
252 |
/* |
|
253 |
* unregisters the python function registered on the server's side |
|
254 |
* while the page was generated. |
|
255 |
*/ |
|
256 |
function unregisterUserCallback(cbname) { |
|
1419 | 257 |
var d = asyncRemoteExec('unregister_user_callback', cbname); |
0 | 258 |
d.addCallback(function() {resetCursor();}); |
259 |
d.addErrback(function(xxx) { |
|
260 |
updateMessage(_("an error occured")); |
|
261 |
log(xxx); |
|
262 |
return resetCursor(); |
|
263 |
}); |
|
264 |
} |
|
265 |
||
266 |
||
267 |
/* executes an async query to the server and replaces a node's |
|
268 |
* content with the query result |
|
269 |
* |
|
270 |
* @param nodeId the placeholder node's id |
|
271 |
* @param rql the RQL query |
|
272 |
* @param vid the vid to apply to the RQL selection (default if not specified) |
|
273 |
* @param extraparmas table of additional query parameters |
|
274 |
*/ |
|
275 |
function replacePageChunk(nodeId, rql, vid, extraparams, /* ... */ swap, callback) { |
|
276 |
var params = null; |
|
277 |
if (callback) { |
|
278 |
params = {callback: callback}; |
|
279 |
} |
|
280 |
||
281 |
var node = jQuery('#' + nodeId)[0]; |
|
282 |
var props = {}; |
|
283 |
if (node) { |
|
284 |
props['rql'] = rql; |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1419
diff
changeset
|
285 |
props['fname'] = 'view'; |
0 | 286 |
props['pageid'] = pageid; |
287 |
if (vid) { props['vid'] = vid; } |
|
288 |
if (extraparams) { jQuery.extend(props, extraparams); } |
|
1419 | 289 |
// FIXME we need to do asURL(props) manually instead of |
0 | 290 |
// passing `props` directly to loadxml because replacePageChunk |
291 |
// is sometimes called (abusively) with some extra parameters in `vid` |
|
292 |
var mode = swap?'swap':'replace'; |
|
1419 | 293 |
var url = JSON_BASE_URL + asURL(props); |
0 | 294 |
jQuery(node).loadxhtml(url, params, 'get', mode); |
295 |
} else { |
|
296 |
log('Node', nodeId, 'not found'); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
/* XXX: this function should go in edition.js but as for now, htmlReplace |
|
301 |
* references it. |
|
302 |
* |
|
303 |
* replace all textareas with fckeditors. |
|
304 |
*/ |
|
305 |
function buildWysiwygEditors(parent) { |
|
306 |
jQuery('textarea').each(function () { |
|
307 |
if (this.getAttribute('cubicweb:type', 'wysiwyg')) { |
|
308 |
if (typeof FCKeditor != "undefined") { |
|
309 |
var fck = new FCKeditor(this.id); |
|
310 |
fck.Config['CustomConfigurationsPath'] = fckconfigpath; |
|
311 |
fck.Config['DefaultLanguage'] = fcklang; |
|
312 |
fck.BasePath = "fckeditor/"; |
|
313 |
fck.ReplaceTextarea(); |
|
314 |
} else { |
|
315 |
log('fckeditor could not be found.'); |
|
316 |
} |
|
317 |
} |
|
318 |
}); |
|
319 |
} |
|
320 |
||
321 |
jQuery(document).ready(buildWysiwygEditors); |
|
322 |
||
323 |
||
1419 | 324 |
/* |
325 |
* takes a list of DOM nodes and removes all empty text nodes |
|
326 |
*/ |
|
327 |
function stripEmptyTextNodes(nodelist) { |
|
328 |
var stripped = []; |
|
329 |
for (var i=0; i < nodelist.length; i++) { |
|
330 |
var node = nodelist[i]; |
|
331 |
if (isTextNode(node) && !node.textContent.strip()) { |
|
332 |
continue; |
|
333 |
} else { |
|
334 |
stripped.push(node); |
|
335 |
} |
|
336 |
} |
|
337 |
return stripped; |
|
338 |
} |
|
339 |
||
0 | 340 |
/* convenience function that returns a DOM node based on req's result. */ |
341 |
function getDomFromResponse(response) { |
|
342 |
if (typeof(response) == 'string') { |
|
343 |
return html2dom(response); |
|
344 |
} |
|
345 |
var doc = response.documentElement; |
|
346 |
var children = doc.childNodes; |
|
347 |
if (!children.length) { |
|
348 |
// no child (error cases) => return the whole document |
|
349 |
return doc.cloneNode(true); |
|
350 |
} |
|
1419 | 351 |
children = stripEmptyTextNodes(children); |
0 | 352 |
if (children.length == 1) { |
353 |
// only one child => return it |
|
354 |
return children[0].cloneNode(true); |
|
355 |
} |
|
356 |
// several children => wrap them in a single node and return the wrap |
|
357 |
return DIV(null, map(methodcaller('cloneNode', true), children)); |
|
358 |
} |
|
359 |
||
360 |
function postJSON(url, data, callback) { |
|
361 |
return jQuery.post(url, data, callback, 'json'); |
|
362 |
} |
|
363 |
||
364 |
function getJSON(url, data, callback){ |
|
365 |
return jQuery.get(url, data, callback, 'json'); |
|
366 |
} |
|
367 |
||
368 |
CubicWeb.provide('ajax.js'); |