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 // we could use this to determine if
61 // we are actually inside a inherited class...
62 // as define() should actually set this up..
64 if (!this.constructor) {
66 this.constructor = XObject;
67 var base = XObject.baseXObject(cfg);
69 XObject.extend(this,base.prototype);
74 // copy down all elements into self..
75 // make an extra copy in this.config?? - which is the one used in the constructor later
79 if (typeof(cfg[i]) == 'function') { // do we skip objects.
82 // these properties are not copied to cfg.
93 this.config[i] = cfg[i];
97 this.items = this.items || [];
100 // pack can be false!
101 if (typeof(this.pack) == 'undefined') {
103 this.pack = [ 'add' ]
105 var Gtk = imports.gi.Gtk;
108 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
115 // interesting question should we call constructor on items here...
116 // as the real work is done in init anyway..
120 for(var i = 0; i < this.items.length;i++) {
121 items.push(this.items[i]);
127 // create XObject for all the children.
128 for(var i = 0; i < items.length;i++) {
130 var base = XObject.baseXObject(items[i]);
131 base = base || XObject;
132 var item = (items[i].constructor == XObject) ? items[i] : new base(items[i]);
134 _this.items.push(item);
143 XObject.prototype = {
145 * @property el {GObject} the Gtk / etc. element.
149 * @property items {Array} list of sub elements
152 * @property parent {XObject} parent Element
156 * @property config {Object} the construction configuration.
160 * Initializes the Element (el) hooks up all the listeners
161 * and packs the children.
162 * you can override this, in child objects, then
163 * do this to do thi initaliztion.
165 * XObject.prototype.init.call(this);
172 //this.items.forEach(function(i) {
176 this.listeners = this.listeners || {};
179 // do we need to call 'beforeInit here?'
182 //if ((this.xtype == 'Include')) {
183 // o = this.pre_registry[cls];
185 var isSeed = typeof(Seed) != 'undefined';
187 // xtype= Gtk.Menu ?? what about c_new stuff?
188 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
189 if (!this.el && typeof(this.xtype) == 'function') {
190 XObject.log("func?" + XObject.keys(this.config).join(','));
191 this.el = this.xtype(this.config);
194 if (!this.el && typeof(this.xtype) == 'object') {
195 XObject.log("obj?" + XObject.keys(this.config).join(','));
196 this.el = new (this.xtype)(this.config);
200 if (!this.el && this.xns) {
202 var NS = imports.gi[this.xns];
204 XObject.error('Invalid xns: ' + this.xns, true);
206 constructor = NS[this.xtype];
208 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
210 this.el = isSeed ? new constructor(this.config) : new constructor();
213 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
215 // always overlay props..
216 // check for 'write' on object..
218 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
220 var gi = GIRepository.IRepository.get_default();
221 var ty = gi.find_by_gtype(this.xtype.type);
223 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
224 var p = GIRepository.object_info_get_property(ty,i);
225 if (GIRepository.property_info_get_flags(p) & 2) {
226 write.push(GIRepository.base_info_get_name(p));
229 XObject.writeablePropsCache[this.xtype.type] = write;
230 print(write.join(", "));
236 for (var i in this.config) {
237 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
240 if (i == 'buttons') { // problem with Gtk.MessageDialog..
243 if (i[0] == '.') { // parent? -
246 this.el[i] = this.config[i];
250 //if (o.xnsid && o.id) {
251 // XObject.registry = XObject.registry || { };
252 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
253 // XObject.registry[o.xnsid][o.id] = this;
257 var type = this.xtype && this.xtype.type ? GObject.type_name(this.xtype.type) : '';
258 XObject.log("add children to " + type);
261 for (var i = 0; i < this.items.length;i++) {
262 _this.addItem(this.items[i],i);
266 for (var i in this.listeners) {
267 this.addListener(i, this.listeners[i]);
270 this.init = XObject.emptyFn;
272 // delete this.listeners ?
273 // do again so child props work!
275 // do we need to call 'init here?'
280 * Adds an item to the object using a new XObject
281 * uses pack property to determine how to add it.
282 * @arg cfg {Object} same as XObject constructor.
284 addItem : function(item, pos)
287 if (typeof(item) == 'undefined') {
288 XObject.error("Invalid Item added to this!");
289 imports.console.dump(this.cfg);
292 // what about extended items!?!?!?
295 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
296 // print("addItem - call init [" + item.pack.join(',') + ']');
298 XObject.error("NO EL!");
299 imports.console.dump(item);
302 print(XObject.type(this.xtype) + ":pack=" + item.pack);
304 if (item.pack===false) { // no packing.. various items have this ..
308 if (typeof(item.pack) == 'function') { // pack is a function..
310 item.pack.apply(item, [ this , item ]);
318 if (typeof(item.pack) == 'string') {
320 item.pack.split(',').forEach(function(e, i) {
322 if (e == 'false') { args.push( false); return; }
323 if (e == 'true') { args.push( true); return; }
324 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
327 //print(args.join(","));
329 pack_m = args.shift();
331 pack_m = item.pack.shift();
336 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
339 name: "ArgumentError",
340 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
348 // finally call the pack method
349 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
351 args.unshift(item.el);
357 XObject.log(pack_m + '[' + args.join(',') +']');
358 //Seed.print('args: ' + args.length);
360 this.el[pack_m].apply(this.el, args);
367 * Connects a method to a signal. (gjs/Seed aware)
369 * @param {String} sig name of signal
370 * @param {Function} fn handler.
372 addListener : function(sig, fn)
375 XObject.log("Add signal " + sig);
377 var _li = XObject.createDelegate(fn,this);
378 // private listeners that are not copied to GTk.
380 if (typeof(Seed) != 'undefined') {
381 // Seed.print(typeof(_li));
382 this.el.signal[sig].connect(_li);
384 this.el.connect( sig, _li);
391 * Finds an object in the child elements using xid of object.
392 * prefix with '.' to look up the tree..
393 * prefix with multiple '..' to look further up..
394 * prefix with '/' to look from the top, eg. '^LeftTree.model'
396 * @param {String} name name of signal
397 * @return {XObject|false} the object if found.
401 XObject.log("SEARCH FOR " + xid + " in " + this.id);
406 name: "ArgumentError",
407 message : "ID not found : empty id"
412 return this.parent.get(xid.substring(1));
416 if (typeof(XObject.cache[xid]) != 'undefined') {
417 return XObject.cache[xid];
419 if (xid.indexOf('.') > -1) {
421 var child = xid.split('.');
422 var nxid = child.shift();
424 child = child.join('.');
425 if (typeof(XObject.cache[nxid]) != 'undefined') {
426 return XObject.cache[nxid].get(child);
437 ret = e.get(xid.substring(1));
442 name: "ArgumentError",
443 message : "ID not found : " + oid
446 XObject.cache[xid] = ret;
447 return XObject.cache[xid];
451 if (xid.indexOf('.') > -1) {
452 child = xid.split('.');
455 child = child.join('.');
458 if (xid == this.id) {
460 return child === false ? this : this.get(child);
463 name: "ArgumentError",
464 message : "ID not found : " + oid
471 this.items.forEach(function(ch) {
481 return child === false ? ret : ret.get(child);
484 name: "ArgumentError",
485 message : "ID not found : " + oid
492 this.items.forEach(function(ch) {
497 XObject.error("invalid item...");
498 imports.console.dump(_this);
509 name: "ArgumentError",
510 message : "ID not found : " + oid
514 return child === false ? ret : ret.get(child);
517 name: "ArgumentError",
518 message : "ID not found : " + oid
528 * Copies all the properties of config to obj.
530 * Pretty much the same as JQuery/Prototype.. or Roo.apply
531 * @param {Object} obj The receiver of the properties
532 * @param {Object} config The source of the properties
533 * @param {Object} defaults A different object that will also be applied for default values
534 * @return {Object} returns obj
535 * @member XObject extend
539 XObject.extend = function(o, c, defaults){
541 // no "this" reference for friendly out of scope calls
542 XObject.extend(o, defaults);
544 if(o && c && typeof c == 'object'){
552 XObject.extend(XObject,
556 * @property {Boolean} debug XObject debugging. - set to true to debug.
561 * @property {Object} cache - cache of object ids
569 emptyFn : function () { },
575 * @param {String|Object} output String to print.
577 log : function(output)
582 print("LOG:" + output);
587 * @param {String|Object} output String to print.
589 error : function(output)
591 print("ERROR: " + output);
595 * @param {String|Object} output String to print.
597 fatal : function(output)
601 name: "ArgumentError",
608 * Copies all the properties of config to obj, if the do not exist.
609 * @param {Object} obj The receiver of the properties
610 * @param {Object} config The source of the properties
611 * @return {Object} returns obj
612 * @member Object extendIf
616 extendIf : function(o, c)
619 if(!o || !c || typeof c != 'object'){
623 if (typeof(o[p]) != 'undefined') {
634 * Extends one class with another class and optionally overrides members with the passed literal. This class
635 * also adds the function "override()" to the class that can be used to override
636 * members on an instance.
639 * MyObject = Object.define(
643 * parentClass, // or Object
645 * ... methods and properties.
648 * @param {Function} constructor The class inheriting the functionality
649 * @param {Object} superclass The class being extended
650 * @param {Object} overrides (optional) A literal with members
651 * @return {Function} constructor (eg. class
657 var io = function(o){
662 return function(constructor, parentClass, overrides) {
663 if (typeof(parentClass) == 'undefined') {
664 XObject.error("XObject.define: Missing parentClass: when applying: " );
665 XObject.error(new String(constructor));
668 if (typeof(parentClass.prototype) == 'undefined') {
669 XObject.error("Missing protype: when applying: " );
670 XObject.error(new String(constructor));
671 XObject.error(new String(parentClass));
674 var F = function(){};
676 var spp = parentClass.prototype;
679 sbp = constructor.prototype = new F();
680 sbp.constructor=constructor;
681 constructor.superclass=spp;
684 if(spp.constructor == Object.prototype.constructor){
685 spp.constructor=parentClass;
688 constructor.override = function(o){
689 Object.extend(constructor.prototype, o);
692 XObject.extend(constructor.prototype, overrides);
699 * returns a list of keys of the object.
700 * @param {Object} obj object to inspect
701 * @return {Array} returns list of kyes
702 * @member XObject keys
713 * return the Gobject name of a constructor - does not appear to work on structs..
714 * @param {Object} gobject ctr
715 * @return {String} returns name
716 * @member XObject type
720 if (typeof(o) == 'object') {
721 return GObject.type_name(o.type);
722 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
727 * return the XObjectBase class for a cfg (which includes an xtype)
728 * @param {Object} configuration.
729 * @return {function} constructor
730 * @member XObject baseXObject
732 baseXObject : function(cfg)
735 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
736 // xns is not a string!!!?
738 if (typeof(cfg.xtype) == 'object') {
739 gname = XObject.type(cfg.xtype);
742 print("TRYING BASE OBJECT : " + gname);
743 // in the situation where we have been called and there is a base object
744 // defining the behavior..
745 // then we should copy the prototypes from the base object into this..
746 var base = gname ? imports.XObjectBase[gname][gname] : false;
751 XObject.log("error finding " + gname + " - " + e.toString());
759 * @member XObject createDelegate
760 * creates a delage metdhod
761 * @param {Function} method to wrap
762 * @param {Object} scope
763 * @param {Array} args to add
764 * @param {Boolean|Number} append arguments or replace after N arguments.
765 * @return {Function} returns the delegate
768 createDelegate : function(method, obj, args, appendArgs){
771 XObject.log("CALL: " + obj.id + ':'+ method.id);
773 var callArgs = args || arguments;
774 if(appendArgs === true){
775 callArgs = Array.prototype.slice.call(arguments, 0);
776 callArgs = callArgs.concat(args);
777 }else if(typeof appendArgs == "number"){
778 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
779 var applyArgs = [appendArgs, 0].concat(args); // create method call params
780 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
782 return method.apply(obj || window, callArgs);