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