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 XObject/[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");
62 this.cfg = XObject.extend({}, cfg);
64 this.constructor = XObject;
68 // start by seeing if we have a base class....
70 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
71 // xns is not a string!!!?
73 if (typeof(cfg.xtype) == 'object') {
74 gname = GObject.type_name(cfg.xtype.type);
75 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
78 var base = gname ? imports.XObjectBase[gname][gname] : false;
80 // print("Overlaying XOBJBECT-BASE." + cfg.xtype);
81 XObject.extend(this,base);
86 XObject.log("error finding " + gname + " - " + e.toString());
90 // copy down all elements into self..
91 // make an extra copy in this.config?? - which is the one used in the constructor later
95 if (typeof(cfg[i]) == 'function') { // do we skip objects.
98 // these properties are not copied to cfg.
109 this.config[i] = cfg[i];
113 this.items = this.items || [];
116 // pack can be false!
117 if (typeof(this.pack) == 'undefined') {
119 this.pack = [ 'add' ]
121 var Gtk = imports.gi.Gtk;
124 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
131 // interesting question should we call constructor on items here...
132 // as the real work is done in init anyway..
136 this.items.forEach(function(i) {
140 // create XObject for all the children.
141 items.forEach(function(i,n) {
143 var item = (i.constructor == XObject) ? o : new XObject(i);
145 _this.items.push(item);
148 if (this.onConstruct) {
149 this.onConstruct.call(this);
156 XObject.prototype = {
158 * @property el {GObject} the Gtk / etc. element.
162 * @property items {Array} list of sub elements
165 * @property parent {XObject} parent Element
169 * @property config {Object} the construction configuration.
173 * Initializes the Element (el) hooks up all the listeners
174 * and packs the children.
175 * you can override this, in child objects, then
176 * do this to do thi initaliztion.
178 * XObject.prototype.init.call(this);
185 //this.items.forEach(function(i) {
189 this.listeners = this.listeners || {};
192 // do we need to call 'beforeInit here?'
195 //if ((this.xtype == 'Include')) {
196 // o = this.pre_registry[cls];
198 var isSeed = typeof(Seed) != 'undefined';
200 // xtype= Gtk.Menu ?? what about c_new stuff?
201 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
202 if (!this.el && typeof(this.xtype) == 'function') {
203 XObject.log("func?" + XObject.keys(this.config).join(','));
204 this.el = this.xtype(this.config);
207 if (!this.el && typeof(this.xtype) == 'object') {
208 XObject.log("obj?" + XObject.keys(this.config).join(','));
209 this.el = new (this.xtype)(this.config);
213 if (!this.el && this.xns) {
215 var NS = imports.gi[this.xns];
217 XObject.error('Invalid xns: ' + this.xns, true);
219 constructor = NS[this.xtype];
221 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
223 this.el = isSeed ? new constructor(this.config) : new constructor();
226 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
228 // always overlay props..
229 // check for 'write' on object..
231 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
233 var gi = GIRepository.IRepository.get_default();
234 var ty = gi.find_by_gtype(this.xtype.type);
236 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
237 var p = GIRepository.object_info_get_property(ty,i);
238 if (GIRepository.property_info_get_flags(p) & 2) {
239 write.push(GIRepository.base_info_get_name(p));
242 XObject.writeablePropsCache[this.xtype.type] = write;
243 print(write.join(", "));
249 for (var i in this.config) {
250 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
253 if (i == 'buttons') { // problem with Gtk.MessageDialog..
256 if (i[0] == '.') { // parent? -
259 this.el[i] = this.config[i];
263 //if (o.xnsid && o.id) {
264 // XObject.registry = XObject.registry || { };
265 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
266 // XObject.registry[o.xnsid][o.id] = this;
269 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
270 XObject.log("add children to " + type);
273 this.items.forEach(function(i,n) {
278 for (var i in this.listeners) {
279 this.addListener(i, this.listeners[i]);
282 this.init = XObject.emptyFn;
284 // delete this.listeners ?
285 // do again so child props work!
287 // do we need to call 'init here?'
293 * Adds an item to the object using a new XObject
294 * uses pack property to determine how to add it.
295 * @arg cfg {Object} same as XObject constructor.
297 addItem : function(item, pos)
300 if (typeof(item) == 'undefined') {
301 XObject.error("Invalid Item added to this!");
302 imports.console.dump(this.cfg);
305 // what about extended items!?!?!?
308 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
309 // print("addItem - call init [" + item.pack.join(',') + ']');
311 XObject.error("NO EL!");
312 imports.console.dump(item);
315 print(XObject.type(this.xtype) + ":pack=" + item.pack);
317 if (item.pack===false) { // no packing.. various items have this ..
321 if (typeof(item.pack) == 'function') { // pack is a function..
323 item.pack.apply(item, [ this , item ]);
331 if (typeof(item.pack) == 'string') {
333 item.pack.split(',').forEach(function(e, i) {
335 if (e == 'false') { args.push( false); return; }
336 if (e == 'true') { args.push( true); return; }
337 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
340 //print(args.join(","));
342 pack_m = args.shift();
344 pack_m = item.pack.shift();
349 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
352 name: "ArgumentError",
353 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
361 // finally call the pack method
362 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
364 args.unshift(item.el);
370 XObject.log(pack_m + '[' + args.join(',') +']');
371 //Seed.print('args: ' + args.length);
373 this.el[pack_m].apply(this.el, args);
380 * @method addListener
381 * Connects a method to a signal. (gjs/Seed aware)
383 * @arg sig {String} name of signal
384 * @arg fn {Function} handler.
386 addListener : function(sig, fn)
389 XObject.log("Add signal " + sig);
391 var _li = XObject.createDelegate(fn,this);
392 // private listeners that are not copied to GTk.
394 if (typeof(Seed) != 'undefined') {
395 // Seed.print(typeof(_li));
396 this.el.signal[sig].connect(_li);
398 this.el.connect( sig, _li);
405 * Finds an object in the child elements using xid of object.
406 * prefix with '.' to look up the tree..
407 * prefix with multiple '..' to look further up..
408 * prefix with '/' to look from the top, eg. '^LeftTree.model'
410 * @arg name {String} name of signal
411 * @return {XObject|false} the object if found.
415 XObject.log("SEARCH FOR " + xid + " in " + this.id);
420 name: "ArgumentError",
421 message : "ID not found : empty id"
426 return this.parent.get(xid.substring(1));
430 if (typeof(XObject.cache[xid]) != 'undefined') {
431 return XObject.cache[xid];
433 if (xid.indexOf('.') > -1) {
435 var child = xid.split('.');
436 var nxid = child.shift();
438 child = child.join('.');
439 if (typeof(XObject.cache[nxid]) != 'undefined') {
440 return XObject.cache[nxid].get(child);
451 ret = e.get(xid.substring(1));
456 name: "ArgumentError",
457 message : "ID not found : " + oid
460 XObject.cache[xid] = ret;
461 return XObject.cache[xid];
465 if (xid.indexOf('.') > -1) {
466 child = xid.split('.');
469 child = child.join('.');
472 if (xid == this.id) {
474 return child === false ? this : this.get(child);
477 name: "ArgumentError",
478 message : "ID not found : " + oid
485 this.items.forEach(function(ch) {
495 return child === false ? ret : ret.get(child);
498 name: "ArgumentError",
499 message : "ID not found : " + oid
506 this.items.forEach(function(ch) {
511 XObject.error("invalid item...");
512 imports.console.dump(_this);
523 name: "ArgumentError",
524 message : "ID not found : " + oid
528 return child === false ? ret : ret.get(child);
531 name: "ArgumentError",
532 message : "ID not found : " + oid
542 * Copies all the properties of config to obj.
544 * Pretty much the same as JQuery/Prototype.. or Roo.apply
545 * @param {Object} obj The receiver of the properties
546 * @param {Object} config The source of the properties
547 * @param {Object} defaults A different object that will also be applied for default values
548 * @return {Object} returns obj
549 * @member XObject extend
553 XObject.extend = function(o, c, defaults){
555 // no "this" reference for friendly out of scope calls
556 XObject.extend(o, defaults);
558 if(o && c && typeof c == 'object'){
566 XObject.extend(XObject,
570 * @property {Boolean} debug XObject debugging. - set to true to debug.
575 * @property {Object} cache - cache of object ids
583 emptyFn : function () { },
589 * @param {String|Object} output String to print.
591 log : function(output)
596 print("LOG:" + output);
601 * @param {String|Object} output String to print.
603 error : function(output)
605 print("ERROR: " + output);
610 * Copies all the properties of config to obj, if the do not exist.
611 * @param {Object} obj The receiver of the properties
612 * @param {Object} config The source of the properties
613 * @return {Object} returns obj
614 * @member Object extendIf
618 extendIf : function(o, c)
621 if(!o || !c || typeof c != 'object'){
625 if (typeof(o[p]) != 'undefined') {
636 * Extends one class with another class and optionally overrides members with the passed literal. This class
637 * also adds the function "override()" to the class that can be used to override
638 * members on an instance.
641 * MyObject = Object.define(
645 * parentClass, // or Object
647 * ... methods and properties.
650 * @param {Function} constructor The class inheriting the functionality
651 * @param {Object} superclass The class being extended
652 * @param {Object} overrides (optional) A literal with members
653 * @return {Function} constructor (eg. class
659 var io = function(o){
664 return function(constructor, parentClass, overrides) {
665 if (typeof(parentClass) == 'undefined') {
666 XObject.error("XObject.define: Missing parentClass: when applying: " );
667 XObject.error(new String(constructor));
670 if (typeof(parentClass.prototype) == 'undefined') {
671 XObject.error("Missing protype: when applying: " );
672 XObject.error(new String(constructor));
673 XObject.error(new String(parentClass));
676 var F = function(){};
678 var spp = parentClass.prototype;
681 sbp = constructor.prototype = new F();
682 sbp.constructor=constructor;
683 constructor.superclass=spp;
686 if(spp.constructor == Object.prototype.constructor){
687 spp.constructor=parentClass;
690 constructor.override = function(o){
691 Object.extend(constructor.prototype, o);
694 XObject.extend(constructor.prototype, overrides);
701 * returns a list of keys of the object.
702 * @param {Object} obj object to inspect
703 * @return {Array} returns list of kyes
704 * @member XObject keys
715 * return the Gobject name of a constructor
716 * @param {Object} gobject ctr
717 * @return {String} returns name
718 * @member XObject keys
722 if (typeof(cfg.xtype) == 'object') {
723 return GObject.type_name(cfg.xtype.type);
724 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
729 * @member XObject createDelegate
730 * creates a delage metdhod
731 * @param {Function} method to wrap
732 * @param {Object} scope
733 * @param {Array} args to add
734 * @param {Boolean|Number} append arguments or replace after N arguments.
735 * @return {Function} returns the delegate
738 createDelegate : function(method, obj, args, appendArgs){
741 XObject.log("CALL: " + obj.id + ':'+ method.id);
743 var callArgs = args || arguments;
744 if(appendArgs === true){
745 callArgs = Array.prototype.slice.call(arguments, 0);
746 callArgs = callArgs.concat(args);
747 }else if(typeof appendArgs == "number"){
748 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
749 var applyArgs = [appendArgs, 0].concat(args); // create method call params
750 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
752 return method.apply(obj || window, callArgs);