jhbuild.js
[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 = gdir.read_name ? gdir.read_name() :  GLib.dir_read_name(gdir);
112             //console.log('trying ' + path + '/' + fn);
113             if (!fn) {
114                 gdir.close ? gdir.close() : GLib.dir_close(gdir);
115                 return;
116             }
117             if (!fn.match(/.js$/)) {
118                 continue;
119             }
120             var v = fn.replace(/.js$/, '');
121             imports[path + '/' + fn];
122             var assign = imports[path + '/' + fn][v];
123             //console.log('loaded : ' + path + '/'+v);
124             if (!obj[v]) {
125                 Seed.print("using file as object for " + path + ':' + typeof(assign) + ':' + v);
126             }
127             obj[v] = obj[v] || assign;
128             
129         }
130     },
131     /**
132      * create a  delegate..
133      * 
134      */
135
136
137     createDelegate : function(method, obj, args, appendArgs){
138         
139         return function() {
140             var callArgs = args || arguments;
141             if(appendArgs === true){
142                 callArgs = Array.prototype.slice.call(arguments, 0);
143                 callArgs = callArgs.concat(args);
144             }else if(typeof appendArgs == "number"){
145                 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
146                     var applyArgs = [appendArgs, 0].concat(args); // create method call params
147                     Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
148                 }
149                 return method.apply(obj || window, callArgs);
150             };
151     },
152     /**
153      * copy an array...
154      * 
155      */
156     copyArray: function (ar) {
157         var ret = [];
158         for(var i = 0 ; i < ar.length; i++) {
159             ret[i] = ar[i];
160         }
161         return ret;
162     },
163     
164     /**
165      * create a definition of an interface..
166      * 
167      * it get's registered, however not created.
168      * 
169      * 
170      * 
171      */
172     create : function (o)
173     {
174         this.pre_registry[o.xnsid] = o;
175       
176     },
177     
178     
179     /**
180      * factory.. ??? - better name???
181      * 
182      * converts a object structure, and constructs a class... using xtype/xns fields.
183      * 
184      * 
185      * 
186      */
187     
188     xnew: function (o, in_xnsid)
189     {
190         in_xnsid = in_xnsid || '';
191         var xnsid = '' + in_xnsid;
192          
193         if ((o.xtype == 'Include') && (o.xns == 'xnew')) {
194             if (typeof(o.cls) != 'string') {
195                 return xnew.xnew( o.cls.create(), xnsid);
196             }
197             var cls = o.cls;
198             o = this.pre_registry[cls];
199         }
200         if (typeof(o.xtype) == 'function') {
201             return   new o.xtype(o);
202         }
203         o.set = o.set || {}; 
204         o.listeners = o.listeners || {}; 
205         o.packing = o.packing || [ 'add' ]; 
206         o.items = o.items || []; 
207         // handle xnsid..
208         
209         if (typeof(o.xnsid) !='undefined') {
210             Seed.print("\n\n------------------------------------------");
211             Seed.print( o.xnsid);
212             Seed.print("------------------------------------------\n\n");
213             xnsid = ''+ o.xnsid;
214         }
215         if ((typeof(xnsid) != 'undefined') &&  !o.xnsid) {
216             o.xnsid = xnsid;
217         }
218         if (o.xnsid  && o.xid) {
219             xnew.registry = xnew.registry || { };
220             xnew.registry[o.xnsid] = xnew.registry[o.xnsid] || {}; 
221             xnew.registry[o.xnsid][o.xid] = o;
222         }
223         
224         
225         var isSeed = typeof(Seed) != 'undefined';
226         
227         var constructor = false
228         
229      
230         // XNS contructor..
231         Seed.print('xnew : ' + o.xns + '.' + o.xtype);
232         // support for xns='Gtk', xtyle='Window'..
233         var NS = imports.gi[o.xns];
234         if (!NS) {
235             Seed.print('Invalid xns: ' + o.xns);
236         }
237         constructor = NS[o.xtype];
238         if (!constructor) {
239             Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
240         }
241          
242         // how to hanlde gjs constructor???? - can it even be done..
243         
244         o.el  = o.el ||  (isSeed ? new constructor(o) : new constructor());
245         
246         
247         
248         
249         if (o.listeners._new) { // rendered!?!?
250             Seed.print('Call : ' + o.xtype+'.listeners._new');
251             o.listeners._new.call(o);
252         }
253         
254         
255         //Seed.print(o.pack.length);
256         // packing  - if 'add method exists on created node use that..
257         
258         
259         for( var i =0; i < o.items.length;i++) {
260             
261             
262             
263             
264             o.items[i] = xnew.xnew(o.items[i], xnsid);
265             
266             if (typeof(o.items[i].packing) == 'function') {
267                 // parent, child
268                 o.items[i].packing.apply(o, [ o , o.items[i] ]);
269                 o.items[i].xparent = o;
270                 continue;
271             }
272             var pack_m = o.items[i].packing[0];
273             
274             if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
275                 Seed.print('pack method not available : ' + o.xtype + '.' +  pack_m);
276                 continue;
277             }
278             Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
279             // copy..
280             args = this.copyArray(o.items[i].packing);
281             args[0] = o.items[i].el;
282             Seed.print('args: ' + args.length);
283             if (pack_m) {
284                 o.el[pack_m].apply(o.el, args);
285             }
286             
287             o.items[i].xparent = o;
288         }
289         
290         /// Setting stuff...
291         
292         for (var i in o.set) {
293             Seed.print('Set ' + i);
294             if (typeof(o.el[i].apply) !='undefined') {
295                 o.el[i].apply(o.el, o.set[i]);
296                }
297             
298         }
299           
300         for (var i in o.listeners) {
301             if (i.substring(0,1) == '_') {
302                 continue;
303             }
304             Seed.print('Add Listener ' + i);
305             
306             var _li = this.createDelegate(o.listeners[i],o);
307             // private listeners that are not copied to GTk.
308             
309              
310             if (isSeed) {
311               //   Seed.print(typeof(_li));
312                 o.el.signal[i].connect(_li);
313             } else {
314                 o.el.connect( i, _li);
315             }
316              
317         }
318        
319         // apply functions..
320         if (o.listeners._rendered) { // rendered!?!?
321             Seed.print('Call : ' + o.xtype+'.listeners._rendered');
322             o.listeners._rendered.call(o);
323         }
324         
325         return o;
326
327     }
328 };