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 this.constructor = XObject;
63 // start by seeing if we have a base class....
65 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
66 var base = imports.XObject[cfg.xns][cfg.xtype][cfg.xtype];
76 // copy down all elements into self..
80 if (typeof(cfg[i]) == 'function') { // do we skip objects.
83 // these properties are not copied to cfg.
94 this.config[i] = cfg[i];
98 this.items = this.items || [];
101 // pack can be false!
102 if (typeof(this.pack) == 'undefined') {
104 this.pack = [ 'add' ]
106 var Gtk = imports.gi.Gtk;
109 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
125 XObject.prototype = {
127 * @property el {GObject} the Gtk / etc. element.
131 * @property items {Array} list of sub elements
134 * @property parent {XObject} parent Element
138 * @property config {Object} the construction configuration.
142 * Initializes the Element (el) hooks up all the listeners
143 * and packs the children.
144 * you can override this, in child objects, then
145 * do this to do thi initaliztion.
147 * XObject.prototype.init.call(this);
154 this.items.forEach(function(i) {
158 this.listeners = this.listeners || {};
161 // do we need to call 'beforeInit here?'
164 //if ((this.xtype == 'Include')) {
165 // o = this.pre_registry[cls];
167 var isSeed = typeof(Seed) != 'undefined';
169 // xtype= Gtk.Menu ?? what about c_new stuff?
170 if (XObject.debug) print("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
171 if (!this.el && typeof(this.xtype) == 'function') {
172 if (XObject.debug) print("func?" + XObject.keys(this.config).join(','));
173 this.el = this.xtype(this.config);
176 if (!this.el && typeof(this.xtype) == 'object') {
177 if (XObject.debug) print("obj?" + XObject.keys(this.config).join(','));
178 this.el = new (this.xtype)(this.config);
182 if (!this.el && this.xns) {
184 var NS = imports.gi[this.xns];
186 Seed.print('Invalid xns: ' + this.xns);
188 constructor = NS[this.xtype];
190 Seed.print('Invalid xtype: ' + this.xns + '.' + this.xtype);
192 this.el = isSeed ? new constructor(this.config) : new constructor();
195 if (XObject.debug) print("init: ID:"+ this.id +" typeof(el):" + this.el);
197 // always overlay props..
198 // check for 'write' on object..
200 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
202 var gi = GIRepository.IRepository.get_default();
203 var ty = gi.find_by_gtype(this.xtype.type);
205 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
206 var p = GIRepository.object_info_get_property(ty,i);
207 if (GIRepository.property_info_get_flags(p) & 2) {
208 write.push(GIRepository.base_info_get_name(p));
211 XObject.writeablePropsCache[this.xtype.type] = write;
212 print(write.join(", "));
218 for (var i in this.config) {
219 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
222 if (i == 'buttons') { // problem with Gtk.MessageDialog..
225 if (i[0] == '.') { // parent? -
228 this.el[i] = this.config[i];
232 //if (o.xnsid && o.id) {
233 // XObject.registry = XObject.registry || { };
234 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
235 // XObject.registry[o.xnsid][o.id] = this;
238 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
239 print("MAKE " + type);
243 items.forEach(function(i,n) {
245 if (type == 'GtkTable' && i.pack == 'add') {
246 var c = n % _this.config.n_columns;
247 var r = Math.floor(n/_this.config.n_columns);
248 i.pack = [ 'attach', c, c+1, r, r+1,
249 typeof(i.x_options) == 'undefined' ? 5 : i.x_options,
250 typeof(i.y_options) == 'undefined' ? 5 : i.y_options,
251 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding,
252 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding
261 for (var i in this.listeners) {
262 this.addListener(i, this.listeners[i]);
265 // delete this.listeners ?
266 // do again so child props work!
268 // do we need to call 'init here?'
274 * Adds an item to the object using a new XObject
275 * uses pack property to determine how to add it.
276 * @arg cfg {Object} same as XObject constructor.
278 addItem : function(o) {
279 if (typeof(o) == 'undefined') {
280 print("Invalid Item added to this!");
281 imports.console.dump(this);
284 // what about extended items!?!?!?
285 var item = (o.constructor == XObject) ? o : new XObject(o);
287 this.items.push(item);
289 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
290 // print("addItem - call init [" + item.pack.join(',') + ']');
293 imports.console.dump(item);
298 if (item.pack===false) { // no
301 if (typeof(item.pack) == 'function') {
303 item.pack.apply(item, [ this , item ]);
309 if (typeof(item.pack) == 'string') {
311 item.pack.split(',').forEach(function(e, i) {
313 if (e == 'false') { args.push( false); return; }
314 if (e == 'true') { args.push( true); return; }
315 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
318 //print(args.join(","));
320 pack_m = args.shift();
322 pack_m = item.pack.shift();
327 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
330 name: "ArgumentError",
331 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
342 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
344 args.unshift(item.el);
345 if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
346 //Seed.print('args: ' + args.length);
348 this.el[pack_m].apply(this.el, args);
355 * @method addListener
356 * Connects a method to a signal. (gjs/Seed aware)
358 * @arg sig {String} name of signal
359 * @arg fn {Function} handler.
361 addListener : function(sig, fn)
364 if (XObject.debug) Seed.print("Add signal " + sig);
366 var _li = XObject.createDelegate(fn,this);
367 // private listeners that are not copied to GTk.
369 if (typeof(Seed) != 'undefined') {
370 // Seed.print(typeof(_li));
371 this.el.signal[sig].connect(_li);
373 this.el.connect( sig, _li);
380 * Finds an object in the child elements using xid of object.
381 * prefix with '.' to look up the tree..
382 * prefix with multiple '..' to look further up..
383 * prefix with '/' to look from the top, eg. '^LeftTree.model'
385 * @arg name {String} name of signal
386 * @return {XObject|false} the object if found.
390 if (XObject.debug) print("SEARCH FOR " + xid + " in " + this.id);
395 name: "ArgumentError",
396 message : "ID not found : empty id"
401 return this.parent.get(xid.substring(1));
405 if (typeof(XObject.cache[xid]) != 'undefined') {
406 return XObject.cache[xid];
408 if (xid.indexOf('.') > -1) {
410 var child = xid.split('.');
411 var nxid = child.shift();
413 child = child.join('.');
414 if (typeof(XObject.cache[nxid]) != 'undefined') {
415 return XObject.cache[nxid].get(child);
426 ret = e.get(xid.substring(1));
431 name: "ArgumentError",
432 message : "ID not found : " + oid
435 XObject.cache[xid] = ret;
436 return XObject.cache[xid];
440 if (xid.indexOf('.') > -1) {
441 child = xid.split('.');
444 child = child.join('.');
447 if (xid == this.id) {
449 return child === false ? this : this.get(child);
452 name: "ArgumentError",
453 message : "ID not found : " + oid
460 this.items.forEach(function(ch) {
470 return child === false ? ret : ret.get(child);
473 name: "ArgumentError",
474 message : "ID not found : " + oid
481 this.items.forEach(function(ch) {
486 print("invalid item...");
487 imports.console.dump(_this);
498 name: "ArgumentError",
499 message : "ID not found : " + oid
503 return child === false ? ret : ret.get(child);
506 name: "ArgumentError",
507 message : "ID not found : " + oid
517 * Copies all the properties of config to obj.
519 * Pretty much the same as JQuery/Prototype.. or Roo.apply
520 * @param {Object} obj The receiver of the properties
521 * @param {Object} config The source of the properties
522 * @param {Object} defaults A different object that will also be applied for default values
523 * @return {Object} returns obj
524 * @member XObject extend
528 XObject.extend = function(o, c, defaults){
530 // no "this" reference for friendly out of scope calls
531 XObject.extend(o, defaults);
533 if(o && c && typeof c == 'object'){
541 XObject.extend(XObject,
545 * @property {Boolean} debug XObject debugging. - set to true to debug.
550 * @property {Object} cache - cache of object ids
556 * Copies all the properties of config to obj, if the do not exist.
557 * @param {Object} obj The receiver of the properties
558 * @param {Object} config The source of the properties
559 * @return {Object} returns obj
560 * @member Object extendIf
564 extendIf : function(o, c)
567 if(!o || !c || typeof c != 'object'){
571 if (typeof(o[p]) != 'undefined') {
582 * Extends one class with another class and optionally overrides members with the passed literal. This class
583 * also adds the function "override()" to the class that can be used to override
584 * members on an instance.
587 * MyObject = Object.define(
591 * parentClass, // or Object
593 * ... methods and properties.
596 * @param {Function} constructor The class inheriting the functionality
597 * @param {Object} superclass The class being extended
598 * @param {Object} overrides (optional) A literal with members
599 * @return {Function} constructor (eg. class
605 var io = function(o){
610 return function(constructor, parentClass, overrides) {
611 if (typeof(parentClass) == 'undefined') {
612 print("XObject.define: Missing parentClass: when applying: " );
613 print(new String(constructor));
616 if (typeof(parentClass.prototype) == 'undefined') {
617 print("Missing protype: when applying: " );
618 print(new String(constructor));
619 print(new String(parentClass));
622 var F = function(){};
624 var spp = parentClass.prototype;
627 sbp = constructor.prototype = new F();
628 sbp.constructor=constructor;
629 constructor.superclass=spp;
632 if(spp.constructor == Object.prototype.constructor){
633 spp.constructor=parentClass;
636 constructor.override = function(o){
637 Object.extend(constructor.prototype, o);
640 XObject.extend(constructor.prototype, overrides);
647 * returns a list of keys of the object.
648 * @param {Object} obj object to inspect
649 * @return {Array} returns list of kyes
650 * @member XObject keys
662 * @member XObject createDelegate
663 * creates a delage metdhod
664 * @param {Function} method to wrap
665 * @param {Object} scope
666 * @param {Array} args to add
667 * @param {Boolean|Number} append arguments or replace after N arguments.
668 * @return {Function} returns the delegate
671 createDelegate : function(method, obj, args, appendArgs){
674 if (XObject.debug) print("CALL: " + obj.id + ':'+ method.id);
676 var callArgs = args || arguments;
677 if(appendArgs === true){
678 callArgs = Array.prototype.slice.call(arguments, 0);
679 callArgs = callArgs.concat(args);
680 }else if(typeof appendArgs == "number"){
681 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
682 var applyArgs = [appendArgs, 0].concat(args); // create method call params
683 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
685 return method.apply(obj || window, callArgs);