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 * He's some questions.
34 * - should we have a special property to use as the constructor / gobject.properties rather
35 * than sending all basic types to this?
37 * @cfg xtype {String|Function} constructor or string.
38 * @cfg id {String} (optional) id for registry
39 * @cfg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
40 * @cfg items {Array} (optional) list of child elements which will be constructed.. using XObject
41 * @cfg listeners {Object} (optional) map Gobject signals to functions
42 * @cfg pack {Function|String|Array} (optional) how this object gets added to it's parent
43 * @cfg el {Object} (optional) premade GObject
52 function XObject (cfg) {
53 // first apply cfg if set.
54 //print("new XOBJECT!!!");
56 this.constructor = XObject;
60 // start by seeing if we have a base class....
62 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
63 var base = imports.XObject[cfg.xns][cfg.xtype][cfg.xtype];
73 // copy down all elements into self..
77 if (typeof(cfg[i]) == 'function') { // do we skip objects.
80 // these properties are not copied to cfg.
91 this.config[i] = cfg[i];
95 this.items = this.items || [];
99 if (typeof(this.pack) == 'undefined') {
101 this.pack = [ 'add' ]
103 var Gtk = imports.gi.Gtk;
106 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
122 XObject.prototype = {
124 * @property el {GObject} the Gtk / etc. element.
128 * @property items {Array} list of sub elements
131 * @property parent {XObject} parent Element
135 * @property config {Object} the construction configuration.
139 * Initializes the Element (el) hooks up all the listeners
140 * and packs the children.
141 * you can override this, in child objects, then
142 * do this to do thi initaliztion.
144 * XObject.prototype.init.call(this);
151 this.items.forEach(function(i) {
155 this.listeners = this.listeners || {};
158 // do we need to call 'beforeInit here?'
161 //if ((this.xtype == 'Include')) {
162 // o = this.pre_registry[cls];
164 var isSeed = typeof(Seed) != 'undefined';
166 // xtype= Gtk.Menu ?? what about c_new stuff?
167 if (XObject.debug) print("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
168 if (!this.el && typeof(this.xtype) == 'function') {
169 if (XObject.debug) print("func?" + XObject.keys(this.config).join(','));
170 this.el = this.xtype(this.config);
173 if (!this.el && typeof(this.xtype) == 'object') {
174 if (XObject.debug) print("obj?" + XObject.keys(this.config).join(','));
175 this.el = new (this.xtype)(this.config);
179 if (!this.el && this.xns) {
181 var NS = imports.gi[this.xns];
183 Seed.print('Invalid xns: ' + this.xns);
185 constructor = NS[this.xtype];
187 Seed.print('Invalid xtype: ' + this.xns + '.' + this.xtype);
189 this.el = isSeed ? new constructor(this.config) : new constructor();
192 if (XObject.debug) print("init: ID:"+ this.id +" typeof(el):" + this.el);
194 // always overlay props..
195 // check for 'write' on object..
197 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
199 var gi = GIRepository.IRepository.get_default();
200 var ty = gi.find_by_gtype(this.xtype.type);
202 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
203 var p = GIRepository.object_info_get_property(ty,i);
204 if (GIRepository.property_info_get_flags(p) & 2) {
205 write.push(GIRepository.base_info_get_name(p));
208 XObject.writeablePropsCache[this.xtype.type] = write;
209 print(write.join(", "));
215 for (var i in this.config) {
216 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
219 if (i == 'buttons') { // problem with Gtk.MessageDialog..
222 if (i[0] == '.') { // parent? -
225 this.el[i] = this.config[i];
229 //if (o.xnsid && o.id) {
230 // XObject.registry = XObject.registry || { };
231 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
232 // XObject.registry[o.xnsid][o.id] = this;
235 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
236 print("MAKE " + type);
240 items.forEach(function(i,n) {
242 if (type == 'GtkTable' && i.pack == 'add') {
243 var c = n % _this.config.n_columns;
244 var r = Math.floor(n/_this.config.n_columns);
245 i.pack = [ 'attach', c, c+1, r, r+1,
246 typeof(i.x_options) == 'undefined' ? 5 : i.x_options,
247 typeof(i.y_options) == 'undefined' ? 5 : i.y_options,
248 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding,
249 typeof(i.x_padding) == 'undefined' ? 0 : i.x_padding
258 for (var i in this.listeners) {
259 this.addListener(i, this.listeners[i]);
262 // delete this.listeners ?
263 // do again so child props work!
265 // do we need to call 'init here?'
271 * Adds an item to the object using a new XObject
272 * uses pack property to determine how to add it.
273 * @arg cfg {Object} same as XObject constructor.
275 addItem : function(o) {
276 if (typeof(o) == 'undefined') {
277 print("Invalid Item added to this!");
278 imports.console.dump(this);
281 // what about extended items!?!?!?
282 var item = (o.constructor == XObject) ? o : new XObject(o);
284 this.items.push(item);
286 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
287 // print("addItem - call init [" + item.pack.join(',') + ']');
290 imports.console.dump(item);
295 if (item.pack===false) { // no
298 if (typeof(item.pack) == 'function') {
300 item.pack.apply(item, [ this , item ]);
306 if (typeof(item.pack) == 'string') {
308 item.pack.split(',').forEach(function(e, i) {
310 if (e == 'false') { args.push( false); return; }
311 if (e == 'true') { args.push( true); return; }
312 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
315 //print(args.join(","));
317 pack_m = args.shift();
319 pack_m = item.pack.shift();
324 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
327 name: "ArgumentError",
328 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
339 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
341 args.unshift(item.el);
342 if (XObject.debug) print(pack_m + '[' + args.join(',') +']');
343 //Seed.print('args: ' + args.length);
345 this.el[pack_m].apply(this.el, args);
352 * @method addListener
353 * Connects a method to a signal. (gjs/Seed aware)
355 * @arg sig {String} name of signal
356 * @arg fn {Function} handler.
358 addListener : function(sig, fn)
361 if (XObject.debug) Seed.print("Add signal " + sig);
363 var _li = XObject.createDelegate(fn,this);
364 // private listeners that are not copied to GTk.
366 if (typeof(Seed) != 'undefined') {
367 // Seed.print(typeof(_li));
368 this.el.signal[sig].connect(_li);
370 this.el.connect( sig, _li);
377 * Finds an object in the child elements using xid of object.
378 * prefix with '.' to look up the tree..
379 * prefix with multiple '..' to look further up..
380 * prefix with '/' to look from the top, eg. '^LeftTree.model'
382 * @arg name {String} name of signal
383 * @return {XObject|false} the object if found.
387 if (XObject.debug) print("SEARCH FOR " + xid + " in " + this.id);
392 name: "ArgumentError",
393 message : "ID not found : empty id"
398 return this.parent.get(xid.substring(1));
402 if (typeof(XObject.cache[xid]) != 'undefined') {
403 return XObject.cache[xid];
405 if (xid.indexOf('.') > -1) {
407 var child = xid.split('.');
408 var nxid = child.shift();
410 child = child.join('.');
411 if (typeof(XObject.cache[nxid]) != 'undefined') {
412 return XObject.cache[nxid].get(child);
423 ret = e.get(xid.substring(1));
428 name: "ArgumentError",
429 message : "ID not found : " + oid
432 XObject.cache[xid] = ret;
433 return XObject.cache[xid];
437 if (xid.indexOf('.') > -1) {
438 child = xid.split('.');
441 child = child.join('.');
444 if (xid == this.id) {
446 return child === false ? this : this.get(child);
449 name: "ArgumentError",
450 message : "ID not found : " + oid
457 this.items.forEach(function(ch) {
467 return child === false ? ret : ret.get(child);
470 name: "ArgumentError",
471 message : "ID not found : " + oid
478 this.items.forEach(function(ch) {
483 print("invalid item...");
484 imports.console.dump(_this);
495 name: "ArgumentError",
496 message : "ID not found : " + oid
500 return child === false ? ret : ret.get(child);
503 name: "ArgumentError",
504 message : "ID not found : " + oid
514 * Copies all the properties of config to obj.
516 * Pretty much the same as JQuery/Prototype.. or Roo.apply
517 * @param {Object} obj The receiver of the properties
518 * @param {Object} config The source of the properties
519 * @param {Object} defaults A different object that will also be applied for default values
520 * @return {Object} returns obj
521 * @member XObject extend
525 XObject.extend = function(o, c, defaults){
527 // no "this" reference for friendly out of scope calls
528 XObject.extend(o, defaults);
530 if(o && c && typeof c == 'object'){
538 XObject.extend(XObject,
542 * @property {Boolean} debug XObject debugging. - set to true to debug.
547 * @property {Object} cache - cache of object ids
553 * Copies all the properties of config to obj, if the do not exist.
554 * @param {Object} obj The receiver of the properties
555 * @param {Object} config The source of the properties
556 * @return {Object} returns obj
557 * @member Object extendIf
561 extendIf : function(o, c){
563 if(!o || !c || typeof c != 'object'){
567 if (typeof(o[p]) != 'undefined') {
578 * Extends one class with another class and optionally overrides members with the passed literal. This class
579 * also adds the function "override()" to the class that can be used to override
580 * members on an instance.
583 * MyObject = Object.define(
587 * parentClass, // or Object
589 * ... methods and properties.
592 * @param {Function} constructor The class inheriting the functionality
593 * @param {Object} superclass The class being extended
594 * @param {Object} overrides (optional) A literal with members
595 * @return {Function} constructor (eg. class
600 var io = function(o){
605 return function(constructor, parentClass, overrides) {
606 if (typeof(parentClass) == 'undefined') {
607 print("XObject.define: Missing parentClass: when applying: " );
608 print(new String(constructor));
611 if (typeof(parentClass.prototype) == 'undefined') {
612 print("Missing protype: when applying: " );
613 print(new String(constructor));
614 print(new String(parentClass));
617 var F = function(){};
619 var spp = parentClass.prototype;
622 sbp = constructor.prototype = new F();
623 sbp.constructor=constructor;
624 constructor.superclass=spp;
627 if(spp.constructor == Object.prototype.constructor){
628 spp.constructor=parentClass;
631 constructor.override = function(o){
632 Object.extend(constructor.prototype, o);
635 XObject.extend(constructor.prototype, overrides);
642 * returns a list of keys of the object.
643 * @param {Object} obj object to inspect
644 * @return {Array} returns list of kyes
645 * @member XObject keys
657 * @member XObject createDelegate
658 * creates a delage metdhod
659 * @param {Function} method to wrap
660 * @param {Object} scope
661 * @param {Array} args to add
662 * @param {Boolean|Number} append arguments or replace after N arguments.
663 * @return {Function} returns the delegate
666 createDelegate : function(method, obj, args, appendArgs){
669 if (XObject.debug) print("CALL: " + obj.id + ':'+ method.id);
671 var callArgs = args || arguments;
672 if(appendArgs === true){
673 callArgs = Array.prototype.slice.call(arguments, 0);
674 callArgs = callArgs.concat(args);
675 }else if(typeof appendArgs == "number"){
676 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
677 var applyArgs = [appendArgs, 0].concat(args); // create method call params
678 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
680 return method.apply(obj || window, callArgs);