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.
12 * D) It provides classic OO constructors for Javascript (extend/define)
13 * E) It does not modify any buildin prototypes..
15 * Extend this.. to use it's wonderful features..
18 * XObject = imports.XObject.XObject;
27 * Xyz.init(); // create and show.
31 * @arg xtype {String|Function} constructor or string.
32 * @arg id {String} (optional) id for registry
33 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
34 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
35 * @arg listeners {Object} (optional) map Gobject signals to functions
36 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
37 * @arg el {Object} (optional) premade GObject
39 * --- needs a xdebug option!
42 * He's some questions.
43 * - should we generate ID's for all elements? (if so we probably need to garbage collect)
44 * - should we have a special property to use as the constructor / gobject.properties rather
45 * than sending all basic types to this?
50 function XObject (cfg) {
51 // first apply cfg if set.
54 this.init = cfg.init; // override!
64 * @property el {GObject} the Gtk / etc. element.
68 * @property items {Array} list of sub elements
71 * @property parent {XObject} parent Element
75 * @property config {Object} the construction configuration.
79 * Initializes the Element (el) hooks up all the listeners
80 * and packs the children.
81 * you can override this, in child objects, then
82 * do this to do thi initaliztion.
84 * XObject.prototype.init.call(this);
89 var cfg = this.config;
91 print("new xobj?" + XObject.keys(cfg).join(','));
95 cfg.items = cfg.items || [];
97 XObject.extend(o, cfg); // copy everything into o.
99 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
101 XObject.extend(this, o);
105 this.listeners = this.listeners || {};
108 // remove objects/functions from o, so they can be sent to the contructor.
110 if ((typeof(o[i]) == 'object') ||
111 (typeof(o[i]) == 'function') ||
122 // do we need to call 'beforeInit here?'
125 //if ((this.xtype == 'Include')) {
126 // o = this.pre_registry[cls];
128 var isSeed = typeof(Seed) != 'undefined';
130 // xtype= Gtk.Menu ?? what about c_new stuff?
132 if (typeof(this.xtype) == 'function') {
133 print("func?" + XObject.keys(o).join(','));
134 this.el = this.el || this.xtype(o);
136 if (typeof(this.xtype) == 'object') {
137 print("obj?" + XObject.keys(o).join(','));
138 this.el = this.el || new this.xtype(o);
141 if (!this.el && o.xns) {
143 var NS = imports.gi[o.xns];
145 Seed.print('Invalid xns: ' + o.xns);
147 constructor = NS[o.xtype];
149 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
151 this.el = isSeed ? new constructor(o) : new constructor();
154 // always overlay props..
159 //if (o.xnsid && o.id) {
160 // XObject.registry = XObject.registry || { };
161 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
162 // XObject.registry[o.xnsid][o.id] = this;
165 cfg.items.forEach(this.addItem, this);
167 for (var i in this.listeners) {
168 this.addListener(i, this.listeners[i]);
170 // delete this.listeners ?
173 // do we need to call 'init here?'
179 * Adds an item to the object using a new XObject
180 * uses pack property to determine how to add it.
181 * @arg cfg {Object} same as XObject constructor.
183 addItem : function(o) {
186 var item = (o.constructor == XObject) ? o : new XObject(o);
189 this.items.push(item);
191 if (item.pack===false) { // no
194 if (typeof(item.pack) == 'function') {
196 item.pack.apply(o, [ o , o.items[i] ]);
202 if (typeof(item.pack) == 'string') {
205 pack_m = item.pack.shift();
210 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
211 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
216 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
218 args.unshift(item.el);
219 print('[' + args.join(',') +']');
220 //Seed.print('args: ' + args.length);
222 this.el[pack_m].apply(this.el, args);
229 * @method addListener
230 * Connects a method to a signal. (gjs/Seed aware)
232 * @arg sig {String} name of signal
233 * @arg fn {Function} handler.
235 addListener : function(sig, fn)
238 Seed.print("Add signal " + sig);
240 var _li = XObject.createDelegate(fn,this);
241 // private listeners that are not copied to GTk.
243 if (typeof(Seed) != 'undefined') {
244 // Seed.print(typeof(_li));
245 this.el.signal[sig].connect(_li);
247 this.el.connect( sig, _li);
254 * Finds an object in the child elements using xid of object.
255 * prefix with '.' to look up the tree.. multiple '..' to look further up..
257 * @arg name {String} name of signal
258 * @return {XObject|false} the object if found.
264 return this.parent.get(xid.substring(1));
268 this.items.forEach(function(ch) {
278 this.items.forEach(function(ch) {
292 * Copies all the properties of config to obj.
294 * Pretty much the same as JQuery/Prototype..
295 * @param {Object} obj The receiver of the properties
296 * @param {Object} config The source of the properties
297 * @param {Object} defaults A different object that will also be applied for default values
298 * @return {Object} returns obj
299 * @member XObject extend
303 XObject.extend = function(o, c, defaults){
305 // no "this" reference for friendly out of scope calls
306 XObject.extend(o, defaults);
308 if(o && c && typeof c == 'object'){
316 XObject.extend(XObject,
319 * Copies all the properties of config to obj, if the do not exist.
320 * @param {Object} obj The receiver of the properties
321 * @param {Object} config The source of the properties
322 * @return {Object} returns obj
323 * @member Object extendIf
327 extendIf : function(o, c){
329 if(!o || !c || typeof c != 'object'){
333 if (typeof(o[p]) != 'undefined') {
344 * Extends one class with another class and optionally overrides members with the passed literal. This class
345 * also adds the function "override()" to the class that can be used to override
346 * members on an instance.
349 * MyObject = Object.define(
353 * parentClass, // or Object
355 * ... methods and properties.
358 * @param {Function} constructor The class inheriting the functionality
359 * @param {Object} superclass The class being extended
360 * @param {Object} overrides (optional) A literal with members
361 * @return {Function} constructor (eg. class
366 var io = function(o){
371 return function(sb, sp, overrides) {
372 if (typeof(sp) == 'undefined') {
373 // error condition - try and dump..
374 throw "Missing superclass: when applying: " + sb
377 var F = function(){}, sbp, spp = sp.prototype;
379 sbp = sb.prototype = new F();
384 if(spp.constructor == Object.prototype.constructor){
388 sb.override = function(o){
389 Object.extend(sb.prototype, o);
392 Object.extend(sb.prototype, overrides);
399 * returns a list of keys of the object.
400 * @param {Object} obj object to inspect
401 * @return {Array} returns list of kyes
402 * @member XObject keys
414 * @member XObject createDelegate
415 * creates a delage metdhod
416 * @param {Function} method to wrap
417 * @param {Object} scope
418 * @param {Array} args to add
419 * @param {Boolean|Number} append arguments or replace after N arguments.
420 * @return {Function} returns the delegate
423 createDelegate : function(method, obj, args, appendArgs){
426 var callArgs = args || arguments;
427 if(appendArgs === true){
428 callArgs = Array.prototype.slice.call(arguments, 0);
429 callArgs = callArgs.concat(args);
430 }else if(typeof appendArgs == "number"){
431 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
432 var applyArgs = [appendArgs, 0].concat(args); // create method call params
433 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
435 return method.apply(obj || window, callArgs);