summaryrefslogtreecommitdiff
path: root/swat.obsolete/apps/qooxdoo-examples/test/Compile_1.html
blob: 8821238bea06babeceafd2f5542b87b93961683b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>qooxdoo &raquo; Demo</title>
  <link type="text/css" rel="stylesheet" href="../../resource/css/layout.css"/>
  <!--[if IE]>
  <link type="text/css" rel="stylesheet" href="../../resource/css/layout_ie.css"/>
  <![endif]-->
  <script type="text/javascript" src="../../script/qx.js"></script>
</head>
<body>
  <script type="text/javascript" src="../../script/layout.js"></script>

  <div id="demoDescription">
  </div>

  <textarea id="resultjs" style="position:absolute;width:600px;height:400px;background:white;top:120px;left:20px;border:1px solid black;overflow:auto"></textarea>
  <textarea id="resulthtml" style="position:absolute;width:600px;height:100px;background:white;top:530px;left:20px;border:1px solid black;overflow:auto"></textarea>

  <script type="text/javascript">
    jsout = document.getElementById("resultjs");
    htmlout = document.getElementById("resulthtml");

    var _emptyTags = {
       "IMG":   true,
       "BR":    true,
       "INPUT": true,
       "META":  true,
       "LINK":  true,
       "PARAM": true,
       "HR":    true
    };

    HTMLElement.prototype.__defineGetter__("outerHTML", function () {
       var attrs = this.attributes;
       var str = "<" + this.tagName.toLowerCase();
       for (var i = 0; i < attrs.length; i++)
          str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

       if (_emptyTags[this.tagName])
          return str + ">";

       return str + ">" + this.innerHTML + "</" + this.tagName.toLowerCase() + ">";
    });








    qx.core.Init.getInstance().defineMain(function()
    {
      a1 = new qx.ui.form.Button("Hello World", "icon/32/penguin.png");
      a1.setLocation(20, 50);
      qx.ui.core.ClientDocument.getInstance().add(a1);

      a2 = new qx.ui.form.Button("Hello World", "icon/32/appearance.png");
      a2.setLocation(220, 50);
      qx.ui.core.ClientDocument.getInstance().add(a2);
    });

    qx.core.Init.getInstance().defineFinalize(function()
    {
        var str = "qx.core.Init.getInstance().defineMain(function()\n{\n";




        qx.core.Init.getInstance().debug("Updating HTML");

        htmlout.value = a1.getElement().outerHTML + a2.getElement().outerHTML;








        qx.core.Init.getInstance().debug("Serialize...");
        var vBase1 = serializeMain(a1);
        var vBase2 = serializeMain(a2);

        qx.core.Init.getInstance().debug("Base1: " + vBase1);
        qx.core.Init.getInstance().debug("Base2: " + vBase2);
        qx.core.Init.getInstance().debug("Tree Size: " + vObjectCache.length);
        qx.core.Init.getInstance().debug("Generating Code...");

        for (var i=0, n, v, c, cv, l=vObjectCache.length; i<l; i++)
        {
          v = vObjectCache[i].value;
          c = vObjectCache[i].compiled;

          switch(typeof v)
          {
            case qx.constant.Type.OBJECT:
              if (v == null)
              {
                str += "$" + i + " = null;\n";
              }
              else if (v instanceof qx.core.Object)
              {
                // qx.core.Init.getInstance().debug("QXOBJECT: " + i + "=" + vObjectCache[i].value);

                if (typeof c == qx.constant.Type.STRING && c.indexOf("$OBJECTMAPPER-") == 0)
                {
                  cv = c.replace("$OBJECTMAPPER-", "");

                  switch(cv)
                  {
                    case "qx.ui.core.ClientDocument":
                      str += "$" + i + " = qx.core.Init.getComponent().getClientWindow()().getClientDocument();\n";
                      break;

                    case "qx.manager.object.ImageManager":
                      str += "$" + i + " = new qx.manager.object.ImageManager;\n";
                      break;
                  };
                }
                else
                {
                  str += "var f = new Function();\n";
                  str += "f.prototype = " + v.classname + ".prototype;\n";
                  str += "$" + i + " = new f;\n";
                  str += "QxObjectDataBase[" + v._hashCode + "] = $" + i + ";\n";

                  // constructor call
                  // str += v.classname + ".call($" + i + ");\n";

                  if (v instanceof qx.ui.core.Widget && v.isCreated())
                  {
                    // map elements
                    str += "$" + i + "._valueElement = cssQuery(\"[qxhashcode=" + v._hashCode + "]\")[0];\n";
                    str += "$" + i + "._element = $" + i + "._valueElement;\n";
                    str += "$" + i + "._style = $" + i + "._valueElement.style;\n";

                    // map qx_Widget attribute of node to widget
                    str += "$" + i + "._valueElement.qx_Widget = $" + i + ";\n";

                    if (v instanceof qx.ui.basic.Image)
                    {
                      // assign _image
                      str += "$" + i + "._image = $" + i + "._valueElement.getElementsByTagName(\"img\")[0];\n";
                    };
                  }
                  else if (v instanceof qx.io.image.ImagePreloader)
                  {
                    str += "$" + i + "._element = new Image;\n";
                    str += "$" + i + "._element.src = \"" + v.getSource() + "\";\n";
                  };
                };
              }
              else if (v instanceof Array)
              {
                // qx.core.Init.getInstance().debug("JSARRAY: " + i + "=" + vObjectCache[i].value);

                str += "$" + i + " = [];\n";
              }
              else
              {
                // qx.core.Init.getInstance().debug("JSHASHTABLE: " + i + "=" + vObjectCache[i].value);

                str += "$" + i + " = {};\n";
              };

              break;

            case qx.constant.Type.STRING:
              str += "$" + i + " = \"" + vObjectCache[i].value + "\";\n";
              break;

            case qx.constant.Type.NUMBER:
            case qx.constant.Type.BOOLEAN:
              str += "$" + i + " = " + v + ";\n";
              break;

            case qx.constant.Type.UNDEFINED:
              // qx.core.Init.getInstance().debug("Undefined value found: " + v);
              str += "$" + i + " = null;\n";
              break;

            case qx.constant.Type.FUNCTION:
              if (typeof c == qx.constant.Type.STRING && c.indexOf("$FUNCTIONMAPPER-") == 0)
              {
                cv = c.replace("$FUNCTIONMAPPER-", "").split("|");
                str += "$" + i + "= QxObjectDataBase[" + cv[1] + "]." + cv[0] + ";\n";
              };
              break;

            default:
              qx.core.Init.getInstance().debug("Unsupported value: " + v);
              str += "var $" + i + " = \"UNSUPPORTED VALUE\";\n";

          };
        };







        for (var i=0, n, c, v, cv, l=vObjectCache.length; i<l; i++)
        {
          v = vObjectCache[i].value;
          c = vObjectCache[i].compiled;

          if (typeof v == qx.constant.Type.OBJECT && v != null)
          {
            if (v instanceof qx.core.Object)
            {
              if (typeof c == qx.constant.Type.STRING && c.indexOf("$OBJECTMAPPER-") == 0)
              {
                continue;
              };

              for (vKey in vObjectCache[i].compiled)
              {
                if (vObjectCache[i].compiled[vKey] != null)
                {
                  str += "$" + i + "." + vKey + " = $" + vObjectCache[i].compiled[vKey] + ";\n";
                };
              };
            }
            else if (v instanceof Array)
            {
              for (var ia=0, la=vObjectCache[i].compiled.length; ia<la; ia++)
              {
                str += "$" + i + "[" + ia + "] = $" + vObjectCache[i].compiled[ia] + ";\n";
              };
            }
            else
            {
              for (vKey in vObjectCache[i].compiled)
              {
                if (vObjectCache[i].compiled[vKey] != null)
                {
                  str += "$" + i + "[\"" + vKey + "\"] = $" + vObjectCache[i].compiled[vKey] + ";\n";
                };
              };
            };
          };
        };





        str += "};";

        jsout.value = str;

        qx.core.Init.getInstance().debug("Done");
      });




      var vObjectCache = [];
      var vLastListenerContext = null;

      function serializeMain(vValue)
      {
        var vValue, vIter, vLength;

        // Completly ignore the window and document built-in object
        if (vValue == window || vValue == document) {
          return null;
        };

        // Completly ignore DOM nodes
        if (qx.util.Validation.isValid(vValue) && typeof vValue.nodeType != qx.constant.Type.UNDEFINED) {
          return null;
        };

        for (vIter=0, vLength=vObjectCache.length; vIter<vLength; vIter++)
        {
          if (vObjectCache[vIter].value === vValue)
          {
            return vIter;
          };
        };

        //QxDebug("Serializer", "Known: " + vObjectCache);
        //QxDebug("Serializer", "Serialize: " + vValue + "(" + typeof vValue + ")");

        var vPos = vObjectCache.length;

        vObjectCache[vPos] = { value : vValue };
        vObjectCache[vPos].compiled = serializeWrapper(vValue);

        return vPos;
      };

      function serializeWrapper(vInput)
      {
        switch(typeof vInput)
        {
          case qx.constant.Type.STRING:
          case qx.constant.Type.NUMBER:
          case qx.constant.Type.BOOLEAN:
            return serializeSimple(vInput);

          case qx.constant.Type.OBJECT:
            if (vInput == null)
            {
              return serializeSimple(vInput);
            }
            else if (vInput instanceof Array)
            {
              return serializeArray(vInput);
            }
            else
            {
              return serializeObject(vInput);
            };

          case qx.constant.Type.FUNCTION:
            //QxDebug("Serializer", "FUNCTION: " + vInput);
            return null;
        };

        return null;
      };

      function serializeSimple(vSimple) {
        return vSimple;
      };

      function serializeObject(vObject)
      {
        if (vObject instanceof qx.ui.core.ClientDocument) {
          return "$OBJECTMAPPER-qx.ui.core.ClientDocument";
        }
        else if (vObject instanceof qx.manager.object.ImageManager) {
          return "$OBJECTMAPPER-qx.manager.object.ImageManager";
        };

        var vKey;
        var vOut = {};

        for (vKey in vObject)
        {
          // ignore css reference
          switch(vKey)
          {
            case "_style":
              continue;

            case "_listeners":
              // QxDebug("Serializer", "Found Listeners: " + vKey + "::" + vObject);
              vLastListenerContext = vObject;
              break;
          };

          if (typeof vObject[vKey] == qx.constant.Type.FUNCTION)
          {
            if (!(vObject instanceof qx.core.Object) && vLastListenerContext)
            {
              var to = new vLastListenerContext.constructor;
              var tm = null;

              for (vSubKey in to)
              {
                if (to[vSubKey] === vObject[vKey])
                {
                  tm = vSubKey;
                  break;
                };
              };

              if (tm)
              {
                qx.core.Init.getInstance().debug("Serializer", "Function Mapped: " + tm);
                var ts = { value : vObject[vKey], compiled : "$FUNCTIONMAPPER-" + tm + "|" + vLastListenerContext._hashCode };
                vObjectCache.push(ts);
                vOut[vKey] = vObjectCache.length-1;
                continue;
              }
              else
              {
                qx.core.Init.getInstance().debug("Serializer", "Could not map function: " + vKey + "[" + vLastListenerContext + "]");
              };

              continue;
            }
            else
            {
              continue;
            };
          };

          vOut[vKey] = serializeMain(vObject[vKey]);
        };

        return vOut;
      };

      function serializeArray(vArray)
      {
        var vKey;
        var vOut = [];

        for (var vIter=0, vLength=vArray.length; vIter<vLength; vIter++) {
          vOut.push(serializeMain(vArray[vIter]));
        };

        return vOut;
      };

  </script>
</body>
</html>