All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rwt.qx.Class.js Maven / Gradle / Ivy

Go to download

The Rich Ajax Platform lets you build rich, Ajax-enabled Web applications.

There is a newer version: 3.29.0
Show newest version
/*******************************************************************************
 * Copyright: 2004, 2012 1&1 Internet AG, Germany, http://www.1und1.de,
 *                       and EclipseSource
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    1&1 Internet AG and others - original API and implementation
 *    EclipseSource - adaptation for the Eclipse Rich Ajax Platform
 ******************************************************************************/

/**
 * This class is one of the most important parts of qooxdoo's
 * object-oriented features.
 *
 * Its {@link #define} method is used to create qooxdoo classes.
 *
 * Each instance of a class defined by {@link #define} has
 * the following keys attached to the constructor and the prototype:
 *
 * 
 * 
 * 
 * 
 * 
 * 
classnameThe fully-qualified name of the class (e.g. "qx.ui.core.Widget").
basenameThe namespace part of the class name (e.g. "qx.ui.core").
constructorA reference to the constructor of the class.
superclassA reference to the constructor of the super class.
* * Each method may access static members of the same class by using * this.self(arguments) ({@link rwt.qx.Object#self}): *
 * statics : { FOO : "bar" },
 * members: {
 *   baz: function(x) {
 *     this.self(arguments).FOO;
 *     ...
 *   }
 * }
 * 
* * Each overriding method may call the overridden method by using * this.base(arguments [, ...]) ({@link rwt.qx.Object#base}). This is also true for calling * the constructor of the superclass. *
 * members: {
 *   foo: function(x) {
 *     this.base(arguments, x);
 *     ...
 *   }
 * }
 * 
*/ /*global alert:false */ rwt.qx.Class.define( "rwt.qx.Class", { statics : { _normalizeConfig : function( config ) { if( !config ) { var config = {}; } if( config.include && !( config.include instanceof Array ) ) { config.include = [ config.include ]; } if( config.implement && !( config.implement instanceof Array ) ) { config.implement = [ config.implement ]; } if( !config.hasOwnProperty( "extend" ) && !config.type ) { config.type = "static"; } return config; }, /** * Define a new class using the qooxdoo class system. This sets up the * namespace for the class and generates the class from the definition map. * * Example: *
     * rwt.qx.Class.define("name",
     * {
     *   extend : Object, // superclass
     *   implement : [Interfaces],
     *   include : [Mixins],
     *
     *   statics:
     *   {
     *     CONSTANT : 3.141,
     *
     *     publicMethod: function() {},
     *     _protectedMethod: function() {},
     *     __privateMethod: function() {}
     *   },
     *
     *   properties:
     *   {
     *     "tabIndexOld": { type: "number", defaultValue : -1, _legacy : true }
     *     "tabIndex": { check: "Number", init : -1 }
     *   },
     *
     *   members:
     *   {
     *     publicField: "foo",
     *     publicMethod: function() {},
     *
     *     _protectedField: "bar",
     *     _protectedMethod: function() {},
     *
     *     __privateField: "baz",
     *     __privateMethod: function() {}
     *   }
     * });
     * 
* * @type static * @param name {String} Name of the class * @param config {Map ? null} Class definition structure. The configuration map has the following keys: * * * * * * * * * * * * * * * *
NameTypeDescription
typeString * Type of the class. Valid types are "abstract", "static" and "singleton". * If unset it defaults to a regular non-static class. *
extendClassThe super class the current class inherits from.
implementInterface | Interface[]Single interface or array of interfaces the class implements.
includeMixin | Mixin[]Single mixin or array of mixins, which will be merged into the class.
constructFunctionThe constructor of the class.
staticsMapMap of static members of the class.
propertiesMapMap of property definitions. For a description of the format of a property definition see * {@link rwt.qx.Property} or the legacy version {@link rwt.qx.LegacyProperty}.
membersMapMap of instance members of the class.
settingsMapMap of settings for this class. For a description of the format of a setting see * {@link rwt.qx.Setting}.
variantsMapMap of settings for this class. For a description of the format of a setting see * {@link rwt.util.Variant}
eventsMap * Map of events the class fires. The keys are the names of the events and the values are the * corresponding event type class names. *
deferFunctionFunction that is called at the end of processing the class declaration. It allows access to the declared statics, members and properties.
destructFunctionThe destructor of the class.
* @return {void} * @throws TODOC */ define : function( name, config ) { if( this._stopLoading ) { throw new Error( "Stop loading " + name ); } try { config = this._normalizeConfig( config ); this.__validateConfig( name, config ); var clazz; if( !config.extend ) { clazz = config.statics || {}; } else { if( !config.construct ) { config.construct = this.__createDefaultConstructor(); } clazz = this.__wrapConstructor(config.construct, name, config.type); if( config.statics ) { var key; for( var i = 0, a = rwt.util.Objects.getKeys( config.statics ), l = a.length; i < l; i++ ) { key = a[ i ]; clazz[ key ] = config.statics[ key ]; } } } var basename = this.createNamespace( name, clazz, false ); clazz.name = clazz.classname = name; clazz.basename = basename; this.__registry[ name ] = clazz; // Attach toString if( !clazz.hasOwnProperty( "toString" ) ) { clazz.toString = this.genericToString; } if( config.extend ) { var superproto = config.extend.prototype; var Helper = this.__createEmptyFunction(); Helper.prototype = superproto; var proto = new Helper(); clazz.prototype = proto; proto.name = proto.classname = name; proto.basename = basename; config.construct.base = clazz.superclass = config.extend; config.construct.self = clazz.constructor = proto.constructor = clazz; if( config.destruct ) { clazz.$$destructor = config.destruct; } var that = this; clazz.$$initializer = function() { //console.log( "init " + name ); if( config.properties ) { that.__addProperties(clazz, config.properties, true); } if( config.members ) { that.__addMembers(clazz, config.members, true, true, false); } if( config.events ) { that.__addEvents(clazz, config.events, true); } if( config.include ) { for (var i=0, l=config.include.length; iWARNING: You may break working classes and features. * * @type static * @param clazz {Class} An existing class which should be modified by including the mixin. * @param mixin {Mixin} The mixin to be included. */ patch : function(clazz, mixin) { rwt.qx.Class.__addMixin(clazz, mixin, true); }, /** * Whether a class is a direct or indirect sub class of another class, * or both classes coincide. * * @type static * @param clazz {Class} the class to check. * @param superClass {Class} the potential super class * @return {Boolean} whether clazz is a sub class of superClass. */ isSubClassOf : function(clazz, superClass) { if (!clazz) { return false; } if (clazz == superClass) { return true; } if (clazz.prototype instanceof superClass) { return true; } return false; }, /** * Returns the definition of the given property. Returns null * if the property does not exist. * * TODO: Correctly support refined properties? * * @type member * @param clazz {Class} class to check * @param name {String} name of the event to check for * @return {Map|null} whether the object support the given event. */ getPropertyDefinition : function(clazz, name) { while (clazz) { if (clazz.$$properties && clazz.$$properties[name]) { return clazz.$$properties[name]; } clazz = clazz.superclass; } return null; }, /** * Returns the class or one of its superclasses which contains the * declaration for the given property in its class definition. Returns null * if the property is not specified anywhere. * * @param clazz {Class} class to look for the property * @param name {String} name of the property * @return {Class | null} The class which includes the property */ getByProperty : function(clazz, name) { while (clazz) { if (clazz.$$properties && clazz.$$properties[name]) { return clazz; } clazz = clazz.superclass; } return null; }, /** * Whether a class has the given property * * @type member * @param clazz {Class} class to check * @param name {String} name of the property to check for * @return {Boolean} whether the class includes the given property. */ hasProperty : function(clazz, name) { return !!this.getPropertyDefinition(clazz, name); }, /** * Returns the event type of the given event. Returns null if * the event does not exist. * * @type member * @param clazz {Class} class to check * @param name {String} name of the event * @return {Map|null} Event type of the given event. */ getEventType : function(clazz, name) { var clazz = clazz.constructor; while (clazz.superclass) { if (clazz.$$events && clazz.$$events[name] !== undefined) { return clazz.$$events[name]; } clazz = clazz.superclass; } return null; }, /** * Whether a class supports the given event type * * @type member * @param clazz {Class} class to check * @param name {String} name of the event to check for * @return {Boolean} whether the class supports the given event. */ supportsEvent : function(clazz, name) { return !!this.getEventType(clazz, name); }, /** * Whether a class directly includes a mixin. * * @type static * @param clazz {Class} class to check * @param mixin {Mixin} the mixin to check for * @return {Boolean} whether the class includes the mixin directly. */ hasOwnMixin: function(clazz, mixin) { return clazz.$$includes && clazz.$$includes.indexOf(mixin) !== -1; }, /** * Returns the class or one of its superclasses which contains the * declaration for the given mixin. Returns null if the mixin is not * specified anywhere. * * @param clazz {Class} class to look for the mixin * @param mixin {Mixin} mixin to look for * @return {Class | null} The class which directly includes the given mixin */ getByMixin : function(clazz, mixin) { var list, i, l; while (clazz) { if (clazz.$$includes) { list = clazz.$$flatIncludes; for (i=0, l=list.length; i 0 ) { target = inits.pop(); target.$$initializer(); delete target.$$initializer; } } }, /** * Generate a wrapper of the original class constructor in order to enable * some of the advanced OO features (e.g. abstract class, singleton, mixins) * * @param construct {Function} the original constructor * @param name {String} name of the class * @param type {String} the user specified class type */ __wrapConstructor : function(construct, name, type) { var init = this.__initializeClass; var wrapper = function() { // We can access the class/statics using arguments.callee var clazz=arguments.callee.constructor; init( clazz ); // Attach local properties if( !clazz.$$propertiesAttached ) { rwt.qx.Property.attach( clazz ); } // Execute default constructor var retval=clazz.$$original.apply(this,arguments); // Initialize local mixins if(clazz.$$includes){var mixins=clazz.$$flatIncludes; for(var i=0,l=mixins.length;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy