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