XObject.js
[gitlive] / XObject.js
1 //<script type="text/javascript">
2
3 /**
4  * XObject
5  * Yet another attempt to create a usable object construction library for seed..
6  *
7  * Extend this.. to use it's wonderful features..
8  * 
9  * 
10  */
11
12 function XObject (cfg) {
13     // first apply cfg if set.
14     o =  {};
15     o.pack = o.pack || 'add';
16        
17     XObject.extend(o, cfg); // copy everything into o.
18     XObject.extend(this, o);
19     
20     // remove items.
21     this.items = [];
22     
23     
24     
25     // remove objects/functions from o, so they can be sent to the contructor.
26     for (var i in o) {
27         if ((typeof(o[i]) == 'object') || 
28             (typeof(o[i]) == 'function') || 
29             i == 'pack'
30         ) {
31             delete o[i];
32         }
33     }
34     
35     this.listeners = this.listeners || {}; 
36     
37     
38     
39     
40     // handle include?
41     //if ((this.xtype == 'Include')) {
42     //    o = this.pre_registry[cls];
43     //}
44     var isSeed = typeof(Seed) != 'undefined';
45      
46     // xtype= Gtk.Menu ?? what about c_new stuff?
47     if (typeof(this.xtype) == 'function') {
48         this.el = this.el ||  new this.xtype(o);
49     }
50     
51     if (!this.el && o.xns) 
52         var NS = imports.gi[o.xns];
53         if (!NS) {
54             Seed.print('Invalid xns: ' + o.xns);
55         }
56         constructor = NS[o.xtype];
57         if (!constructor) {
58             Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
59         }
60         this.el  = this.el ||  (isSeed ? new constructor(o) : new constructor());
61     }
62     
63     
64     // register it!
65     if (o.xnsid  && o.xid) {
66         XObject.registry = XObject.registry || { };
67         XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {}; 
68         XObject.registry[o.xnsid][o.xid] = this;
69     }
70     
71     cfg.items.forEach(this.addItem, this);
72     
73     
74 }
75 XObject.prototype = {
76     /**
77      * @property el {GObject} the Gtk / etc. element.
78      */
79     el : false, 
80     /*
81      * @property items {Array} list of sub elements
82      */
83     /**
84      * @property parent {XObject} parent Element
85      */
86      
87     addItem : function(o) {
88         var item = new XObject(o);
89         
90         this.items.push(item);
91         
92          
93         if (typeof(item.pack) == 'function') {
94             // parent, child
95             item.pack.apply(o, [ o , o.items[i] ]);
96             item.parent = this;
97             return;
98         }
99         
100         
101         var pack_m = typeof(item.pack) == 'string' ?  item.pack :  item.pack.shift();
102         
103         // handle error.
104         if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
105             Seed.print('pack method not available : ' + o.xtype + '.' +  pack_m);
106             return;
107         }
108         
109         
110         //Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
111         // copy.
112         var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
113         item.pack.unshift(item.el);
114         //Seed.print('args: ' + args.length);
115         if (pack_m) {
116             this.el[pack_m].apply(item.el, args);
117         }
118         
119         item.parent = this;
120         
121     }
122     
123 }
124 xnew: function (o, in_xnsid)
125     {
126          
127          
128          
129         
130         var isSeed = typeof(Seed) != 'undefined';
131         
132         var constructor = false
133         
134      
135         // XNS contructor..
136         Seed.print('xnew : ' + o.xns + '.' + o.xtype);
137         // support for xns='Gtk', xtyle='Window'..
138         var NS = imports.gi[o.xns];
139         if (!NS) {
140             Seed.print('Invalid xns: ' + o.xns);
141         }
142         constructor = NS[o.xtype];
143         if (!constructor) {
144             Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
145         }
146          
147         // how to hanlde gjs constructor???? - can it even be done..
148         
149         o.el  = o.el ||  (isSeed ? new constructor(o) : new constructor());
150         
151         
152         
153         
154         if (o.listeners._new) { // rendered!?!?
155             Seed.print('Call : ' + o.xtype+'.listeners._new');
156             o.listeners._new.call(o);
157         }
158         
159         
160         //Seed.print(o.pack.length);
161         // packing  - if 'add method exists on created node use that..
162         
163         
164         for( var i =0; i < o.items.length;i++) {
165             
166             
167             
168             
169             o.items[i] = xnew.xnew(o.items[i], xnsid);
170             
171             if (typeof(o.items[i].packing) == 'function') {
172                 // parent, child
173                 o.items[i].packing.apply(o, [ o , o.items[i] ]);
174                 o.items[i].xparent = o;
175                 continue;
176             }
177             var pack_m = o.items[i].packing[0];
178             
179             if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
180                 Seed.print('pack method not available : ' + o.xtype + '.' +  pack_m);
181                 continue;
182             }
183             Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
184             // copy..
185             args = this.copyArray(o.items[i].packing);
186             args[0] = o.items[i].el;
187             Seed.print('args: ' + args.length);
188             if (pack_m) {
189                 o.el[pack_m].apply(o.el, args);
190             }
191             
192             o.items[i].xparent = o;
193         }
194         
195         /// Setting stuff...
196         
197         for (var i in o.set) {
198             Seed.print('Set ' + i);
199             if (typeof(o.el[i].apply) !='undefined') {
200                 o.el[i].apply(o.el, o.set[i]);
201                }
202             
203         }
204           
205         for (var i in o.listeners) {
206             if (i.substring(0,1) == '_') {
207                 continue;
208             }
209             Seed.print('Add Listener ' + i);
210             
211             var _li = this.createDelegate(o.listeners[i],o);
212             // private listeners that are not copied to GTk.
213             
214              
215             if (isSeed) {
216               //   Seed.print(typeof(_li));
217                 o.el.signal[i].connect(_li);
218             } else {
219                 o.el.connect( i, _li);
220             }
221              
222         }
223        
224         // apply functions..
225         if (o.listeners._rendered) { // rendered!?!?
226             Seed.print('Call : ' + o.xtype+'.listeners._rendered');
227             o.listeners._rendered.call(o);
228         }
229         
230         return o;
231
232     }