Openovate | Open Source Innovations
eve.js

Jump to

this.getSpace = function() , this.setSpace = function() , this.freeSpace = function() , this.isSpace = function() , this.args = function(args) , this.type = function( obj, has ) , this.uid = function() , Source Code

this.getSpace = function() 

Gets a value given the path in the registry.

eve.getSpace(string[, string..]) //--> returns the //value given the name space path within the given object

eve.getSpace('test', 'node') //--> for this case it would return //null if no 'test' or no 'node' are definied in that order

Return variable

this.setSpace = function() 

Creates the name space given the space and sets the

eve.setSpace(string[, string..], variable) //--> sets the //value given the name space path data registry to the last argument value

eve.setSpace('test', 'node', function(){}) - for this case it would //alter the data registry setting a new name space 'node' after 'test' //and assign it an empty function

Return this

this.freeSpace = function() 

Unset a name space eve.freeSpace(string[, string..]); //--> unsets the //value given the name space path

eve.freeSpace('test') //--> for this case it would //alter the registry deleting all information about 'test'

Return this

this.isSpace = function() 

Checks to see if a name is taken

eve.isSpace(string[, string..]); //--> checks //to see if the name space path is set

eve.isSpace('test'); //--> for this case it would return true

Return bool

this.args = function(args) 

Converts the arguments Object to an array.

Return array

this.type = function( obj, has ) 

A more detailed version of type of

Arguments

variableanything
string|arraytype/s you are expecting or comparing to

Return array|bool

this.uid = function() 

Returns a unique ID; should only be used internally.

Return int

Source Code

