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.
23 cfg.items = cfg.items || [];
25 XObject.extend(o, cfg); // copy everything into o.
27 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
29 XObject.extend(this, o);
33 this.listeners = this.listeners || {};
36 // remove objects/functions from o, so they can be sent to the contructor.
38 if ((typeof(o[i]) == 'object') ||
39 (typeof(o[i]) == 'function') ||
49 // do we need to call 'beforeInit here?'
52 //if ((this.xtype == 'Include')) {
53 // o = this.pre_registry[cls];
55 var isSeed = typeof(Seed) != 'undefined';
57 // xtype= Gtk.Menu ?? what about c_new stuff?
58 if (typeof(this.xtype) == 'function') {
59 this.el = this.el || this.xtype(o);
61 if (typeof(this.xtype) == 'object') {
63 this.el = this.el || new this.xtype(o);
66 if (!this.el && o.xns) {
68 var NS = imports.gi[o.xns];
70 Seed.print('Invalid xns: ' + o.xns);
72 constructor = NS[o.xtype];
74 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
76 this.el = isSeed ? new constructor(o) : new constructor();
78 // this might work for gjs!?
86 //if (o.xnsid && o.xid) {
87 // XObject.registry = XObject.registry || { };
88 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
89 // XObject.registry[o.xnsid][o.xid] = this;
92 cfg.items.forEach(this.addItem, this);
94 for (var i in this.listeners) {
95 this.addListener(i, this.listeners[i]);
97 // delete this.listeners ?
100 // do we need to call 'init here?'
106 XObject.prototype = {
108 * @property el {GObject} the Gtk / etc. element.
112 * @property items {Array} list of sub elements
115 * @property parent {XObject} parent Element
119 * Adds an item to the object using a new XObject
120 * uses pack property to determine how to add it.
121 * @arg cfg {Object} same as XObject constructor.
123 addItem : function(o) {
124 var item = new XObject(o);
126 this.items.push(item);
128 if (item.pack===false) { // no
131 if (typeof(item.pack) == 'function') {
133 item.pack.apply(o, [ o , o.items[i] ]);
139 if (typeof(item.pack) == 'string') {
142 pack_m = item.pack.shift();
147 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
148 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
153 Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
155 args.unshift(item.el);
156 print('[' + args.join(',') +']');
157 //Seed.print('args: ' + args.length);
159 this.el[pack_m].apply(item.el, args);
166 * @method addListener
167 * Connects a method to a signal. (gjs/Seed aware)
169 * @arg sig {String} name of signal
170 * @arg fn {Function} handler.
172 addListener : function(sig, fn)
175 Seed.print("Add signal " + sig);
177 var _li = XObject.createDelegate(fn,this);
178 // private listeners that are not copied to GTk.
180 if (typeof(Seed) != 'undefined') {
181 // Seed.print(typeof(_li));
182 this.el.signal[sig].connect(_li);
184 this.el.connect( sig, _li);
191 * Finds an object in the child elements using xid of object.
193 * @arg name {String} name of signal
194 * @return {XObject|false} the object if found.
199 this.items.forEach(function(ch) {
209 this.items.forEach(function(ch) {
223 * Copies all the properties of config to obj.
225 * Pretty much the same as JQuery/Prototype..
226 * @param {Object} obj The receiver of the properties
227 * @param {Object} config The source of the properties
228 * @param {Object} defaults A different object that will also be applied for default values
229 * @return {Object} returns obj
230 * @member XObject extend
234 XObject.extend = function(o, c, defaults){
236 // no "this" reference for friendly out of scope calls
237 XObject.extend(o, defaults);
239 if(o && c && typeof c == 'object'){
247 XObject.extend(XObject,
250 * Copies all the properties of config to obj, if the do not exist.
251 * @param {Object} obj The receiver of the properties
252 * @param {Object} config The source of the properties
253 * @return {Object} returns obj
254 * @member Object extendIf
258 extendIf : function(o, c){
260 if(!o || !c || typeof c != 'object'){
264 if (typeof(o[p]) != 'undefined') {
275 * Extends one class with another class and optionally overrides members with the passed literal. This class
276 * also adds the function "override()" to the class that can be used to override
277 * members on an instance.
280 * MyObject = Object.define(
284 * parentClass, // or Object
286 * ... methods and properties.
289 * @param {Function} constructor The class inheriting the functionality
290 * @param {Object} superclass The class being extended
291 * @param {Object} overrides (optional) A literal with members
292 * @return {Function} constructor (eg. class
297 var io = function(o){
302 return function(sb, sp, overrides) {
303 if (typeof(sp) == 'undefined') {
304 // error condition - try and dump..
305 throw "Missing superclass: when applying: " + sb
308 var F = function(){}, sbp, spp = sp.prototype;
310 sbp = sb.prototype = new F();
315 if(spp.constructor == Object.prototype.constructor){
319 sb.override = function(o){
320 Object.extend(sb.prototype, o);
323 Object.extend(sb.prototype, overrides);
330 * returns a list of keys of the object.
331 * @param {Object} obj object to inspect
332 * @return {Array} returns list of kyes
333 * @member XObject keys
345 * @member XObject createDelegate
346 * creates a delage metdhod
347 * @param {Function} method to wrap
348 * @param {Object} scope
349 * @param {Array} args to add
350 * @param {Boolean|Number} append arguments or replace after N arguments.
351 * @return {Function} returns the delegate
354 createDelegate : function(method, obj, args, appendArgs){
357 var callArgs = args || arguments;
358 if(appendArgs === true){
359 callArgs = Array.prototype.slice.call(arguments, 0);
360 callArgs = callArgs.concat(args);
361 }else if(typeof appendArgs == "number"){
362 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
363 var applyArgs = [appendArgs, 0].concat(args); // create method call params
364 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
366 return method.apply(obj || window, callArgs);