//<script type="text/javascript">
-
+var GIRepository = imports.gi.GIRepository;
+var GObject = imports.gi.GObject;
/**
* XObject
* Yet another attempt to create a usable object construction library for seed..
+ *
+ * Why is this useful?
+ * A) It turns rather messy code into a tree structure, making it easy to find code relating to
+ * an interface element
+ * B) In theory it should be gjs/Seed compatible..
+ * C) It provides getElementById style lookups for elements.
+ * D) It provides classic OO constructors for Javascript (extend/define)
+ * E) It does not modify any buildin prototypes..
*
* Extend this.. to use it's wonderful features..
*
+ * normal usage:
+ * XObject = imports.XObject.XObject;
+ *
+ * Xyz = new XObject({
+ * xtype: Gtk.Window,
+ * id : 'window',
+ * items : [
+ *
+ * ]
+ * });
+ * Xyz.init(); // create and show.
+ *
+ *
*
* @arg xtype {String|Function} constructor or string.
- * @arg xid {String} (optional) id for registry
+ * @arg id {String} (optional) id for registry
* @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
* @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
* @arg listeners {Object} (optional) map Gobject signals to functions
* @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
* @arg el {Object} (optional) premade GObject
+ *
+ * --- needs a xdebug option!
+ *
+ *
+ * He's some questions.
+ * - should we generate ID's for all elements? (if so we probably need to garbage collect)
+ * - should we have a special property to use as the constructor / gobject.properties rather
+ * than sending all basic types to this?
+ *
+ *
*/
function XObject (cfg) {
// first apply cfg if set.
- o = {};
- o.pack = o.pack || 'add';
- cfg.items = cfg.items || [];
-
- XObject.extend(o, cfg); // copy everything into o.
- XObject.extend(this, o);
+ //print("new XOBJECT!!!");
+ this.config = {};
+ this.constructor = XObject;
- // remove items.
+ // copy down all elements into self..
- this.listeners = this.listeners || {};
- this.items = [];
-
- // remove objects/functions from o, so they can be sent to the contructor.
- for (var i in o) {
- if ((typeof(o[i]) == 'object') ||
- (typeof(o[i]) == 'function') ||
- i == 'pack' ||
- i == 'xid' ||
- i == 'xtype' ||
- i == 'xns'
- ) {
- delete o[i];
+ for (var i in cfg) {
+ this[i] = cfg[i];
+ if (typeof(cfg[i]) == 'function') { // do we skip objects.
+ continue;
}
+ // these properties are not copied to cfg.
+ if ( i == 'pack' ||
+ i == 'items' ||
+ i == 'id' ||
+ i == 'xtype' ||
+ i == 'xdebug' ||
+ i == 'xns') {
+ continue;
+ }
+
+
+ this.config[i] = cfg[i];
}
-
- // do we need to call 'beforeInit here?'
-
- // handle include?
- //if ((this.xtype == 'Include')) {
- // o = this.pre_registry[cls];
- //}
- var isSeed = typeof(Seed) != 'undefined';
-
- // xtype= Gtk.Menu ?? what about c_new stuff?
- if (typeof(this.xtype) == 'function') {
- this.el = this.el || new this.xtype(o);
- }
-
- if (!this.el && o.xns) {
+ this.items = this.items || [];
+ // pack can be false!
+ if (typeof(this.pack) == 'undefined') {
- var NS = imports.gi[o.xns];
- if (!NS) {
- Seed.print('Invalid xns: ' + o.xns);
- }
- constructor = NS[o.xtype];
- if (!constructor) {
- Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
+ this.pack = [ 'add' ]
+ /*
+ var Gtk = imports.gi.Gtk;
+ switch (true) {
+ // any others!!
+ case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
+
}
- this.el = isSeed ? new constructor(o) : new constructor();
- if (!isSeed) {
- // this might work for gjs!?
- for (var i in o) {
- this.el[i] = o;
- }
- }
- }
-
- // register it!
- //if (o.xnsid && o.xid) {
- // XObject.registry = XObject.registry || { };
- // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
- // XObject.registry[o.xnsid][o.xid] = this;
- //}
-
- cfg.items.forEach(this.addItem, this);
-
- for (var i in this.listeners) {
- this.addListener(i, this.listeners[i]);
+ */
+
}
- // delete this.listeners ?
- // do we need to call 'init here?'
}
/**
* @property parent {XObject} parent Element
*/
+
+ /**
+ * @property config {Object} the construction configuration.
+ */
+ /**
+ * @method init
+ * Initializes the Element (el) hooks up all the listeners
+ * and packs the children.
+ * you can override this, in child objects, then
+ * do this to do thi initaliztion.
+ *
+ * XObject.prototype.init.call(this);
+ *
+ */
+ init : function()
+ {
+
+ var items = [];
+ this.items.forEach(function(i) {
+ items.push(i);
+ });
+ // remove items.
+ this.listeners = this.listeners || {};
+ this.items = [];
+
+ // do we need to call 'beforeInit here?'
+
+ // handle include?
+ //if ((this.xtype == 'Include')) {
+ // o = this.pre_registry[cls];
+ //}
+ var isSeed = typeof(Seed) != 'undefined';
+
+ // xtype= Gtk.Menu ?? what about c_new stuff?
+ if (XObject.debug) print("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
+ if (!this.el && typeof(this.xtype) == 'function') {
+ // gjs implements xtype as function in seed they are undefined..
+ if (XObject.debug) print("func?" + XObject.keys(this.config).join(','));
+ try {
+ this.el = this.xtype(this.config);
+ } catch(e) {
+ this.el = false;
+ }
+
+
+ }
+ if (!this.el && typeof(this.xtype) == 'object') {
+ if (XObject.debug) print("obj?" + XObject.keys(this.config).join(','));
+ this.el = new (this.xtype)(this.config);
+
+ }
+ //print(this.el);
+ if (!this.el && this.xns) {
+
+ var NS = imports.gi[this.xns];
+ if (!NS) {
+ Seed.print('Invalid xns: ' + this.xns);
+ }
+ constructor = NS[this.xtype];
+ if (!constructor) {
+ Seed.print('Invalid xtype: ' + this.xns + '.' + this.xtype);
+ }
+ this.el = isSeed ? new constructor(this.config) : new constructor();
+
+ }
+ if (XObject.debug) print("init: ID:"+ this.id +" typeof(el):" + this.el);
+
+ // always overlay props..
+ // check for 'write' on object..
+ /*
+ if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
+
+ var gi = GIRepository.IRepository.get_default();
+ var ty = gi.find_by_gtype(this.xtype.type);
+ var write = [];
+ for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
+ var p = GIRepository.object_info_get_property(ty,i);
+ if (GIRepository.property_info_get_flags(p) & 2) {
+ write.push(GIRepository.base_info_get_name(p));
+ }
+ }
+ XObject.writeablePropsCache[this.xtype.type] = write;
+ print(write.join(", "));
+ }
+
+ */
+
+
+ for (var i in this.config) {
+ if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
+ continue;
+ }
+ if (i == 'buttons') { // problem with Gtk.MessageDialog..
+ continue;
+ }
+ if (i[0] == '.') { // parent? -
+ continue;
+ }
+ this.el[i] = this.config[i];
+ }
+
+ // register it!
+ //if (o.xnsid && o.id) {
+ // XObject.registry = XObject.registry || { };
+ // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
+ // XObject.registry[o.xnsid][o.id] = this;
+ //}
+
+ var type = this.xtype && this.xtype.type ? GObject.type_name(this.xtype.type) : '';
+ print("MAKE " + type);
+
+
+ var _this=this;
+ items.forEach(function(i,n) {
+
+ if (type == 'GtkTable' && i.pack == 'add') {
+ var c = n % _this.config.n_columns;
+ var r = Math.floor(n/_this.config.n_columns);
+ i.pack = [ 'attach', c, c+1, r, r+1,
+ typeof(i.x_options) == 'undefined' ? 5 : i.x_options,
+ typeof(i.y_options) == 'undefined' ? 5 : i.y_options,
+ typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding,
+ typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding
+
+ ]
+ }
+
+ _this.addItem(i);
+ })
+
+
+ for (var i in this.listeners) {
+ this.addListener(i, this.listeners[i]);
+ }
+
+ // delete this.listeners ?
+ // do again so child props work!
+
+ // do we need to call 'init here?'
+ },
+
/**
* @method addItem
* Adds an item to the object using a new XObject
* @arg cfg {Object} same as XObject constructor.
*/
addItem : function(o) {
- var item = new XObject(o);
-
+ if (typeof(o) == 'undefined') {
+ print("Invalid Item added to this!");
+ imports.console.dump(this);
+ Seed.quit();
+ }
+ // what about extended items!?!?!?
+ var item = (o.constructor == XObject) ? o : new XObject(o);
+ item.parent = this;
this.items.push(item);
-
+ item.init();
+ //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
+ // print("addItem - call init [" + item.pack.join(',') + ']');
+ if (!item.el) {
+ print("NO EL!");
+ imports.console.dump(item);
+ Seed.quit();
+ }
+
+ if (item.pack===false) { // no
+ return;
+ }
if (typeof(item.pack) == 'function') {
// parent, child
- item.pack.apply(o, [ o , o.items[i] ]);
+ item.pack.apply(item, [ this , item ]);
item.parent = this;
return;
}
-
-
- var pack_m = typeof(item.pack) == 'string' ? item.pack : item.pack.shift();
+ var args = [];
+ var pack_m = false;
+ if (typeof(item.pack) == 'string') {
+
+ item.pack.split(',').forEach(function(e, i) {
+
+ if (e == 'false') { args.push( false); return; }
+ if (e == 'true') { args.push( true); return; }
+ if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
+ args.push(e);
+ });
+ //print(args.join(","));
+
+ pack_m = args.shift();
+ } else {
+ pack_m = item.pack.shift();
+ args = item.pack;
+ }
// handle error.
- if (pack_m && typeof(o.el[pack_m]) == 'undefined') {
- Seed.print('pack method not available : ' + o.xtype + '.' + pack_m);
+ if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
+
+ throw {
+ name: "ArgumentError",
+ message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
+
+ }
+
+
+
+
return;
}
- //Seed.print('Pack ' + o.xtype + '.'+ pack_m + '(' + o.items[i].xtype + ')');
- // copy.
- var args = Array.prototype.slice.call(typeof(item.pack) == 'string' ? [] : item.pack);
- item.pack.unshift(item.el);
+ //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
+
+ args.unshift(item.el);
+ if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
//Seed.print('args: ' + args.length);
if (pack_m) {
- this.el[pack_m].apply(item.el, args);
+ this.el[pack_m].apply(this.el, args);
}
- item.parent = this;
+
},
/**
* @arg sig {String} name of signal
* @arg fn {Function} handler.
*/
- addListener : function(sig, fn) {
-
+ addListener : function(sig, fn)
+ {
+
+ if (XObject.debug) Seed.print("Add signal " + sig);
+ fn.id= sig;
var _li = XObject.createDelegate(fn,this);
// private listeners that are not copied to GTk.
/**
* @method get
* Finds an object in the child elements using xid of object.
+ * prefix with '.' to look up the tree..
+ * prefix with multiple '..' to look further up..
+ * prefix with '/' to look from the top, eg. '^LeftTree.model'
*
* @arg name {String} name of signal
* @return {XObject|false} the object if found.
*/
get : function(xid)
{
+ if (XObject.debug) print("SEARCH FOR " + xid + " in " + this.id);
var ret= false;
- this.items.each(function(ch) {
- if (ch.xid == xid) {
+ var oid = '' + xid;
+ if (!xid.length) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : empty id"
+ }
+ }
+
+ if (xid[0] == '.') {
+ return this.parent.get(xid.substring(1));
+ }
+ if (xid[0] == '/') {
+
+ if (typeof(XObject.cache[xid]) != 'undefined') {
+ return XObject.cache[xid];
+ }
+ if (xid.indexOf('.') > -1) {
+
+ var child = xid.split('.');
+ var nxid = child.shift();
+
+ child = child.join('.');
+ if (typeof(XObject.cache[nxid]) != 'undefined') {
+ return XObject.cache[nxid].get(child);
+ }
+
+
+ }
+ var e = this;
+ while (e.parent) {
+ e = e.parent;
+ }
+
+ try {
+ ret = e.get(xid.substring(1));
+ } catch (ex) { }
+
+ if (!ret) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : " + oid
+ }
+ }
+ XObject.cache[xid] = ret;
+ return XObject.cache[xid];
+ }
+ var child = false;
+
+ if (xid.indexOf('.') > -1) {
+ child = xid.split('.');
+ xid = child.shift();
+
+ child = child.join('.');
+
+ }
+ if (xid == this.id) {
+ try {
+ return child === false ? this : this.get(child);
+ } catch (ex) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : " + oid
+ }
+ }
+
+ }
+
+
+ this.items.forEach(function(ch) {
+ if (ret) {
+ return;
+ }
+ if (ch.id == xid) {
ret = ch;
- return true;
}
})
if (ret) {
- return ret;
+ try {
+ return child === false ? ret : ret.get(child);
+ } catch (ex) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : " + oid
+ }
+ }
+
}
// iterate children.
- this.items.each(function(ch) {
- ret = ch.get(xid);
+ var _this = this;
+ this.items.forEach(function(ch) {
if (ret) {
- return true;
+ return;
}
- })
- return ret;
+ if (!ch.get) {
+ print("invalid item...");
+ imports.console.dump(_this);
+ Seed.quit();
+ }
+ try {
+ ret = ch.get(xid);
+ } catch (ex) { }
+
+
+ });
+ if (!ret) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : " + oid
+ }
+ }
+ try {
+ return child === false ? ret : ret.get(child);
+ } catch (ex) {
+ throw {
+ name: "ArgumentError",
+ message : "ID not found : " + oid
+ }
+ }
}
}
-
+
/**
* Copies all the properties of config to obj.
*
XObject.extend(XObject,
{
+
+ /**
+ * @property {Boolean} debug XObject debugging. - set to true to debug.
+ *
+ */
+ debug : true,
+ /**
+ * @property {Object} cache - cache of object ids
+ *
+ */
+ cache: { },
+
/**
* Copies all the properties of config to obj, if the do not exist.
* @param {Object} obj The receiver of the properties
* members on an instance.
*
* usage:
- * MyObject = Object.define(
+ * MyObject = XObject.define(
* function(...) {
* ....
* },
this[m] = o[m];
}
};
- return function(sb, sp, overrides) {
- if (typeof(sp) == 'undefined') {
- // error condition - try and dump..
- throw "Missing superclass: when applying: " + sb
+ return function(constructor, parentClass, overrides) {
+ if (typeof(parentClass) == 'undefined') {
+ print("XObject.define: Missing parentClass: when applying: " );
+ print(new String(constructor));
+ Seed.quit();
}
-
- var F = function(){}, sbp, spp = sp.prototype;
+ if (typeof(parentClass.prototype) == 'undefined') {
+ print("Missing protype: when applying: " );
+ print(new String(constructor));
+ print(new String(parentClass));
+ Seed.quit();
+ }
+ var F = function(){};
+ var sbp;
+ var spp = parentClass.prototype;
+
F.prototype = spp;
- sbp = sb.prototype = new F();
- sbp.constructor=sb;
- sb.superclass=spp;
+ sbp = constructor.prototype = new F();
+ sbp.constructor=constructor;
+ constructor.superclass=spp;
// extends Object.
if(spp.constructor == Object.prototype.constructor){
- spp.constructor=sp;
+ spp.constructor=parentClass;
}
-
- sb.override = function(o){
- Object.extend(sb.prototype, o);
+
+ constructor.override = function(o){
+ Object.extend(constructor.prototype, o);
};
sbp.override = io;
- Object.extend(sb.prototype, overrides);
- return sb;
+ XObject.extend(constructor.prototype, overrides);
+ return constructor;
};
}(),
* returns a list of keys of the object.
* @param {Object} obj object to inspect
* @return {Array} returns list of kyes
- * @member Object keys
+ * @member XObject keys
*/
keys : function(o)
{
var ret = [];
for(var i in o) {
- ret.push[i];
+ ret.push(i);
}
return ret;
+ },
+
+ /**
+ * @member XObject createDelegate
+ * creates a delage metdhod
+ * @param {Function} method to wrap
+ * @param {Object} scope
+ * @param {Array} args to add
+ * @param {Boolean|Number} append arguments or replace after N arguments.
+ * @return {Function} returns the delegate
+ */
+
+ createDelegate : function(method, obj, args, appendArgs){
+
+ return function() {
+ if (XObject.debug) print("CALL: " + obj.id + ':'+ method.id);
+
+ var callArgs = args || arguments;
+ if(appendArgs === true){
+ callArgs = Array.prototype.slice.call(arguments, 0);
+ callArgs = callArgs.concat(args);
+ }else if(typeof appendArgs == "number"){
+ callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
+ var applyArgs = [appendArgs, 0].concat(args); // create method call params
+ Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
+ }
+ return method.apply(obj || window, callArgs);
+ };
}
+
});
\ No newline at end of file