1 //<script type="text/javascript">
2 GIRepository = imports.gi.GIRepository;
3 GObject = imports.gi.GObject;
6 * Yet another attempt to create a usable object construction library for seed..
9 * A) It turns rather messy code into a tree structure, making it easy to find code relating to
10 * an interface element
11 * B) In theory it should be gjs/Seed compatible..
12 * C) It provides getElementById style lookups for elements.
13 * D) It provides classic OO constructors for Javascript (extend/define)
14 * E) It does not modify any buildin prototypes..
16 * Extend this.. to use it's wonderful features..
19 * XObject = imports.XObject.XObject;
28 * Xyz.init(); // create and show.
31 * use XObject.debug = 1 to turn on debugging
33 * If XObjectBase/[xns]/[xtype].js exists, it will use this to override properties..
36 * He's some questions.
37 * - should we have a special property to use as the constructor / gobject.properties rather
38 * than sending all basic types to this?
40 * @cfg xtype {String|Function} constructor or string.
41 * @cfg id {String} (optional) id for registry
42 * @cfg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
43 * @cfg items {Array} (optional) list of child elements which will be constructed.. using XObject
44 * @cfg listeners {Object} (optional) map Gobject signals to functions
45 * @cfg pack {Function|String|Array} (optional) how this object gets added to it's parent
46 * @cfg el {Object} (optional) premade GObject
55 function XObject (cfg) {
56 // first apply cfg if set.
57 //print("new XOBJECT!!!");
59 //print ("XObject ctr");
61 this.config = {}; // used to initialize GObject
63 this.cfg = XObject.extend({}, cfg); // used to store original configuration.. for referencing..
65 // we could use this to determine if
66 // we are actually inside a inherited class...
67 // as define() should actually set this up..
68 this.constructor = XObject;
72 // start by seeing if we have a base class....
74 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
75 // xns is not a string!!!?
77 if (typeof(cfg.xtype) == 'object') {
78 gname = GObject.type_name(cfg.xtype.type);
79 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
82 var base = gname ? imports.XObjectBase[gname][gname] : false;
84 // print("Overlaying XOBJBECT-BASE." + cfg.xtype);
85 XObject.extend(this,base);
90 XObject.log("error finding " + gname + " - " + e.toString());
94 // copy down all elements into self..
95 // make an extra copy in this.config?? - which is the one used in the constructor later
99 if (typeof(cfg[i]) == 'function') { // do we skip objects.
102 // these properties are not copied to cfg.
113 this.config[i] = cfg[i];
117 this.items = this.items || [];
120 // pack can be false!
121 if (typeof(this.pack) == 'undefined') {
123 this.pack = [ 'add' ]
125 var Gtk = imports.gi.Gtk;
128 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
135 // interesting question should we call constructor on items here...
136 // as the real work is done in init anyway..
140 this.items.forEach(function(i) {
144 // create XObject for all the children.
145 items.forEach(function(i,n) {
147 var item = (i.constructor == XObject) ? o : new XObject(i);
149 _this.items.push(item);
152 if (this.onConstruct) {
153 this.onConstruct.call(this);
160 XObject.prototype = {
162 * @property el {GObject} the Gtk / etc. element.
166 * @property items {Array} list of sub elements
169 * @property parent {XObject} parent Element
173 * @property config {Object} the construction configuration.
177 * Initializes the Element (el) hooks up all the listeners
178 * and packs the children.
179 * you can override this, in child objects, then
180 * do this to do thi initaliztion.
182 * XObject.prototype.init.call(this);
189 //this.items.forEach(function(i) {
193 this.listeners = this.listeners || {};
196 // do we need to call 'beforeInit here?'
199 //if ((this.xtype == 'Include')) {
200 // o = this.pre_registry[cls];
202 var isSeed = typeof(Seed) != 'undefined';
204 // xtype= Gtk.Menu ?? what about c_new stuff?
205 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
206 if (!this.el && typeof(this.xtype) == 'function') {
207 XObject.log("func?" + XObject.keys(this.config).join(','));
208 this.el = this.xtype(this.config);
211 if (!this.el && typeof(this.xtype) == 'object') {
212 XObject.log("obj?" + XObject.keys(this.config).join(','));
213 this.el = new (this.xtype)(this.config);
217 if (!this.el && this.xns) {
219 var NS = imports.gi[this.xns];
221 XObject.error('Invalid xns: ' + this.xns, true);
223 constructor = NS[this.xtype];
225 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
227 this.el = isSeed ? new constructor(this.config) : new constructor();
230 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
232 // always overlay props..
233 // check for 'write' on object..
235 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
237 var gi = GIRepository.IRepository.get_default();
238 var ty = gi.find_by_gtype(this.xtype.type);
240 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
241 var p = GIRepository.object_info_get_property(ty,i);
242 if (GIRepository.property_info_get_flags(p) & 2) {
243 write.push(GIRepository.base_info_get_name(p));
246 XObject.writeablePropsCache[this.xtype.type] = write;
247 print(write.join(", "));
253 for (var i in this.config) {
254 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
257 if (i == 'buttons') { // problem with Gtk.MessageDialog..
260 if (i[0] == '.') { // parent? -
263 this.el[i] = this.config[i];
267 //if (o.xnsid && o.id) {
268 // XObject.registry = XObject.registry || { };
269 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
270 // XObject.registry[o.xnsid][o.id] = this;
273 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
274 XObject.log("add children to " + type);
277 this.items.forEach(function(i,n) {
282 for (var i in this.listeners) {
283 this.addListener(i, this.listeners[i]);
286 this.init = XObject.emptyFn;
288 // delete this.listeners ?
289 // do again so child props work!
291 // do we need to call 'init here?'
297 * Adds an item to the object using a new XObject
298 * uses pack property to determine how to add it.
299 * @arg cfg {Object} same as XObject constructor.
301 addItem : function(item, pos)
304 if (typeof(item) == 'undefined') {
305 XObject.error("Invalid Item added to this!");
306 imports.console.dump(this.cfg);
309 // what about extended items!?!?!?
312 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
313 // print("addItem - call init [" + item.pack.join(',') + ']');
315 XObject.error("NO EL!");
316 imports.console.dump(item);
319 print(XObject.type(this.xtype) + ":pack=" + item.pack);
321 if (item.pack===false) { // no packing.. various items have this ..
325 if (typeof(item.pack) == 'function') { // pack is a function..
327 item.pack.apply(item, [ this , item ]);
335 if (typeof(item.pack) == 'string') {
337 item.pack.split(',').forEach(function(e, i) {
339 if (e == 'false') { args.push( false); return; }
340 if (e == 'true') { args.push( true); return; }
341 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
344 //print(args.join(","));
346 pack_m = args.shift();
348 pack_m = item.pack.shift();
353 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
356 name: "ArgumentError",
357 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
365 // finally call the pack method
366 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
368 args.unshift(item.el);
374 XObject.log(pack_m + '[' + args.join(',') +']');
375 //Seed.print('args: ' + args.length);
377 this.el[pack_m].apply(this.el, args);
384 * @method addListener
385 * Connects a method to a signal. (gjs/Seed aware)
387 * @arg sig {String} name of signal
388 * @arg fn {Function} handler.
390 addListener : function(sig, fn)
393 XObject.log("Add signal " + sig);
395 var _li = XObject.createDelegate(fn,this);
396 // private listeners that are not copied to GTk.
398 if (typeof(Seed) != 'undefined') {
399 // Seed.print(typeof(_li));
400 this.el.signal[sig].connect(_li);
402 this.el.connect( sig, _li);
409 * Finds an object in the child elements using xid of object.
410 * prefix with '.' to look up the tree..
411 * prefix with multiple '..' to look further up..
412 * prefix with '/' to look from the top, eg. '^LeftTree.model'
414 * @arg name {String} name of signal
415 * @return {XObject|false} the object if found.
419 XObject.log("SEARCH FOR " + xid + " in " + this.id);
424 name: "ArgumentError",
425 message : "ID not found : empty id"
430 return this.parent.get(xid.substring(1));
434 if (typeof(XObject.cache[xid]) != 'undefined') {
435 return XObject.cache[xid];
437 if (xid.indexOf('.') > -1) {
439 var child = xid.split('.');
440 var nxid = child.shift();
442 child = child.join('.');
443 if (typeof(XObject.cache[nxid]) != 'undefined') {
444 return XObject.cache[nxid].get(child);
455 ret = e.get(xid.substring(1));
460 name: "ArgumentError",
461 message : "ID not found : " + oid
464 XObject.cache[xid] = ret;
465 return XObject.cache[xid];
469 if (xid.indexOf('.') > -1) {
470 child = xid.split('.');
473 child = child.join('.');
476 if (xid == this.id) {
478 return child === false ? this : this.get(child);
481 name: "ArgumentError",
482 message : "ID not found : " + oid
489 this.items.forEach(function(ch) {
499 return child === false ? ret : ret.get(child);
502 name: "ArgumentError",
503 message : "ID not found : " + oid
510 this.items.forEach(function(ch) {
515 XObject.error("invalid item...");
516 imports.console.dump(_this);
527 name: "ArgumentError",
528 message : "ID not found : " + oid
532 return child === false ? ret : ret.get(child);
535 name: "ArgumentError",
536 message : "ID not found : " + oid
546 * Copies all the properties of config to obj.
548 * Pretty much the same as JQuery/Prototype.. or Roo.apply
549 * @param {Object} obj The receiver of the properties
550 * @param {Object} config The source of the properties
551 * @param {Object} defaults A different object that will also be applied for default values
552 * @return {Object} returns obj
553 * @member XObject extend
557 XObject.extend = function(o, c, defaults){
559 // no "this" reference for friendly out of scope calls
560 XObject.extend(o, defaults);
562 if(o && c && typeof c == 'object'){
570 XObject.extend(XObject,
574 * @property {Boolean} debug XObject debugging. - set to true to debug.
579 * @property {Object} cache - cache of object ids
587 emptyFn : function () { },
593 * @param {String|Object} output String to print.
595 log : function(output)
600 print("LOG:" + output);
605 * @param {String|Object} output String to print.
607 error : function(output)
609 print("ERROR: " + output);
613 * @param {String|Object} output String to print.
615 fatal : function(output)
619 name: "ArgumentError",
626 * Copies all the properties of config to obj, if the do not exist.
627 * @param {Object} obj The receiver of the properties
628 * @param {Object} config The source of the properties
629 * @return {Object} returns obj
630 * @member Object extendIf
634 extendIf : function(o, c)
637 if(!o || !c || typeof c != 'object'){
641 if (typeof(o[p]) != 'undefined') {
652 * Extends one class with another class and optionally overrides members with the passed literal. This class
653 * also adds the function "override()" to the class that can be used to override
654 * members on an instance.
657 * MyObject = Object.define(
661 * parentClass, // or Object
663 * ... methods and properties.
666 * @param {Function} constructor The class inheriting the functionality
667 * @param {Object} superclass The class being extended
668 * @param {Object} overrides (optional) A literal with members
669 * @return {Function} constructor (eg. class
675 var io = function(o){
680 return function(constructor, parentClass, overrides) {
681 if (typeof(parentClass) == 'undefined') {
682 XObject.error("XObject.define: Missing parentClass: when applying: " );
683 XObject.error(new String(constructor));
686 if (typeof(parentClass.prototype) == 'undefined') {
687 XObject.error("Missing protype: when applying: " );
688 XObject.error(new String(constructor));
689 XObject.error(new String(parentClass));
692 var F = function(){};
694 var spp = parentClass.prototype;
697 sbp = constructor.prototype = new F();
698 sbp.constructor=constructor;
699 constructor.superclass=spp;
702 if(spp.constructor == Object.prototype.constructor){
703 spp.constructor=parentClass;
706 constructor.override = function(o){
707 Object.extend(constructor.prototype, o);
710 XObject.extend(constructor.prototype, overrides);
717 * returns a list of keys of the object.
718 * @param {Object} obj object to inspect
719 * @return {Array} returns list of kyes
720 * @member XObject keys
731 * return the Gobject name of a constructor
732 * @param {Object} gobject ctr
733 * @return {String} returns name
734 * @member XObject keys
738 if (typeof(o) == 'object') {
739 return GObject.type_name(o.type);
740 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
745 * @member XObject createDelegate
746 * creates a delage metdhod
747 * @param {Function} method to wrap
748 * @param {Object} scope
749 * @param {Array} args to add
750 * @param {Boolean|Number} append arguments or replace after N arguments.
751 * @return {Function} returns the delegate
754 createDelegate : function(method, obj, args, appendArgs){
757 XObject.log("CALL: " + obj.id + ':'+ method.id);
759 var callArgs = args || arguments;
760 if(appendArgs === true){
761 callArgs = Array.prototype.slice.call(arguments, 0);
762 callArgs = callArgs.concat(args);
763 }else if(typeof appendArgs == "number"){
764 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
765 var applyArgs = [appendArgs, 0].concat(args); // create method call params
766 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
768 return method.apply(obj || window, callArgs);