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
50 function XObject (cfg) {
51 // first apply cfg if set.
52 //print("new XOBJECT!!!");
54 //print ("XObject ctr");
56 this.config = {}; // used to initialize GObject
58 this.cfg = XObject.extend({}, cfg); // used to store original configuration.. for referencing..
60 // used by baseobject to create fake child elements on init..
65 // we could use this to determine if
66 // we are actually inside a inherited class...
67 // as define() should actually set this up..
69 if (!this.constructor) {
71 this.constructor = XObject;
72 var base = XObject.baseXObject(cfg);
74 XObject.extend(this, base.prototype);
79 // copy down all elements into self..
80 // make an extra copy in this.config?? - which is the one used in the constructor later
84 if (typeof(cfg[i]) == 'function') { // do we skip objects.
87 // these properties are not copied to cfg.
98 this.config[i] = cfg[i];
102 this.items = this.items || [];
105 // pack can be false!
106 if (typeof(this.pack) == 'undefined') {
108 this.pack = [ 'add' ]
110 var Gtk = imports.gi.Gtk;
113 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
120 // interesting question should we call constructor on items here...
121 // as the real work is done in init anyway..
125 for(var i = 0; i < this.items.length;i++) {
126 items.push(this.items[i]);
132 // create XObject for all the children.
133 for(var i = 0; i < items.length;i++) {
135 var base = XObject.baseXObject(items[i]);
136 base = base || XObject;
137 var item = (items[i].constructor == XObject) ? items[i] : new base(items[i]);
139 _this.items.push(item);
148 XObject.prototype = {
150 * @property el {GObject} the Gtk / etc. element.
154 * @property items {Array} list of sub elements
157 * @property parent {XObject} parent Element
161 * @property config {Object} the construction configuration.
165 * Initializes the Element (el) hooks up all the listeners
166 * and packs the children.
167 * you can override this, in child objects, then
168 * do this to do thi initaliztion.
170 * XObject.prototype.init.call(this);
173 init : function(parent)
177 //this.items.forEach(function(i) {
181 this.listeners = this.listeners || {};
184 // do we need to call 'beforeInit here?'
187 //if ((this.xtype == 'Include')) {
188 // o = this.pre_registry[cls];
190 var isSeed = typeof(Seed) != 'undefined';
192 // xtype= Gtk.Menu ?? what about c_new stuff?
193 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
194 if (!this.el && typeof(this.xtype) == 'function') {
195 XObject.log("func?" + XObject.keys(this.config).join(','));
196 this.el = this.xtype(this.config);
199 if (!this.el && typeof(this.xtype) == 'object') {
200 XObject.log("obj?" + XObject.keys(this.config).join(','));
201 this.el = new (this.xtype)(this.config);
205 if (!this.el && this.xns) {
207 var NS = imports.gi[this.xns];
209 XObject.error('Invalid xns: ' + this.xns, true);
211 constructor = NS[this.xtype];
213 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
215 this.el = isSeed ? new constructor(this.config) : new constructor();
218 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
220 // always overlay props..
221 // check for 'write' on object..
223 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
225 var gi = GIRepository.IRepository.get_default();
226 var ty = gi.find_by_gtype(this.xtype.type);
228 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
229 var p = GIRepository.object_info_get_property(ty,i);
230 if (GIRepository.property_info_get_flags(p) & 2) {
231 write.push(GIRepository.base_info_get_name(p));
234 XObject.writeablePropsCache[this.xtype.type] = write;
235 print(write.join(", "));
241 for (var i in this.config) {
242 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
245 if (i == 'buttons') { // problem with Gtk.MessageDialog..
248 if (i[0] == '.') { // parent? -
251 this.el[i] = this.config[i];
255 //if (o.xnsid && o.id) {
256 // XObject.registry = XObject.registry || { };
257 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
258 // XObject.registry[o.xnsid][o.id] = this;
261 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
262 XObject.log("add children to " + type);
265 for (var i = 0; i < this.items.length;i++) {
266 _this.addItem(this.items[i],i);
270 for (var i in this.listeners) {
271 this.addListener(i, this.listeners[i]);
274 this.init = XObject.emptyFn;
276 // delete this.listeners ?
277 // do again so child props work!
279 // do we need to call 'init here?'
284 * Adds an item to the object using a new XObject
285 * uses pack property to determine how to add it.
286 * @arg cfg {Object} same as XObject constructor.
288 addItem : function(item, pos)
291 if (typeof(item) == 'undefined') {
292 XObject.error("Invalid Item added to this!");
293 imports.console.dump(this.cfg);
296 // what about extended items!?!?!?
299 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
300 // print("addItem - call init [" + item.pack.join(',') + ']');
302 XObject.error("NO EL!");
303 imports.console.dump(item);
306 XObject.log(XObject.type(this.xtype) + ":pack=" + item.pack);
308 if (item.pack===false) { // no packing.. various items have this ..
312 if (typeof(item.pack) == 'function') { // pack is a function..
314 item.pack.apply(item, [ this , item ]);
322 if (typeof(item.pack) == 'string') {
324 item.pack.split(',').forEach(function(e, i) {
326 if (e == 'false') { args.push( false); return; }
327 if (e == 'true') { args.push( true); return; }
328 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
331 //print(args.join(","));
333 pack_m = args.shift();
335 pack_m = item.pack.shift();
340 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
343 name: "ArgumentError",
344 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
352 // finally call the pack method
353 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
355 args.unshift(item.el);
361 XObject.log(pack_m + '[' + args.join(',') +']');
362 //Seed.print('args: ' + args.length);
364 this.el[pack_m].apply(this.el, args);
371 * Connects a method to a signal. (gjs/Seed aware)
373 * @param {String} sig name of signal
374 * @param {Function} fn handler.
376 addListener : function(sig, fn)
379 XObject.log("Add signal " + sig);
381 var _li = XObject.createDelegate(fn,this);
382 // private listeners that are not copied to GTk.
384 if (typeof(Seed) != 'undefined') {
385 // Seed.print(typeof(_li));
386 this.el.signal[sig].connect(_li);
388 this.el.connect( sig, _li);
395 * Finds an object in the child elements using xid of object.
396 * prefix with '.' to look up the tree..
397 * prefix with multiple '..' to look further up..
398 * prefix with '/' to look from the top, eg. '^LeftTree.model'
400 * @param {String} name name of signal
401 * @return {XObject|false} the object if found.
405 XObject.log("SEARCH FOR " + xid + " in " + this.id);
410 name: "ArgumentError",
411 message : "ID not found : empty id"
416 return this.parent.get(xid.substring(1));
420 if (typeof(XObject.cache[xid]) != 'undefined') {
421 return XObject.cache[xid];
423 if (xid.indexOf('.') > -1) {
425 var child = xid.split('.');
426 var nxid = child.shift();
428 child = child.join('.');
429 if (typeof(XObject.cache[nxid]) != 'undefined') {
430 return XObject.cache[nxid].get(child);
441 ret = e.get(xid.substring(1));
446 name: "ArgumentError",
447 message : "ID not found : " + oid
450 XObject.cache[xid] = ret;
451 return XObject.cache[xid];
455 if (xid.indexOf('.') > -1) {
456 child = xid.split('.');
459 child = child.join('.');
462 if (xid == this.id) {
464 return child === false ? this : this.get(child);
467 name: "ArgumentError",
468 message : "ID not found : " + oid
475 this.items.forEach(function(ch) {
485 return child === false ? ret : ret.get(child);
488 name: "ArgumentError",
489 message : "ID not found : " + oid
496 this.items.forEach(function(ch) {
501 XObject.error("invalid item...");
502 imports.console.dump(_this);
513 name: "ArgumentError",
514 message : "ID not found : " + oid
518 return child === false ? ret : ret.get(child);
521 name: "ArgumentError",
522 message : "ID not found : " + oid
532 * Copies all the properties of config to obj.
534 * Pretty much the same as JQuery/Prototype.. or Roo.apply
535 * @param {Object} obj The receiver of the properties
536 * @param {Object} config The source of the properties
537 * @param {Object} defaults A different object that will also be applied for default values
538 * @return {Object} returns obj
539 * @member XObject extend
543 XObject.extend = function(o, c, defaults){
545 // no "this" reference for friendly out of scope calls
546 XObject.extend(o, defaults);
548 if(o && c && typeof c == 'object'){
556 XObject.extend(XObject,
560 * @property {Boolean} debug XObject debugging. - set to true to debug.
565 * @property {Object} cache - cache of object ids
573 emptyFn : function () { },
579 * @param {String|Object} output String to print.
581 log : function(output)
586 print("LOG:" + output);
591 * @param {String|Object} output String to print.
593 error : function(output)
595 print("ERROR: " + output);
599 * @param {String|Object} output String to print.
601 fatal : function(output)
605 name: "ArgumentError",
612 * Copies all the properties of config to obj, if the do not exist.
613 * @param {Object} obj The receiver of the properties
614 * @param {Object} config The source of the properties
615 * @return {Object} returns obj
616 * @member Object extendIf
620 extendIf : function(o, c)
623 if(!o || !c || typeof c != 'object'){
627 if (typeof(o[p]) != 'undefined') {
638 * Extends one class with another class and optionally overrides members with the passed literal. This class
639 * also adds the function "override()" to the class that can be used to override
640 * members on an instance.
643 * MyObject = Object.define(
647 * parentClass, // or Object
649 * ... methods and properties.
652 * @param {Function} constructor The class inheriting the functionality
653 * @param {Object} superclass The class being extended
654 * @param {Object} overrides (optional) A literal with members
655 * @return {Function} constructor (eg. class
661 var io = function(o){
666 return function(constructor, parentClass, overrides) {
667 if (typeof(parentClass) == 'undefined') {
668 XObject.error("XObject.define: Missing parentClass: when applying: " );
669 XObject.error(new String(constructor));
672 if (typeof(parentClass.prototype) == 'undefined') {
673 XObject.error("Missing protype: when applying: " );
674 XObject.error(new String(constructor));
675 XObject.error(new String(parentClass));
678 var F = function(){};
680 var spp = parentClass.prototype;
683 sbp = constructor.prototype = new F();
684 sbp.constructor=constructor;
685 constructor.superclass=spp;
688 if(spp.constructor == Object.prototype.constructor){
689 spp.constructor=parentClass;
692 constructor.override = function(o){
693 Object.extend(constructor.prototype, o);
696 XObject.extend(constructor.prototype, overrides);
703 * returns a list of keys of the object.
704 * @param {Object} obj object to inspect
705 * @return {Array} returns list of kyes
706 * @member XObject keys
717 * return the Gobject name of a constructor - does not appear to work on structs..
718 * @param {Object} gobject ctr
719 * @return {String} returns name
720 * @member XObject type
724 if (typeof(o) == 'object') {
725 return GObject.type_name(o.type);
726 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
731 * return the XObjectBase class for a cfg (which includes an xtype)
732 * @param {Object} configuration.
733 * @return {function} constructor
734 * @member XObject baseXObject
736 baseXObject : function(cfg)
739 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
740 // xns is not a string!!!?
742 if (typeof(cfg.xtype) == 'object') {
743 gname = XObject.type(cfg.xtype);
746 if (typeof(cfg.xtype) == 'string') {
750 XObject.log("TRYING BASE OBJECT : " + gname);
751 // in the situation where we have been called and there is a base object
752 // defining the behavior..
753 // then we should copy the prototypes from the base object into this..
755 // see if file exists???
757 var base = gname ? imports.XObjectBase[gname][gname] : false;
762 XObject.log("error finding " + gname + " - " + e.toString());
770 * @member XObject createDelegate
771 * creates a delage metdhod
772 * @param {Function} method to wrap
773 * @param {Object} scope
774 * @param {Array} args to add
775 * @param {Boolean|Number} append arguments or replace after N arguments.
776 * @return {Function} returns the delegate
779 createDelegate : function(method, obj, args, appendArgs){
782 XObject.log("CALL: " + obj.id + ':'+ method.id);
784 var callArgs = args || arguments;
785 if(appendArgs === true){
786 callArgs = Array.prototype.slice.call(arguments, 0);
787 callArgs = callArgs.concat(args);
788 }else if(typeof appendArgs == "number"){
789 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
790 var applyArgs = [appendArgs, 0].concat(args); // create method call params
791 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
793 return method.apply(obj || window, callArgs);