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