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     
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         var _li = XObject.createDelegate(fn,this);
161         // private listeners that are not copied to GTk.
162         
163         if (typeof(Seed) != 'undefined') {
164           //   Seed.print(typeof(_li));
165             this.el.signal[sig].connect(_li);
166         } else {
167             this.el.connect( sig, _li);
168         }
169              
170         
171     },
172      /**
173       * @method get
174       * Finds an object in the child elements using xid of object.
175       * 
176       * @arg name  {String} name of signal
177       * @return   {XObject|false} the object if found.
178       */
179     get : function(xid)
180     {
181         var ret=  false;
182         this.items.each(function(ch) {
183             if (ch.xid == xid) {
184                 ret = ch;
185                 return true;
186             }
187         })
188         if (ret) {
189             return ret;
190         }
191         // iterate children.
192         this.items.each(function(ch) {
193             ret = ch.get(xid);
194             if (ret) {
195                 return true;
196             }
197         })
198         return ret;
199     }
200       
201       
202
203          
204         
205 /**
206  * Copies all the properties of config to obj.
207  *
208  * Pretty much the same as JQuery/Prototype..
209  * @param {Object} obj The receiver of the properties
210  * @param {Object} config The source of the properties
211  * @param {Object} defaults A different object that will also be applied for default values
212  * @return {Object} returns obj
213  * @member XObject extend
214  */
215
216
217 XObject.extend = function(o, c, defaults){
218     if(defaults){
219         // no "this" reference for friendly out of scope calls
220         XObject.extend(o, defaults);
221     }
222     if(o && c && typeof c == 'object'){
223         for(var p in c){
224             o[p] = c[p];
225         }
226     }
227     return o;
228 };
229
230 XObject.extend(XObject,
231 {
232     /**
233      * Copies all the properties of config to obj, if the do not exist.
234      * @param {Object} obj The receiver of the properties
235      * @param {Object} config The source of the properties
236      * @return {Object} returns obj
237      * @member Object extendIf
238      */
239
240
241     extendIf : function(o, c){
242
243         if(!o || !c || typeof c != 'object'){
244             return o;
245         }
246         for(var p in c){
247             if (typeof(o[p]) != 'undefined') {
248                 continue;
249             }
250             o[p] = c[p];
251         }
252         return o;
253     },
254
255  
256
257     /**
258      * Extends one class with another class and optionally overrides members with the passed literal. This class
259      * also adds the function "override()" to the class that can be used to override
260      * members on an instance.
261      *
262      * usage:
263      * MyObject = Object.define(
264      *     function(...) {
265      *          ....
266      *     },
267      *     parentClass, // or Object
268      *     {
269      *        ... methods and properties.
270      *     }
271      * });
272      * @param {Function} constructor The class inheriting the functionality
273      * @param {Object} superclass The class being extended
274      * @param {Object} overrides (optional) A literal with members
275      * @return {Function} constructor (eg. class
276      * @method define
277      */
278     define : function(){
279         // inline overrides
280         var io = function(o){
281             for(var m in o){
282                 this[m] = o[m];
283             }
284         };
285         return function(sb, sp, overrides) {
286             if (typeof(sp) == 'undefined') {
287                 // error condition - try and dump..
288                 throw "Missing superclass: when applying: " + sb
289             }
290
291             var F = function(){}, sbp, spp = sp.prototype;
292             F.prototype = spp;
293             sbp = sb.prototype = new F();
294             sbp.constructor=sb;
295             sb.superclass=spp;
296
297             // extends Object.
298             if(spp.constructor == Object.prototype.constructor){
299                 spp.constructor=sp;
300             }
301
302             sb.override = function(o){
303                 Object.extend(sb.prototype, o);
304             };
305             sbp.override = io;
306             Object.extend(sb.prototype, overrides);
307             return sb;
308         };
309     }(),
310
311          
312     /**
313      * returns a list of keys of the object.
314      * @param {Object} obj object to inspect
315      * @return {Array} returns list of kyes
316      * @member XObject keys
317      */
318     keys : function(o)
319     {
320         var ret = [];
321         for(var i in o) {
322             ret.push[i];
323         }
324         return ret;
325     },
326       
327     /**
328      * @member XObject createDelegate
329      * creates a delage metdhod
330      * @param {Function} method to wrap
331      * @param {Object} scope 
332      * @param {Array} args to add
333      * @param {Boolean|Number} append arguments or replace after N arguments.
334      * @return {Function} returns the delegate
335      */
336
337     createDelegate : function(method, obj, args, appendArgs){
338         
339         return function() {
340             var callArgs = args || arguments;
341             if(appendArgs === true){
342                 callArgs = Array.prototype.slice.call(arguments, 0);
343                 callArgs = callArgs.concat(args);
344             }else if(typeof appendArgs == "number"){
345                 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
346                     var applyArgs = [appendArgs, 0].concat(args); // create method call params
347                     Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
348                 }
349                 return method.apply(obj || window, callArgs);
350             };
351     }
352     
353 });