author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 03 Jul 2009 17:58:36 +0200 | |
branch | stable |
changeset 2257 | 2c9eceeaae6a |
parent 1801 | 672acc730ce5 |
child 2338 | 3f7c7fbae94e |
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 |
} |
955 | 62 |
if (typeof roundedCornersOnLoad != 'undefined') { |
63 |
roundedCornersOnLoad(); |
|
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); |
|
234 |
} |
|
235 |
}); |
|
236 |
d.addCallback(resetCursor); |
|
237 |
d.addErrback(function(xxx) { |
|
238 |
updateMessage(_("an error occured")); |
|
239 |
log(xxx); |
|
240 |
}); |
|
241 |
return d; |
|
242 |
} |
|
243 |
||
244 |
/* XXX: HTML architecture of cubicweb boxes is a bit strange */ |
|
245 |
function reloadBox(boxid, rql) { |
|
225
b90904dc3069
[javascript] reloadBox: return deferred for further callback chaining
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
12
diff
changeset
|
246 |
return reloadComponent(boxid, rql, 'boxes', boxid); |
0 | 247 |
} |
248 |
||
249 |
function userCallbackThenUpdateUI(cbname, compid, rql, msg, registry, nodeid) { |
|
1419 | 250 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 251 |
d.addCallback(function() { |
252 |
reloadComponent(compid, rql, registry, nodeid); |
|
253 |
if (msg) { updateMessage(msg); } |
|
254 |
}); |
|
255 |
d.addCallback(resetCursor); |
|
256 |
d.addErrback(function(xxx) { |
|
257 |
updateMessage(_("an error occured")); |
|
258 |
log(xxx); |
|
259 |
return resetCursor(); |
|
260 |
}); |
|
261 |
} |
|
262 |
||
263 |
function userCallbackThenReloadPage(cbname, msg) { |
|
1419 | 264 |
var d = asyncRemoteExec('user_callback', cbname); |
0 | 265 |
d.addCallback(function() { |
266 |
window.location.reload(); |
|
267 |
if (msg) { updateMessage(msg); } |
|
268 |
}); |
|
269 |
d.addCallback(resetCursor); |
|
270 |
d.addErrback(function(xxx) { |
|
271 |
updateMessage(_("an error occured")); |
|
272 |
log(xxx); |
|
273 |
return resetCursor(); |
|
274 |
}); |
|
275 |
} |
|
276 |
||
277 |
/* |
|
278 |
* unregisters the python function registered on the server's side |
|
279 |
* while the page was generated. |
|
280 |
*/ |
|
281 |
function unregisterUserCallback(cbname) { |
|
1419 | 282 |
var d = asyncRemoteExec('unregister_user_callback', cbname); |
0 | 283 |
d.addCallback(function() {resetCursor();}); |
284 |
d.addErrback(function(xxx) { |
|
285 |
updateMessage(_("an error occured")); |
|
286 |
log(xxx); |
|
287 |
return resetCursor(); |
|
288 |
}); |
|
289 |
} |
|
290 |
||
291 |
||
292 |
/* executes an async query to the server and replaces a node's |
|
293 |
* content with the query result |
|
294 |
* |
|
295 |
* @param nodeId the placeholder node's id |
|
296 |
* @param rql the RQL query |
|
297 |
* @param vid the vid to apply to the RQL selection (default if not specified) |
|
298 |
* @param extraparmas table of additional query parameters |
|
299 |
*/ |
|
300 |
function replacePageChunk(nodeId, rql, vid, extraparams, /* ... */ swap, callback) { |
|
301 |
var params = null; |
|
302 |
if (callback) { |
|
303 |
params = {callback: callback}; |
|
304 |
} |
|
305 |
||
306 |
var node = jQuery('#' + nodeId)[0]; |
|
307 |
var props = {}; |
|
308 |
if (node) { |
|
309 |
props['rql'] = rql; |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1419
diff
changeset
|
310 |
props['fname'] = 'view'; |
0 | 311 |
props['pageid'] = pageid; |
312 |
if (vid) { props['vid'] = vid; } |
|
313 |
if (extraparams) { jQuery.extend(props, extraparams); } |
|
1419 | 314 |
// FIXME we need to do asURL(props) manually instead of |
0 | 315 |
// passing `props` directly to loadxml because replacePageChunk |
316 |
// is sometimes called (abusively) with some extra parameters in `vid` |
|
317 |
var mode = swap?'swap':'replace'; |
|
1419 | 318 |
var url = JSON_BASE_URL + asURL(props); |
0 | 319 |
jQuery(node).loadxhtml(url, params, 'get', mode); |
320 |
} else { |
|
321 |
log('Node', nodeId, 'not found'); |
|
322 |
} |
|
323 |
} |
|
324 |
||
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
|
325 |
/* |
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 |
* 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
|
327 |
* @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
|
328 |
* 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
|
329 |
* - '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
|
330 |
* - '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
|
331 |
* - '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
|
332 |
*/ |
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 |
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
|
334 |
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
|
335 |
} |
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 |
|
0 | 337 |
/* XXX: this function should go in edition.js but as for now, htmlReplace |
338 |
* references it. |
|
339 |
* |
|
340 |
* replace all textareas with fckeditors. |
|
341 |
*/ |
|
342 |
function buildWysiwygEditors(parent) { |
|
343 |
jQuery('textarea').each(function () { |
|
344 |
if (this.getAttribute('cubicweb:type', 'wysiwyg')) { |
|
345 |
if (typeof FCKeditor != "undefined") { |
|
346 |
var fck = new FCKeditor(this.id); |
|
347 |
fck.Config['CustomConfigurationsPath'] = fckconfigpath; |
|
348 |
fck.Config['DefaultLanguage'] = fcklang; |
|
349 |
fck.BasePath = "fckeditor/"; |
|
350 |
fck.ReplaceTextarea(); |
|
351 |
} else { |
|
352 |
log('fckeditor could not be found.'); |
|
353 |
} |
|
354 |
} |
|
355 |
}); |
|
356 |
} |
|
357 |
||
358 |
jQuery(document).ready(buildWysiwygEditors); |
|
359 |
||
360 |
||
1419 | 361 |
/* |
362 |
* takes a list of DOM nodes and removes all empty text nodes |
|
363 |
*/ |
|
364 |
function stripEmptyTextNodes(nodelist) { |
|
365 |
var stripped = []; |
|
366 |
for (var i=0; i < nodelist.length; i++) { |
|
367 |
var node = nodelist[i]; |
|
368 |
if (isTextNode(node) && !node.textContent.strip()) { |
|
369 |
continue; |
|
370 |
} else { |
|
371 |
stripped.push(node); |
|
372 |
} |
|
373 |
} |
|
374 |
return stripped; |
|
375 |
} |
|
376 |
||
0 | 377 |
/* convenience function that returns a DOM node based on req's result. */ |
378 |
function getDomFromResponse(response) { |
|
379 |
if (typeof(response) == 'string') { |
|
380 |
return html2dom(response); |
|
381 |
} |
|
382 |
var doc = response.documentElement; |
|
383 |
var children = doc.childNodes; |
|
384 |
if (!children.length) { |
|
385 |
// no child (error cases) => return the whole document |
|
386 |
return doc.cloneNode(true); |
|
387 |
} |
|
1419 | 388 |
children = stripEmptyTextNodes(children); |
0 | 389 |
if (children.length == 1) { |
390 |
// only one child => return it |
|
391 |
return children[0].cloneNode(true); |
|
392 |
} |
|
393 |
// several children => wrap them in a single node and return the wrap |
|
394 |
return DIV(null, map(methodcaller('cloneNode', true), children)); |
|
395 |
} |
|
396 |
||
397 |
function postJSON(url, data, callback) { |
|
398 |
return jQuery.post(url, data, callback, 'json'); |
|
399 |
} |
|
400 |
||
401 |
function getJSON(url, data, callback){ |
|
402 |
return jQuery.get(url, data, callback, 'json'); |
|
403 |
} |
|
404 |
||
405 |
CubicWeb.provide('ajax.js'); |