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..
13 * @arg xtype {String|Function} constructor or string.
14 * @arg xid {String} (optional) id for registry
15 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
16 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
17 * @arg listeners {Object} (optional) map Gobject signals to functions
18 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
19 * @arg el {Object} (optional) premade GObject
21 * --- needs a xdebug option!
24 function XObject (cfg) {
25 // first apply cfg if set.
35 * @property el {GObject} the Gtk / etc. element.
39 * @property items {Array} list of sub elements
42 * @property parent {XObject} parent Element
46 * @property config {Object} the construction configuration.
50 * Initializes the Element (el) hooks up all the listeners
51 * and packs the children.
55 var cfg = this.config;
57 print("new xobj?" + XObject.keys(cfg).join(','));
61 cfg.items = cfg.items || [];
63 XObject.extend(o, cfg); // copy everything into o.
65 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
67 XObject.extend(this, o);
71 this.listeners = this.listeners || {};
74 // remove objects/functions from o, so they can be sent to the contructor.
76 if ((typeof(o[i]) == 'object') ||
77 (typeof(o[i]) == 'function') ||
88 // do we need to call 'beforeInit here?'
91 //if ((this.xtype == 'Include')) {
92 // o = this.pre_registry[cls];
94 var isSeed = typeof(Seed) != 'undefined';
96 // xtype= Gtk.Menu ?? what about c_new stuff?
98 if (typeof(this.xtype) == 'function') {
99 print("func?" + XObject.keys(o).join(','));
100 this.el = this.el || this.xtype(o);
102 if (typeof(this.xtype) == 'object') {
103 print("obj?" + XObject.keys(o).join(','));
104 this.el = this.el || new this.xtype(o);
107 if (!this.el && o.xns) {
109 var NS = imports.gi[o.xns];
111 Seed.print('Invalid xns: ' + o.xns);
113 constructor = NS[o.xtype];
115 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
117 this.el = isSeed ? new constructor(o) : new constructor();
120 // always overlay props..
125 //if (o.xnsid && o.xid) {
126 // XObject.registry = XObject.registry || { };
127 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
128 // XObject.registry[o.xnsid][o.xid] = this;
131 cfg.items.forEach(this.addItem, this);
133 for (var i in this.listeners) {
134 this.addListener(i, this.listeners[i]);
136 // delete this.listeners ?
139 // do we need to call 'init here?'
145 * Adds an item to the object using a new XObject
146 * uses pack property to determine how to add it.
147 * @arg cfg {Object} same as XObject constructor.
149 addItem : function(o) {
152 var item = (o.constructor == XObject) ? o : new XObject(o);
154 this.items.push(item);
156 if (item.pack===false) { // no
159 if (typeof(item.pack) == 'function') {
161 item.pack.apply(o, [ o , o.items[i] ]);
167 if (typeof(item.pack) == 'string') {
170 pack_m = item.pack.shift();
175 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
176 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
181 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
183 args.unshift(item.el);
184 print('[' + args.join(',') +']');
185 //Seed.print('args: ' + args.length);
187 this.el[pack_m].apply(this.el, args);
194 * @method addListener
195 * Connects a method to a signal. (gjs/Seed aware)
197 * @arg sig {String} name of signal
198 * @arg fn {Function} handler.
200 addListener : function(sig, fn)
203 Seed.print("Add signal " + sig);
205 var _li = XObject.createDelegate(fn,this);
206 // private listeners that are not copied to GTk.
208 if (typeof(Seed) != 'undefined') {
209 // Seed.print(typeof(_li));
210 this.el.signal[sig].connect(_li);
212 this.el.connect( sig, _li);
219 * Finds an object in the child elements using xid of object.
220 * prefix with '.' to look up the tree.. multiple '..' to look further up..
222 * @arg name {String} name of signal
223 * @return {XObject|false} the object if found.
229 return this.parent.get(xid.substring(1));
233 this.items.forEach(function(ch) {
243 this.items.forEach(function(ch) {
257 * Copies all the properties of config to obj.
259 * Pretty much the same as JQuery/Prototype..
260 * @param {Object} obj The receiver of the properties
261 * @param {Object} config The source of the properties
262 * @param {Object} defaults A different object that will also be applied for default values
263 * @return {Object} returns obj
264 * @member XObject extend
268 XObject.extend = function(o, c, defaults){
270 // no "this" reference for friendly out of scope calls
271 XObject.extend(o, defaults);
273 if(o && c && typeof c == 'object'){
281 XObject.extend(XObject,
284 * Copies all the properties of config to obj, if the do not exist.
285 * @param {Object} obj The receiver of the properties
286 * @param {Object} config The source of the properties
287 * @return {Object} returns obj
288 * @member Object extendIf
292 extendIf : function(o, c){
294 if(!o || !c || typeof c != 'object'){
298 if (typeof(o[p]) != 'undefined') {
309 * Extends one class with another class and optionally overrides members with the passed literal. This class
310 * also adds the function "override()" to the class that can be used to override
311 * members on an instance.
314 * MyObject = Object.define(
318 * parentClass, // or Object
320 * ... methods and properties.
323 * @param {Function} constructor The class inheriting the functionality
324 * @param {Object} superclass The class being extended
325 * @param {Object} overrides (optional) A literal with members
326 * @return {Function} constructor (eg. class
331 var io = function(o){
336 return function(sb, sp, overrides) {
337 if (typeof(sp) == 'undefined') {
338 // error condition - try and dump..
339 throw "Missing superclass: when applying: " + sb
342 var F = function(){}, sbp, spp = sp.prototype;
344 sbp = sb.prototype = new F();
349 if(spp.constructor == Object.prototype.constructor){
353 sb.override = function(o){
354 Object.extend(sb.prototype, o);
357 Object.extend(sb.prototype, overrides);
364 * returns a list of keys of the object.
365 * @param {Object} obj object to inspect
366 * @return {Array} returns list of kyes
367 * @member XObject keys
379 * @member XObject createDelegate
380 * creates a delage metdhod
381 * @param {Function} method to wrap
382 * @param {Object} scope
383 * @param {Array} args to add
384 * @param {Boolean|Number} append arguments or replace after N arguments.
385 * @return {Function} returns the delegate
388 createDelegate : function(method, obj, args, appendArgs){
391 var callArgs = args || arguments;
392 if(appendArgs === true){
393 callArgs = Array.prototype.slice.call(arguments, 0);
394 callArgs = callArgs.concat(args);
395 }else if(typeof appendArgs == "number"){
396 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
397 var applyArgs = [appendArgs, 0].concat(args); // create method call params
398 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
400 return method.apply(obj || window, callArgs);