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..
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
18 * --- needs a xdebug option!
21 function XObject (cfg) {
22 // first apply cfg if set.
24 print("new xobj?" + XObject.keys(cfg).join(','));
28 cfg.items = cfg.items || [];
30 XObject.extend(o, cfg); // copy everything into o.
32 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
34 XObject.extend(this, o);
38 this.listeners = this.listeners || {};
41 // remove objects/functions from o, so they can be sent to the contructor.
43 if ((typeof(o[i]) == 'object') ||
44 (typeof(o[i]) == 'function') ||
55 // do we need to call 'beforeInit here?'
58 //if ((this.xtype == 'Include')) {
59 // o = this.pre_registry[cls];
61 var isSeed = typeof(Seed) != 'undefined';
63 // xtype= Gtk.Menu ?? what about c_new stuff?
65 if (typeof(this.xtype) == 'function') {
66 print("func?" + XObject.keys(o).join(','));
67 this.el = this.el || this.xtype(o);
69 if (typeof(this.xtype) == 'object') {
70 print("obj?" + XObject.keys(o).join(','));
71 this.el = this.el || new this.xtype(o);
74 if (!this.el && o.xns) {
76 var NS = imports.gi[o.xns];
78 Seed.print('Invalid xns: ' + o.xns);
80 constructor = NS[o.xtype];
82 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
84 this.el = isSeed ? new constructor(o) : new constructor();
87 // always overlay props..
92 //if (o.xnsid && o.xid) {
93 // XObject.registry = XObject.registry || { };
94 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
95 // XObject.registry[o.xnsid][o.xid] = this;
98 cfg.items.forEach(this.addItem, this);
100 for (var i in this.listeners) {
101 this.addListener(i, this.listeners[i]);
103 // delete this.listeners ?
106 // do we need to call 'init here?'
112 XObject.prototype = {
114 * @property el {GObject} the Gtk / etc. element.
118 * @property items {Array} list of sub elements
121 * @property parent {XObject} parent Element
125 * Adds an item to the object using a new XObject
126 * uses pack property to determine how to add it.
127 * @arg cfg {Object} same as XObject constructor.
129 addItem : function(o) {
130 var item = new XObject(o);
132 this.items.push(item);
134 if (item.pack===false) { // no
137 if (typeof(item.pack) == 'function') {
139 item.pack.apply(o, [ o , o.items[i] ]);
145 if (typeof(item.pack) == 'string') {
148 pack_m = item.pack.shift();
153 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
154 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
159 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
161 args.unshift(item.el);
162 print('[' + args.join(',') +']');
163 //Seed.print('args: ' + args.length);
165 this.el[pack_m].apply(this.el, args);
172 * @method addListener
173 * Connects a method to a signal. (gjs/Seed aware)
175 * @arg sig {String} name of signal
176 * @arg fn {Function} handler.
178 addListener : function(sig, fn)
181 Seed.print("Add signal " + sig);
183 var _li = XObject.createDelegate(fn,this);
184 // private listeners that are not copied to GTk.
186 if (typeof(Seed) != 'undefined') {
187 // Seed.print(typeof(_li));
188 this.el.signal[sig].connect(_li);
190 this.el.connect( sig, _li);
197 * Finds an object in the child elements using xid of object.
198 * prefix with '.' to look up the tree.. multiple '..' to look further up..
200 * @arg name {String} name of signal
201 * @return {XObject|false} the object if found.
207 return this.parent.get(xid.substring(1));
211 this.items.forEach(function(ch) {
221 this.items.forEach(function(ch) {
235 * Copies all the properties of config to obj.
237 * Pretty much the same as JQuery/Prototype..
238 * @param {Object} obj The receiver of the properties
239 * @param {Object} config The source of the properties
240 * @param {Object} defaults A different object that will also be applied for default values
241 * @return {Object} returns obj
242 * @member XObject extend
246 XObject.extend = function(o, c, defaults){
248 // no "this" reference for friendly out of scope calls
249 XObject.extend(o, defaults);
251 if(o && c && typeof c == 'object'){
259 XObject.extend(XObject,
262 * Copies all the properties of config to obj, if the do not exist.
263 * @param {Object} obj The receiver of the properties
264 * @param {Object} config The source of the properties
265 * @return {Object} returns obj
266 * @member Object extendIf
270 extendIf : function(o, c){
272 if(!o || !c || typeof c != 'object'){
276 if (typeof(o[p]) != 'undefined') {
287 * Extends one class with another class and optionally overrides members with the passed literal. This class
288 * also adds the function "override()" to the class that can be used to override
289 * members on an instance.
292 * MyObject = Object.define(
296 * parentClass, // or Object
298 * ... methods and properties.
301 * @param {Function} constructor The class inheriting the functionality
302 * @param {Object} superclass The class being extended
303 * @param {Object} overrides (optional) A literal with members
304 * @return {Function} constructor (eg. class
309 var io = function(o){
314 return function(sb, sp, overrides) {
315 if (typeof(sp) == 'undefined') {
316 // error condition - try and dump..
317 throw "Missing superclass: when applying: " + sb
320 var F = function(){}, sbp, spp = sp.prototype;
322 sbp = sb.prototype = new F();
327 if(spp.constructor == Object.prototype.constructor){
331 sb.override = function(o){
332 Object.extend(sb.prototype, o);
335 Object.extend(sb.prototype, overrides);
342 * returns a list of keys of the object.
343 * @param {Object} obj object to inspect
344 * @return {Array} returns list of kyes
345 * @member XObject keys
357 * @member XObject createDelegate
358 * creates a delage metdhod
359 * @param {Function} method to wrap
360 * @param {Object} scope
361 * @param {Array} args to add
362 * @param {Boolean|Number} append arguments or replace after N arguments.
363 * @return {Function} returns the delegate
366 createDelegate : function(method, obj, args, appendArgs){
369 var callArgs = args || arguments;
370 if(appendArgs === true){
371 callArgs = Array.prototype.slice.call(arguments, 0);
372 callArgs = callArgs.concat(args);
373 }else if(typeof appendArgs == "number"){
374 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
375 var applyArgs = [appendArgs, 0].concat(args); // create method call params
376 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
378 return method.apply(obj || window, callArgs);