Thanks Yanick, will look at your code in a few mins.

In the meantime here is some code that I added to provide OO namespace
functionality I need for a project we're working on (please bear in
mind that my javascript skills have rarely gone beyond the odd piece
of DOM manipulation and as such it is probably not the most elegant of
solutions):

getClassName()
getNamespace()
package(namespace)
include(namespace)

---

Test Scipt
====================

// function to output debug info
function out(obj) {
        alert(obj.getClassName() +
                " : "+obj.getNamespace());
}

// define the classes
Class.create('org.protoypejs.Foo', {
        initialize: function() {
                out(this);
        }
});
Class.create('org.protoypejs.Bar', org.protoypejs.Foo, { });

Class.create('Yahoo', {
        initialize: function() {
                out(this);
        }
});
Class.create('LiveSearch', Yahoo, { });

// create some test objects in
// the org.protoypejs namespace
var test1 = new org.protoypejs.Foo();
var test2 = new org.protoypejs.Bar();

// create some objects in the
// local namespace objects
var test3 = new Yahoo();
var test4 = new LiveSearch();

// change the package
// (the namespace for new class definitions)
package("org.protoypejs");

// create classes called Ho and Hum
Class.create('Ho', {
        initialize: function() {
                out(this);
        }
});
Class.create('Hum', org.protoypejs.Ho, {
        initialize: function() {
                out(this);
        }
});
var test5 = new org.protoypejs.Ho();
var test6 = new org.protoypejs.Hum();

// include a namespace
include("org.protoypejs");
var test7 = new Ho();
var test8 = new Hum();

---

Modifications to prototype.js
====================

var Prototype = {
  Version: '1.6.0.2',

  Package: '',

  Browser: {
    IE:     !!(window.attachEvent && !window.opera),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 &&
navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
  },

  BrowserFeatures: {
    XPath: !!document.evaluate,
    ElementExtensions: !!window.HTMLElement,
    SpecificElementExtensions:
      document.createElement('div').__proto__ &&
      document.createElement('div').__proto__ !==
        document.createElement('form').__proto__
  },

  ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>',
  JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,

  emptyFunction: function() { },
  K: function(x) { return x }
};

if (Prototype.Browser.MobileSafari)
        Prototype.BrowserFeatures.SpecificElementExtensions = false;


/*
 * Set the default namespace for new class declarations
 *
 * package("org.prototypejs");
 *
 * Class.create('TestClass1', { });
 * Class.create('TestClass2', { });
 *
 * will define two classes org.prototypejs.TestClass1
 * and org.prototypejs.TestClass2. Alternatively you
 * could define the classes using:
 *
 * package("");
 *
 * Class.create('org.prototypejs.TestClass1', { });
 * Class.create('org.prototypejs.TestClass2', { });
 */
function package(ns) {
        Prototype.Package = ns;
}

/*
 * Include a namespace into the global namespace
 *
 * This allows you to access classes within a namespace
 * from within the global scope. For example:
 *
 * package("");
 * Class.create('org.prototypejs.TestClass1', { });
 *
 * // long winded
 * var myTestClass1 = new org.prototypejs.TestClass1();
 *
 * // better
 * include("org.prototypejs");
 * var myTestClass1 = new TestClass1();
 */
function include(ns) {
        var nsParts = ns.split(".");
        for (var c in window.org.protoypejs) {
                // could add a check here to see
                // if we're overwriting something
                // in the global namespace...

                // need to get rid of this eval...
                window[c] = eval(ns+"."+c);
        }
}

/* Based on Alex Arnell's inheritance implementation. */
var Class = {
  create: function() {

    var parent = null, properties = $A(arguments);

    // some new variables
    var fullClassName   = "";
    var className               = "";
    var namespace               = "";

    if(Object.isString(properties[0])) {
        if(Prototype.Package=="") {
                fullClassName = properties.shift();
        } else {
                fullClassName = Prototype.Package+"."+properties.shift();
        }
    }

    if (Object.isFunction(properties[0]))
                parent = properties.shift();

    function klass() {
                this.initialize.apply(this, arguments);
    }

    Object.extend(klass, Class.Methods);
    klass.superclass = parent;
    klass.subclasses = [];

    // extract classname and namespace and add to window
    if(fullClassName!="") {
        className=fullClassName.substring(fullClassName.lastIndexOf(".")
+1);
        namespace=fullClassName.substring(0,
fullClassName.lastIndexOf("."));
        var nsParts = fullClassName.split(".");

                /* create a namespace */
                if(window[nsParts[0]]==undefined) {
                        window[nsParts[0]] = new Object();
                }
                var lastTmp = window[nsParts[0]];
                for(i=1; i<nsParts.length; i++) {
                        if(lastTmp[nsParts[i]]==undefined) {
                        lastTmp[nsParts[i]] = new Object();
                        }
                }

                /* reference to window */
        if(nsParts.length>1) {
                var tmp = window[nsParts[0]];
                for(i=1; i<nsParts.length-1; i++) {
                        tmp = tmp[nsParts[i]];
                }
                tmp[className] = klass;
        } else {
                window[className] = klass;
        }
    }

        // store the classname and namespace and add get methods
        klass.className = className;
        klass.namespace = namespace;
        klass.prototype.constructor.addMethods({
                getClassName: function() {
                        return this.constructor.className;
                },
                getNamespace: function() {
                        return this.constructor.namespace;
                }
        });

    if (parent) {
      var subclass = function() { };
      subclass.prototype = parent.prototype;
      klass.prototype = new subclass;
      parent.subclasses.push(klass);
    }

    for (var i = 0; i < properties.length; i++)
      klass.addMethods(properties[i]);

    if (!klass.prototype.initialize)
      klass.prototype.initialize = Prototype.emptyFunction;

    klass.prototype.constructor = klass;

    return klass;
  }
 };
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to