1 //<script type="text/javascript">
3 * Copyright(c) 2010 Alan Knowles <alan@akbkhome.com>
9 * Yet another attempt to create a usable object construction library for seed..
12 * A) It turns rather messy code into a tree structure, making it easy to find code relating to
13 * an interface element
14 * B) In theory it should be gjs/Seed compatible..
15 * C) It provides getElementById style lookups for elements.
16 * D) It provides classic OO constructors for Javascript (extend/define)
17 * E) It does not modify any buildin prototypes..
19 * Extend this.. to use it's wonderful features..
22 * XObject = imports.XObject.XObject;
31 * Xyz.init(); // create and show.
35 * @arg xtype {String|Function} constructor or string.
36 * @arg id {String} (optional) id for registry
37 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
38 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
39 * @arg listeners {Object} (optional) map Gobject signals to functions
40 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
41 * @arg el {Object} (optional) premade GObject
43 * --- needs a xdebug option!
46 * He's some questions.
47 * - should we generate ID's for all elements? (if so we probably need to garbage collect)
48 * - should we have a special property to use as the constructor / gobject.properties rather
49 * than sending all basic types to this?
54 function XObject (cfg) {
55 // first apply cfg if set.
58 this.init = cfg.init; // override!
68 * @property el {GObject} the Gtk / etc. element.
72 * @property items {Array} list of sub elements
75 * @property parent {XObject} parent Element
79 * @property config {Object} the construction configuration.
83 * Initializes the Element (el) hooks up all the listeners
84 * and packs the children.
85 * you can override this, in child objects, then
86 * do this to do thi initaliztion.
88 * XObject.prototype.init.call(this);
93 var cfg = this.config;
95 print("new xobj?" + XObject.keys(cfg).join(','));
99 cfg.items = cfg.items || [];
101 XObject.extend(o, cfg); // copy everything into o.
103 o.pack = typeof(o.pack) == 'undefined' ? 'add' : o.pack;
105 XObject.extend(this, o);
109 this.listeners = this.listeners || {};
112 // remove objects/functions from o, so they can be sent to the contructor.
114 if ((typeof(o[i]) == 'object') ||
115 (typeof(o[i]) == 'function') ||
126 // do we need to call 'beforeInit here?'
129 //if ((this.xtype == 'Include')) {
130 // o = this.pre_registry[cls];
132 var isSeed = typeof(Seed) != 'undefined';
134 // xtype= Gtk.Menu ?? what about c_new stuff?
136 if (typeof(this.xtype) == 'function') {
137 print("func?" + XObject.keys(o).join(','));
138 this.el = this.el || this.xtype(o);
140 if (typeof(this.xtype) == 'object') {
141 print("obj?" + XObject.keys(o).join(','));
142 this.el = this.el || new this.xtype(o);
145 if (!this.el && o.xns) {
147 var NS = imports.gi[o.xns];
149 Seed.print('Invalid xns: ' + o.xns);
151 constructor = NS[o.xtype];
153 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
155 this.el = isSeed ? new constructor(o) : new constructor();
158 // always overlay props..
163 //if (o.xnsid && o.id) {
164 // XObject.registry = XObject.registry || { };
165 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
166 // XObject.registry[o.xnsid][o.id] = this;
169 cfg.items.forEach(this.addItem, this);
171 for (var i in this.listeners) {
172 this.addListener(i, this.listeners[i]);
174 // delete this.listeners ?
177 // do we need to call 'init here?'
183 * Adds an item to the object using a new XObject
184 * uses pack property to determine how to add it.
185 * @arg cfg {Object} same as XObject constructor.
187 addItem : function(o) {
190 var item = (o.constructor == XObject) ? o : new XObject(o);
193 this.items.push(item);
195 if (item.pack===false) { // no
198 if (typeof(item.pack) == 'function') {
200 item.pack.apply(o, [ o , o.items[i] ]);
206 if (typeof(item.pack) == 'string') {
209 pack_m = item.pack.shift();
214 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
215 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
220 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
222 args.unshift(item.el);
223 print('[' + args.join(',') +']');
224 //Seed.print('args: ' + args.length);
226 this.el[pack_m].apply(this.el, args);
233 * @method addListener
234 * Connects a method to a signal. (gjs/Seed aware)
236 * @arg sig {String} name of signal
237 * @arg fn {Function} handler.
239 addListener : function(sig, fn)
242 Seed.print("Add signal " + sig);
244 var _li = XObject.createDelegate(fn,this);
245 // private listeners that are not copied to GTk.
247 if (typeof(Seed) != 'undefined') {
248 // Seed.print(typeof(_li));
249 this.el.signal[sig].connect(_li);
251 this.el.connect( sig, _li);
258 * Finds an object in the child elements using xid of object.
259 * prefix with '.' to look up the tree.. multiple '..' to look further up..
261 * @arg name {String} name of signal
262 * @return {XObject|false} the object if found.
268 return this.parent.get(xid.substring(1));
272 this.items.forEach(function(ch) {
282 this.items.forEach(function(ch) {
296 * Copies all the properties of config to obj.
298 * Pretty much the same as JQuery/Prototype..
299 * @param {Object} obj The receiver of the properties
300 * @param {Object} config The source of the properties
301 * @param {Object} defaults A different object that will also be applied for default values
302 * @return {Object} returns obj
303 * @member XObject extend
307 XObject.extend = function(o, c, defaults){
309 // no "this" reference for friendly out of scope calls
310 XObject.extend(o, defaults);
312 if(o && c && typeof c == 'object'){
320 XObject.extend(XObject,
323 * Copies all the properties of config to obj, if the do not exist.
324 * @param {Object} obj The receiver of the properties
325 * @param {Object} config The source of the properties
326 * @return {Object} returns obj
327 * @member Object extendIf
331 extendIf : function(o, c){
333 if(!o || !c || typeof c != 'object'){
337 if (typeof(o[p]) != 'undefined') {
348 * Extends one class with another class and optionally overrides members with the passed literal. This class
349 * also adds the function "override()" to the class that can be used to override
350 * members on an instance.
353 * MyObject = Object.define(
357 * parentClass, // or Object
359 * ... methods and properties.
362 * @param {Function} constructor The class inheriting the functionality
363 * @param {Object} superclass The class being extended
364 * @param {Object} overrides (optional) A literal with members
365 * @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
406 * @member XObject keys
418 * @member XObject createDelegate
419 * creates a delage metdhod
420 * @param {Function} method to wrap
421 * @param {Object} scope
422 * @param {Array} args to add
423 * @param {Boolean|Number} append arguments or replace after N arguments.
424 * @return {Function} returns the delegate
429 * x = XObject.createDelegate(a, this);
433 createDelegate : function(method, obj, args, appendArgs){
436 var callArgs = args || arguments;
437 if(appendArgs === true){
438 callArgs = Array.prototype.slice.call(arguments, 0);
439 callArgs = callArgs.concat(args);
440 }else if(typeof appendArgs == "number"){
441 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
442 var applyArgs = [appendArgs, 0].concat(args); // create method call params
443 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
445 return method.apply(obj || window, callArgs);