author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2012-03-17 21:08:23 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2012-03-17 21:08:23 (UTC) |
commit | 928f3f3ed3981f7f81b69ed94f2a315205db39fa (patch) (unidiff) | |
tree | 8a47229b56e4c906de8512baf0c5ca100bc03dfb /frontend/gamma/js/MochiKit/Async.js | |
parent | bf7d8191a3a6dbd092a88911098a3e7f6cf30cf7 (diff) | |
download | clipperz-928f3f3ed3981f7f81b69ed94f2a315205db39fa.zip clipperz-928f3f3ed3981f7f81b69ed94f2a315205db39fa.tar.gz clipperz-928f3f3ed3981f7f81b69ed94f2a315205db39fa.tar.bz2 |
Fixed frontend properties and updated MochiKit version
Diffstat (limited to 'frontend/gamma/js/MochiKit/Async.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/Async.js | 120 |
1 files changed, 93 insertions, 27 deletions
diff --git a/frontend/gamma/js/MochiKit/Async.js b/frontend/gamma/js/MochiKit/Async.js index c7408e7..cc43835 100644 --- a/frontend/gamma/js/MochiKit/Async.js +++ b/frontend/gamma/js/MochiKit/Async.js | |||
@@ -7,9 +7,9 @@ See <http://mochikit.com/> for documentation, downloads, license, etc. | |||
7 | (c) 2005 Bob Ippolito. All rights Reserved. | 7 | (c) 2005 Bob Ippolito. All rights Reserved. |
8 | 8 | ||
9 | ***/ | 9 | ***/ |
10 | 10 | ||
11 | MochiKit.Base._module('Async', '1.5', ['Base']); | 11 | MochiKit.Base.module(MochiKit, 'Async', '1.5', ['Base']); |
12 | 12 | ||
13 | /** @id MochiKit.Async.Deferred */ | 13 | /** @id MochiKit.Async.Deferred */ |
14 | MochiKit.Async.Deferred = function (/* optional */ canceller) { | 14 | MochiKit.Async.Deferred = function (/* optional */ canceller) { |
15 | this.chain = []; | 15 | this.chain = []; |
@@ -19,42 +19,51 @@ MochiKit.Async.Deferred = function (/* optional */ canceller) { | |||
19 | this.results = [null, null]; | 19 | this.results = [null, null]; |
20 | this.canceller = canceller; | 20 | this.canceller = canceller; |
21 | this.silentlyCancelled = false; | 21 | this.silentlyCancelled = false; |
22 | this.chained = false; | 22 | this.chained = false; |
23 | this.finalized = false; | ||
23 | }; | 24 | }; |
24 | 25 | ||
25 | MochiKit.Async.Deferred.prototype = { | 26 | MochiKit.Async.Deferred.prototype = { |
26 | /** @id MochiKit.Async.Deferred.prototype.repr */ | 27 | /** @id MochiKit.Async.Deferred.prototype.repr */ |
27 | repr: function () { | 28 | repr: function () { |
28 | var state; | 29 | return 'Deferred(' + this.id + ', ' + this.state() + ')'; |
29 | if (this.fired == -1) { | ||
30 | state = 'unfired'; | ||
31 | } else if (this.fired === 0) { | ||
32 | state = 'success'; | ||
33 | } else { | ||
34 | state = 'error'; | ||
35 | } | ||
36 | return 'Deferred(' + this.id + ', ' + state + ')'; | ||
37 | }, | 30 | }, |
38 | 31 | ||
39 | toString: MochiKit.Base.forwardCall("repr"), | 32 | toString: MochiKit.Base.forwardCall("repr"), |
40 | 33 | ||
41 | _nextId: MochiKit.Base.counter(), | 34 | _nextId: MochiKit.Base.counter(), |
42 | 35 | ||
36 | /** @id MochiKit.Async.Deferred.prototype.state */ | ||
37 | state: function () { | ||
38 | if (this.fired == -1) { | ||
39 | return 'unfired'; | ||
40 | } else if (this.fired === 0) { | ||
41 | return 'success'; | ||
42 | } else { | ||
43 | return 'error'; | ||
44 | } | ||
45 | }, | ||
46 | |||
43 | /** @id MochiKit.Async.Deferred.prototype.cancel */ | 47 | /** @id MochiKit.Async.Deferred.prototype.cancel */ |
44 | cancel: function () { | 48 | cancel: function (e) { |
45 | var self = MochiKit.Async; | 49 | var self = MochiKit.Async; |
46 | if (this.fired == -1) { | 50 | if (this.fired == -1) { |
47 | if (this.canceller) { | 51 | if (this.canceller) { |
48 | this.canceller(this); | 52 | this.canceller(this); |
49 | } else { | 53 | } else { |
50 | this.silentlyCancelled = true; | 54 | this.silentlyCancelled = true; |
51 | } | 55 | } |
52 | if (this.fired == -1) { | 56 | if (this.fired == -1) { |
53 | this.errback(new self.CancelledError(this)); | 57 | if (typeof(e) === 'string') { |
58 | e = new self.GenericError(e); | ||
59 | } else if (!(e instanceof Error)) { | ||
60 | e = new self.CancelledError(this); | ||
61 | } | ||
62 | this.errback(e); | ||
54 | } | 63 | } |
55 | } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { | 64 | } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) { |
56 | this.results[0].cancel(); | 65 | this.results[0].cancel(e); |
57 | } | 66 | } |
58 | }, | 67 | }, |
59 | 68 | ||
60 | _resback: function (res) { | 69 | _resback: function (res) { |
@@ -64,9 +73,11 @@ MochiKit.Async.Deferred.prototype = { | |||
64 | 73 | ||
65 | ***/ | 74 | ***/ |
66 | this.fired = ((res instanceof Error) ? 1 : 0); | 75 | this.fired = ((res instanceof Error) ? 1 : 0); |
67 | this.results[this.fired] = res; | 76 | this.results[this.fired] = res; |
68 | this._fire(); | 77 | if (this.paused === 0) { |
78 | this._fire(); | ||
79 | } | ||
69 | }, | 80 | }, |
70 | 81 | ||
71 | _check: function () { | 82 | _check: function () { |
72 | if (this.fired != -1) { | 83 | if (this.fired != -1) { |
@@ -128,15 +139,36 @@ MochiKit.Async.Deferred.prototype = { | |||
128 | addCallbacks: function (cb, eb) { | 139 | addCallbacks: function (cb, eb) { |
129 | if (this.chained) { | 140 | if (this.chained) { |
130 | throw new Error("Chained Deferreds can not be re-used"); | 141 | throw new Error("Chained Deferreds can not be re-used"); |
131 | } | 142 | } |
143 | if (this.finalized) { | ||
144 | throw new Error("Finalized Deferreds can not be re-used"); | ||
145 | } | ||
132 | this.chain.push([cb, eb]); | 146 | this.chain.push([cb, eb]); |
133 | if (this.fired >= 0) { | 147 | if (this.fired >= 0) { |
134 | this._fire(); | 148 | this._fire(); |
135 | } | 149 | } |
136 | return this; | 150 | return this; |
137 | }, | 151 | }, |
138 | 152 | ||
153 | /** @id MochiKit.Async.Deferred.prototype.setFinalizer */ | ||
154 | setFinalizer: function (fn) { | ||
155 | if (this.chained) { | ||
156 | throw new Error("Chained Deferreds can not be re-used"); | ||
157 | } | ||
158 | if (this.finalized) { | ||
159 | throw new Error("Finalized Deferreds can not be re-used"); | ||
160 | } | ||
161 | if (arguments.length > 1) { | ||
162 | fn = MochiKit.Base.partial.apply(null, arguments); | ||
163 | } | ||
164 | this._finalizer = fn; | ||
165 | if (this.fired >= 0) { | ||
166 | this._fire(); | ||
167 | } | ||
168 | return this; | ||
169 | }, | ||
170 | |||
139 | _fire: function () { | 171 | _fire: function () { |
140 | /*** | 172 | /*** |
141 | 173 | ||
142 | Used internally to exhaust the callback sequence when a result | 174 | Used internally to exhaust the callback sequence when a result |
@@ -159,13 +191,10 @@ MochiKit.Async.Deferred.prototype = { | |||
159 | res = f(res); | 191 | res = f(res); |
160 | fired = ((res instanceof Error) ? 1 : 0); | 192 | fired = ((res instanceof Error) ? 1 : 0); |
161 | if (res instanceof MochiKit.Async.Deferred) { | 193 | if (res instanceof MochiKit.Async.Deferred) { |
162 | cb = function (res) { | 194 | cb = function (res) { |
163 | self._resback(res); | ||
164 | self.paused--; | 195 | self.paused--; |
165 | if ((self.paused === 0) && (self.fired >= 0)) { | 196 | self._resback(res); |
166 | self._fire(); | ||
167 | } | ||
168 | }; | 197 | }; |
169 | this.paused++; | 198 | this.paused++; |
170 | } | 199 | } |
171 | } catch (err) { | 200 | } catch (err) { |
@@ -177,8 +206,12 @@ MochiKit.Async.Deferred.prototype = { | |||
177 | } | 206 | } |
178 | } | 207 | } |
179 | this.fired = fired; | 208 | this.fired = fired; |
180 | this.results[fired] = res; | 209 | this.results[fired] = res; |
210 | if (this.chain.length == 0 && this.paused === 0 && this._finalizer) { | ||
211 | this.finalized = true; | ||
212 | this._finalizer(res); | ||
213 | } | ||
181 | if (cb && this.paused) { | 214 | if (cb && this.paused) { |
182 | // this is for "tail recursion" in case the dependent deferred | 215 | // this is for "tail recursion" in case the dependent deferred |
183 | // is already fired | 216 | // is already fired |
184 | res.addBoth(cb); | 217 | res.addBoth(cb); |
@@ -248,9 +281,9 @@ MochiKit.Base.update(MochiKit.Async, { | |||
248 | } | 281 | } |
249 | var status = null; | 282 | var status = null; |
250 | try { | 283 | try { |
251 | status = this.status; | 284 | status = this.status; |
252 | if (!status && m.isNotEmpty(this.responseText)) { | 285 | if (!status && (this.response || m.isNotEmpty(this.responseText))) { |
253 | // 0 or undefined seems to mean cached or local | 286 | // 0 or undefined seems to mean cached or local |
254 | status = 304; | 287 | status = 304; |
255 | } | 288 | } |
256 | } catch (e) { | 289 | } catch (e) { |
@@ -336,9 +369,10 @@ MochiKit.Base.update(MochiKit.Async, { | |||
336 | queryString: undefined, | 369 | queryString: undefined, |
337 | username: undefined, | 370 | username: undefined, |
338 | password: undefined, | 371 | password: undefined, |
339 | headers: undefined, | 372 | headers: undefined, |
340 | mimeType: undefined | 373 | mimeType: undefined, |
374 | responseType: undefined | ||
341 | */ | 375 | */ |
342 | }, opts); | 376 | }, opts); |
343 | var self = MochiKit.Async; | 377 | var self = MochiKit.Async; |
344 | var req = self.getXMLHttpRequest(); | 378 | var req = self.getXMLHttpRequest(); |
@@ -370,8 +404,11 @@ MochiKit.Base.update(MochiKit.Async, { | |||
370 | var value = header[1]; | 404 | var value = header[1]; |
371 | req.setRequestHeader(name, value); | 405 | req.setRequestHeader(name, value); |
372 | } | 406 | } |
373 | } | 407 | } |
408 | if ("responseType" in opts && "responseType" in req) { | ||
409 | req.responseType = opts.responseType; | ||
410 | } | ||
374 | return self.sendXMLHttpRequest(req, opts.sendContent); | 411 | return self.sendXMLHttpRequest(req, opts.sendContent); |
375 | }, | 412 | }, |
376 | 413 | ||
377 | _buildURL: function (url/*, ...*/) { | 414 | _buildURL: function (url/*, ...*/) { |
@@ -403,18 +440,46 @@ MochiKit.Base.update(MochiKit.Async, { | |||
403 | d = d.addCallback(self.evalJSONRequest); | 440 | d = d.addCallback(self.evalJSONRequest); |
404 | return d; | 441 | return d; |
405 | }, | 442 | }, |
406 | 443 | ||
444 | /** @id MochiKit.Async.loadScript */ | ||
445 | loadScript: function (url) { | ||
446 | var d = new MochiKit.Async.Deferred(); | ||
447 | var script = document.createElement("script"); | ||
448 | script.type = "text/javascript"; | ||
449 | script.src = url; | ||
450 | script.onload = function () { | ||
451 | script.onload = null; | ||
452 | script.onerror = null; | ||
453 | script.onreadystatechange = null; | ||
454 | script = null; | ||
455 | d.callback(); | ||
456 | }; | ||
457 | script.onerror = function (msg) { | ||
458 | script.onload = null; | ||
459 | script.onerror = null; | ||
460 | script.onreadystatechange = null; | ||
461 | script = null; | ||
462 | msg = "Failed to load script at " + url + ": " + msg; | ||
463 | d.errback(new URIError(msg, url)); | ||
464 | } | ||
465 | script.onreadystatechange = function () { | ||
466 | if (script.readyState == "loaded" || script.readyState == "complete") { | ||
467 | script.onload(); | ||
468 | } else { | ||
469 | // IE doesn't bother to report errors... | ||
470 | MochiKit.Async.callLater(10, script.onerror, "Script loading timed out") | ||
471 | } | ||
472 | }; | ||
473 | document.getElementsByTagName("head")[0].appendChild(script); | ||
474 | return d; | ||
475 | }, | ||
476 | |||
407 | /** @id MochiKit.Async.wait */ | 477 | /** @id MochiKit.Async.wait */ |
408 | wait: function (seconds, /* optional */value) { | 478 | wait: function (seconds, /* optional */value) { |
409 | var d = new MochiKit.Async.Deferred(); | 479 | var d = new MochiKit.Async.Deferred(); |
410 | var m = MochiKit.Base; | 480 | var cb = MochiKit.Base.bind("callback", d, value); |
411 | if (typeof(value) != 'undefined') { | 481 | var timeout = setTimeout(cb, Math.floor(seconds * 1000)); |
412 | d.addCallback(function () { return value; }); | ||
413 | } | ||
414 | var timeout = setTimeout( | ||
415 | m.bind("callback", d), | ||
416 | Math.floor(seconds * 1000)); | ||
417 | d.canceller = function () { | 482 | d.canceller = function () { |
418 | try { | 483 | try { |
419 | clearTimeout(timeout); | 484 | clearTimeout(timeout); |
420 | } catch (e) { | 485 | } catch (e) { |
@@ -509,8 +574,9 @@ MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, f | |||
509 | 574 | ||
510 | }; | 575 | }; |
511 | 576 | ||
512 | MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); | 577 | MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred(); |
578 | MochiKit.Async.DeferredList.prototype.constructor = MochiKit.Async.DeferredList; | ||
513 | 579 | ||
514 | MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) { | 580 | MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) { |
515 | this.resultList[index] = [succeeded, result]; | 581 | this.resultList[index] = [succeeded, result]; |
516 | this.finishedCount += 1; | 582 | this.finishedCount += 1; |