/**
* @author Matan Lurey
*/
(function(){
/**
* @alias JSOOP
* JavaScript Object Oriented Programming Library
*/
var JSOOP = window.JSOOP = (JSOOP || {});
/* Imports and Dependencies */
if (!window.JSCore || (window.JSCore.VERSION < 1.00))
throw new Error('JSOOP requires JSCore 1.00 initialized before this script');
// Import
var JSCore = window.JSCore;
/**
* @alias JSOOP.createClass
* Creates and returns a Class
* @param {Object} properties
* @return {Function}
*/
JSOOP.createClass = function createClass(properties) {
var constructor = properties.constructor ||
properties.extend ? function(){return properties.extend.apply(this, arguments);} : function(){};
if (properties.extend)
constructor.prototype = new properties.extend;
if (properties.implement)
JSCore.forEach(JSCore.toArray(properties.implement), function(e){
JSCore.copy(constructor.prototype, e);
});
if (properties.instance)
JSCore.copy(constructor.prototype, properties.instance);
if (properties.statics)
JSCore.copy(constructor, properties.statics);
return constructor;
};
/**
* @alias JSOOP.findObject
* Finds and returns the object literal defined, or undefined if it does not exist.
* A distinct and safe alternative to using eval.
* @param {String} absoluteName a named object, such as org.lurey.ui.Widget
* @param {Booean} [create] assign to true if the object should be created if it does not exist
* @param {Object} [context] the starting context for searching, by default it is global
* @return {Object}
*/
JSOOP.findObject = function findObject(absoluteName, create, context) {
var context = context || window;
var nameParts = absoluteName.split('.');
for (var i = 0, l = nameParts.length; i < l; ++i) {
var curNamePart = nameParts[i];
if (!JSCore.isDefined(context[curNamePart]))
if (create)
context = (context[curNamePart] = {});
else
return undefined;
else
context = context[curNamePart];
}
return context;
};
/**
* @alias JSOOP.getNamespace
* Finds, creates (if needed) and returns the object literal defined
* @param {String} absoluteName a named object, such as org.lurey.ui
* @param {Object} [context] the starting context for searching, by default it is global
* @return {Object}
*/
JSOOP.getNamespace = function getNamespace(absoluteName, context) {
return JSOOP.findObject(absoluteName, true, context);
};
function _usingWildcard(absoluteName, contextObject) {
var namedObject = JSOOP.findObject(absoluteName);
for (var k in namedObject)
contextObject[k] = JSOOP.findObject(absoluteName + '.' + k);
};
/**
* @alias JSOOP.using
* Returns an object with all defined variables imported to it
* @param {Arguments} args object literals, wildcards supported
* @return {Object}
*/
JSOOP.using = function using() {
var contextObject = {};
JSCore.forEach(arguments, function(e){
var nameParts = e.split('.');
var absoluteName = nameParts.slice(0, nameParts.length - 1).join('.');
var objectName = nameParts[nameParts.length - 1];
if (objectName == '*')
return _usingWildcard(absoluteName, contextObject);
contextObject[objectName] = JSOOP.findObject(e);
});
return contextObject;
};
})();