1 //<script type="text/javascript">
3 * Copyright(c) 2010 Alan Knowles <alan@akbkhome.com>
10 * Yet another attempt to create a usable object construction library for seed..
13 * A) It turns rather messy code into a tree structure, making it easy to find code relating to
14 * an interface element
15 * B) In theory it should be gjs/Seed compatible..
16 * C) It provides getElementById style lookups for elements.
17 * D) It provides classic OO constructors for Javascript (extend/define)
18 * E) It does not modify any buildin prototypes..
20 * Extend this.. to use it's wonderful features..
24 * XObject = imports.XObject.XObject;
33 * Xyz.init(); // create and show.
36 * He's some questions.
37 * - should we generate ID's for all elements? (if so we probably need to garbage collect)
38 * - should we have a special property to use as the constructor / gobject.properties rather
39 * than sending all basic types to this?
43 * @cfg xtype {String|Function} constructor or string.
44 * @cfg id {String} (optional) id for registry
45 * @cfg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
46 * @cfg items {Array} (optional) list of child elements which will be constructed.. using XObject
47 * @cfg listeners {Object} (optional) map Gobject signals to functions
48 * @cfg pack {Function|String|Array} (optional) how this object gets added to it's parent
49 * @cfg el {Object} (optional) premade GObject
53 function XObject (cfg) {
54 // first apply cfg if set.
57 this.init = cfg.init; // override!
68 * the Gtk / GObject element.
73 * list of sub elements
83 * the constructor configuration.
87 * Initializes the Element (el) hooks up all the listeners
88 * and packs the children.
89 * you can override this, in child objects, then
90 * do this to do thi initaliztion.
92 * XObject.prototype.init.call(this);
97 var cfg = this.config;
99 print("new xobj?" + XObject.keys(cfg).join(','));
103 cfg.items = cfg.items || [];
105 XObject.extend(o, cfg); // copy everything into o.
107 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
109 XObject.extend(this, o);
113 this.listeners = this.listeners || {};
116 // remove objects/functions from o, so they can be sent to the contructor.
118 if ((typeof(o[i]) == 'object') ||
119 (typeof(o[i]) == 'function') ||
130 // do we need to call 'beforeInit here?'
133 //if ((this.xtype == 'Include')) {
134 // o = this.pre_registry[cls];
136 var isSeed = typeof(Seed) != 'undefined';
138 // xtype= Gtk.Menu ?? what about c_new stuff?
140 if (typeof(this.xtype) == 'function') {
141 print("func?" + XObject.keys(o).join(','));
142 this.el = this.el || this.xtype(o);
144 if (typeof(this.xtype) == 'object') {
145 print("obj?" + XObject.keys(o).join(','));
146 this.el = this.el || new this.xtype(o);
149 if (!this.el && o.xns) {
151 var NS = imports.gi[o.xns];
153 Seed.print('Invalid xns: ' + o.xns);
155 constructor = NS[o.xtype];
157 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
159 this.el = isSeed ? new constructor(o) : new constructor();
162 // always overlay props..
167 //if (o.xnsid && o.id) {
168 // XObject.registry = XObject.registry || { };
169 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
170 // XObject.registry[o.xnsid][o.id] = this;
173 cfg.items.forEach(this.addItem, this);
175 for (var i in this.listeners) {
176 this.addListener(i, this.listeners[i]);
178 // delete this.listeners ?
181 // do we need to call 'init here?'
186 * Adds an item to the object using a new XObject
187 * uses pack property to determine how to add it.
188 * @param {Object} o same as XObject constructor.
190 addItem : function(o) {
193 var item = (o.constructor == XObject) ? o : new XObject(o);
196 this.items.push(item);
198 if (item.pack===false) { // no
201 if (typeof(item.pack) == 'function') {
203 item.pack.apply(o, [ o , o.items[i] ]);
209 if (typeof(item.pack) == 'string') {
212 pack_m = item.pack.shift();
217 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
218 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
223 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
225 args.unshift(item.el);
226 print('[' + args.join(',') +']');
227 //Seed.print('args: ' + args.length);
229 this.el[pack_m].apply(this.el, args);
236 * Connects a method to a signal. (gjs/Seed aware)
238 * @param {String} sig name of signal
239 * @param {Function} fn handler.
241 addListener : function(sig, fn)
244 Seed.print("Add signal " + sig);
246 var _li = XObject.createDelegate(fn,this);
247 // private listeners that are not copied to GTk.
249 if (typeof(Seed) != 'undefined') {
250 // Seed.print(typeof(_li));
251 this.el.signal[sig].connect(_li);
253 this.el.connect( sig, _li);
259 * Finds an object in the child elements using xid of object.
260 * prefix with '.' to look up the tree.. multiple '..' to look further up..
262 * @param {String} name name of signal
263 * @return {XObject|false} the object if found.
269 return this.parent.get(xid.substring(1));
273 this.items.forEach(function(ch) {
283 this.items.forEach(function(ch) {
297 * Copies all the properties of config to obj.
299 * Pretty much the same as JQuery/Prototype..
300 * @param {Object} obj The receiver of the properties
301 * @param {Object} config The source of the properties
302 * @param {Object} defaults A different object that will also be applied for default values
303 * @return {Object} returns obj
308 XObject.extend = function(o, c, defaults){
310 // no "this" reference for friendly out of scope calls
311 XObject.extend(o, defaults);
313 if(o && c && typeof c == 'object'){
321 XObject = XObject.extend(XObject,
324 * Copies all the properties of config to obj, if the do not exist.
325 * @param {Object} obj The receiver of the properties
326 * @param {Object} config The source of the properties
327 * @return {Object} returns obj
332 extendIf : function(o, c){
334 if(!o || !c || typeof c != 'object'){
338 if (typeof(o[p]) != 'undefined') {
349 * Extends one class with another class and optionally overrides members with the passed literal. This class
350 * also adds the function "override()" to the class that can be used to override
351 * members on an instance.
354 * MyObject = Object.define(
358 * parentClass, // or Object
360 * ... methods and properties.
363 * @param {Function} constructor The class inheriting the functionality
364 * @param {Object} superclass The class being extended
365 * @param {Object} overrides (optional) A literal with members
366 * @return {Function} constructor (eg. class
370 var io = function(o){
375 return function(sb, sp, overrides) {
376 if (typeof(sp) == 'undefined') {
377 // error condition - try and dump..
378 throw "Missing superclass: when applying: " + sb
381 var F = function(){}, sbp, spp = sp.prototype;
383 sbp = sb.prototype = new F();
388 if(spp.constructor == Object.prototype.constructor){
392 sb.override = function(o){
393 Object.extend(sb.prototype, o);
396 XObject.extend(sb.prototype, overrides);
403 * returns a list of keys of the object.
404 * @param {Object} obj object to inspect
405 * @return {Array} returns list of kyes
417 * creates a delegate method (binds scope to a method call)
418 * @param {Function} method to wrap
419 * @param {Object} scope
420 * @param {Array} args to add
421 * @param {Boolean|Number} append arguments or replace after N arguments.
422 * @return {Function} returns the delegate
427 * x = XObject.createDelegate(a, this);
431 createDelegate : function(method, obj, args, appendArgs){
434 var callArgs = args || arguments;
435 if(appendArgs === true){
436 callArgs = Array.prototype.slice.call(arguments, 0);
437 callArgs = callArgs.concat(args);
438 }else if(typeof appendArgs == "number"){
439 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
440 var applyArgs = [appendArgs, 0].concat(args); // create method call params
441 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
443 return method.apply(obj || window, callArgs);