author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Thu, 17 Jun 2010 15:08:25 +0200 | |
branch | stable |
changeset 5772 | 4283ee58acaf |
parent 5186 | f3c2cb460ad9 |
child 5658 | 7b9553a9db65 |
permissions | -rw-r--r-- |
0 | 1 |
/* MochiKit -> jQuery compatibility module */ |
2 |
||
3 |
function forEach(array, func) { |
|
4 |
for (var i=0, length=array.length; i<length; i++) { |
|
5 |
func(array[i]); |
|
6 |
} |
|
7 |
} |
|
8 |
||
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
9 |
// XXX looks completely unused (candidate for removal) |
0 | 10 |
function getElementsByTagAndClassName(tag, klass, root) { |
11 |
root = root || document; |
|
12 |
// FIXME root is not used in this compat implementation |
|
13 |
return jQuery(tag + '.' + klass); |
|
14 |
} |
|
15 |
||
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
16 |
/* jQUery flattens arrays returned by the mapping function: |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
17 |
>>> y = ['a:b:c', 'd:e'] |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
18 |
>>> jQuery.map(y, function(y) { return y.split(':');}) |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
19 |
["a", "b", "c", "d", "e"] |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
20 |
// where one would expect: |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
21 |
[ ["a", "b", "c"], ["d", "e"] ] |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
22 |
XXX why not the same argument order as $.map and forEach ? |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
23 |
*/ |
0 | 24 |
function map(func, array) { |
25 |
var result = []; |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
26 |
for (var i=0, length=array.length; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
27 |
i<length; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
28 |
i++) { |
0 | 29 |
result.push(func(array[i])); |
30 |
} |
|
31 |
return result; |
|
32 |
} |
|
33 |
||
34 |
function findValue(array, element) { |
|
35 |
return jQuery.inArray(element, array); |
|
36 |
} |
|
37 |
||
38 |
function filter(func, array) { |
|
39 |
return jQuery.grep(array, func); |
|
40 |
} |
|
41 |
||
42 |
function noop() {} |
|
43 |
||
44 |
function addElementClass(node, klass) { |
|
45 |
jQuery(node).addClass(klass); |
|
46 |
} |
|
47 |
||
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
48 |
// XXX looks completely unused (candidate for removal) |
0 | 49 |
function toggleElementClass(node, klass) { |
50 |
jQuery(node).toggleClass(klass); |
|
51 |
} |
|
52 |
||
53 |
function removeElementClass(node, klass) { |
|
54 |
jQuery(node).removeClass(klass); |
|
55 |
} |
|
56 |
||
12
95bc1d15349b
javascript cleaning
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
57 |
hasElementClass = jQuery.className.has; |
0 | 58 |
|
59 |
||
60 |
function partial(func) { |
|
61 |
var args = sliceList(arguments, 1); |
|
62 |
return function() { |
|
63 |
return func.apply(null, merge(args, arguments)); |
|
12
95bc1d15349b
javascript cleaning
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
64 |
}; |
0 | 65 |
} |
66 |
||
67 |
||
68 |
function log() { |
|
69 |
// XXX dummy implementation |
|
70 |
// console.log.apply(arguments); ??? |
|
71 |
var args = []; |
|
72 |
for (var i=0; i<arguments.length; i++) { |
|
73 |
args.push(arguments[i]); |
|
74 |
} |
|
75 |
if (typeof(window) != "undefined" && window.console |
|
76 |
&& window.console.log) { |
|
77 |
window.console.log(args.join(' ')); |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
function getNodeAttribute(node, attribute) { |
|
82 |
return jQuery(node).attr(attribute); |
|
83 |
} |
|
84 |
||
85 |
function isArray(it){ // taken from dojo |
|
86 |
return it && (it instanceof Array || typeof it == "array"); |
|
87 |
} |
|
88 |
||
89 |
function isString(it){ // taken from dojo |
|
90 |
return !!arguments.length && it != null && (typeof it == "string" || it instanceof String); |
|
91 |
} |
|
92 |
||
93 |
||
94 |
function isArrayLike(it) { // taken from dojo |
|
95 |
return (it && it !== undefined && |
|
96 |
// keep out built-in constructors (Number, String, ...) which have length |
|
97 |
// properties |
|
98 |
!isString(it) && !jQuery.isFunction(it) && |
|
99 |
!(it.tagName && it.tagName.toLowerCase() == 'form') && |
|
100 |
(isArray(it) || isFinite(it.length))); |
|
101 |
} |
|
102 |
||
103 |
||
104 |
function getNode(node) { |
|
105 |
if (typeof(node) == 'string') { |
|
106 |
return document.getElementById(node); |
|
107 |
} |
|
108 |
return node; |
|
109 |
} |
|
110 |
||
111 |
/* safe version of jQuery('#nodeid') because we use ':' in nodeids |
|
112 |
* which messes with jQuery selection mechanism |
|
113 |
*/ |
|
114 |
function jqNode(node) { |
|
115 |
node = getNode(node); |
|
116 |
if (node) { |
|
117 |
return jQuery(node); |
|
118 |
} |
|
119 |
return null; |
|
120 |
} |
|
121 |
||
122 |
function evalJSON(json) { // trust source |
|
123 |
return eval("(" + json + ")"); |
|
124 |
} |
|
125 |
||
126 |
function urlEncode(str) { |
|
127 |
if (typeof(encodeURIComponent) != "undefined") { |
|
128 |
return encodeURIComponent(str).replace(/\'/g, '%27'); |
|
129 |
} else { |
|
130 |
return escape(str).replace(/\+/g, '%2B').replace(/\"/g,'%22').rval.replace(/\'/g, '%27'); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
function swapDOM(dest, src) { |
|
135 |
dest = getNode(dest); |
|
136 |
var parent = dest.parentNode; |
|
137 |
if (src) { |
|
138 |
src = getNode(src); |
|
139 |
parent.replaceChild(src, dest); |
|
140 |
} else { |
|
141 |
parent.removeChild(dest); |
|
142 |
} |
|
143 |
return src; |
|
144 |
} |
|
145 |
||
146 |
function replaceChildNodes(node/*, nodes...*/) { |
|
147 |
var elem = getNode(node); |
|
148 |
arguments[0] = elem; |
|
149 |
var child; |
|
150 |
while ((child = elem.firstChild)) { |
|
151 |
elem.removeChild(child); |
|
152 |
} |
|
153 |
if (arguments.length < 2) { |
|
154 |
return elem; |
|
155 |
} else { |
|
156 |
for (var i=1; i<arguments.length; i++) { |
|
157 |
elem.appendChild(arguments[i]); |
|
158 |
} |
|
159 |
return elem; |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
update = jQuery.extend; |
|
164 |
||
165 |
||
166 |
function createDomFunction(tag) { |
|
167 |
||
168 |
function builddom(params, children) { |
|
169 |
var node = document.createElement(tag); |
|
170 |
for (key in params) { |
|
171 |
var value = params[key]; |
|
172 |
if (key.substring(0, 2) == 'on') { |
|
173 |
// this is an event handler definition |
|
174 |
if (typeof value == 'string') { |
|
175 |
// litteral definition |
|
176 |
value = new Function(value); |
|
177 |
} |
|
178 |
node[key] = value; |
|
179 |
} else { // normal node attribute |
|
3096
8ee9529d8367
[views] #343641: fix calendar with IE6/7
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
2537
diff
changeset
|
180 |
jQuery(node).attr(key, params[key]); |
0 | 181 |
} |
182 |
} |
|
183 |
if (children) { |
|
184 |
if (!isArrayLike(children)) { |
|
185 |
children = [children]; |
|
186 |
for (var i=2; i<arguments.length; i++) { |
|
187 |
var arg = arguments[i]; |
|
188 |
if (isArray(arg)) { |
|
189 |
children = merge(children, arg); |
|
190 |
} else { |
|
191 |
children.push(arg); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
for (var i=0; i<children.length; i++) { |
|
196 |
var child = children[i]; |
|
197 |
if (typeof child == "string" || typeof child == "number") { |
|
198 |
child = document.createTextNode(child); |
|
199 |
} |
|
200 |
node.appendChild(child); |
|
201 |
} |
|
202 |
} |
|
203 |
return node; |
|
204 |
} |
|
205 |
return builddom; |
|
206 |
} |
|
207 |
||
208 |
A = createDomFunction('a'); |
|
209 |
BUTTON = createDomFunction('button'); |
|
210 |
BR = createDomFunction('br'); |
|
211 |
CANVAS = createDomFunction('canvas'); |
|
212 |
DD = createDomFunction('dd'); |
|
213 |
DIV = createDomFunction('div'); |
|
214 |
DL = createDomFunction('dl'); |
|
215 |
DT = createDomFunction('dt'); |
|
216 |
FIELDSET = createDomFunction('fieldset'); |
|
217 |
FORM = createDomFunction('form'); |
|
218 |
H1 = createDomFunction('H1'); |
|
219 |
H2 = createDomFunction('H2'); |
|
220 |
H3 = createDomFunction('H3'); |
|
221 |
H4 = createDomFunction('H4'); |
|
222 |
H5 = createDomFunction('H5'); |
|
223 |
H6 = createDomFunction('H6'); |
|
224 |
HR = createDomFunction('hr'); |
|
225 |
IMG = createDomFunction('img'); |
|
226 |
INPUT = createDomFunction('input'); |
|
227 |
LABEL = createDomFunction('label'); |
|
228 |
LEGEND = createDomFunction('legend'); |
|
229 |
LI = createDomFunction('li'); |
|
230 |
OL = createDomFunction('ol'); |
|
231 |
OPTGROUP = createDomFunction('optgroup'); |
|
232 |
OPTION = createDomFunction('option'); |
|
233 |
P = createDomFunction('p'); |
|
234 |
PRE = createDomFunction('pre'); |
|
235 |
SELECT = createDomFunction('select'); |
|
236 |
SPAN = createDomFunction('span'); |
|
237 |
STRONG = createDomFunction('strong'); |
|
238 |
TABLE = createDomFunction('table'); |
|
239 |
TBODY = createDomFunction('tbody'); |
|
240 |
TD = createDomFunction('td'); |
|
241 |
TEXTAREA = createDomFunction('textarea'); |
|
242 |
TFOOT = createDomFunction('tfoot'); |
|
243 |
TH = createDomFunction('th'); |
|
244 |
THEAD = createDomFunction('thead'); |
|
245 |
TR = createDomFunction('tr'); |
|
246 |
TT = createDomFunction('tt'); |
|
247 |
UL = createDomFunction('ul'); |
|
248 |
||
249 |
// cubicweb specific |
|
1117
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
250 |
//IFRAME = createDomFunction('iframe'); |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
251 |
function IFRAME(params){ |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
252 |
if ('name' in params){ |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
253 |
try { |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
254 |
var node = document.createElement('<iframe name="'+params['name']+'">'); |
2537
73c83c14dd2c
[javascript] fix IFRAME implementation and make it work in IE 6/7 + FF3 with text/html served pages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1563
diff
changeset
|
255 |
} catch (ex) { |
73c83c14dd2c
[javascript] fix IFRAME implementation and make it work in IE 6/7 + FF3 with text/html served pages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1563
diff
changeset
|
256 |
var node = document.createElement('iframe'); |
73c83c14dd2c
[javascript] fix IFRAME implementation and make it work in IE 6/7 + FF3 with text/html served pages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1563
diff
changeset
|
257 |
node.id = node.name = params.name; |
73c83c14dd2c
[javascript] fix IFRAME implementation and make it work in IE 6/7 + FF3 with text/html served pages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1563
diff
changeset
|
258 |
} |
1117
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
259 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
260 |
else{ |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
261 |
var node = document.createElement('iframe'); |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
262 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
263 |
for (key in params) { |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
264 |
if (key != 'name'){ |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
265 |
var value = params[key]; |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
266 |
if (key.substring(0, 2) == 'on') { |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
267 |
// this is an event handler definition |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
268 |
if (typeof value == 'string') { |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
269 |
// litteral definition |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
270 |
value = new Function(value); |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
271 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
272 |
node[key] = value; |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
273 |
} else { // normal node attribute |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
274 |
node.setAttribute(key, params[key]); |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
275 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
276 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
277 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
278 |
return node; |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
279 |
} |
20a21b3153b0
define an IFRAME function to fix a bug on IE6 and IE7 to avoid to open a new window
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
226
diff
changeset
|
280 |
|
0 | 281 |
|
282 |
// dummy ultra minimalist implementation on deferred for jQuery |
|
283 |
function Deferred() { |
|
284 |
this.__init__(this); |
|
285 |
} |
|
286 |
||
287 |
jQuery.extend(Deferred.prototype, { |
|
288 |
__init__: function() { |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
289 |
this._onSuccess = []; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
290 |
this._onFailure = []; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
291 |
this._req = null; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
292 |
this._result = null; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
293 |
this._error = null; |
0 | 294 |
}, |
295 |
||
296 |
addCallback: function(callback) { |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
297 |
if (this._req.readyState == 4) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
298 |
if (this._result) { callback.apply(null, this._result, this._req); } |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
299 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
300 |
else { this._onSuccess.push([callback, sliceList(arguments, 1)]); } |
0 | 301 |
return this; |
302 |
}, |
|
303 |
||
304 |
addErrback: function(callback) { |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
305 |
if (this._req.readyState == 4) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
306 |
if (this._error) { callback.apply(null, this._error, this._req); } |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
307 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
308 |
else { this._onFailure.push([callback, sliceList(arguments, 1)]); } |
0 | 309 |
return this; |
310 |
}, |
|
311 |
||
312 |
success: function(result) { |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
313 |
this._result = result; |
0 | 314 |
try { |
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
315 |
for (var i=0; i<this._onSuccess.length; i++) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
316 |
var callback = this._onSuccess[i][0]; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
317 |
var args = merge([result, this._req], this._onSuccess[i][1]); |
0 | 318 |
callback.apply(null, args); |
319 |
} |
|
320 |
} catch (error) { |
|
321 |
this.error(this.xhr, null, error); |
|
322 |
} |
|
323 |
}, |
|
324 |
||
325 |
error: function(xhr, status, error) { |
|
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
326 |
this._error = error; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
327 |
for (var i=0; i<this._onFailure.length; i++) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
328 |
var callback = this._onFailure[i][0]; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
329 |
var args = merge([error, this._req], this._onFailure[i][1]); |
0 | 330 |
callback.apply(null, args); |
331 |
} |
|
332 |
} |
|
333 |
||
334 |
}); |
|
335 |
||
336 |
||
5186
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
337 |
/* |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
338 |
* Asynchronously load an url and return a deferred |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
339 |
* whose callbacks args are decoded according to |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
340 |
* the Content-Type response header |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
341 |
*/ |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
342 |
function loadRemote(url, data, reqtype) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
343 |
var d = new Deferred(); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
344 |
jQuery.ajax({ |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
345 |
url: url, |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
346 |
type: reqtype, |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
347 |
data: data, |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
348 |
|
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
349 |
beforeSend: function(xhr) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
350 |
d._req = xhr; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
351 |
}, |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
352 |
|
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
353 |
success: function(data, status) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
354 |
if (d._req.getResponseHeader("content-type") == 'application/json') { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
355 |
data = evalJSON(data); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
356 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
357 |
d.success(data); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
358 |
}, |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
359 |
|
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
360 |
error: function(xhr, status, error) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
361 |
try { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
362 |
if (xhr.status == 500) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
363 |
var reason_dict = evalJSON(xhr.responseText); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
364 |
d.error(xhr, status, reason_dict['reason']); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
365 |
return; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
366 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
367 |
} catch(exc) { |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
368 |
log('error with server side error report:' + exc); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
369 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
370 |
d.error(xhr, status, null); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
371 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
372 |
}); |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
373 |
return d; |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
374 |
} |
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
375 |
|
f3c2cb460ad9
[doc] note on pytestconf, fixlets
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3096
diff
changeset
|
376 |
|
0 | 377 |
/** @id MochiKit.DateTime.toISOTime */ |
378 |
toISOTime = function (date, realISO/* = false */) { |
|
379 |
if (typeof(date) == "undefined" || date === null) { |
|
380 |
return null; |
|
381 |
} |
|
382 |
var hh = date.getHours(); |
|
383 |
var mm = date.getMinutes(); |
|
384 |
var ss = date.getSeconds(); |
|
385 |
var lst = [ |
|
386 |
((realISO && (hh < 10)) ? "0" + hh : hh), |
|
387 |
((mm < 10) ? "0" + mm : mm), |
|
388 |
((ss < 10) ? "0" + ss : ss) |
|
389 |
]; |
|
390 |
return lst.join(":"); |
|
391 |
}; |
|
392 |
||
393 |
_padTwo = function (n) { |
|
394 |
return (n > 9) ? n : "0" + n; |
|
395 |
}; |
|
396 |
||
397 |
/** @id MochiKit.DateTime.toISODate */ |
|
398 |
toISODate = function (date) { |
|
399 |
if (typeof(date) == "undefined" || date === null) { |
|
400 |
return null; |
|
401 |
} |
|
402 |
return [ |
|
403 |
date.getFullYear(), |
|
404 |
_padTwo(date.getMonth() + 1), |
|
405 |
_padTwo(date.getDate()) |
|
406 |
].join("-"); |
|
407 |
}; |
|
408 |
||
409 |
||
410 |
/** @id MochiKit.DateTime.toISOTimeStamp */ |
|
411 |
toISOTimestamp = function (date, realISO/* = false*/) { |
|
412 |
if (typeof(date) == "undefined" || date === null) { |
|
413 |
return null; |
|
414 |
} |
|
415 |
var sep = realISO ? "T" : " "; |
|
416 |
var foot = realISO ? "Z" : ""; |
|
417 |
if (realISO) { |
|
418 |
date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); |
|
419 |
} |
|
420 |
return toISODate(date) + sep + toISOTime(date, realISO) + foot; |
|
421 |
}; |
|
422 |
||
423 |
||
424 |
||
425 |
/* depth-first implementation of the nodeWalk function found |
|
426 |
* in MochiKit.Base |
|
427 |
* cf. http://mochikit.com/doc/html/MochiKit/Base.html#fn-nodewalk |
|
428 |
*/ |
|
429 |
function nodeWalkDepthFirst(node, visitor) { |
|
430 |
var children = visitor(node); |
|
431 |
if (children) { |
|
432 |
for(var i=0; i<children.length; i++) { |
|
433 |
nodeWalkDepthFirst(children[i], visitor); |
|
434 |
} |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
||
439 |
/* Returns true if all the given Array-like or string arguments are not empty (obj.length > 0) */ |
|
440 |
function isNotEmpty(obj) { |
|
441 |
for (var i = 0; i < arguments.length; i++) { |
|
442 |
var o = arguments[i]; |
|
443 |
if (!(o && o.length)) { |
|
444 |
return false; |
|
445 |
} |
|
446 |
} |
|
447 |
return true; |
|
448 |
} |
|
449 |
||
450 |
/** this implementation comes from MochiKit */ |
|
451 |
function formContents(elem/* = document.body */) { |
|
452 |
var names = []; |
|
453 |
var values = []; |
|
454 |
if (typeof(elem) == "undefined" || elem === null) { |
|
455 |
elem = document.body; |
|
456 |
} else { |
|
457 |
elem = getNode(elem); |
|
458 |
} |
|
459 |
nodeWalkDepthFirst(elem, function (elem) { |
|
460 |
var name = elem.name; |
|
461 |
if (isNotEmpty(name)) { |
|
462 |
var tagName = elem.tagName.toUpperCase(); |
|
463 |
if (tagName === "INPUT" |
|
464 |
&& (elem.type == "radio" || elem.type == "checkbox") |
|
465 |
&& !elem.checked |
|
466 |
) { |
|
467 |
return null; |
|
468 |
} |
|
469 |
if (tagName === "SELECT") { |
|
470 |
if (elem.type == "select-one") { |
|
471 |
if (elem.selectedIndex >= 0) { |
|
472 |
var opt = elem.options[elem.selectedIndex]; |
|
473 |
var v = opt.value; |
|
474 |
if (!v) { |
|
475 |
var h = opt.outerHTML; |
|
476 |
// internet explorer sure does suck. |
|
477 |
if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
|
478 |
v = opt.text; |
|
479 |
} |
|
480 |
} |
|
481 |
names.push(name); |
|
482 |
values.push(v); |
|
483 |
return null; |
|
484 |
} |
|
485 |
// no form elements? |
|
486 |
names.push(name); |
|
487 |
values.push(""); |
|
488 |
return null; |
|
489 |
} else { |
|
490 |
var opts = elem.options; |
|
491 |
if (!opts.length) { |
|
492 |
names.push(name); |
|
493 |
values.push(""); |
|
494 |
return null; |
|
495 |
} |
|
496 |
for (var i = 0; i < opts.length; i++) { |
|
497 |
var opt = opts[i]; |
|
498 |
if (!opt.selected) { |
|
499 |
continue; |
|
500 |
} |
|
501 |
var v = opt.value; |
|
502 |
if (!v) { |
|
503 |
var h = opt.outerHTML; |
|
504 |
// internet explorer sure does suck. |
|
505 |
if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
|
506 |
v = opt.text; |
|
507 |
} |
|
508 |
} |
|
509 |
names.push(name); |
|
510 |
values.push(v); |
|
511 |
} |
|
512 |
return null; |
|
513 |
} |
|
514 |
} |
|
515 |
if (tagName === "FORM" || tagName === "P" || tagName === "SPAN" |
|
516 |
|| tagName === "DIV" |
|
517 |
) { |
|
518 |
return elem.childNodes; |
|
519 |
} |
|
520 |
names.push(name); |
|
521 |
values.push(elem.value || ''); |
|
522 |
return null; |
|
523 |
} |
|
524 |
return elem.childNodes; |
|
525 |
}); |
|
526 |
return [names, values]; |
|
527 |
} |
|
528 |
||
529 |
function merge(array1, array2) { |
|
530 |
var result = []; |
|
531 |
for (var i=0,length=arguments.length; i<length; i++) { |
|
532 |
var array = arguments[i]; |
|
533 |
for (var j=0,alength=array.length; j<alength; j++) { |
|
534 |
result.push(array[j]); |
|
535 |
} |
|
536 |
} |
|
537 |
return result; |
|
538 |
} |
|
539 |
||
540 |
var KEYS = { |
|
541 |
KEY_ESC: 27, |
|
542 |
KEY_ENTER: 13 |
|
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1419
diff
changeset
|
543 |
}; |
0 | 544 |
|
545 |
||
1563
b130c6cec8c2
loadJSON -> loadRemote, correctly handle json
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1419
diff
changeset
|
546 |