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.
55 this.init = cfg.init; // override!
65 * @property el {GObject} the Gtk / etc. element.
69 * @property items {Array} list of sub elements
72 * @property parent {XObject} parent Element
76 * @property config {Object} the construction configuration.
80 * Initializes the Element (el) hooks up all the listeners
81 * and packs the children.
82 * you can override this, in child objects, then
83 * do this to do thi initaliztion.
85 * XObject.prototype.init.call(this);
90 var cfg = this.config;
92 print("new xobj?" + XObject.keys(cfg).join(','));
96 cfg.items = cfg.items || [];
98 XObject.extend(o, cfg); // copy everything into o.
100 this.pack = typeof(cfg.pack) == 'undefined' ? ['add'] : cfg.pack;
102 XObject.extend(this, o);
106 this.listeners = this.listeners || {};
109 // remove objects/functions from o, so they can be sent to the contructor.
111 if ((typeof(o[i]) == 'object') ||
112 (typeof(o[i]) == 'function') ||
123 // do we need to call 'beforeInit here?'
126 //if ((this.xtype == 'Include')) {
127 // o = this.pre_registry[cls];
129 var isSeed = typeof(Seed) != 'undefined';
131 // xtype= Gtk.Menu ?? what about c_new stuff?
132 print("init: typeof(xtype): " + typeof(this.xtype));
133 if (!this.el && typeof(this.xtype) == 'function') {
134 print("func?" + XObject.keys(o).join(','));
135 this.el = this.xtype(o);
137 if (!this.el && typeof(this.xtype) == 'object') {
138 print("obj?" + XObject.keys(o).join(','));
139 this.el = new (this.xtype)(o);
142 if (!this.el && o.xns) {
144 var NS = imports.gi[o.xns];
146 Seed.print('Invalid xns: ' + o.xns);
148 constructor = NS[o.xtype];
150 Seed.print('Invalid xtype: ' + o.xns + '.' + o.xtype);
152 this.el = isSeed ? new constructor(o) : new constructor();
155 print("init: typeof(el):" + typeof(this.el));
157 // always overlay props..
158 // check for 'write' on object..
160 if (typeof(XObject.writeablePropsCache[this.xtype.type]) == 'undefined') {
162 var gi = GIRepository.IRepository.get_default();
163 var ty = gi.find_by_gtype(this.xtype.type);
165 for (var i =0; i < GIRepository.object_info_get_n_properties(ty);i++) {
166 var p = GIRepository.object_info_get_property(ty,i);
167 if (GIRepository.property_info_get_flags(p) & 2) {
168 write.push(GIRepository.base_info_get_name(p));
171 XObject.writeablePropsCache[this.xtype.type] = write;
172 print(write.join(", "));
178 // only write to writable properties
179 // if (XObject.writeablePropsCache[this.xtype.type].indexOf(i) < 0) {
185 //if (o.xnsid && o.id) {
186 // XObject.registry = XObject.registry || { };
187 // XObject.registry[o.xnsid] = XObject.registry[o.xnsid] || {};
188 // XObject.registry[o.xnsid][o.id] = this;
191 cfg.items.forEach(this.addItem, this);
193 for (var i in this.listeners) {
194 this.addListener(i, this.listeners[i]);
196 // delete this.listeners ?
199 // do we need to call 'init here?'
205 * Adds an item to the object using a new XObject
206 * uses pack property to determine how to add it.
207 * @arg cfg {Object} same as XObject constructor.
209 addItem : function(o) {
213 var item = (o.constructor == XObject) ? o : new XObject(o);
216 print("CTR:PROTO:" + ( item.id ? item.id : '??'));
217 print("addItem - call init [" + item.pack.join(',') + ']');
220 this.items.push(item);
222 if (item.pack===false) { // no
225 if (typeof(item.pack) == 'function') {
227 item.pack.apply(o, [ o , o.items[i] ]);
233 if (typeof(item.pack) == 'string') {
236 pack_m = item.pack.shift();
241 if (pack_m && typeof(this.el[pack_m]) == 'undefined') {
242 Seed.print('pack method not available : ' + this.xtype + '.' + pack_m);
247 //Seed.print('Pack ' + this.el + '.'+ pack_m + '(' + item.el + ')');
249 args.unshift(item.el);
250 print('[' + args.join(',') +']');
251 //Seed.print('args: ' + args.length);
253 this.el[pack_m].apply(this.el, args);
260 * @method addListener
261 * Connects a method to a signal. (gjs/Seed aware)
263 * @arg sig {String} name of signal
264 * @arg fn {Function} handler.
266 addListener : function(sig, fn)
269 Seed.print("Add signal " + sig);
271 var _li = XObject.createDelegate(fn,this);
272 // private listeners that are not copied to GTk.
274 if (typeof(Seed) != 'undefined') {
275 // Seed.print(typeof(_li));
276 this.el.signal[sig].connect(_li);
278 this.el.connect( sig, _li);
285 * Finds an object in the child elements using xid of object.
286 * prefix with '.' to look up the tree.. multiple '..' to look further up..
288 * @arg name {String} name of signal
289 * @return {XObject|false} the object if found.
295 return this.parent.get(xid.substring(1));
299 this.items.forEach(function(ch) {
309 this.items.forEach(function(ch) {
322 XObject.writeablePropsCache = { };
325 * Copies all the properties of config to obj.
327 * Pretty much the same as JQuery/Prototype..
328 * @param {Object} obj The receiver of the properties
329 * @param {Object} config The source of the properties
330 * @param {Object} defaults A different object that will also be applied for default values
331 * @return {Object} returns obj
332 * @member XObject extend
336 XObject.extend = function(o, c, defaults){
338 // no "this" reference for friendly out of scope calls
339 XObject.extend(o, defaults);
341 if(o && c && typeof c == 'object'){
349 XObject.extend(XObject,
352 * Copies all the properties of config to obj, if the do not exist.
353 * @param {Object} obj The receiver of the properties
354 * @param {Object} config The source of the properties
355 * @return {Object} returns obj
356 * @member Object extendIf
360 extendIf : function(o, c){
362 if(!o || !c || typeof c != 'object'){
366 if (typeof(o[p]) != 'undefined') {
377 * Extends one class with another class and optionally overrides members with the passed literal. This class
378 * also adds the function "override()" to the class that can be used to override
379 * members on an instance.
382 * MyObject = Object.define(
386 * parentClass, // or Object
388 * ... methods and properties.
391 * @param {Function} constructor The class inheriting the functionality
392 * @param {Object} superclass The class being extended
393 * @param {Object} overrides (optional) A literal with members
394 * @return {Function} constructor (eg. class
399 var io = function(o){
404 return function(constructor, parentClass, overrides) {
405 if (typeof(parentClass) == 'undefined') {
406 print("XObject.define: Missing parentClass: when applying: " );
407 print(new String(constructor));
410 if (typeof(parentClass.prototype) == 'undefined') {
411 print("Missing protype: when applying: " );
412 print(new String(constructor));
413 print(new String(parentClass));
416 var F = function(){};
418 var spp = parentClass.prototype;
421 sbp = constructor.prototype = new F();
422 sbp.constructor=constructor;
423 constructor.superclass=spp;
426 if(spp.constructor == Object.prototype.constructor){
427 spp.constructor=parentClass;
430 constructor.override = function(o){
431 Object.extend(constructor.prototype, o);
434 XObject.extend(constructor.prototype, overrides);
441 * returns a list of keys of the object.
442 * @param {Object} obj object to inspect
443 * @return {Array} returns list of kyes
444 * @member XObject keys
456 * @member XObject createDelegate
457 * creates a delage metdhod
458 * @param {Function} method to wrap
459 * @param {Object} scope
460 * @param {Array} args to add
461 * @param {Boolean|Number} append arguments or replace after N arguments.
462 * @return {Function} returns the delegate
465 createDelegate : function(method, obj, args, appendArgs){
468 var callArgs = args || arguments;
469 if(appendArgs === true){
470 callArgs = Array.prototype.slice.call(arguments, 0);
471 callArgs = callArgs.concat(args);
472 }else if(typeof appendArgs == "number"){
473 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
474 var applyArgs = [appendArgs, 0].concat(args); // create method call params
475 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
477 return method.apply(obj || window, callArgs);