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