1 //<script type="text/javascript">
5 * Yet another attempt to create a usable object construction library for seed..
8 * A) It turns rather messy code into a tree structure, making it easy to find code relating to
10 * B) In theory it should be gjs/Seed compatible..
11 * C) It provides getElementById style lookups for elements.
13 * Extend this.. to use it's wonderful features..
23 * Xyz.init(); // create and show.
27 * @arg xtype {String|Function} constructor or string.
28 * @arg id {String} (optional) id for registry
29 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
30 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
31 * @arg listeners {Object} (optional) map Gobject signals to functions
32 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
33 * @arg el {Object} (optional) premade GObject
35 * --- needs a xdebug option!
38 * He's some questions.
39 * - should we generate ID's for all elements? (if so we probably need to garbage collect)
40 * - should we have a special property to use as the constructor / gobject.properties rather
41 * than sending all basic types to this?
46 function XObject (cfg) {
47 // first apply cfg if set.
50 this.init = cfg.init; // override!
60 * @property el {GObject} the Gtk / etc. element.
64 * @property items {Array} list of sub elements
67 * @property parent {XObject} parent Element
71 * @property config {Object} the construction configuration.
75 * Initializes the Element (el) hooks up all the listeners
76 * and packs the children.
77 * you can override this, in child objects, then
78 * do this to do thi initaliztion.
80 * XObject.prototype.init.call(this);
85 var cfg = this.config;
87 print("new xobj?" + XObject.keys(cfg).join(','));
91 cfg.items = cfg.items || [];
93 XObject.extend(o, cfg); // copy everything into o.
95 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
97 XObject.extend(this, o);
101 this.listeners = this.listeners || {};
104 // remove objects/functions from o, so they can be sent to the contructor.
106 if ((typeof(o[i]) == 'object') ||
107 (typeof(o[i]) == 'function') ||
118 // do we need to call 'beforeInit here?'
121 //if ((this.xtype == 'Include')) {
122 // o = this.pre_registry[cls];
124 var isSeed = typeof(Seed) != 'undefined';
126 // xtype= Gtk.Menu ?? what about c_new stuff?
128 if (typeof(this.xtype) == 'function') {
129 print("func?" + XObject.keys(o).join(','));
130 this.el = this.el || this.xtype(o);
132 if (typeof(this.xtype) == 'object') {
133 print("obj?" + XObject.keys(o).join(','));
134 this.el = this.el || new this.xtype(o);
137 if (!this.el && o.xns) {
139 var NS = imports.gi[o.xns];
141 Seed.print('Invalid xns: ' + o.xns);
143 constructor = NS[o.xtype];
145 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
147 this.el = isSeed ? new constructor(o) : new constructor();
150 // always overlay props..
155 //if (o.xnsid && o.id) {
156 // XObject.registry = XObject.registry || { };
157 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
158 // XObject.registry[o.xnsid][o.id] = this;
161 cfg.items.forEach(this.addItem, this);
163 for (var i in this.listeners) {
164 this.addListener(i, this.listeners[i]);
166 // delete this.listeners ?
169 // do we need to call 'init here?'
175 * Adds an item to the object using a new XObject
176 * uses pack property to determine how to add it.
177 * @arg cfg {Object} same as XObject constructor.
179 addItem : function(o) {
182 var item = (o.constructor == XObject) ? o : new XObject(o);
185 this.items.push(item);
187 if (item.pack===false) { // no
190 if (typeof(item.pack) == 'function') {
192 item.pack.apply(o, [ o , o.items[i] ]);
198 if (typeof(item.pack) == 'string') {
201 pack_m = item.pack.shift();
206 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
207 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
212 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
214 args.unshift(item.el);
215 print('[' + args.join(',') +']');
216 //Seed.print('args: ' + args.length);
218 this.el[pack_m].apply(this.el, args);
225 * @method addListener
226 * Connects a method to a signal. (gjs/Seed aware)
228 * @arg sig {String} name of signal
229 * @arg fn {Function} handler.
231 addListener : function(sig, fn)
234 Seed.print("Add signal " + sig);
236 var _li = XObject.createDelegate(fn,this);
237 // private listeners that are not copied to GTk.
239 if (typeof(Seed) != 'undefined') {
240 // Seed.print(typeof(_li));
241 this.el.signal[sig].connect(_li);
243 this.el.connect( sig, _li);
250 * Finds an object in the child elements using xid of object.
251 * prefix with '.' to look up the tree.. multiple '..' to look further up..
253 * @arg name {String} name of signal
254 * @return {XObject|false} the object if found.
260 return this.parent.get(xid.substring(1));
264 this.items.forEach(function(ch) {
274 this.items.forEach(function(ch) {
288 * Copies all the properties of config to obj.
290 * Pretty much the same as JQuery/Prototype..
291 * @param {Object} obj The receiver of the properties
292 * @param {Object} config The source of the properties
293 * @param {Object} defaults A different object that will also be applied for default values
294 * @return {Object} returns obj
295 * @member XObject extend
299 XObject.extend = function(o, c, defaults){
301 // no "this" reference for friendly out of scope calls
302 XObject.extend(o, defaults);
304 if(o && c && typeof c == 'object'){
312 XObject.extend(XObject,
315 * Copies all the properties of config to obj, if the do not exist.
316 * @param {Object} obj The receiver of the properties
317 * @param {Object} config The source of the properties
318 * @return {Object} returns obj
319 * @member Object extendIf
323 extendIf : function(o, c){
325 if(!o || !c || typeof c != 'object'){
329 if (typeof(o[p]) != 'undefined') {
340 * Extends one class with another class and optionally overrides members with the passed literal. This class
341 * also adds the function "override()" to the class that can be used to override
342 * members on an instance.
345 * MyObject = Object.define(
349 * parentClass, // or Object
351 * ... methods and properties.
354 * @param {Function} constructor The class inheriting the functionality
355 * @param {Object} superclass The class being extended
356 * @param {Object} overrides (optional) A literal with members
357 * @return {Function} constructor (eg. class
362 var io = function(o){
367 return function(sb, sp, overrides) {
368 if (typeof(sp) == 'undefined') {
369 // error condition - try and dump..
370 throw "Missing superclass: when applying: " + sb
373 var F = function(){}, sbp, spp = sp.prototype;
375 sbp = sb.prototype = new F();
380 if(spp.constructor == Object.prototype.constructor){
384 sb.override = function(o){
385 Object.extend(sb.prototype, o);
388 Object.extend(sb.prototype, overrides);
395 * returns a list of keys of the object.
396 * @param {Object} obj object to inspect
397 * @return {Array} returns list of kyes
398 * @member XObject keys
410 * @member XObject createDelegate
411 * creates a delage metdhod
412 * @param {Function} method to wrap
413 * @param {Object} scope
414 * @param {Array} args to add
415 * @param {Boolean|Number} append arguments or replace after N arguments.
416 * @return {Function} returns the delegate
419 createDelegate : function(method, obj, args, appendArgs){
422 var callArgs = args || arguments;
423 if(appendArgs === true){
424 callArgs = Array.prototype.slice.call(arguments, 0);
425 callArgs = callArgs.concat(args);
426 }else if(typeof appendArgs == "number"){
427 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
428 var applyArgs = [appendArgs, 0].concat(args); // create method call params
429 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
431 return method.apply(obj || window, callArgs);