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;
72 var base = XObject.baseXObject(cfg);
74 XObject.extend(this,base.prototype);
79 // copy down all elements into self..
80 // make an extra copy in this.config?? - which is the one used in the constructor later
84 if (typeof(cfg[i]) == 'function') { // do we skip objects.
87 // these properties are not copied to cfg.
98 this.config[i] = cfg[i];
102 this.items = this.items || [];
105 // pack can be false!
106 if (typeof(this.pack) == 'undefined') {
108 this.pack = [ 'add' ]
110 var Gtk = imports.gi.Gtk;
113 case (this.xtype == Gtk.MenuItem): this.pack = [ 'append' ]; break;
120 // interesting question should we call constructor on items here...
121 // as the real work is done in init anyway..
125 this.items.forEach(function(i) {
129 // create XObject for all the children.
130 items.forEach(function(i,n) {
131 var base = XObject.baseXObject(i);
132 base = base || XObject;
133 var item = (i.constructor == XObject) ? i : new base(i);
135 _this.items.push(item);
144 XObject.prototype = {
146 * @property el {GObject} the Gtk / etc. element.
150 * @property items {Array} list of sub elements
153 * @property parent {XObject} parent Element
157 * @property config {Object} the construction configuration.
161 * Initializes the Element (el) hooks up all the listeners
162 * and packs the children.
163 * you can override this, in child objects, then
164 * do this to do thi initaliztion.
166 * XObject.prototype.init.call(this);
173 //this.items.forEach(function(i) {
177 this.listeners = this.listeners || {};
180 // do we need to call 'beforeInit here?'
183 //if ((this.xtype == 'Include')) {
184 // o = this.pre_registry[cls];
186 var isSeed = typeof(Seed) != 'undefined';
188 // xtype= Gtk.Menu ?? what about c_new stuff?
189 XObject.log("init: ID:"+ this.id +" typeof(xtype): " + typeof(this.xtype));
190 if (!this.el && typeof(this.xtype) == 'function') {
191 XObject.log("func?" + XObject.keys(this.config).join(','));
192 this.el = this.xtype(this.config);
195 if (!this.el && typeof(this.xtype) == 'object') {
196 XObject.log("obj?" + XObject.keys(this.config).join(','));
197 this.el = new (this.xtype)(this.config);
201 if (!this.el && this.xns) {
203 var NS = imports.gi[this.xns];
205 XObject.error('Invalid xns: ' + this.xns, true);
207 constructor = NS[this.xtype];
209 XObject.error('Invalid xtype: ' + this.xns + '.' + this.xtype);
211 this.el = isSeed ? new constructor(this.config) : new constructor();
214 XObject.log("init: ID:"+ this.id +" typeof(el):" + this.el);
216 // always overlay props..
217 // check for 'write' on object..
219 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
221 var gi = GIRepository.IRepository.get_default();
222 var ty = gi.find_by_gtype(this.xtype.type);
224 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
225 var p = GIRepository.object_info_get_property(ty,i);
226 if (GIRepository.property_info_get_flags(p) & 2) {
227 write.push(GIRepository.base_info_get_name(p));
230 XObject.writeablePropsCache[this.xtype.type] = write;
231 print(write.join(", "));
237 for (var i in this.config) {
238 if (i == 'type') { // problem with Gtk.Window... - not decided on a better way to handle this.
241 if (i == 'buttons') { // problem with Gtk.MessageDialog..
244 if (i[0] == '.') { // parent? -
247 this.el[i] = this.config[i];
251 //if (o.xnsid && o.id) {
252 // XObject.registry = XObject.registry || { };
253 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
254 // XObject.registry[o.xnsid][o.id] = this;
257 var type = this.xtype.type ? GObject.type_name(this.xtype.type) : '';
258 XObject.log("add children to " + type);
261 this.items.forEach(function(i,n) {
266 for (var i in this.listeners) {
267 this.addListener(i, this.listeners[i]);
270 this.init = XObject.emptyFn;
272 // delete this.listeners ?
273 // do again so child props work!
275 // do we need to call 'init here?'
281 * Adds an item to the object using a new XObject
282 * uses pack property to determine how to add it.
283 * @arg cfg {Object} same as XObject constructor.
285 addItem : function(item, pos)
288 if (typeof(item) == 'undefined') {
289 XObject.error("Invalid Item added to this!");
290 imports.console.dump(this.cfg);
293 // what about extended items!?!?!?
296 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
297 // print("addItem - call init [" + item.pack.join(',') + ']');
299 XObject.error("NO EL!");
300 imports.console.dump(item);
303 print(XObject.type(this.xtype) + ":pack=" + item.pack);
305 if (item.pack===false) { // no packing.. various items have this ..
309 if (typeof(item.pack) == 'function') { // pack is a function..
311 item.pack.apply(item, [ this , item ]);
319 if (typeof(item.pack) == 'string') {
321 item.pack.split(',').forEach(function(e, i) {
323 if (e == 'false') { args.push( false); return; }
324 if (e == 'true') { args.push( true); return; }
325 if (!isNaN(parseInt(e))) { args.push( parseInt(e)); return; }
328 //print(args.join(","));
330 pack_m = args.shift();
332 pack_m = item.pack.shift();
337 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
340 name: "ArgumentError",
341 message : 'pack method not available : ' + this.id + " : " + this.xtype + '.' + pack_m + " ADDING " + item.el
349 // finally call the pack method
350 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
352 args.unshift(item.el);
358 XObject.log(pack_m + '[' + args.join(',') +']');
359 //Seed.print('args: ' + args.length);
361 this.el[pack_m].apply(this.el, args);
368 * @method addListener
369 * Connects a method to a signal. (gjs/Seed aware)
371 * @arg sig {String} name of signal
372 * @arg fn {Function} handler.
374 addListener : function(sig, fn)
377 XObject.log("Add signal " + sig);
379 var _li = XObject.createDelegate(fn,this);
380 // private listeners that are not copied to GTk.
382 if (typeof(Seed) != 'undefined') {
383 // Seed.print(typeof(_li));
384 this.el.signal[sig].connect(_li);
386 this.el.connect( sig, _li);
393 * Finds an object in the child elements using xid of object.
394 * prefix with '.' to look up the tree..
395 * prefix with multiple '..' to look further up..
396 * prefix with '/' to look from the top, eg. '^LeftTree.model'
398 * @arg name {String} name of signal
399 * @return {XObject|false} the object if found.
403 XObject.log("SEARCH FOR " + xid + " in " + this.id);
408 name: "ArgumentError",
409 message : "ID not found : empty id"
414 return this.parent.get(xid.substring(1));
418 if (typeof(XObject.cache[xid]) != 'undefined') {
419 return XObject.cache[xid];
421 if (xid.indexOf('.') > -1) {
423 var child = xid.split('.');
424 var nxid = child.shift();
426 child = child.join('.');
427 if (typeof(XObject.cache[nxid]) != 'undefined') {
428 return XObject.cache[nxid].get(child);
439 ret = e.get(xid.substring(1));
444 name: "ArgumentError",
445 message : "ID not found : " + oid
448 XObject.cache[xid] = ret;
449 return XObject.cache[xid];
453 if (xid.indexOf('.') > -1) {
454 child = xid.split('.');
457 child = child.join('.');
460 if (xid == this.id) {
462 return child === false ? this : this.get(child);
465 name: "ArgumentError",
466 message : "ID not found : " + oid
473 this.items.forEach(function(ch) {
483 return child === false ? ret : ret.get(child);
486 name: "ArgumentError",
487 message : "ID not found : " + oid
494 this.items.forEach(function(ch) {
499 XObject.error("invalid item...");
500 imports.console.dump(_this);
511 name: "ArgumentError",
512 message : "ID not found : " + oid
516 return child === false ? ret : ret.get(child);
519 name: "ArgumentError",
520 message : "ID not found : " + oid
530 * Copies all the properties of config to obj.
532 * Pretty much the same as JQuery/Prototype.. or Roo.apply
533 * @param {Object} obj The receiver of the properties
534 * @param {Object} config The source of the properties
535 * @param {Object} defaults A different object that will also be applied for default values
536 * @return {Object} returns obj
537 * @member XObject extend
541 XObject.extend = function(o, c, defaults){
543 // no "this" reference for friendly out of scope calls
544 XObject.extend(o, defaults);
546 if(o && c && typeof c == 'object'){
554 XObject.extend(XObject,
558 * @property {Boolean} debug XObject debugging. - set to true to debug.
563 * @property {Object} cache - cache of object ids
571 emptyFn : function () { },
577 * @param {String|Object} output String to print.
579 log : function(output)
584 print("LOG:" + output);
589 * @param {String|Object} output String to print.
591 error : function(output)
593 print("ERROR: " + output);
597 * @param {String|Object} output String to print.
599 fatal : function(output)
603 name: "ArgumentError",
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 - does not appear to work on structs..
716 * @param {Object} gobject ctr
717 * @return {String} returns name
718 * @member XObject type
722 if (typeof(o) == 'object') {
723 return GObject.type_name(o.type);
724 // print("GNAME:" +gname + " GTYPE:"+cfg.xtype.type);
729 * return the XObjectBase class for a cfg (which includes an xtype)
730 * @param {Object} configuration.
731 * @return {function} constructor
732 * @member XObject baseXObject
734 baseXObject : function(cfg)
737 // loocks for XObject/Gtk/TreeView.js [ TreeView = { .... } ]
738 // xns is not a string!!!?
740 if (typeof(cfg.xtype) == 'object') {
741 gname = XObject.type(cfg.xtype);
744 print("TRYING BASE OBJECT : " + gname);
745 // in the situation where we have been called and there is a base object
746 // defining the behavior..
747 // then we should copy the prototypes from the base object into this..
748 var base = gname ? imports.XObjectBase[gname][gname] : false;
753 XObject.log("error finding " + gname + " - " + e.toString());
761 * @member XObject createDelegate
762 * creates a delage metdhod
763 * @param {Function} method to wrap
764 * @param {Object} scope
765 * @param {Array} args to add
766 * @param {Boolean|Number} append arguments or replace after N arguments.
767 * @return {Function} returns the delegate
770 createDelegate : function(method, obj, args, appendArgs){
773 XObject.log("CALL: " + obj.id + ':'+ method.id);
775 var callArgs = args || arguments;
776 if(appendArgs === true){
777 callArgs = Array.prototype.slice.call(arguments, 0);
778 callArgs = callArgs.concat(args);
779 }else if(typeof appendArgs == "number"){
780 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
781 var applyArgs = [appendArgs, 0].concat(args); // create method call params
782 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
784 return method.apply(obj || window, callArgs);