var eve = (function(settings) {
	/*	Class Definition
	--------------------------------*/
	var c = function() {
		/*	Constants
		--------------------------------*/
		this.CLASSNAME	= name;
		this.BROWSER	= null;
		
		/*	Public Properties
		--------------------------------*/
		this.settings = settings || {};//global settings
		
		/*	Private Properties
		--------------------------------*/
		var _uid = 0;	//uid counter
		var _data = {};	//data registry
		
		/*	Registry Methods
		--------------------------------*/
		/**
		 * Gets a value given the path in the registry.
		 * <p>
		 * eve.getSpace(string[, string..]) //--> returns the 
		 * //value given the name space path within the given object
		 * <p>
		 * eve.getSpace('test', 'node') //--> for this case it would return 
		 * //null if no 'test' or no 'node' are definied in that order
		 * 
		 * @return variable
		 */
		this.getSpace = function() {
			//get the arguments and transform it to an array
			var args = this.args(arguments);
			
			//what object are we looking into?
			//if the first argument is not an object
			//use data registry
			
			//we do not want to use the type function 
			//in this case because we don't care what 
			//type of object it is
			var data = typeof args[0] == 'object' ? args.shift() : _data;
			
			if(args.length > 0)
			{		
				var result;
				//foreach argument
				this.parse(args, function(key, value, length) {
					//this is the last item
					if((key+1) == length)
					{
						//assign it to result
						//exit loop
						result = data[value];
						return false;
					}
					//the name does not exist
					if(!data[value])
					{
						//assign result to null
						//exit loop
						result = null;
						return false;
					}
					
					//walk the object
					data = data[value];
				});
				return result;
			}
			
			//if no arguments
			//return the whole object
			return data;
		};
		
		/**
		 * Creates the name space given the space and sets the 
		 * <p>
		 * eve.setSpace(string[, string..], variable) //--> sets the  
		 * //value given the name space path data registry to the last argument value
		 * <p>
		 * eve.setSpace('test', 'node', function(){}) - for this case it would 
		 * //alter the data registry setting a new name space 'node' after 'test'
		 * //and assign it an empty function
		 *
		 * @return this
		 */
		this.setSpace = function() {
			//get the arguments and transform it to an array
			var args = this.args(arguments);
			
			//what object are we looking into?
			//if the first argument is not an object
			//use data registry
			
			//we do not want to use the type function 
			//in this case because we don't care what 
			//type of object it is
			var data = typeof args[0] == 'object' ? args.shift() : _data;
			
			if(args.length > 1)
			{
				var last, index; 
				//foreach argument
				this.parse(args, this.bind(function(key, value, length) {
					//this is the last item
					if((key+1) == length)
					{	
						//set it to the value
						//exit loop
						last[index] = value;
						return false;
					}
					//the name does not exist
					if(!data[value])
					{
						//lets create the name
						data[value] = {};
						if(value == '[]')
						{// {test2:  [{test3:1209}]}
							last[index] = [];
							last[index].push({});
							value = 0;
							data = last[index];
						}
						
					}
					
					//walk the object
					last = data;
					index = value;
					data = data[value];
				}, this));
			}
			
			//allow chainability
			return this;
		};
		
		/**
		 * Unset a name space
		 * eve.freeSpace(string[, string..]); //--> unsets the  
		 * //value given the name space path
		 * <p>
		 * eve.freeSpace('test') //--> for this case it would 
		 * //alter the registry deleting all information about 'test'
		 * 
		 * @return this
		 */
		this.freeSpace = function() {
			//get the arguments and transform it to an array
			var args = this.args(arguments);
			
			//what object are we looking into?
			//if the first argument is not an object
			//use data registry
			
			//we do not want to use the type function 
			//in this case because we don't care what 
			//type of object it is
			var data = typeof args[0] == 'object' ? args.shift() : _data;
			
			if(args.length > 0)
			{
				//foreach argument
				this.parse(args, function(key, value, length) {
					//this is the last item
					if((key+1) == length)
					{
						//delete it
						//exit loop
						delete data[value];
						return false;
					}
					
					//walk the object
					data = data[value];
				});
			}
			
			//allow chainability
			return this;
		};
		
		/**
		 * Checks to see if a name is taken
		 * <p>
		 * eve.isSpace(string[, string..]); //--> checks
		 * //to see if the name space path is set
		 * <p>
		 * eve.isSpace('test'); //--> for this case it would return true
		 *
		 * @return bool
		 */
		this.isSpace = function() {
			//get the arguments and transform it to an array
			var args = this.args(arguments);
			
			//what object are we looking into?
			//if the first argument is not an object
			//use data registry
			
			//we do not want to use the type function 
			//in this case because we don't care what 
			//type of object it is
			var data = typeof args[0] == 'object' ? args.shift() : _data;
			
			if(args.length > 0)
			{
				//foreach argument
				return this.parse(args, function(key, value, length) {
					//the name does not exist
					if(!data[value])
					{
						//exit loop
						//false will be returned
						return false;
					}
					
					//walk the object
					data = data[value];
				}, {bool: true});
				
			}
			
			//if no arguments 
			//then return false
			return false;
		};
		
		/*	Misc Utility Methods
		--------------------------------*/
		/**
		 * Converts the arguments Object to an array.
		 *
		 * @param arguments
		 * @return array
		 */
		this.args = function(args) {
			return Array.prototype.slice.call(args);
		};
		
		/**
		 * A more detailed version of type of
		 *
		 * @param variable anything
		 * @param string|array type/s you are expecting or comparing to
		 * @return array|bool
		 */
		this.type = function( obj, has ) {
			has = typeof has == 'string' ? [has] : has;
			has = has instanceof Array ? has : [];
			
			var value = (function(obj) {
				var test, type = typeof obj;
			
				//is it a function?
				if( type == 'function' )
				{
					test = obj.toString();
					//is it a regex?
					if( ( /^\/.*\/$/ ).test(test))
					{
						return 'regexp';
					}
					
					if( ( /^\[object.*\]$/i ).test(test))
					{
						type = 'object';
					}
				}
				
				if(type != 'object')
				{
					return type;
				}
				
				//typical cases
				switch(obj)
				{
					case null:
						return 'null';
					case window:
						return 'window';
					case window.event:	
						return 'event';
					case document:
						return 'document';
				}
				
				//is it an eve class?
				if(obj.CLASSNAME)
				{
					return obj.CLASSNAME;
				}
				
				if( window.event && (event.type == obj.type))
				{
					return 'event';
				}
				
				if( obj.nodeType !== null )
				{	
					switch(obj.nodeType)
					{
						case 1:
							return 'element';
						case 3:
							return 'textnode';
					}
				}
				
				if( obj.constructor !== null )
				{
					switch( obj.constructor )
					{																	
						case Array:
							type = 'array';
							break;
						case Date:
							return 'date';
						case RegExp:
							return 'regexp';
						case Object:
							type = 'object';
							break;
						case ReferenceError:
							return 'error';
						default:
							test = obj.constructor.toString().match( /\s*function (.*)\(/ );
							if( test !== null )
							{
								return test[1];
							}
					}
				}
				
				if( obj.toString !== null )
				{
					test = obj.toString().match( /^\[object (.*)\]$/i );
					if(test !== null)	
					{
						switch(test[1].toLowerCase())
						{
							case 'window':
								return 'window';
							case 'event':
								return 'event';
							case 'math':
								return 'math';
							case 'error':	
								return 'error';
							case 'nodelist':
							case 'htmlcollection':
							case 'elementarray':
								return 'domcollection';
						}
					}
				}
				
				if(obj.callee != null)
				{
					return 'arguments';
				}
				
				return type;
			})(obj);
			
			if(has.length > 0)
			{
				return this.has(has, value);
			}
			return value;
		};
		
		/**
		 * Returns a unique ID; should only be used internally.
		 *
		 * @return int
		 */
		this.uid = function() {
			return _uid ++;
		};
		
		/*	Browser Detect
		--------------------------------*/
		this.BROWSER = (function(version) {
			//Go from most to least used
			//IE all versions
			var test = /MSIE\x20[0-9]*/g.exec(navigator.userAgent);
			if(test)
			{
				test = test[0].replace('MS', '').split(' ');
				return version ? test : test[0];
			}
			
			//FF all versions
			test = /Firefox\/[0-9]*/g.exec(navigator.userAgent);
			if(test)
			{
				test = test[0].split('/');
				return version ? test : test[0];
			}
			
			//Chrome all versions
			test = /Chrome\/[0-9]*/g.exec(navigator.userAgent);
			if(test)
			{
				test = test[0].split('/');
				return version ? test : test[0];
			}
			
			//Safari all versions
			test = /[0-9]*\.[0-9]*\x20Safari/g.exec(navigator.userAgent);
			if(test)
			{
				test = test[0].split(' ').reverse();
				test[1] = /^[A-Z0-9]*/g.exec(test[1])[0];
				return version ? test : test[0];
			}
			
			//Opera all versions
			test = /^Opera\/[0-9]*/g.exec(navigator.userAgent);
			if(test)
			{
				test = test[0].split('/');
				return version ? test : test[0];
			}
			
			return version ? [navigator.userAgent, false] : navigator.userAgent;
		})(true);
	},
	
	//link new class to outer 
	//and inner global scope
	$ = new c();

	return $;
})();
Contact Us | openovate.com. All rights reserved.