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.
32 * @arg xtype {String|Function} constructor or string.
33 * @arg id {String} (optional) id for registry
34 * @arg xns {String|Object} (optional) namespace eg. Gtk or 'Gtk' - used with xtype.
35 * @arg items {Array} (optional) list of child elements which will be constructed.. using XObject
36 * @arg listeners {Object} (optional) map Gobject signals to functions
37 * @arg pack {Function|String|Array} (optional) how this object gets added to it's parent
38 * @arg el {Object} (optional) premade GObject
40 * --- needs a xdebug option!
43 * He's some questions.
44 * - should we generate ID's for all elements? (if so we probably need to garbage collect)
45 * - should we have a special property to use as the constructor / gobject.properties rather
46 * than sending all basic types to this?
51 function XObject (cfg) {
52 // first apply cfg if set.
54 //print("new XOBJECT!!!");
56 this.init = cfg.init; // override!
58 this.constructor = XObject;
67 * @property el {GObject} the Gtk / etc. element.
71 * @property items {Array} list of sub elements
74 * @property parent {XObject} parent Element
78 * @property config {Object} the construction configuration.
82 * Initializes the Element (el) hooks up all the listeners
83 * and packs the children.
84 * you can override this, in child objects, then
85 * do this to do thi initaliztion.
87 * XObject.prototype.init.call(this);
92 var cfg = this.config;
94 print("init:XOBJECT?" + XObject.keys(cfg).join(','));
98 cfg.items = cfg.items || [];
100 XObject.extend(o, cfg); // copy everything into o.
102 this.pack = typeof(cfg.pack) == 'undefined' ? ['add'] : cfg.pack;
104 XObject.extend(this, o);
108 this.listeners = this.listeners || {};
111 // remove objects/functions from o, so they can be sent to the contructor.
113 if ((typeof(o[i]) == 'object') ||
114 (typeof(o[i]) == 'function') ||
125 // do we need to call 'beforeInit here?'
128 //if ((this.xtype == 'Include')) {
129 // o = this.pre_registry[cls];
131 var isSeed = typeof(Seed) != 'undefined';
133 // xtype= Gtk.Menu ?? what about c_new stuff?
134 print("init: typeof(xtype): " + typeof(this.xtype));
135 if (!this.el && typeof(this.xtype) == 'function') {
136 print("func?" + XObject.keys(o).join(','));
137 this.el = this.xtype(o);
139 if (!this.el && typeof(this.xtype) == 'object') {
140 print("obj?" + XObject.keys(o).join(','));
141 this.el = new (this.xtype)(o);
144 if (!this.el && o.xns) {
146 var NS = imports.gi[o.xns];
148 Seed.print('Invalid xns: ' + o.xns);
150 constructor = NS[o.xtype];
152 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
154 this.el = isSeed ? new constructor(o) : new constructor();
157 print("init: typeof(el):" + typeof(this.el));
159 // always overlay props..
160 // check for 'write' on object..
162 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
164 var gi = GIRepository.IRepository.get_default();
165 var ty = gi.find_by_gtype(this.xtype.type);
167 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
168 var p = GIRepository.object_info_get_property(ty,i);
169 if (GIRepository.property_info_get_flags(p) & 2) {
170 write.push(GIRepository.base_info_get_name(p));
173 XObject.writeablePropsCache[this.xtype.type] = write;
174 print(write.join(", "));
180 // only write to writable properties
181 // if (XObject.writeablePropsCache[this.xtype.type].indexOf(i) < 0) {
187 //if (o.xnsid && o.id) {
188 // XObject.registry = XObject.registry || { };
189 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
190 // XObject.registry[o.xnsid][o.id] = this;
193 cfg.items.forEach(function(i) {
198 for (var i in this.listeners) {
199 this.addListener(i, this.listeners[i]);
201 // delete this.listeners ?
204 // do we need to call 'init here?'
210 * Adds an item to the object using a new XObject
211 * uses pack property to determine how to add it.
212 * @arg cfg {Object} same as XObject constructor.
214 addItem : function(o) {
217 var item = (o.constructor == XObject) ? o : new XObject(o);
220 //print("CTR:PROTO:" + ( item.id ? item.id : '??'));
221 // print("addItem - call init [" + item.pack.join(',') + ']');
224 imports.console.dump(item);
229 this.items.push(item);
231 if (item.pack===false) { // no
234 if (typeof(item.pack) == 'function') {
236 item.pack.apply(o, [ o , o.items[i] ]);
242 if (typeof(item.pack) == 'string') {
245 pack_m = item.pack.shift();
250 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
251 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
256 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
258 args.unshift(item.el);
259 print('[' + args.join(',') +']');
260 //Seed.print('args: ' + args.length);
262 this.el[pack_m].apply(this.el, args);
269 * @method addListener
270 * Connects a method to a signal. (gjs/Seed aware)
272 * @arg sig {String} name of signal
273 * @arg fn {Function} handler.
275 addListener : function(sig, fn)
278 Seed.print("Add signal " + sig);
280 var _li = XObject.createDelegate(fn,this);
281 // private listeners that are not copied to GTk.
283 if (typeof(Seed) != 'undefined') {
284 // Seed.print(typeof(_li));
285 this.el.signal[sig].connect(_li);
287 this.el.connect( sig, _li);
294 * Finds an object in the child elements using xid of object.
295 * prefix with '.' to look up the tree.. multiple '..' to look further up..
297 * @arg name {String} name of signal
298 * @return {XObject|false} the object if found.
304 return this.parent.get(xid.substring(1));
308 this.items.forEach(function(ch) {
318 this.items.forEach(function(ch) {
331 XObject.writeablePropsCache = { };
334 * Copies all the properties of config to obj.
336 * Pretty much the same as JQuery/Prototype..
337 * @param {Object} obj The receiver of the properties
338 * @param {Object} config The source of the properties
339 * @param {Object} defaults A different object that will also be applied for default values
340 * @return {Object} returns obj
341 * @member XObject extend
345 XObject.extend = function(o, c, defaults){
347 // no "this" reference for friendly out of scope calls
348 XObject.extend(o, defaults);
350 if(o && c && typeof c == 'object'){
358 XObject.extend(XObject,
361 * Copies all the properties of config to obj, if the do not exist.
362 * @param {Object} obj The receiver of the properties
363 * @param {Object} config The source of the properties
364 * @return {Object} returns obj
365 * @member Object extendIf
369 extendIf : function(o, c){
371 if(!o || !c || typeof c != 'object'){
375 if (typeof(o[p]) != 'undefined') {
386 * Extends one class with another class and optionally overrides members with the passed literal. This class
387 * also adds the function "override()" to the class that can be used to override
388 * members on an instance.
391 * MyObject = Object.define(
395 * parentClass, // or Object
397 * ... methods and properties.
400 * @param {Function} constructor The class inheriting the functionality
401 * @param {Object} superclass The class being extended
402 * @param {Object} overrides (optional) A literal with members
403 * @return {Function} constructor (eg. class
408 var io = function(o){
413 return function(constructor, parentClass, overrides) {
414 if (typeof(parentClass) == 'undefined') {
415 print("XObject.define: Missing parentClass: when applying: " );
416 print(new String(constructor));
419 if (typeof(parentClass.prototype) == 'undefined') {
420 print("Missing protype: when applying: " );
421 print(new String(constructor));
422 print(new String(parentClass));
425 var F = function(){};
427 var spp = parentClass.prototype;
430 sbp = constructor.prototype = new F();
431 sbp.constructor=constructor;
432 constructor.superclass=spp;
435 if(spp.constructor == Object.prototype.constructor){
436 spp.constructor=parentClass;
439 constructor.override = function(o){
440 Object.extend(constructor.prototype, o);
443 XObject.extend(constructor.prototype, overrides);
450 * returns a list of keys of the object.
451 * @param {Object} obj object to inspect
452 * @return {Array} returns list of kyes
453 * @member XObject keys
465 * @member XObject createDelegate
466 * creates a delage metdhod
467 * @param {Function} method to wrap
468 * @param {Object} scope
469 * @param {Array} args to add
470 * @param {Boolean|Number} append arguments or replace after N arguments.
471 * @return {Function} returns the delegate
474 createDelegate : function(method, obj, args, appendArgs){
477 var callArgs = args || arguments;
478 if(appendArgs === true){
479 callArgs = Array.prototype.slice.call(arguments, 0);
480 callArgs = callArgs.concat(args);
481 }else if(typeof appendArgs == "number"){
482 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
483 var applyArgs = [appendArgs, 0].concat(args); // create method call params
484 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
486 return method.apply(obj || window, callArgs);