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