1 //<script type="text/javascript">
5 * Yet another attempt to create a usable object construction library for seed..
7 * Extend this.. to use it's wonderful features..
13 * @arg xtype {String|Function} constructor or string.
14 * @arg xid {String} (optional) id for registry
15 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
16 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
17 * @arg listeners {Object} (optional) map Gobject signals to functions
18 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
19 * @arg el {Object} (optional) premade GObject
21 * --- needs a xdebug option!
24 function XObject (cfg) {
25 // first apply cfg if set.
35 * @property el {GObject} the Gtk / etc. element.
39 * @property items {Array} list of sub elements
42 * @property parent {XObject} parent Element
46 * @property config {Object} the construction configuration.
50 * Initializes the Element (el) hooks up all the listeners
51 * and packs the children.
55 var cfg = this.config;
57 print("new xobj?" + XObject.keys(cfg).join(','));
61 cfg.items = cfg.items || [];
63 XObject.extend(o, cfg); // copy everything into o.
65 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
67 XObject.extend(this, o);
71 this.listeners = this.listeners || {};
74 // remove objects/functions from o, so they can be sent to the contructor.
76 if ((typeof(o[i]) == 'object') ||
77 (typeof(o[i]) == 'function') ||
88 // do we need to call 'beforeInit here?'
91 //if ((this.xtype == 'Include')) {
92 // o = this.pre_registry[cls];
94 var isSeed = typeof(Seed) != 'undefined';
96 // xtype= Gtk.Menu ?? what about c_new stuff?
98 if (typeof(this.xtype) == 'function') {
99 print("func?" + XObject.keys(o).join(','));
100 this.el = this.el || this.xtype(o);
102 if (typeof(this.xtype) == 'object') {
103 print("obj?" + XObject.keys(o).join(','));
104 this.el = this.el || new this.xtype(o);
107 if (!this.el && o.xns) {
109 var NS = imports.gi[o.xns];
111 Seed.print('Invalid xns: ' + o.xns);
113 constructor = NS[o.xtype];
115 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
117 this.el = isSeed ? new constructor(o) : new constructor();
120 // always overlay props..
125 //if (o.xnsid && o.xid) {
126 // XObject.registry = XObject.registry || { };
127 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
128 // XObject.registry[o.xnsid][o.xid] = this;
131 cfg.items.forEach(this.addItem, this);
133 for (var i in this.listeners) {
134 this.addListener(i, this.listeners[i]);
136 // delete this.listeners ?
139 // do we need to call 'init here?'
145 * Adds an item to the object using a new XObject
146 * uses pack property to determine how to add it.
147 * @arg cfg {Object} same as XObject constructor.
149 addItem : function(o) {
153 var item = o.constructor == XObject ? o : xnew XObject(o);
155 this.items.push(item);
157 if (item.pack===false) { // no
160 if (typeof(item.pack) == 'function') {
162 item.pack.apply(o, [ o , o.items[i] ]);
168 if (typeof(item.pack) == 'string') {
171 pack_m = item.pack.shift();
176 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
177 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
182 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
184 args.unshift(item.el);
185 print('[' + args.join(',') +']');
186 //Seed.print('args: ' + args.length);
188 this.el[pack_m].apply(this.el, args);
195 * @method addListener
196 * Connects a method to a signal. (gjs/Seed aware)
198 * @arg sig {String} name of signal
199 * @arg fn {Function} handler.
201 addListener : function(sig, fn)
204 Seed.print("Add signal " + sig);
206 var _li = XObject.createDelegate(fn,this);
207 // private listeners that are not copied to GTk.
209 if (typeof(Seed) != 'undefined') {
210 // Seed.print(typeof(_li));
211 this.el.signal[sig].connect(_li);
213 this.el.connect( sig, _li);
220 * Finds an object in the child elements using xid of object.
221 * prefix with '.' to look up the tree.. multiple '..' to look further up..
223 * @arg name {String} name of signal
224 * @return {XObject|false} the object if found.
230 return this.parent.get(xid.substring(1));
234 this.items.forEach(function(ch) {
244 this.items.forEach(function(ch) {
258 * Copies all the properties of config to obj.
260 * Pretty much the same as JQuery/Prototype..
261 * @param {Object} obj The receiver of the properties
262 * @param {Object} config The source of the properties
263 * @param {Object} defaults A different object that will also be applied for default values
264 * @return {Object} returns obj
265 * @member XObject extend
269 XObject.extend = function(o, c, defaults){
271 // no "this" reference for friendly out of scope calls
272 XObject.extend(o, defaults);
274 if(o && c && typeof c == 'object'){
282 XObject.extend(XObject,
285 * Copies all the properties of config to obj, if the do not exist.
286 * @param {Object} obj The receiver of the properties
287 * @param {Object} config The source of the properties
288 * @return {Object} returns obj
289 * @member Object extendIf
293 extendIf : function(o, c){
295 if(!o || !c || typeof c != 'object'){
299 if (typeof(o[p]) != 'undefined') {
310 * Extends one class with another class and optionally overrides members with the passed literal. This class
311 * also adds the function "override()" to the class that can be used to override
312 * members on an instance.
315 * MyObject = Object.define(
319 * parentClass, // or Object
321 * ... methods and properties.
324 * @param {Function} constructor The class inheriting the functionality
325 * @param {Object} superclass The class being extended
326 * @param {Object} overrides (optional) A literal with members
327 * @return {Function} constructor (eg. class
332 var io = function(o){
337 return function(sb, sp, overrides) {
338 if (typeof(sp) == 'undefined') {
339 // error condition - try and dump..
340 throw "Missing superclass: when applying: " + sb
343 var F = function(){}, sbp, spp = sp.prototype;
345 sbp = sb.prototype = new F();
350 if(spp.constructor == Object.prototype.constructor){
354 sb.override = function(o){
355 Object.extend(sb.prototype, o);
358 Object.extend(sb.prototype, overrides);
365 * returns a list of keys of the object.
366 * @param {Object} obj object to inspect
367 * @return {Array} returns list of kyes
368 * @member XObject keys
380 * @member XObject createDelegate
381 * creates a delage metdhod
382 * @param {Function} method to wrap
383 * @param {Object} scope
384 * @param {Array} args to add
385 * @param {Boolean|Number} append arguments or replace after N arguments.
386 * @return {Function} returns the delegate
389 createDelegate : function(method, obj, args, appendArgs){
392 var callArgs = args || arguments;
393 if(appendArgs === true){
394 callArgs = Array.prototype.slice.call(arguments, 0);
395 callArgs = callArgs.concat(args);
396 }else if(typeof appendArgs == "number"){
397 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
398 var applyArgs = [appendArgs, 0].concat(args); // create method call params
399 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
401 return method.apply(obj || window, callArgs);