r20446: rename swat directory to swat.obsolete; keeping it around since there is...
[samba.git] / swat.obsolete / apps / qooxdoo-examples / test / Compile_1.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4   <title>qooxdoo &raquo; Demo</title>
5   <link type="text/css" rel="stylesheet" href="../../resource/css/layout.css"/>
6   <!--[if IE]>
7   <link type="text/css" rel="stylesheet" href="../../resource/css/layout_ie.css"/>
8   <![endif]-->
9   <script type="text/javascript" src="../../script/qx.js"></script>
10 </head>
11 <body>
12   <script type="text/javascript" src="../../script/layout.js"></script>
13
14   <div id="demoDescription">
15   </div>
16
17   <textarea id="resultjs" style="position:absolute;width:600px;height:400px;background:white;top:120px;left:20px;border:1px solid black;overflow:auto"></textarea>
18   <textarea id="resulthtml" style="position:absolute;width:600px;height:100px;background:white;top:530px;left:20px;border:1px solid black;overflow:auto"></textarea>
19
20   <script type="text/javascript">
21     jsout = document.getElementById("resultjs");
22     htmlout = document.getElementById("resulthtml");
23
24     var _emptyTags = {
25        "IMG":   true,
26        "BR":    true,
27        "INPUT": true,
28        "META":  true,
29        "LINK":  true,
30        "PARAM": true,
31        "HR":    true
32     };
33
34     HTMLElement.prototype.__defineGetter__("outerHTML", function () {
35        var attrs = this.attributes;
36        var str = "<" + this.tagName.toLowerCase();
37        for (var i = 0; i < attrs.length; i++)
38           str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";
39
40        if (_emptyTags[this.tagName])
41           return str + ">";
42
43        return str + ">" + this.innerHTML + "</" + this.tagName.toLowerCase() + ">";
44     });
45
46
47
48
49
50
51
52
53     qx.core.Init.getInstance().defineMain(function()
54     {
55       a1 = new qx.ui.form.Button("Hello World", "icon/32/penguin.png");
56       a1.setLocation(20, 50);
57       qx.ui.core.ClientDocument.getInstance().add(a1);
58
59       a2 = new qx.ui.form.Button("Hello World", "icon/32/appearance.png");
60       a2.setLocation(220, 50);
61       qx.ui.core.ClientDocument.getInstance().add(a2);
62     });
63
64     qx.core.Init.getInstance().defineFinalize(function()
65     {
66         var str = "qx.core.Init.getInstance().defineMain(function()\n{\n";
67
68
69
70
71         qx.core.Init.getInstance().debug("Updating HTML");
72
73         htmlout.value = a1.getElement().outerHTML + a2.getElement().outerHTML;
74
75
76
77
78
79
80
81
82         qx.core.Init.getInstance().debug("Serialize...");
83         var vBase1 = serializeMain(a1);
84         var vBase2 = serializeMain(a2);
85
86         qx.core.Init.getInstance().debug("Base1: " + vBase1);
87         qx.core.Init.getInstance().debug("Base2: " + vBase2);
88         qx.core.Init.getInstance().debug("Tree Size: " + vObjectCache.length);
89         qx.core.Init.getInstance().debug("Generating Code...");
90
91         for (var i=0, n, v, c, cv, l=vObjectCache.length; i<l; i++)
92         {
93           v = vObjectCache[i].value;
94           c = vObjectCache[i].compiled;
95
96           switch(typeof v)
97           {
98             case qx.constant.Type.OBJECT:
99               if (v == null)
100               {
101                 str += "$" + i + " = null;\n";
102               }
103               else if (v instanceof qx.core.Object)
104               {
105                 // qx.core.Init.getInstance().debug("QXOBJECT: " + i + "=" + vObjectCache[i].value);
106
107                 if (typeof c == qx.constant.Type.STRING && c.indexOf("$OBJECTMAPPER-") == 0)
108                 {
109                   cv = c.replace("$OBJECTMAPPER-", "");
110
111                   switch(cv)
112                   {
113                     case "qx.ui.core.ClientDocument":
114                       str += "$" + i + " = qx.core.Init.getComponent().getClientWindow()().getClientDocument();\n";
115                       break;
116
117                     case "qx.manager.object.ImageManager":
118                       str += "$" + i + " = new qx.manager.object.ImageManager;\n";
119                       break;
120                   };
121                 }
122                 else
123                 {
124                   str += "var f = new Function();\n";
125                   str += "f.prototype = " + v.classname + ".prototype;\n";
126                   str += "$" + i + " = new f;\n";
127                   str += "QxObjectDataBase[" + v._hashCode + "] = $" + i + ";\n";
128
129                   // constructor call
130                   // str += v.classname + ".call($" + i + ");\n";
131
132                   if (v instanceof qx.ui.core.Widget && v.isCreated())
133                   {
134                     // map elements
135                     str += "$" + i + "._valueElement = cssQuery(\"[qxhashcode=" + v._hashCode + "]\")[0];\n";
136                     str += "$" + i + "._element = $" + i + "._valueElement;\n";
137                     str += "$" + i + "._style = $" + i + "._valueElement.style;\n";
138
139                     // map qx_Widget attribute of node to widget
140                     str += "$" + i + "._valueElement.qx_Widget = $" + i + ";\n";
141
142                     if (v instanceof qx.ui.basic.Image)
143                     {
144                       // assign _image
145                       str += "$" + i + "._image = $" + i + "._valueElement.getElementsByTagName(\"img\")[0];\n";
146                     };
147                   }
148                   else if (v instanceof qx.io.image.ImagePreloader)
149                   {
150                     str += "$" + i + "._element = new Image;\n";
151                     str += "$" + i + "._element.src = \"" + v.getSource() + "\";\n";
152                   };
153                 };
154               }
155               else if (v instanceof Array)
156               {
157                 // qx.core.Init.getInstance().debug("JSARRAY: " + i + "=" + vObjectCache[i].value);
158
159                 str += "$" + i + " = [];\n";
160               }
161               else
162               {
163                 // qx.core.Init.getInstance().debug("JSHASHTABLE: " + i + "=" + vObjectCache[i].value);
164
165                 str += "$" + i + " = {};\n";
166               };
167
168               break;
169
170             case qx.constant.Type.STRING:
171               str += "$" + i + " = \"" + vObjectCache[i].value + "\";\n";
172               break;
173
174             case qx.constant.Type.NUMBER:
175             case qx.constant.Type.BOOLEAN:
176               str += "$" + i + " = " + v + ";\n";
177               break;
178
179             case qx.constant.Type.UNDEFINED:
180               // qx.core.Init.getInstance().debug("Undefined value found: " + v);
181               str += "$" + i + " = null;\n";
182               break;
183
184             case qx.constant.Type.FUNCTION:
185               if (typeof c == qx.constant.Type.STRING && c.indexOf("$FUNCTIONMAPPER-") == 0)
186               {
187                 cv = c.replace("$FUNCTIONMAPPER-", "").split("|");
188                 str += "$" + i + "= QxObjectDataBase[" + cv[1] + "]." + cv[0] + ";\n";
189               };
190               break;
191
192             default:
193               qx.core.Init.getInstance().debug("Unsupported value: " + v);
194               str += "var $" + i + " = \"UNSUPPORTED VALUE\";\n";
195
196           };
197         };
198
199
200
201
202
203
204
205         for (var i=0, n, c, v, cv, l=vObjectCache.length; i<l; i++)
206         {
207           v = vObjectCache[i].value;
208           c = vObjectCache[i].compiled;
209
210           if (typeof v == qx.constant.Type.OBJECT && v != null)
211           {
212             if (v instanceof qx.core.Object)
213             {
214               if (typeof c == qx.constant.Type.STRING && c.indexOf("$OBJECTMAPPER-") == 0)
215               {
216                 continue;
217               };
218
219               for (vKey in vObjectCache[i].compiled)
220               {
221                 if (vObjectCache[i].compiled[vKey] != null)
222                 {
223                   str += "$" + i + "." + vKey + " = $" + vObjectCache[i].compiled[vKey] + ";\n";
224                 };
225               };
226             }
227             else if (v instanceof Array)
228             {
229               for (var ia=0, la=vObjectCache[i].compiled.length; ia<la; ia++)
230               {
231                 str += "$" + i + "[" + ia + "] = $" + vObjectCache[i].compiled[ia] + ";\n";
232               };
233             }
234             else
235             {
236               for (vKey in vObjectCache[i].compiled)
237               {
238                 if (vObjectCache[i].compiled[vKey] != null)
239                 {
240                   str += "$" + i + "[\"" + vKey + "\"] = $" + vObjectCache[i].compiled[vKey] + ";\n";
241                 };
242               };
243             };
244           };
245         };
246
247
248
249
250
251         str += "};";
252
253         jsout.value = str;
254
255         qx.core.Init.getInstance().debug("Done");
256       });
257
258
259
260
261       var vObjectCache = [];
262       var vLastListenerContext = null;
263
264       function serializeMain(vValue)
265       {
266         var vValue, vIter, vLength;
267
268         // Completly ignore the window and document built-in object
269         if (vValue == window || vValue == document) {
270           return null;
271         };
272
273         // Completly ignore DOM nodes
274         if (qx.util.Validation.isValid(vValue) && typeof vValue.nodeType != qx.constant.Type.UNDEFINED) {
275           return null;
276         };
277
278         for (vIter=0, vLength=vObjectCache.length; vIter<vLength; vIter++)
279         {
280           if (vObjectCache[vIter].value === vValue)
281           {
282             return vIter;
283           };
284         };
285
286         //QxDebug("Serializer", "Known: " + vObjectCache);
287         //QxDebug("Serializer", "Serialize: " + vValue + "(" + typeof vValue + ")");
288
289         var vPos = vObjectCache.length;
290
291         vObjectCache[vPos] = { value : vValue };
292         vObjectCache[vPos].compiled = serializeWrapper(vValue);
293
294         return vPos;
295       };
296
297       function serializeWrapper(vInput)
298       {
299         switch(typeof vInput)
300         {
301           case qx.constant.Type.STRING:
302           case qx.constant.Type.NUMBER:
303           case qx.constant.Type.BOOLEAN:
304             return serializeSimple(vInput);
305
306           case qx.constant.Type.OBJECT:
307             if (vInput == null)
308             {
309               return serializeSimple(vInput);
310             }
311             else if (vInput instanceof Array)
312             {
313               return serializeArray(vInput);
314             }
315             else
316             {
317               return serializeObject(vInput);
318             };
319
320           case qx.constant.Type.FUNCTION:
321             //QxDebug("Serializer", "FUNCTION: " + vInput);
322             return null;
323         };
324
325         return null;
326       };
327
328       function serializeSimple(vSimple) {
329         return vSimple;
330       };
331
332       function serializeObject(vObject)
333       {
334         if (vObject instanceof qx.ui.core.ClientDocument) {
335           return "$OBJECTMAPPER-qx.ui.core.ClientDocument";
336         }
337         else if (vObject instanceof qx.manager.object.ImageManager) {
338           return "$OBJECTMAPPER-qx.manager.object.ImageManager";
339         };
340
341         var vKey;
342         var vOut = {};
343
344         for (vKey in vObject)
345         {
346           // ignore css reference
347           switch(vKey)
348           {
349             case "_style":
350               continue;
351
352             case "_listeners":
353               // QxDebug("Serializer", "Found Listeners: " + vKey + "::" + vObject);
354               vLastListenerContext = vObject;
355               break;
356           };
357
358           if (typeof vObject[vKey] == qx.constant.Type.FUNCTION)
359           {
360             if (!(vObject instanceof qx.core.Object) && vLastListenerContext)
361             {
362               var to = new vLastListenerContext.constructor;
363               var tm = null;
364
365               for (vSubKey in to)
366               {
367                 if (to[vSubKey] === vObject[vKey])
368                 {
369                   tm = vSubKey;
370                   break;
371                 };
372               };
373
374               if (tm)
375               {
376                 qx.core.Init.getInstance().debug("Serializer", "Function Mapped: " + tm);
377                 var ts = { value : vObject[vKey], compiled : "$FUNCTIONMAPPER-" + tm + "|" + vLastListenerContext._hashCode };
378                 vObjectCache.push(ts);
379                 vOut[vKey] = vObjectCache.length-1;
380                 continue;
381               }
382               else
383               {
384                 qx.core.Init.getInstance().debug("Serializer", "Could not map function: " + vKey + "[" + vLastListenerContext + "]");
385               };
386
387               continue;
388             }
389             else
390             {
391               continue;
392             };
393           };
394
395           vOut[vKey] = serializeMain(vObject[vKey]);
396         };
397
398         return vOut;
399       };
400
401       function serializeArray(vArray)
402       {
403         var vKey;
404         var vOut = [];
405
406         for (var vIter=0, vLength=vArray.length; vIter<vLength; vIter++) {
407           vOut.push(serializeMain(vArray[vIter]));
408         };
409
410         return vOut;
411       };
412
413   </script>
414 </body>
415 </html>