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..
69 if (!this.constructor) {
71 this.constructor = XObject;
73 // start by seeing if we have a base class....
75 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
76 // xns is not a string!!!?
78 if (typeof(cfg.xtype) == 'object') {
79 gname = GObject.type_name(cfg.xtype.type);
80 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
83 // in the situation where we have been called and there is a base object
84 // defining the behavior..
85 // then we should copy the prototypes from the base object into this..
86 var base = gname ? imports.XObjectBase[gname][gname] : false;
88 // print("Overlaying XOBJBECT-BASE." + cfg.xtype);
89 XObject.extend(this,base.prototype);
94 XObject.log("error finding " + gname + " - " + e.toString());
98 // copy down all elements into self..
99 // make an extra copy in this.config?? - which is the one used in the constructor later
103 if (typeof(cfg[i]) == 'function') { // do we skip objects.
106 // these properties are not copied to cfg.
117 this.config[i] = cfg[i];
121 this.items = this.items || [];
124 // pack can be false!
125 if (typeof(this.pack) == 'undefined') {
127 this.pack = [ 'add' ]
129 var Gtk = imports.gi.Gtk;
132 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
139 // interesting question should we call constructor on items here...
140 // as the real work is done in init anyway..
144 this.items.forEach(function(i) {
148 // create XObject for all the children.
149 items.forEach(function(i,n) {
151 var item = (i.constructor == XObject) ? o : new XObject(i);
153 _this.items.push(item);
156 if (this.onConstruct) {
157 this.onConstruct.call(this);
164 XObject.prototype = {
166 * @property el {GObject} the Gtk / etc. element.
170 * @property items {Array} list of sub elements
173 * @property parent {XObject} parent Element
177 * @property config {Object} the construction configuration.
181 * Initializes the Element (el) hooks up all the listeners
182 * and packs the children.
183 * you can override this, in child objects, then
184 * do this to do thi initaliztion.
186 * XObject.prototype.init.call(this);
193 //this.items.forEach(function(i) {
197 this.listeners = this.listeners || {};
200 // do we need to call 'beforeInit here?'
203 //if ((this.xtype == 'Include')) {
204 // o = this.pre_registry[cls];
206 var isSeed = typeof(Seed) != 'undefined';
208 // xtype= Gtk.Menu ?? what about c_new stuff?
209 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
210 if (!this.el && typeof(this.xtype) == 'function') {
211 XObject.log("func?" + XObject.keys(this.config).join(','));
212 this.el = this.xtype(this.config);
215 if (!this.el && typeof(this.xtype) == 'object') {
216 XObject.log("obj?" + XObject.keys(this.config).join(','));
217 this.el = new (this.xtype)(this.config);
221 if (!this.el && this.xns) {
223 var NS = imports.gi[this.xns];
225 XObject.error('Invalid xns: ' + this.xns, true);
227 constructor = NS[this.xtype];
229 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
231 this.el = isSeed ? new constructor(this.config) : new constructor();
234 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
236 // always overlay props..
237 // check for 'write' on object..
239 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
241 var gi = GIRepository.IRepository.get_default();
242 var ty = gi.find_by_gtype(this.xtype.type);
244 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
245 var p = GIRepository.object_info_get_property(ty,i);
246 if (GIRepository.property_info_get_flags(p) & 2) {
247 write.push(GIRepository.base_info_get_name(p));
250 XObject.writeablePropsCache[this.xtype.type] = write;
251 print(write.join(", "));
257 for (var i in this.config) {
258 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
261 if (i == 'buttons') { // problem with Gtk.MessageDialog..
264 if (i[0] == '.') { // parent? -
267 this.el[i] = this.config[i];
271 //if (o.xnsid && o.id) {
272 // XObject.registry = XObject.registry || { };
273 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
274 // XObject.registry[o.xnsid][o.id] = this;
277 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
278 XObject.log("add children to " + type);
281 this.items.forEach(function(i,n) {
286 for (var i in this.listeners) {
287 this.addListener(i, this.listeners[i]);
290 this.init = XObject.emptyFn;
292 // delete this.listeners ?
293 // do again so child props work!
295 // do we need to call 'init here?'
301 * Adds an item to the object using a new XObject
302 * uses pack property to determine how to add it.
303 * @arg cfg {Object} same as XObject constructor.
305 addItem : function(item, pos)
308 if (typeof(item) == 'undefined') {
309 XObject.error("Invalid Item added to this!");
310 imports.console.dump(this.cfg);
313 // what about extended items!?!?!?
316 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
317 // print("addItem - call init [" + item.pack.join(',') + ']');
319 XObject.error("NO EL!");
320 imports.console.dump(item);
323 print(XObject.type(this.xtype) + ":pack=" + item.pack);
325 if (item.pack===false) { // no packing.. various items have this ..
329 if (typeof(item.pack) == 'function') { // pack is a function..
331 item.pack.apply(item, [ this , item ]);
339 if (typeof(item.pack) == 'string') {
341 item.pack.split(',').forEach(function(e, i) {
343 if (e == 'false') { args.push( false); return; }
344 if (e == 'true') { args.push( true); return; }
345 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
348 //print(args.join(","));
350 pack_m = args.shift();
352 pack_m = item.pack.shift();
357 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
360 name: "ArgumentError",
361 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
369 // finally call the pack method
370 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
372 args.unshift(item.el);
378 XObject.log(pack_m + '[' + args.join(',') +']');
379 //Seed.print('args: ' + args.length);
381 this.el[pack_m].apply(this.el, args);
388 * @method addListener
389 * Connects a method to a signal. (gjs/Seed aware)
391 * @arg sig {String} name of signal
392 * @arg fn {Function} handler.
394 addListener : function(sig, fn)
397 XObject.log("Add signal " + sig);
399 var _li = XObject.createDelegate(fn,this);
400 // private listeners that are not copied to GTk.
402 if (typeof(Seed) != 'undefined') {
403 // Seed.print(typeof(_li));
404 this.el.signal[sig].connect(_li);
406 this.el.connect( sig, _li);
413 * Finds an object in the child elements using xid of object.
414 * prefix with '.' to look up the tree..
415 * prefix with multiple '..' to look further up..
416 * prefix with '/' to look from the top, eg. '^LeftTree.model'
418 * @arg name {String} name of signal
419 * @return {XObject|false} the object if found.
423 XObject.log("SEARCH FOR " + xid + " in " + this.id);
428 name: "ArgumentError",
429 message : "ID not found : empty id"
434 return this.parent.get(xid.substring(1));
438 if (typeof(XObject.cache[xid]) != 'undefined') {
439 return XObject.cache[xid];
441 if (xid.indexOf('.') > -1) {
443 var child = xid.split('.');
444 var nxid = child.shift();
446 child = child.join('.');
447 if (typeof(XObject.cache[nxid]) != 'undefined') {
448 return XObject.cache[nxid].get(child);
459 ret = e.get(xid.substring(1));
464 name: "ArgumentError",
465 message : "ID not found : " + oid
468 XObject.cache[xid] = ret;
469 return XObject.cache[xid];
473 if (xid.indexOf('.') > -1) {
474 child = xid.split('.');
477 child = child.join('.');
480 if (xid == this.id) {
482 return child === false ? this : this.get(child);
485 name: "ArgumentError",
486 message : "ID not found : " + oid
493 this.items.forEach(function(ch) {
503 return child === false ? ret : ret.get(child);
506 name: "ArgumentError",
507 message : "ID not found : " + oid
514 this.items.forEach(function(ch) {
519 XObject.error("invalid item...");
520 imports.console.dump(_this);
531 name: "ArgumentError",
532 message : "ID not found : " + oid
536 return child === false ? ret : ret.get(child);
539 name: "ArgumentError",
540 message : "ID not found : " + oid
550 * Copies all the properties of config to obj.
552 * Pretty much the same as JQuery/Prototype.. or Roo.apply
553 * @param {Object} obj The receiver of the properties
554 * @param {Object} config The source of the properties
555 * @param {Object} defaults A different object that will also be applied for default values
556 * @return {Object} returns obj
557 * @member XObject extend
561 XObject.extend = function(o, c, defaults){
563 // no "this" reference for friendly out of scope calls
564 XObject.extend(o, defaults);
566 if(o && c && typeof c == 'object'){
574 XObject.extend(XObject,
578 * @property {Boolean} debug XObject debugging. - set to true to debug.
583 * @property {Object} cache - cache of object ids
591 emptyFn : function () { },
597 * @param {String|Object} output String to print.
599 log : function(output)
604 print("LOG:" + output);
609 * @param {String|Object} output String to print.
611 error : function(output)
613 print("ERROR: " + output);
617 * @param {String|Object} output String to print.
619 fatal : function(output)
623 name: "ArgumentError",
630 * Copies all the properties of config to obj, if the do not exist.
631 * @param {Object} obj The receiver of the properties
632 * @param {Object} config The source of the properties
633 * @return {Object} returns obj
634 * @member Object extendIf
638 extendIf : function(o, c)
641 if(!o || !c || typeof c != 'object'){
645 if (typeof(o[p]) != 'undefined') {
656 * Extends one class with another class and optionally overrides members with the passed literal. This class
657 * also adds the function "override()" to the class that can be used to override
658 * members on an instance.
661 * MyObject = Object.define(
665 * parentClass, // or Object
667 * ... methods and properties.
670 * @param {Function} constructor The class inheriting the functionality
671 * @param {Object} superclass The class being extended
672 * @param {Object} overrides (optional) A literal with members
673 * @return {Function} constructor (eg. class
679 var io = function(o){
684 return function(constructor, parentClass, overrides) {
685 if (typeof(parentClass) == 'undefined') {
686 XObject.error("XObject.define: Missing parentClass: when applying: " );
687 XObject.error(new String(constructor));
690 if (typeof(parentClass.prototype) == 'undefined') {
691 XObject.error("Missing protype: when applying: " );
692 XObject.error(new String(constructor));
693 XObject.error(new String(parentClass));
696 var F = function(){};
698 var spp = parentClass.prototype;
701 sbp = constructor.prototype = new F();
702 sbp.constructor=constructor;
703 constructor.superclass=spp;
706 if(spp.constructor == Object.prototype.constructor){
707 spp.constructor=parentClass;
710 constructor.override = function(o){
711 Object.extend(constructor.prototype, o);
714 XObject.extend(constructor.prototype, overrides);
721 * returns a list of keys of the object.
722 * @param {Object} obj object to inspect
723 * @return {Array} returns list of kyes
724 * @member XObject keys
735 * return the Gobject name of a constructor
736 * @param {Object} gobject ctr
737 * @return {String} returns name
738 * @member XObject keys
742 if (typeof(o) == 'object') {
743 return GObject.type_name(o.type);
744 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
749 * @member XObject createDelegate
750 * creates a delage metdhod
751 * @param {Function} method to wrap
752 * @param {Object} scope
753 * @param {Array} args to add
754 * @param {Boolean|Number} append arguments or replace after N arguments.
755 * @return {Function} returns the delegate
758 createDelegate : function(method, obj, args, appendArgs){
761 XObject.log("CALL: " + obj.id + ':'+ method.id);
763 var callArgs = args || arguments;
764 if(appendArgs === true){
765 callArgs = Array.prototype.slice.call(arguments, 0);
766 callArgs = callArgs.concat(args);
767 }else if(typeof appendArgs == "number"){
768 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
769 var applyArgs = [appendArgs, 0].concat(args); // create method call params
770 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
772 return method.apply(obj || window, callArgs);