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  * @arg xtype {String|Function} constructor or string.
11  * @arg xid {String}  (optional) id for registry
12  * @arg xns {String|Object}   (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
13  * @arg items {Array}   (optional) list of child elements which will be constructed.. using XObject
14  * @arg listeners {Object}   (optional) map Gobject signals to functions
15  * @arg pack {Function|String|Array}   (optional) how this object gets added to it's parent
16  * @arg el {Object}   (optional) premade GObject
17  */
18
19 function XObject (cfg) {
20     // first apply cfg if set.
21     o =  {};
22     o.pack = o.pack || 'add';
23     cfg.items = cfg.items || [];
24     
25     XObject.extend(o, cfg); // copy everything into o.
26     XObject.extend(this, o);
27     
28     // remove items.
29     
30     this.listeners = this.listeners || {}; 
31     this.items = [];
32     
33     // remove objects/functions from o, so they can be sent to the contructor.
34     for (var i in o) {
35         if ((typeof(o[i]) == 'object') || 
36             (typeof(o[i]) == 'function') || 
37             i == 'pack' ||
38             i == 'xid' ||
39             i == 'xtype' ||
40             i == 'xns'
41         ) {
42             delete o[i];
43         }
44     }
45     
46     // do we need to call 'beforeInit here?'
47      
48     // handle include?
49     //if ((this.xtype == 'Include')) {
50     //    o = this.pre_registry[cls];
51     //}
52     var isSeed = typeof(Seed) != 'undefined';
53      
54     // xtype= Gtk.Menu ?? what about c_new stuff?
55     if (typeof(this.xtype) == 'function') {
56         this.el = this.el ||  new this.xtype(o);
57     }
58     print(typeof(this.xtype));
59     if (!this.el && o.xns) {
60         
61         var NS = imports.gi[o.xns];
62         if (!NS) {
63             Seed.print('Invalid xns: ' + o.xns);
64         }
65         constructor = NS[o.xtype];
66         if (!constructor) {
67             Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
68         }
69         this.el  =   isSeed ? new constructor(o) : new constructor();
70         if (!isSeed) {
71             // this might work for gjs!?
72             for (var i in o) {
73                 this.el[i] = o;
74             }
75         }
76     }
77     
78     // register it!
79     //if (o.xnsid  && o.xid) {
80      //   XObject.registry = XObject.registry || { };
81      //   XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {}; 
82      //   XObject.registry[o.xnsid][o.xid] = this;
83     //}
84     
85     cfg.items.forEach(this.addItem, this);
86     
87     for (var i in this.listeners) {
88         this.addListener(i, this.listeners[i]);
89     }
90     // delete this.listeners ?
91     
92     
93     // do we need to call 'init here?'
94     
95 }
96
97
98
99 XObject.prototype = {
100     /**
101      * @property el {GObject} the Gtk / etc. element.
102      */
103     el : false, 
104     /*
105      * @property items {Array} list of sub elements
106      */
107     /**
108      * @property parent {XObject} parent Element
109      */
110      /**
111       * @method addItem
112       * Adds an item to the object using a new XObject
113       * uses pack property to determine how to add it.
114       * @arg cfg {Object} same as XObject constructor.
115       */
116     addItem : function(o) {
117         var item = new XObject(o);
118         
119         this.items.push(item);
120         
121          
122         if (typeof(item.pack) == 'function') {
123             // parent, child
124             item.pack.apply(o, [ o , o.items[i] ]);
125             item.parent = this;
126             return;
127         }
128         
129         
130         var pack_m = typeof(item.pack) == 'string' ?  item.pack :  item.pack.shift();
131         
132         // handle error.
133         if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
134             Seed.print('pack method not available : ' + o.xtype + '.' +  pack_m);
135             return;
136         }
137         
138         
139         //Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
140         // copy.
141         var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
142         item.pack.unshift(item.el);
143         //Seed.print('args: ' + args.length);
144         if (pack_m) {
145             this.el[pack_m].apply(item.el, args);
146         }
147         
148         item.parent = this;
149         
150     },
151     /**
152       * @method addListener
153       * Connects a method to a signal. (gjs/Seed aware)
154       * 
155       * @arg sig  {String} name of signal
156       * @arg fn  {Function} handler.
157       */
158     addListener  : function(sig, fn) 
159     {
160  
161         Seed.print("Add signal " + sig);
162  
163         var _li = XObject.createDelegate(fn,this);
164         // private listeners that are not copied to GTk.
165         
166         if (typeof(Seed) != 'undefined') {
167           //   Seed.print(typeof(_li));
168             this.el.signal[sig].connect(_li);
169         } else {
170             this.el.connect( sig, _li);
171         }
172              
173         
174     },
175      /**
176       * @method get
177       * Finds an object in the child elements using xid of object.
178       * 
179       * @arg name  {String} name of signal
180       * @return   {XObject|false} the object if found.
181       */
182     get : function(xid)
183     {
184         var ret=  false;
185         this.items.each(function(ch) {
186             if (ch.xid == xid) {
187                 ret = ch;
188                 return true;
189             }
190         })
191         if (ret) {
192             return ret;
193         }
194         // iterate children.
195         this.items.each(function(ch) {
196             ret = ch.get(xid);
197             if (ret) {
198                 return true;
199             }
200         })
201         return ret;
202     }
203       
204       
205
206          
207         
208 /**
209  * Copies all the properties of config to obj.
210  *
211  * Pretty much the same as JQuery/Prototype..
212  * @param {Object} obj The receiver of the properties
213  * @param {Object} config The source of the properties
214  * @param {Object} defaults A different object that will also be applied for default values
215  * @return {Object} returns obj
216  * @member XObject extend
217  */
218
219
220 XObject.extend = function(o, c, defaults){
221     if(defaults){
222         // no "this" reference for friendly out of scope calls
223         XObject.extend(o, defaults);
224     }
225     if(o && c && typeof c == 'object'){
226         for(var p in c){
227             o[p] = c[p];
228         }
229     }
230     return o;
231 };
232
233 XObject.extend(XObject,
234 {
235     /**
236      * Copies all the properties of config to obj, if the do not exist.
237      * @param {Object} obj The receiver of the properties
238      * @param {Object} config The source of the properties
239      * @return {Object} returns obj
240      * @member Object extendIf
241      */
242
243
244     extendIf : function(o, c){
245
246         if(!o || !c || typeof c != 'object'){
247             return o;
248         }
249         for(var p in c){
250             if (typeof(o[p]) != 'undefined') {
251                 continue;
252             }
253             o[p] = c[p];
254         }
255         return o;
256     },
257
258  
259
260     /**
261      * Extends one class with another class and optionally overrides members with the passed literal. This class
262      * also adds the function "override()" to the class that can be used to override
263      * members on an instance.
264      *
265      * usage:
266      * MyObject = Object.define(
267      *     function(...) {
268      *          ....
269      *     },
270      *     parentClass, // or Object
271      *     {
272      *        ... methods and properties.
273      *     }
274      * });
275      * @param {Function} constructor The class inheriting the functionality
276      * @param {Object} superclass The class being extended
277      * @param {Object} overrides (optional) A literal with members
278      * @return {Function} constructor (eg. class
279      * @method define
280      */
281     define : function(){
282         // inline overrides
283         var io = function(o){
284             for(var m in o){
285                 this[m] = o[m];
286             }
287         };
288         return function(sb, sp, overrides) {
289             if (typeof(sp) == 'undefined') {
290                 // error condition - try and dump..
291                 throw "Missing superclass: when applying: " + sb
292             }
293
294             var F = function(){}, sbp, spp = sp.prototype;
295             F.prototype = spp;
296             sbp = sb.prototype = new F();
297             sbp.constructor=sb;
298             sb.superclass=spp;
299
300             // extends Object.
301             if(spp.constructor == Object.prototype.constructor){
302                 spp.constructor=sp;
303             }
304
305             sb.override = function(o){
306                 Object.extend(sb.prototype, o);
307             };
308             sbp.override = io;
309             Object.extend(sb.prototype, overrides);
310             return sb;
311         };
312     }(),
313
314          
315     /**
316      * returns a list of keys of the object.
317      * @param {Object} obj object to inspect
318      * @return {Array} returns list of kyes
319      * @member XObject keys
320      */
321     keys : function(o)
322     {
323         var ret = [];
324         for(var i in o) {
325             ret.push[i];
326         }
327         return ret;
328     },
329       
330     /**
331      * @member XObject createDelegate
332      * creates a delage metdhod
333      * @param {Function} method to wrap
334      * @param {Object} scope 
335      * @param {Array} args to add
336      * @param {Boolean|Number} append arguments or replace after N arguments.
337      * @return {Function} returns the delegate
338      */
339
340     createDelegate : function(method, obj, args, appendArgs){
341         
342         return function() {
343             var callArgs = args || arguments;
344             if(appendArgs === true){
345                 callArgs = Array.prototype.slice.call(arguments, 0);
346                 callArgs = callArgs.concat(args);
347             }else if(typeof appendArgs == "number"){
348                 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
349                     var applyArgs = [appendArgs, 0].concat(args); // create method call params
350                     Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
351                 }
352                 return method.apply(obj || window, callArgs);
353             };
354     }
355     
356 });