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 || this.xtype(o);
58 if (typeof(this.xtype) == 'object') {
59 this.el = this.el || new this.xtype(o);
62 if (!this.el && o.xns) {
64 var NS = imports.gi[o.xns];
66 Seed.print('Invalid xns: ' + o.xns);
68 constructor = NS[o.xtype];
70 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
72 this.el = isSeed ? new constructor(o) : new constructor();
74 // this might work for gjs!?
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;
88 cfg.items.forEach(this.addItem, this);
90 for (var i in this.listeners) {
91 this.addListener(i, this.listeners[i]);
93 // delete this.listeners ?
96 // do we need to call 'init here?'
102 XObject.prototype = {
104 * @property el {GObject} the Gtk / etc. element.
108 * @property items {Array} list of sub elements
111 * @property parent {XObject} parent Element
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.
119 addItem : function(o) {
120 var item = new XObject(o);
122 this.items.push(item);
124 if (item.pack && item.pack===false) { // no
127 if (typeof(item.pack) == 'function') {
129 item.pack.apply(o, [ o , o.items[i] ]);
135 var pack_m = typeof(item.pack) == 'string' ? item.pack : item.pack.shift();
138 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
139 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
144 //Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
146 var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
147 args.unshift(item.el);
148 //Seed.print('args: ' + args.length);
150 this.el[pack_m].apply(item.el, args);
157 * @method addListener
158 * Connects a method to a signal. (gjs/Seed aware)
160 * @arg sig {String} name of signal
161 * @arg fn {Function} handler.
163 addListener : function(sig, fn)
166 Seed.print("Add signal " + sig);
168 var _li = XObject.createDelegate(fn,this);
169 // private listeners that are not copied to GTk.
171 if (typeof(Seed) != 'undefined') {
172 // Seed.print(typeof(_li));
173 this.el.signal[sig].connect(_li);
175 this.el.connect( sig, _li);
182 * Finds an object in the child elements using xid of object.
184 * @arg name {String} name of signal
185 * @return {XObject|false} the object if found.
190 this.items.each(function(ch) {
200 this.items.each(function(ch) {
214 * Copies all the properties of config to obj.
216 * Pretty much the same as JQuery/Prototype..
217 * @param {Object} obj The receiver of the properties
218 * @param {Object} config The source of the properties
219 * @param {Object} defaults A different object that will also be applied for default values
220 * @return {Object} returns obj
221 * @member XObject extend
225 XObject.extend = function(o, c, defaults){
227 // no "this" reference for friendly out of scope calls
228 XObject.extend(o, defaults);
230 if(o && c && typeof c == 'object'){
238 XObject.extend(XObject,
241 * Copies all the properties of config to obj, if the do not exist.
242 * @param {Object} obj The receiver of the properties
243 * @param {Object} config The source of the properties
244 * @return {Object} returns obj
245 * @member Object extendIf
249 extendIf : function(o, c){
251 if(!o || !c || typeof c != 'object'){
255 if (typeof(o[p]) != 'undefined') {
266 * Extends one class with another class and optionally overrides members with the passed literal. This class
267 * also adds the function "override()" to the class that can be used to override
268 * members on an instance.
271 * MyObject = Object.define(
275 * parentClass, // or Object
277 * ... methods and properties.
280 * @param {Function} constructor The class inheriting the functionality
281 * @param {Object} superclass The class being extended
282 * @param {Object} overrides (optional) A literal with members
283 * @return {Function} constructor (eg. class
288 var io = function(o){
293 return function(sb, sp, overrides) {
294 if (typeof(sp) == 'undefined') {
295 // error condition - try and dump..
296 throw "Missing superclass: when applying: " + sb
299 var F = function(){}, sbp, spp = sp.prototype;
301 sbp = sb.prototype = new F();
306 if(spp.constructor == Object.prototype.constructor){
310 sb.override = function(o){
311 Object.extend(sb.prototype, o);
314 Object.extend(sb.prototype, overrides);
321 * returns a list of keys of the object.
322 * @param {Object} obj object to inspect
323 * @return {Array} returns list of kyes
324 * @member XObject keys
336 * @member XObject createDelegate
337 * creates a delage metdhod
338 * @param {Function} method to wrap
339 * @param {Object} scope
340 * @param {Array} args to add
341 * @param {Boolean|Number} append arguments or replace after N arguments.
342 * @return {Function} returns the delegate
345 createDelegate : function(method, obj, args, appendArgs){
348 var callArgs = args || arguments;
349 if(appendArgs === true){
350 callArgs = Array.prototype.slice.call(arguments, 0);
351 callArgs = callArgs.concat(args);
352 }else if(typeof appendArgs == "number"){
353 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
354 var applyArgs = [appendArgs, 0].concat(args); // create method call params
355 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
357 return method.apply(obj || window, callArgs);