author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Mon, 27 Jul 2009 10:06:13 +0200 | |
changeset 2512 | 106b2a05dc88 |
parent 2338 | 3f7c7fbae94e |
child 3011 | 657dfe374b08 |
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 = []; |
2257
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
14 |
var jqtagfilter = tag + '[' + srcattr + ']'; |
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
15 |
jQuery('head ' + jqtagfilter).each(function(i) { |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
16 |
loaded.push(this.getAttribute(srcattr)); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
17 |
}); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
18 |
node.find(tag).each(function(i) { |
2257
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
19 |
if (this.getAttribute(srcattr)) { |
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
20 |
if (!loaded.contains(this.getAttribute(srcattr))) { |
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
21 |
jQuery(this).appendTo(head); |
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
22 |
} |
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
23 |
} else { |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
24 |
jQuery(this).appendTo(head); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
25 |
} |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
26 |
}); |
2257
2c9eceeaae6a
fix w/ tags without src/href
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1801
diff
changeset
|
27 |
node.find(jqtagfilter).remove(); |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
28 |
} |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
29 |
|
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
|
30 |
/* |
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
|
31 |
* 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
|
32 |
* 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
|
33 |
* 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
|
34 |
*/ |
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
|
35 |
function loadAjaxHtmlHead(node) { |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
36 |
var head = jQuery('head'); |
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
37 |
node = jQuery(node).find('div.ajaxHtmlHead'); |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
38 |
_loadAjaxHtmlHead(node, head, 'script', 'src'); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
39 |
_loadAjaxHtmlHead(node, head, 'link', 'href'); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
40 |
node.find('*').appendTo(head); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
41 |
} |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
42 |
|
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
43 |
function preprocessAjaxLoad(node, newdomnode) { |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
44 |
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
|
45 |
} |
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
|
46 |
|
1407
75863d3ffd9b
cleanup (undue req param)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1320
diff
changeset
|
47 |
function postAjaxLoad(node) { |
540
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
48 |
// 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
|
49 |
if (typeof(Sortable) != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
50 |
Sortable.sortTables(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
51 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
52 |
// 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
|
53 |
if (typeof(FCKeditor) != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
54 |
buildWysiwygEditors(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
55 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
56 |
if (typeof initFacetBoxEvents != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
57 |
initFacetBoxEvents(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
58 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
59 |
if (typeof buildWidgets != 'undefined') { |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
60 |
buildWidgets(node); |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
61 |
} |
2338
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2257
diff
changeset
|
62 |
if (typeof roundedCorners != 'undefined') { |
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2257
diff
changeset
|
63 |
roundedCorners(node); |
955 | 64 |
} |
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
65 |
loadDynamicFragments(node); |
1419 | 66 |
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
|
67 |
} |
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
68 |
|
0 | 69 |
// cubicweb loadxhtml plugin to make jquery handle xhtml response |
70 |
jQuery.fn.loadxhtml = function(url, data, reqtype, mode) { |
|
71 |
var ajax = null; |
|
72 |
if (reqtype == 'post') { |
|
73 |
ajax = jQuery.post; |
|
74 |
} else { |
|
75 |
ajax = jQuery.get; |
|
76 |
} |
|
77 |
if (this.size() > 1) { |
|
78 |
log('loadxhtml was called with more than one element'); |
|
79 |
} |
|
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
|
80 |
var node = this.get(0); // only consider the first element |
0 | 81 |
mode = mode || 'replace'; |
82 |
var callback = null; |
|
83 |
if (data && data.callback) { |
|
84 |
callback = data.callback; |
|
85 |
delete data.callback; |
|
86 |
} |
|
87 |
ajax(url, data, function(response) { |
|
88 |
var domnode = getDomFromResponse(response); |
|
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
89 |
preprocessAjaxLoad(node, domnode); |
0 | 90 |
if (mode == 'swap') { |
91 |
var origId = node.id; |
|
92 |
node = swapDOM(node, domnode); |
|
93 |
if (!node.id) { |
|
94 |
node.id = origId; |
|
95 |
} |
|
96 |
} else if (mode == 'replace') { |
|
97 |
jQuery(node).empty().append(domnode); |
|
98 |
} else if (mode == 'append') { |
|
99 |
jQuery(node).append(domnode); |
|
100 |
} |
|
1407
75863d3ffd9b
cleanup (undue req param)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1320
diff
changeset
|
101 |
postAjaxLoad(node); |
0 | 102 |
while (jQuery.isFunction(callback)) { |
103 |
callback = callback.apply(this, [domnode]); |
|
104 |
} |
|
105 |
}); |
|
540
e5c97f6f119d
[javascript] extract ajax postprocessing in a separate function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
492
diff
changeset
|
106 |
}; |
0 | 107 |
|
108 |
||
109 |
||
110 |
/* finds each dynamic fragment in the page and executes the |
|
111 |
* the associated RQL to build them (Async call) |
|
112 |
*/ |
|
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
113 |
function loadDynamicFragments(node) { |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
114 |
if (node) { |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
115 |
var fragments = jQuery(node).find('div.dynamicFragment'); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
116 |
} else { |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
117 |
var fragments = jQuery('div.dynamicFragment'); |
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
118 |
} |
0 | 119 |
if (fragments.length == 0) { |
120 |
return; |
|
121 |
} |
|
122 |
if (typeof LOADING_MSG == 'undefined') { |
|
123 |
LOADING_MSG = 'loading'; // this is only a safety belt, it should not happen |
|
124 |
} |
|
125 |
for(var i=0; i<fragments.length; i++) { |
|
126 |
var fragment = fragments[i]; |
|
127 |
fragment.innerHTML = '<h3>' + LOADING_MSG + ' ... <img src="data/loading.gif" /></h3>'; |
|
128 |
var rql = getNodeAttribute(fragment, 'cubicweb:rql'); |
|
129 |
var vid = getNodeAttribute(fragment, 'cubicweb:vid'); |
|
130 |
var extraparams = {}; |
|
131 |
var actrql = getNodeAttribute(fragment, 'cubicweb:actualrql'); |
|
132 |
if (actrql) { extraparams['actualrql'] = actrql; } |
|
133 |
var fbvid = getNodeAttribute(fragment, 'cubicweb:fallbackvid'); |
|
134 |
if (fbvid) { extraparams['fallbackvid'] = fbvid; } |
|
135 |
||
136 |
replacePageChunk(fragment.id, rql, vid, extraparams); |
|
137 |
} |
|
138 |
} |
|
139 |
||
1512
c51ca5f49a78
fix insertion of head content on ajax queries
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
140 |
jQuery(document).ready(function() {loadDynamicFragments();}); |
0 | 141 |
|
142 |
//============= base AJAX functions to make remote calls =====================// |
|
143 |
||
144 |
function remoteCallFailed(err, req) { |
|
145 |
if (req.status == 500) { |
|
146 |
updateMessage(err); |
|
147 |
} else { |
|
148 |
updateMessage(_("an error occured while processing your request")); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
||
153 |
/* |
|
1419 | 154 |
* This function will call **synchronously** a remote method on the cubicweb server |
155 |
* @param fname: the function name to call (as exposed by the JSONController) |
|
156 |
* |
|
157 |
* additional arguments will be directly passed to the specified function |
|
0 | 158 |
* |
1419 | 159 |
* It looks at http headers to guess the response type. |
0 | 160 |
*/ |
1419 | 161 |
function remoteExec(fname /* ... */) { |
162 |
setProgressCursor(); |
|
163 |
var props = {'fname' : fname, 'pageid' : pageid, |
|
164 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
165 |
var result = jQuery.ajax({url: JSON_BASE_URL, data: props, async: false}).responseText; |
|
166 |
if (result) { |
|
167 |
result = evalJSON(result); |
|
168 |
} |
|
169 |
resetCursor(); |
|
170 |
return result; |
|
0 | 171 |
} |
172 |
||
173 |
/* |
|
1419 | 174 |
* This function will call **asynchronously** a remote method on the json |
175 |
* controller of the cubicweb http server |
|
176 |
* |
|
0 | 177 |
* @param fname: the function name to call (as exposed by the JSONController) |
1419 | 178 |
* |
0 | 179 |
* additional arguments will be directly passed to the specified function |
1419 | 180 |
* |
0 | 181 |
* It looks at http headers to guess the response type. |
182 |
*/ |
|
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
183 |
|
1419 | 184 |
function asyncRemoteExec(fname /* ... */) { |
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
185 |
setProgressCursor(); |
1419 | 186 |
var props = {'fname' : fname, 'pageid' : pageid, |
187 |
'arg': map(jQuery.toJSON, sliceList(arguments, 1))}; |
|
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
188 |
var deferred = loadRemote(JSON_BASE_URL, props, 'POST'); |
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
189 |
deferred = deferred.addErrback(remoteCallFailed); |
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
190 |
deferred = deferred.addErrback(resetCursor); |
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
191 |
deferred = deferred.addCallback(resetCursor); |
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1512
diff
changeset
|
192 |
return deferred; |
0 | 193 |
} |
194 |
||
195 |
||
196 |
/* emulation of gettext's _ shortcut |
|
197 |
*/ |
|
198 |
function _(message) { |
|
1419 | 199 |
return remoteExec('i18n', [message])[0]; |
0 | 200 |
} |
201 |
||
202 |
function userCallback(cbname) { |
|
1419 | 203 |
asyncRemoteExec('user_callback', cbname); |
0 | 204 |
} |
205 |
||
206 |
function unloadPageData() { |
|
207 |
// NOTE: do not make async calls on unload if you want to avoid |
|
208 |
// strange bugs |
|
1419 | 209 |
remoteExec('unload_page_data'); |
0 | 210 |
} |
211 |
||
212 |
function openHash() { |
|
213 |
if (document.location.hash) { |
|
214 |
var nid = document.location.hash.replace('#', ''); |
|
215 |
var node = jQuery('#' + nid); |
|
216 |
if (node) { removeElementClass(node, "hidden"); } |
|
217 |
}; |
|
218 |
} |
|
219 |
jQuery(document).ready(openHash); |
|
220 |
||
221 |
function reloadComponent(compid, rql, registry, nodeid, extraargs) { |
|
222 |
registry = registry || 'components'; |
|
223 |
rql = rql || ''; |
|
224 |
nodeid = nodeid || (compid + 'Component'); |
|
225 |
extraargs = extraargs || {}; |
|
226 |
var node = getNode(nodeid); |
|
1419 | 227 |
var d = asyncRemoteExec('component', compid, rql, registry, extraargs); |
0 | 228 |
d.addCallback(function(result, req) { |
229 |
var domnode = getDomFromResponse(result); |
|
230 |
if (node) { |
|
231 |
// make sure the component is visible |
|
232 |
removeElementClass(node, "hidden"); |
|
233 |
swapDOM(node, domnode); |
|
2338
3f7c7fbae94e
call postAjaxLoad in reloadComponent; add corners only on reloaded elements
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2257
diff
changeset
|
234 |
postAjaxLoad(domnode); |
0 | 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 |
||
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
|
326 |
/* |
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 |
* 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
|
328 |
* @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
|
329 |
* 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
|
330 |
* - '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
|
331 |
* - '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
|
332 |
* - '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
|
333 |
*/ |
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
|
334 |
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
|
335 |
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
|
336 |
} |
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
|
337 |
|
0 | 338 |
/* XXX: this function should go in edition.js but as for now, htmlReplace |
339 |
* references it. |
|
340 |
* |
|
341 |
* replace all textareas with fckeditors. |
|
342 |
*/ |
|
343 |
function buildWysiwygEditors(parent) { |
|
344 |
jQuery('textarea').each(function () { |
|
345 |
if (this.getAttribute('cubicweb:type', 'wysiwyg')) { |
|
346 |
if (typeof FCKeditor != "undefined") { |
|
347 |
var fck = new FCKeditor(this.id); |
|
348 |
fck.Config['CustomConfigurationsPath'] = fckconfigpath; |
|
349 |
fck.Config['DefaultLanguage'] = fcklang; |
|
350 |
fck.BasePath = "fckeditor/"; |
|
351 |
fck.ReplaceTextarea(); |
|
352 |
} else { |
|
353 |
log('fckeditor could not be found.'); |
|
354 |
} |
|
355 |
} |
|
356 |
}); |
|
357 |
} |
|
358 |
||
359 |
jQuery(document).ready(buildWysiwygEditors); |
|
360 |
||
361 |
||
1419 | 362 |
/* |
363 |
* takes a list of DOM nodes and removes all empty text nodes |
|
364 |
*/ |
|
365 |
function stripEmptyTextNodes(nodelist) { |
|
366 |
var stripped = []; |
|
367 |
for (var i=0; i < nodelist.length; i++) { |
|
368 |
var node = nodelist[i]; |
|
369 |
if (isTextNode(node) && !node.textContent.strip()) { |
|
370 |
continue; |
|
371 |
} else { |
|
372 |
stripped.push(node); |
|
373 |
} |
|
374 |
} |
|
375 |
return stripped; |
|
376 |
} |
|
377 |
||
0 | 378 |
/* convenience function that returns a DOM node based on req's result. */ |
379 |
function getDomFromResponse(response) { |
|
380 |
if (typeof(response) == 'string') { |
|
381 |
return html2dom(response); |
|
382 |
} |
|
383 |
var doc = response.documentElement; |
|
384 |
var children = doc.childNodes; |
|
385 |
if (!children.length) { |
|
386 |
// no child (error cases) => return the whole document |
|
387 |
return doc.cloneNode(true); |
|
388 |
} |
|
1419 | 389 |
children = stripEmptyTextNodes(children); |
0 | 390 |
if (children.length == 1) { |
391 |
// only one child => return it |
|
392 |
return children[0].cloneNode(true); |
|
393 |
} |
|
394 |
// several children => wrap them in a single node and return the wrap |
|
395 |
return DIV(null, map(methodcaller('cloneNode', true), children)); |
|
396 |
} |
|
397 |
||
398 |
function postJSON(url, data, callback) { |
|
399 |
return jQuery.post(url, data, callback, 'json'); |
|
400 |
} |
|
401 |
||
402 |
function getJSON(url, data, callback){ |
|
403 |
return jQuery.get(url, data, callback, 'json'); |
|
404 |
} |
|
405 |
||
406 |
CubicWeb.provide('ajax.js'); |