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