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.constructor = XObject;
66 // start by seeing if we have a base class....
68 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
69 // xns is not a string!!!?
71 if (typeof(cfg.xtype) == 'object') {
72 gname = GObject.type_name(cfg.xtype.type);
73 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
76 var base = gname ? imports.XObjectBase[gname][gname] : false;
78 // print("Overlaying XOBJBECT-BASE." + cfg.xtype);
79 XObject.extend(this,base);
84 print("error finding " + gname + " - " + e.toString());
88 // copy down all elements into self..
92 if (typeof(cfg[i]) == 'function') { // do we skip objects.
95 // these properties are not copied to cfg.
106 this.config[i] = cfg[i];
110 this.items = this.items || [];
113 // pack can be false!
114 if (typeof(this.pack) == 'undefined') {
116 this.pack = [ 'add' ]
118 var Gtk = imports.gi.Gtk;
121 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
128 // interesting question should we call constructor on items here...
129 // as the real work is done in init anyway..
133 this.items.forEach(function(i) {
137 items.forEach(function(i,n) {
140 if (type == 'GtkTable' && i.pack == 'add') {
141 var c = n % _this.config.n_columns;
142 var r = Math.floor(n/_this.config.n_columns);
143 i.pack = [ 'attach', c, c+1, r, r+1,
144 typeof(i.x_options) == 'undefined' ? 5 : i.x_options,
145 typeof(i.y_options) == 'undefined' ? 5 : i.y_options,
146 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding,
147 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding
152 var item = (i.constructor == XObject) ? o : new XObject(i);
154 _this.items.push(item);
157 if (this.onConstruct) {
158 this.onConstruct.call(this);
165 XObject.prototype = {
167 * @property el {GObject} the Gtk / etc. element.
171 * @property items {Array} list of sub elements
174 * @property parent {XObject} parent Element
178 * @property config {Object} the construction configuration.
182 * Initializes the Element (el) hooks up all the listeners
183 * and packs the children.
184 * you can override this, in child objects, then
185 * do this to do thi initaliztion.
187 * XObject.prototype.init.call(this);
194 //this.items.forEach(function(i) {
198 this.listeners = this.listeners || {};
201 // do we need to call 'beforeInit here?'
204 //if ((this.xtype == 'Include')) {
205 // o = this.pre_registry[cls];
207 var isSeed = typeof(Seed) != 'undefined';
209 // xtype= Gtk.Menu ?? what about c_new stuff?
210 if (XObject.debug) print("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
211 if (!this.el && typeof(this.xtype) == 'function') {
212 if (XObject.debug) print("func?" + XObject.keys(this.config).join(','));
213 this.el = this.xtype(this.config);
216 if (!this.el && typeof(this.xtype) == 'object') {
217 if (XObject.debug) print("obj?" + XObject.keys(this.config).join(','));
218 this.el = new (this.xtype)(this.config);
222 if (!this.el && this.xns) {
224 var NS = imports.gi[this.xns];
226 Seed.print('Invalid xns: ' + this.xns);
228 constructor = NS[this.xtype];
230 Seed.print('Invalid xtype: ' + this.xns + '.' + this.xtype);
232 this.el = isSeed ? new constructor(this.config) : new constructor();
235 if (XObject.debug) print("init: ID:"+ this.id +" typeof(el):" + this.el);
237 // always overlay props..
238 // check for 'write' on object..
240 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
242 var gi = GIRepository.IRepository.get_default();
243 var ty = gi.find_by_gtype(this.xtype.type);
245 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
246 var p = GIRepository.object_info_get_property(ty,i);
247 if (GIRepository.property_info_get_flags(p) & 2) {
248 write.push(GIRepository.base_info_get_name(p));
251 XObject.writeablePropsCache[this.xtype.type] = write;
252 print(write.join(", "));
258 for (var i in this.config) {
259 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
262 if (i == 'buttons') { // problem with Gtk.MessageDialog..
265 if (i[0] == '.') { // parent? -
268 this.el[i] = this.config[i];
272 //if (o.xnsid && o.id) {
273 // XObject.registry = XObject.registry || { };
274 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
275 // XObject.registry[o.xnsid][o.id] = this;
278 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
279 print("add children to " + type);
282 this.items.forEach(function(i,n) {
287 for (var i in this.listeners) {
288 this.addListener(i, this.listeners[i]);
291 this.init = XObject.emptyFn;
293 // delete this.listeners ?
294 // do again so child props work!
296 // do we need to call 'init here?'
302 * Adds an item to the object using a new XObject
303 * uses pack property to determine how to add it.
304 * @arg cfg {Object} same as XObject constructor.
306 addItem : function(item, pos)
309 if (typeof(item) == 'undefined') {
310 print("Invalid Item added to this!");
311 imports.console.dump(this);
314 // what about extended items!?!?!?
317 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
318 // print("addItem - call init [" + item.pack.join(',') + ']');
321 imports.console.dump(item);
326 if (item.pack===false) { // no packing.. various items have this ..
330 if (typeof(item.pack) == 'function') { // pack is a function..
332 item.pack.apply(item, [ this , item ]);
340 if (typeof(item.pack) == 'string') {
342 item.pack.split(',').forEach(function(e, i) {
344 if (e == 'false') { args.push( false); return; }
345 if (e == 'true') { args.push( true); return; }
346 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
349 //print(args.join(","));
351 pack_m = args.shift();
353 pack_m = item.pack.shift();
358 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
361 name: "ArgumentError",
362 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
370 // finally call the pack method
371 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
373 args.unshift(item.el);
377 if (this.parent && this.parent.xtype == 'GtkTable' && item.pack == 'add') {
378 var c = n % this.parent.config.n_columns;
379 var r = Math.floor(pos/_this.parent.config.n_columns);
380 item.pack = [ 'attach', c, c+1, r, r+1,
381 typeof(item.x_options) == 'undefined' ? 5 : item.x_options,
382 typeof(item.y_options) == 'undefined' ? 5 : item.y_options,
383 typeof(item.x_padding) == 'undefined' ? 0 : item.x_padding,
384 typeof(item.x_padding) == 'undefined' ? 0 : item.x_padding
392 if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
393 //Seed.print('args: ' + args.length);
395 this.el[pack_m].apply(this.el, args);
402 * @method addListener
403 * Connects a method to a signal. (gjs/Seed aware)
405 * @arg sig {String} name of signal
406 * @arg fn {Function} handler.
408 addListener : function(sig, fn)
411 if (XObject.debug) Seed.print("Add signal " + sig);
413 var _li = XObject.createDelegate(fn,this);
414 // private listeners that are not copied to GTk.
416 if (typeof(Seed) != 'undefined') {
417 // Seed.print(typeof(_li));
418 this.el.signal[sig].connect(_li);
420 this.el.connect( sig, _li);
427 * Finds an object in the child elements using xid of object.
428 * prefix with '.' to look up the tree..
429 * prefix with multiple '..' to look further up..
430 * prefix with '/' to look from the top, eg. '^LeftTree.model'
432 * @arg name {String} name of signal
433 * @return {XObject|false} the object if found.
437 if (XObject.debug) print("SEARCH FOR " + xid + " in " + this.id);
442 name: "ArgumentError",
443 message : "ID not found : empty id"
448 return this.parent.get(xid.substring(1));
452 if (typeof(XObject.cache[xid]) != 'undefined') {
453 return XObject.cache[xid];
455 if (xid.indexOf('.') > -1) {
457 var child = xid.split('.');
458 var nxid = child.shift();
460 child = child.join('.');
461 if (typeof(XObject.cache[nxid]) != 'undefined') {
462 return XObject.cache[nxid].get(child);
473 ret = e.get(xid.substring(1));
478 name: "ArgumentError",
479 message : "ID not found : " + oid
482 XObject.cache[xid] = ret;
483 return XObject.cache[xid];
487 if (xid.indexOf('.') > -1) {
488 child = xid.split('.');
491 child = child.join('.');
494 if (xid == this.id) {
496 return child === false ? this : this.get(child);
499 name: "ArgumentError",
500 message : "ID not found : " + oid
507 this.items.forEach(function(ch) {
517 return child === false ? ret : ret.get(child);
520 name: "ArgumentError",
521 message : "ID not found : " + oid
528 this.items.forEach(function(ch) {
533 print("invalid item...");
534 imports.console.dump(_this);
545 name: "ArgumentError",
546 message : "ID not found : " + oid
550 return child === false ? ret : ret.get(child);
553 name: "ArgumentError",
554 message : "ID not found : " + oid
564 * Copies all the properties of config to obj.
566 * Pretty much the same as JQuery/Prototype.. or Roo.apply
567 * @param {Object} obj The receiver of the properties
568 * @param {Object} config The source of the properties
569 * @param {Object} defaults A different object that will also be applied for default values
570 * @return {Object} returns obj
571 * @member XObject extend
575 XObject.extend = function(o, c, defaults){
577 // no "this" reference for friendly out of scope calls
578 XObject.extend(o, defaults);
580 if(o && c && typeof c == 'object'){
588 XObject.extend(XObject,
592 * @property {Boolean} debug XObject debugging. - set to true to debug.
597 * @property {Object} cache - cache of object ids
605 emptyFn : function () { },
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 print("XObject.define: Missing parentClass: when applying: " );
665 print(new String(constructor));
668 if (typeof(parentClass.prototype) == 'undefined') {
669 print("Missing protype: when applying: " );
670 print(new String(constructor));
671 print(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
714 * @member XObject createDelegate
715 * creates a delage metdhod
716 * @param {Function} method to wrap
717 * @param {Object} scope
718 * @param {Array} args to add
719 * @param {Boolean|Number} append arguments or replace after N arguments.
720 * @return {Function} returns the delegate
723 createDelegate : function(method, obj, args, appendArgs){
726 if (XObject.debug) print("CALL: " + obj.id + ':'+ method.id);
728 var callArgs = args || arguments;
729 if(appendArgs === true){
730 callArgs = Array.prototype.slice.call(arguments, 0);
731 callArgs = callArgs.concat(args);
732 }else if(typeof appendArgs == "number"){
733 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
734 var applyArgs = [appendArgs, 0].concat(args); // create method call params
735 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
737 return method.apply(obj || window, callArgs);