author | sylvain.thenault@logilab.fr |
Tue, 28 Apr 2009 17:39:12 +0200 | |
branch | tls-sprint |
changeset 1512 | c51ca5f49a78 |
parent 1498 | 2c6eec0b46b9 |
child 1563 | b130c6cec8c2 |
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'); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
32 |
var node = jQuery(node).find('div.ajaxHtmlHead'); |
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 |
* This function is the equivalent of MochiKit's loadJSONDoc but |
|
149 |
* uses POST instead of GET |
|
150 |
*/ |
|
1419 | 151 |
function loadJSONDocUsingPOST(url, data) { |
0 | 152 |
setProgressCursor(); |
1419 | 153 |
var deferred = loadJSON(url, data, 'POST'); |
0 | 154 |
deferred = deferred.addErrback(remoteCallFailed); |
155 |
deferred = deferred.addCallback(resetCursor); |
|
156 |
return deferred; |
|
157 |
} |
|
158 |
||
159 |
||
160 |
/* |
|
1419 | 161 |
* This function will call **synchronously** a remote method on the cubicweb server |
162 |
* @param fname: the function name to call (as exposed by the JSONController) |
|
163 |
* |
|
164 |
* additional arguments will be directly passed to the specified function |
|
0 | 165 |
* |
1419 | 166 |
* It looks at http headers to guess the response type. |
0 | 167 |
*/ |
1419 | 168 |
function remoteExec(fname /* ... */) { |
169 |
setProgressCursor(); |
|
170 |
var props = {'fname' : fname, 'pageid' : pageid, |
|
171 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
172 |
var result = jQuery.ajax({url: JSON_BASE_URL, data: props, async: false}).responseText; |
|
173 |
if (result) { |
|
174 |
result = evalJSON(result); |
|
175 |
} |
|
176 |
resetCursor(); |
|
177 |
return result; |
|
0 | 178 |
} |
179 |
||
180 |
/* |
|
1419 | 181 |
* This function will call **asynchronously** a remote method on the json |
182 |
* controller of the cubicweb http server |
|
183 |
* |
|
0 | 184 |
* @param fname: the function name to call (as exposed by the JSONController) |
1419 | 185 |
* |
0 | 186 |
* additional arguments will be directly passed to the specified function |
1419 | 187 |
* |
0 | 188 |
* It looks at http headers to guess the response type. |
189 |
*/ |
|
1419 | 190 |
function asyncRemoteExec(fname /* ... */) { |
191 |
var props = {'fname' : fname, 'pageid' : pageid, |
|
192 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
193 |
return loadJSONDocUsingPOST(JSON_BASE_URL, props); |
|
0 | 194 |
} |
195 |
||
196 |
||
197 |
/* emulation of gettext's _ shortcut |
|
198 |
*/ |
|
199 |
function _(message) { |
|
1419 | 200 |
return remoteExec('i18n', [message])[0]; |
0 | 201 |
} |
202 |
||
203 |
function userCallback(cbname) { |
|
1419 | 204 |
asyncRemoteExec('user_callback', cbname); |
0 | 205 |
} |
206 |
||
207 |
function unloadPageData() { |
|
208 |
// NOTE: do not make async calls on unload if you want to avoid |
|
209 |
// strange bugs |
|
1419 | 210 |
remoteExec('unload_page_data'); |
0 | 211 |
} |
212 |
||
213 |
function openHash() { |
|
214 |
if (document.location.hash) { |
|
215 |
var nid = document.location.hash.replace('#', ''); |
|
216 |
var node = jQuery('#' + nid); |
|
217 |
if (node) { removeElementClass(node, "hidden"); } |
|
218 |
}; |
|
219 |
} |
|
220 |
jQuery(document).ready(openHash); |
|
221 |
||
222 |
function reloadComponent(compid, rql, registry, nodeid, extraargs) { |
|
223 |
registry = registry || 'components'; |
|
224 |
rql = rql || ''; |
|
225 |
nodeid = nodeid || (compid + 'Component'); |
|
226 |
extraargs = extraargs || {}; |
|
227 |
var node = getNode(nodeid); |
|
1419 | 228 |
var d = asyncRemoteExec('component', compid, rql, registry, extraargs); |
0 | 229 |
d.addCallback(function(result, req) { |
230 |
var domnode = getDomFromResponse(result); |
|
231 |
if (node) { |
|
232 |
// make sure the component is visible |
|
233 |
removeElementClass(node, "hidden"); |
|
234 |
swapDOM(node, domnode); |
|
235 |
} |
|
236 |
}); |
|
237 |
d.addCallback(resetCursor); |
|
238 |
d.addErrback(function(xxx) { |
|
239 |
updateMessage(_("an error occured")); |
|
240 |
log(xxx); |
|
241 |
}); |
|
242 |
return d; |
|
243 |
} |
|
244 |
||
245 |
/* XXX: HTML architecture of cubicweb boxes is a bit strange */ |
|
246 |
function reloadBox(boxid, rql) { |
|
225
b90904dc3069
[javascript] reloadBox: return deferred for further callback chaining
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
12
diff
changeset
|
247 |
return reloadComponent(boxid, rql, 'boxes', boxid); |
0 | 248 |
} |
249 |
||
250 |
function userCallbackThenUpdateUI(cbname, compid, rql, msg, registry, nodeid) { |
|
1419 | 251 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 252 |
d.addCallback(function() { |
253 |
reloadComponent(compid, rql, registry, nodeid); |
|
254 |
if (msg) { updateMessage(msg); } |
|
255 |
}); |
|
256 |
d.addCallback(resetCursor); |
|
257 |
d.addErrback(function(xxx) { |
|
258 |
updateMessage(_("an error occured")); |
|
259 |
log(xxx); |
|
260 |
return resetCursor(); |
|
261 |
}); |
|
262 |
} |
|
263 |
||
264 |
function userCallbackThenReloadPage(cbname, msg) { |
|
1419 | 265 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 266 |
d.addCallback(function() { |
267 |
window.location.reload(); |
|
268 |
if (msg) { updateMessage(msg); } |
|
269 |
}); |
|
270 |
d.addCallback(resetCursor); |
|
271 |
d.addErrback(function(xxx) { |
|
272 |
updateMessage(_("an error occured")); |
|
273 |
log(xxx); |
|
274 |
return resetCursor(); |
|
275 |
}); |
|
276 |
} |
|
277 |
||
278 |
/* |
|
279 |
* unregisters the python function registered on the server's side |
|
280 |
* while the page was generated. |
|
281 |
*/ |
|
282 |
function unregisterUserCallback(cbname) { |
|
1419 | 283 |
var d = asyncRemoteExec('unregister_user_callback', cbname); |
0 | 284 |
d.addCallback(function() {resetCursor();}); |
285 |
d.addErrback(function(xxx) { |
|
286 |
updateMessage(_("an error occured")); |
|
287 |
log(xxx); |
|
288 |
return resetCursor(); |
|
289 |
}); |
|
290 |
} |
|
291 |
||
292 |
||
293 |
/* executes an async query to the server and replaces a node's |
|
294 |
* content with the query result |
|
295 |
* |
|
296 |
* @param nodeId the placeholder node's id |
|
297 |
* @param rql the RQL query |
|
298 |
* @param vid the vid to apply to the RQL selection (default if not specified) |
|
299 |
* @param extraparmas table of additional query parameters |
|
300 |
*/ |
|
301 |
function replacePageChunk(nodeId, rql, vid, extraparams, /* ... */ swap, callback) { |
|
302 |
var params = null; |
|
303 |
if (callback) { |
|
304 |
params = {callback: callback}; |
|
305 |
} |
|
306 |
||
307 |
var node = jQuery('#' + nodeId)[0]; |
|
308 |
var props = {}; |
|
309 |
if (node) { |
|
310 |
props['rql'] = rql; |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1419
diff
changeset
|
311 |
props['fname'] = 'view'; |
0 | 312 |
props['pageid'] = pageid; |
313 |
if (vid) { props['vid'] = vid; } |
|
314 |
if (extraparams) { jQuery.extend(props, extraparams); } |
|
1419 | 315 |
// FIXME we need to do asURL(props) manually instead of |
0 | 316 |
// passing `props` directly to loadxml because replacePageChunk |
317 |
// is sometimes called (abusively) with some extra parameters in `vid` |
|
318 |
var mode = swap?'swap':'replace'; |
|
1419 | 319 |
var url = JSON_BASE_URL + asURL(props); |
0 | 320 |
jQuery(node).loadxhtml(url, params, 'get', mode); |
321 |
} else { |
|
322 |
log('Node', nodeId, 'not found'); |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
/* XXX: this function should go in edition.js but as for now, htmlReplace |
|
327 |
* references it. |
|
328 |
* |
|
329 |
* replace all textareas with fckeditors. |
|
330 |
*/ |
|
331 |
function buildWysiwygEditors(parent) { |
|
332 |
jQuery('textarea').each(function () { |
|
333 |
if (this.getAttribute('cubicweb:type', 'wysiwyg')) { |
|
334 |
if (typeof FCKeditor != "undefined") { |
|
335 |
var fck = new FCKeditor(this.id); |
|
336 |
fck.Config['CustomConfigurationsPath'] = fckconfigpath; |
|
337 |
fck.Config['DefaultLanguage'] = fcklang; |
|
338 |
fck.BasePath = "fckeditor/"; |
|
339 |
fck.ReplaceTextarea(); |
|
340 |
} else { |
|
341 |
log('fckeditor could not be found.'); |
|
342 |
} |
|
343 |
} |
|
344 |
}); |
|
345 |
} |
|
346 |
||
347 |
jQuery(document).ready(buildWysiwygEditors); |
|
348 |
||
349 |
||
1419 | 350 |
/* |
351 |
* takes a list of DOM nodes and removes all empty text nodes |
|
352 |
*/ |
|
353 |
function stripEmptyTextNodes(nodelist) { |
|
354 |
var stripped = []; |
|
355 |
for (var i=0; i < nodelist.length; i++) { |
|
356 |
var node = nodelist[i]; |
|
357 |
if (isTextNode(node) && !node.textContent.strip()) { |
|
358 |
continue; |
|
359 |
} else { |
|
360 |
stripped.push(node); |
|
361 |
} |
|
362 |
} |
|
363 |
return stripped; |
|
364 |
} |
|
365 |
||
0 | 366 |
/* convenience function that returns a DOM node based on req's result. */ |
367 |
function getDomFromResponse(response) { |
|
368 |
if (typeof(response) == 'string') { |
|
369 |
return html2dom(response); |
|
370 |
} |
|
371 |
var doc = response.documentElement; |
|
372 |
var children = doc.childNodes; |
|
373 |
if (!children.length) { |
|
374 |
// no child (error cases) => return the whole document |
|
375 |
return doc.cloneNode(true); |
|
376 |
} |
|
1419 | 377 |
children = stripEmptyTextNodes(children); |
0 | 378 |
if (children.length == 1) { |
379 |
// only one child => return it |
|
380 |
return children[0].cloneNode(true); |
|
381 |
} |
|
382 |
// several children => wrap them in a single node and return the wrap |
|
383 |
return DIV(null, map(methodcaller('cloneNode', true), children)); |
|
384 |
} |
|
385 |
||
386 |
function postJSON(url, data, callback) { |
|
387 |
return jQuery.post(url, data, callback, 'json'); |
|
388 |
} |
|
389 |
||
390 |
function getJSON(url, data, callback){ |
|
391 |
return jQuery.get(url, data, callback, 'json'); |
|
392 |
} |
|
393 |
||
394 |
CubicWeb.provide('ajax.js'); |