r24958: This is the final text, and the final version. I'll send the release
[jelmer/samba4-debian.git] / webapps / qooxdoo-0.6.5-sdk / frontend / framework / source / class / qx / io / image / PreloaderSystem.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2007 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL: http://www.gnu.org/licenses/lgpl.html
12      EPL: http://www.eclipse.org/org/documents/epl-v10.php
13      See the LICENSE file in the project's top-level directory for details.
14
15    Authors:
16      * Sebastian Werner (wpbasti)
17      * Andreas Ecker (ecker)
18
19 ************************************************************************ */
20
21 /* ************************************************************************
22
23 #module(ui_core)
24
25 ************************************************************************ */
26
27 /**
28  * @event completed {qx.event.type.Event}
29  */
30 qx.OO.defineClass("qx.io.image.PreloaderSystem", qx.core.Target,
31 function(vPreloadList, vCallBack, vCallBackScope)
32 {
33   qx.core.Target.call(this);
34
35   this._list = vPreloadList;
36
37   // Create timer
38   this._timer = new qx.client.Timer(this.getSetting("timeout"));
39   this._timer.addEventListener("interval", this._oninterval, this);
40
41   // If we use the compact syntax, automatically add an event listeners and start the loading process
42   if (vCallBack)
43   {
44     this.addEventListener("completed", vCallBack, vCallBackScope || null);
45     this.start();
46   }
47 });
48
49 qx.Proto._stopped = false;
50
51
52
53 /*
54 ---------------------------------------------------------------------------
55   DEFAULT SETTINGS
56 ---------------------------------------------------------------------------
57 */
58
59 qx.Settings.setDefault("timeout", 3000);
60
61
62
63
64
65 /*
66 ---------------------------------------------------------------------------
67   USER ACCESS
68 ---------------------------------------------------------------------------
69 */
70
71 qx.Proto.start = function()
72 {
73   if (qx.lang.Object.isEmpty(this._list))
74   {
75     this.createDispatchEvent("completed");
76     return;
77   }
78
79   for (var vSource in this._list)
80   {
81     var vPreloader = qx.manager.object.ImagePreloaderManager.getInstance().create(qx.manager.object.AliasManager.getInstance().resolvePath(vSource));
82
83     if (vPreloader.isErroneous() || vPreloader.isLoaded())
84     {
85       delete this._list[vSource];
86     }
87     else
88     {
89       vPreloader._origSource = vSource;
90
91       vPreloader.addEventListener("load", this._onload, this);
92       vPreloader.addEventListener("error", this._onerror, this);
93     }
94   }
95
96   // Initial check
97   this._check();
98 }
99
100
101
102
103
104 /*
105 ---------------------------------------------------------------------------
106   EVENT LISTENERS
107 ---------------------------------------------------------------------------
108 */
109
110 qx.Proto._onload = function(e)
111 {
112   delete this._list[e.getTarget()._origSource];
113   this._check();
114 }
115
116 qx.Proto._onerror = function(e)
117 {
118   delete this._list[e.getTarget()._origSource];
119   this._check();
120 }
121
122 qx.Proto._oninterval = function(e)
123 {
124   this.error("Could not preload: " + qx.lang.Object.getKeysAsString(this._list));
125
126   this._stopped = true;
127   this._timer.stop();
128
129   this.createDispatchEvent("completed");
130 }
131
132
133
134
135
136
137 /*
138 ---------------------------------------------------------------------------
139   CHECK
140 ---------------------------------------------------------------------------
141 */
142
143 qx.Proto._check = function()
144 {
145   if (this._stopped) {
146     return;
147   }
148
149   // this.debug("Check: " + qx.lang.Object.getKeysAsString(this._list));
150
151   if (qx.lang.Object.isEmpty(this._list))
152   {
153     this._timer.stop();
154     this.createDispatchEvent("completed");
155   }
156   else
157   {
158     // Restart timer for timeout
159     this._timer.restart();
160   }
161 }
162
163
164
165
166
167
168 /*
169 ---------------------------------------------------------------------------
170   DISPOSER
171 ---------------------------------------------------------------------------
172 */
173
174 qx.Proto.dispose = function()
175 {
176   if (this.getDisposed()) {
177     return true;
178   }
179
180   this._list = null;
181
182   if (this._timer)
183   {
184     this._timer.dispose();
185     this._timer = null;
186   }
187
188   return qx.core.Target.prototype.dispose.call(this);
189 }