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