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