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