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
19 function XObject (cfg) {
20 // first apply cfg if set.
22 o.pack = o.pack || 'add';
23 cfg.items = cfg.items || [];
25 XObject.extend(o, cfg); // copy everything into o.
26 XObject.extend(this, o);
30 this.listeners = this.listeners || {};
33 // remove objects/functions from o, so they can be sent to the contructor.
35 if ((typeof(o[i]) == 'object') ||
36 (typeof(o[i]) == 'function') ||
46 // do we need to call 'beforeInit here?'
49 //if ((this.xtype == 'Include')) {
50 // o = this.pre_registry[cls];
52 var isSeed = typeof(Seed) != 'undefined';
54 // xtype= Gtk.Menu ?? what about c_new stuff?
55 if (typeof(this.xtype) == 'function') {
56 this.el = this.el || new this.xtype(o);
59 if (!this.el && o.xns) {
61 var NS = imports.gi[o.xns];
63 Seed.print('Invalid xns: ' + o.xns);
65 constructor = NS[o.xtype];
67 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
69 this.el = isSeed ? new constructor(o) : new constructor();
71 // this might work for gjs!?
79 //if (o.xnsid && o.xid) {
80 // XObject.registry = XObject.registry || { };
81 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
82 // XObject.registry[o.xnsid][o.xid] = this;
85 cfg.items.forEach(this.addItem, this);
87 for (var i in this.listeners) {
88 this.addListener(i, this.listeners[i]);
90 // delete this.listeners ?
93 // do we need to call 'init here?'
101 * @property el {GObject} the Gtk / etc. element.
105 * @property items {Array} list of sub elements
108 * @property parent {XObject} parent Element
112 * Adds an item to the object using a new XObject
113 * uses pack property to determine how to add it.
114 * @arg cfg {Object} same as XObject constructor.
116 addItem : function(o) {
117 var item = new XObject(o);
119 this.items.push(item);
122 if (typeof(item.pack) == 'function') {
124 item.pack.apply(o, [ o , o.items[i] ]);
130 var pack_m = typeof(item.pack) == 'string' ? item.pack : item.pack.shift();
133 if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
134 Seed.print('pack method not available : ' + o.xtype + '.' + pack_m);
139 //Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
141 var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
142 item.pack.unshift(item.el);
143 //Seed.print('args: ' + args.length);
145 this.el[pack_m].apply(item.el, args);
152 * @method addListener
153 * Connects a method to a signal. (gjs/Seed aware)
155 * @arg sig {String} name of signal
156 * @arg fn {Function} handler.
158 addListener : function(sig, fn) {
160 var _li = XObject.createDelegate(fn,this);
161 // private listeners that are not copied to GTk.
163 if (typeof(Seed) != 'undefined') {
164 // Seed.print(typeof(_li));
165 this.el.signal[sig].connect(_li);
167 this.el.connect( sig, _li);
174 * Finds an object in the child elements using xid of object.
176 * @arg name {String} name of signal
177 * @return {XObject|false} the object if found.
182 this.items.each(function(ch) {
192 this.items.each(function(ch) {
206 * Copies all the properties of config to obj.
208 * Pretty much the same as JQuery/Prototype..
209 * @param {Object} obj The receiver of the properties
210 * @param {Object} config The source of the properties
211 * @param {Object} defaults A different object that will also be applied for default values
212 * @return {Object} returns obj
213 * @member XObject extend
217 XObject.extend = function(o, c, defaults){
219 // no "this" reference for friendly out of scope calls
220 XObject.extend(o, defaults);
222 if(o && c && typeof c == 'object'){
230 XObject.extend(XObject,
233 * Copies all the properties of config to obj, if the do not exist.
234 * @param {Object} obj The receiver of the properties
235 * @param {Object} config The source of the properties
236 * @return {Object} returns obj
237 * @member Object extendIf
241 extendIf : function(o, c){
243 if(!o || !c || typeof c != 'object'){
247 if (typeof(o[p]) != 'undefined') {
258 * Extends one class with another class and optionally overrides members with the passed literal. This class
259 * also adds the function "override()" to the class that can be used to override
260 * members on an instance.
263 * MyObject = Object.define(
267 * parentClass, // or Object
269 * ... methods and properties.
272 * @param {Function} constructor The class inheriting the functionality
273 * @param {Object} superclass The class being extended
274 * @param {Object} overrides (optional) A literal with members
275 * @return {Function} constructor (eg. class
280 var io = function(o){
285 return function(sb, sp, overrides) {
286 if (typeof(sp) == 'undefined') {
287 // error condition - try and dump..
288 throw "Missing superclass: when applying: " + sb
291 var F = function(){}, sbp, spp = sp.prototype;
293 sbp = sb.prototype = new F();
298 if(spp.constructor == Object.prototype.constructor){
302 sb.override = function(o){
303 Object.extend(sb.prototype, o);
306 Object.extend(sb.prototype, overrides);
313 * returns a list of keys of the object.
314 * @param {Object} obj object to inspect
315 * @return {Array} returns list of kyes
316 * @member XObject keys
328 * @member XObject createDelegate
329 * creates a delage metdhod
330 * @param {Function} method to wrap
331 * @param {Object} scope
332 * @param {Array} args to add
333 * @param {Boolean|Number} append arguments or replace after N arguments.
334 * @return {Function} returns the delegate
337 createDelegate : function(method, obj, args, appendArgs){
340 var callArgs = args || arguments;
341 if(appendArgs === true){
342 callArgs = Array.prototype.slice.call(arguments, 0);
343 callArgs = callArgs.concat(args);
344 }else if(typeof appendArgs == "number"){
345 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
346 var applyArgs = [appendArgs, 0].concat(args); // create method call params
347 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
349 return method.apply(obj || window, callArgs);