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') {
62 this.el = this.el || new this.xtype(o);
65 if (!this.el && o.xns) {
67 var NS = imports.gi[o.xns];
69 Seed.print('Invalid xns: ' + o.xns);
71 constructor = NS[o.xtype];
73 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
75 this.el = isSeed ? new constructor(o) : new constructor();
77 // this might work for gjs!?
85 //if (o.xnsid && o.xid) {
86 // XObject.registry = XObject.registry || { };
87 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
88 // XObject.registry[o.xnsid][o.xid] = this;
91 cfg.items.forEach(this.addItem, this);
93 for (var i in this.listeners) {
94 this.addListener(i, this.listeners[i]);
96 // delete this.listeners ?
99 // do we need to call 'init here?'
105 XObject.prototype = {
107 * @property el {GObject} the Gtk / etc. element.
111 * @property items {Array} list of sub elements
114 * @property parent {XObject} parent Element
118 * Adds an item to the object using a new XObject
119 * uses pack property to determine how to add it.
120 * @arg cfg {Object} same as XObject constructor.
122 addItem : function(o) {
123 var item = new XObject(o);
125 this.items.push(item);
127 if (item.pack===false) { // no
130 if (typeof(item.pack) == 'function') {
132 item.pack.apply(o, [ o , o.items[i] ]);
139 var pack_m = typeof(item.pack) == 'string' ? item.pack : item.pack.shift();
142 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
143 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
148 Seed.print('Pack ' + this.xtype + '.'+ pack_m + '(' + item.xtype + ')');
150 var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
151 args.unshift(item.el);
152 //Seed.print('args: ' + args.length);
154 this.el[pack_m].apply(item.el, args);
161 * @method addListener
162 * Connects a method to a signal. (gjs/Seed aware)
164 * @arg sig {String} name of signal
165 * @arg fn {Function} handler.
167 addListener : function(sig, fn)
170 Seed.print("Add signal " + sig);
172 var _li = XObject.createDelegate(fn,this);
173 // private listeners that are not copied to GTk.
175 if (typeof(Seed) != 'undefined') {
176 // Seed.print(typeof(_li));
177 this.el.signal[sig].connect(_li);
179 this.el.connect( sig, _li);
186 * Finds an object in the child elements using xid of object.
188 * @arg name {String} name of signal
189 * @return {XObject|false} the object if found.
194 this.items.forEach(function(ch) {
204 this.items.forEach(function(ch) {
218 * Copies all the properties of config to obj.
220 * Pretty much the same as JQuery/Prototype..
221 * @param {Object} obj The receiver of the properties
222 * @param {Object} config The source of the properties
223 * @param {Object} defaults A different object that will also be applied for default values
224 * @return {Object} returns obj
225 * @member XObject extend
229 XObject.extend = function(o, c, defaults){
231 // no "this" reference for friendly out of scope calls
232 XObject.extend(o, defaults);
234 if(o && c && typeof c == 'object'){
242 XObject.extend(XObject,
245 * Copies all the properties of config to obj, if the do not exist.
246 * @param {Object} obj The receiver of the properties
247 * @param {Object} config The source of the properties
248 * @return {Object} returns obj
249 * @member Object extendIf
253 extendIf : function(o, c){
255 if(!o || !c || typeof c != 'object'){
259 if (typeof(o[p]) != 'undefined') {
270 * Extends one class with another class and optionally overrides members with the passed literal. This class
271 * also adds the function "override()" to the class that can be used to override
272 * members on an instance.
275 * MyObject = Object.define(
279 * parentClass, // or Object
281 * ... methods and properties.
284 * @param {Function} constructor The class inheriting the functionality
285 * @param {Object} superclass The class being extended
286 * @param {Object} overrides (optional) A literal with members
287 * @return {Function} constructor (eg. class
292 var io = function(o){
297 return function(sb, sp, overrides) {
298 if (typeof(sp) == 'undefined') {
299 // error condition - try and dump..
300 throw "Missing superclass: when applying: " + sb
303 var F = function(){}, sbp, spp = sp.prototype;
305 sbp = sb.prototype = new F();
310 if(spp.constructor == Object.prototype.constructor){
314 sb.override = function(o){
315 Object.extend(sb.prototype, o);
318 Object.extend(sb.prototype, overrides);
325 * returns a list of keys of the object.
326 * @param {Object} obj object to inspect
327 * @return {Array} returns list of kyes
328 * @member XObject keys
340 * @member XObject createDelegate
341 * creates a delage metdhod
342 * @param {Function} method to wrap
343 * @param {Object} scope
344 * @param {Array} args to add
345 * @param {Boolean|Number} append arguments or replace after N arguments.
346 * @return {Function} returns the delegate
349 createDelegate : function(method, obj, args, appendArgs){
352 var callArgs = args || arguments;
353 if(appendArgs === true){
354 callArgs = Array.prototype.slice.call(arguments, 0);
355 callArgs = callArgs.concat(args);
356 }else if(typeof appendArgs == "number"){
357 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
358 var applyArgs = [appendArgs, 0].concat(args); // create method call params
359 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
361 return method.apply(obj || window, callArgs);