Intial import
[gnome.introspection-doc-generator] / xnew.js
1 //<script type="text/javascript">
2
3 /**
4  * 
5  * abstraction layer / tree builder etc...
6  * 
7  * 
8  * 
9  * 
10  */
11
12 var console  = imports['console.js'].console;
13
14 //var xnew = imports['xnew.js'].xnew;
15
16 function apply (o, c){
17     if(o && c && typeof c == 'object'){
18         for(var p in c){
19             o[p] = c[p];
20         }
21     }
22     return o;
23 };
24
25
26 var xnew = {
27     registry : { },
28     pre_registry : { },
29     
30     dumpRegistry: function(scope, id)
31     {
32         var ret = { }
33            
34         for (var i in xnew.registry) {
35             ret[i] = [];
36             for(var j in xnew.registry[i]) {
37                 ret[i].push(j);
38            }
39         }
40         return ret;
41         
42     },
43     /**
44      * xnew.get(this, 'someid')
45      * xnew.get(this).someid 
46      * 
47      * fetches from a registry of components.
48      * 
49      * defined by {
50      *       xnsid : 'Builder.test' // at top leve, all sub elements inherit it..
51      *       xid : 'test'
52      * 
53      * 
54      */
55
56     get: function(scope, id)
57     {
58         var s = typeof(scope) == 'object' ? scope.xnsid : scope;
59         
60         if (typeof(this.pre_registry[s]) != 'undefined') {
61             this.xnew(this.pre_registry[s]);
62             delete this.pre_registry[s];
63         }
64         
65         
66         if (typeof(id) == 'undefined') {
67             if (typeof(xnew.registry[s]) !='undefined') {
68                 Seed.print("Success - got " + s);
69                 return xnew.registry[s];
70             }
71             
72             var ar = s.split('.');
73             id = ar.pop();
74             s = ar.join('.');
75         }
76         
77         
78         xnew.registry[s] = xnew.registry[s] || {};
79            
80         
81         var ret = typeof(xnew.registry[s][id]) == 'undefined' ? false : xnew.registry[s][id]
82         
83         
84         Seed.print((ret ? "Success" : "FAILED") + " - check for NS:" + s + " ID: " + id);
85         if (!ret) {
86             console.dump(this.dumpRegistry());
87            }
88         return ret;
89         
90     },
91     
92     
93     /**
94      * load a directory, and overlay it's properties onto the variable provider..
95      * 
96      * 
97      * 
98      */
99
100
101     load: function (obj, path) 
102     {
103         var GLib = imports.gi.GLib;
104         var ret = [];
105         var gdir = GLib.dir_open(__script_path__ + '/' + path,0);
106         
107         
108         
109         while (true) {
110             
111             var fn = GLib.dir_read_name(gdir);
112             //console.log('trying ' + path + '/' + fn);
113             if (!fn) {
114                 GLib.dir_close(gdir);
115                 return;
116             }
117             if (!fn.match(/.js$/)) {
118                 continue;
119             }
120             var v = fn.replace(/.js$/, '');
121             var assign = imports[path + '/' + fn][v];
122             //console.log('loaded : ' + path + '/'+v);
123             if (!obj[v]) {
124                 Seed.print("using file as object for " + path + ':' +v);
125             }
126             obj[v] = obj[v] || assign;
127             
128         }
129     },
130     /**
131      * create a  delegate..
132      * 
133      */
134
135
136     createDelegate : function(method, obj, args, appendArgs){
137         
138         return function() {
139             var callArgs = args || arguments;
140             if(appendArgs === true){
141                 callArgs = Array.prototype.slice.call(arguments, 0);
142                 callArgs = callArgs.concat(args);
143             }else if(typeof appendArgs == "number"){
144                 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
145                     var applyArgs = [appendArgs, 0].concat(args); // create method call params
146                     Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
147                 }
148                 return method.apply(obj || window, callArgs);
149             };
150     },
151     /**
152      * copy an array...
153      * 
154      */
155     copyArray: function (ar) {
156         var ret = [];
157         for(var i = 0 ; i < ar.length; i++) {
158             ret[i] = ar[i];
159         }
160         return ret;
161     },
162     
163     /**
164      * create a definition of an interface..
165      * 
166      * it get's registered, however not created.
167      * 
168      * 
169      * 
170      */
171     create : function (o)
172     {
173         this.pre_registry[o.xnsid] = o;
174       
175     },
176     
177     
178     /**
179      * factory.. ??? - better name???
180      * 
181      * converts a object structure, and constructs a class... using xtype/xns fields.
182      * 
183      * 
184      * 
185      */
186     
187     xnew: function (o, in_xnsid)
188     {
189         in_xnsid = in_xnsid || '';
190         var xnsid = '' + in_xnsid;
191          
192         if ((o.xtype == 'Include') && (o.xns == 'xnew')) {
193             if (typeof(o.cls) != 'string') {
194                 return xnew.xnew( o.cls.create(), xnsid);
195             }
196             var cls = o.cls;
197             o = this.pre_registry[cls];
198         }
199         if (typeof(o.xtype) == 'function') {
200             return   new o.xtype(o);
201         }
202         o.set = o.set || {}; 
203         o.listeners = o.listeners || {}; 
204         o.packing = o.packing || [ 'add' ]; 
205         o.items = o.items || []; 
206         // handle xnsid..
207         
208         if (typeof(o.xnsid) !='undefined') {
209             Seed.print("\n\n------------------------------------------");
210             Seed.print( o.xnsid);
211             Seed.print("------------------------------------------\n\n");
212             xnsid = ''+ o.xnsid;
213         }
214         if ((typeof(xnsid) != 'undefined') &&  !o.xnsid) {
215             o.xnsid = xnsid;
216         }
217         if (o.xnsid  && o.xid) {
218             xnew.registry = xnew.registry || { };
219             xnew.registry[o.xnsid] = xnew.registry[o.xnsid] || {}; 
220             xnew.registry[o.xnsid][o.xid] = o;
221         }
222         
223         
224         var isSeed = typeof(Seed) != 'undefined';
225         
226         var constructor = false
227         
228      
229         // XNS contructor..
230         Seed.print('xnew : ' + o.xns + '.' + o.xtype);
231         // support for xns='Gtk', xtyle='Window'..
232         var NS = imports.gi[o.xns];
233         if (!NS) {
234             Seed.print('Invalid xns: ' + o.xns);
235         }
236         constructor = NS[o.xtype];
237         if (!constructor) {
238             Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
239         }
240          
241         // how to hanlde gjs constructor???? - can it even be done..
242         
243         o.el  = o.el ||  (isSeed ? new constructor(o) : new constructor());
244         
245         
246         
247         
248         if (o.listeners._new) { // rendered!?!?
249             Seed.print('Call : ' + o.xtype+'.listeners._new');
250             o.listeners._new.call(o);
251         }
252         
253         
254         //Seed.print(o.pack.length);
255         // packing  - if 'add method exists on created node use that..
256         
257         
258         for( var i =0; i < o.items.length;i++) {
259             
260             
261             
262             
263             o.items[i] = xnew.xnew(o.items[i], xnsid);
264             
265             if (typeof(o.items[i].packing) == 'function') {
266                 // parent, child
267                 o.items[i].packing.apply(o, [ o , o.items[i] ]);
268                 o.items[i].xparent = o;
269                 continue;
270             }
271             var pack_m = o.items[i].packing[0];
272             
273             if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
274                 Seed.print('pack method not available : ' + o.xtype + '.' +  pack_m);
275                 continue;
276             }
277             Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
278             // copy..
279             args = this.copyArray(o.items[i].packing);
280             args[0] = o.items[i].el;
281             Seed.print('args: ' + args.length);
282             if (pack_m) {
283                 o.el[pack_m].apply(o.el, args);
284             }
285             
286             o.items[i].xparent = o;
287         }
288         
289         /// Setting stuff...
290         
291         for (var i in o.set) {
292             Seed.print('Set ' + i);
293             if (typeof(o.el[i].apply) !='undefined') {
294                 o.el[i].apply(o.el, o.set[i]);
295                }
296             
297         }
298           
299         for (var i in o.listeners) {
300             if (i.substring(0,1) == '_') {
301                 continue;
302             }
303             Seed.print('Add Listener ' + i);
304             
305             var _li = this.createDelegate(o.listeners[i],o);
306             // private listeners that are not copied to GTk.
307             
308              
309             if (isSeed) {
310               //   Seed.print(typeof(_li));
311                 o.el.signal[i].connect(_li);
312             } else {
313                 o.el.connect( i, _li);
314             }
315              
316         }
317        
318         // apply functions..
319         if (o.listeners._rendered) { // rendered!?!?
320             Seed.print('Call : ' + o.xtype+'.listeners._rendered');
321             o.listeners._rendered.call(o);
322         }
323         
324         return o;
325
326     }
327 };