1 /*
  2 SpiderGL Computer Graphics Library
  3 Copyright (c) 2010, Marco Di Benedetto - Visual Computing Lab, ISTI - CNR
  4 All rights reserved.
  5 
  6 Redistribution and use in source and binary forms, with or without
  7 modification, are permitted provided that the following conditions are met:
  8     * Redistributions of source code must retain the above copyright
  9       notice, this list of conditions and the following disclaimer.
 10     * Redistributions in binary form must reproduce the above copyright
 11       notice, this list of conditions and the following disclaimer in the
 12       documentation and/or other materials provided with the distribution.
 13     * Neither the name of SpiderGL nor the
 14       names of its contributors may be used to endorse or promote products
 15       derived from this software without specific prior written permission.
 16 
 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 */
 28 
 29 /**
 30  * @fileOverview Namespace
 31  */
 32 
 33 /**
 34  * The SpiderGL namespace.
 35  *
 36  * @namespace The SpiderGL namespace.
 37  */
 38 var SpiderGL = { };
 39 
 40 /**
 41  * Utility tag for any use.
 42  *
 43  * @type any
 44  */
 45 SpiderGL.TAG = 0;
 46 
 47 /**
 48  * Publishes SpiderGL modules content to a global object.
 49  *
 50  * The effect is to pollute the global object with the SpiderGL symbols. At the same time, it allows a faster and less verbose code.
 51  *
 52  * @param {object} [options] Symbols publishing options.
 53  * @param {object} [options.globalObject=SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT] The global object to which inject SpiderGL symbols.
 54  * @param {string} [options.constantPrefix=SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX] String prefix for constants.
 55  * @param {string} [options.functionPrefix=SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX] String prefix for functions. The first letter of the function identifier will be made capital.
 56  * @param {string} [options.classPrefix=SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX] String prefix for classes and sub-modules.
 57  */
 58 SpiderGL.openNamespace = function (options) {
 59 	options = SpiderGL.Utility.getDefaultObject({
 60 		globalObject   : SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT,
 61 		constantPrefix : SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX,
 62 		functionPrefix : SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX,
 63 		classPrefix    : SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX
 64 	}, options);
 65 
 66 	var constantRegExp = new RegExp("^(([_\$0-9A-Z])+)$");
 67 	function isConstant(name) {
 68 		return constantRegExp.test(name);
 69 	}
 70 
 71 	var namespaceOrClassRegExp = new RegExp("^([A-Z])");
 72 	function isNamespaceOrClass(name) {
 73 		return (namespaceOrClassRegExp.test(name) && !isConstant(name));
 74 	}
 75 
 76 	var functionRegExp = new RegExp("^(([a-z])+([_\$0-9A-Za-z])*)$");
 77 	function isFunction(name) {
 78 		return functionRegExp.test(name);
 79 	}
 80 
 81 	function initialCap(name) {
 82 		return (name.substr(0, 1).toUpperCase() + name.substr(1));
 83 	}
 84 
 85 	var classes    = { };
 86 	var functions  = { };
 87 	var constants  = { };
 88 
 89 	function collect(module) {
 90 		if (!module) return;
 91 		for (var x in module) {
 92 			if (x.substr(0, 1) == "_") continue;
 93 			var y = module[x];
 94 			if (isNamespaceOrClass(x)) {
 95 				//if (classes[x]) log("Duplicate: " + x);
 96 				classes[x] = y;
 97 			}
 98 			else if (isFunction(x)) {
 99 				//if (functions[x]) log("Duplicate: " + x);
100 				functions[x] = y;
101 			}
102 			else if (isConstant(x)) {
103 				//if (constants[x]) log("Duplicate: " + x);
104 				constants[x] = y;
105 			}
106 			else {
107 				//log("Unknown : " + x);
108 			}
109 		}
110 	}
111 
112 	var modules = [
113 		"Core",
114 		"DOM",
115 		"IO",
116 		"Math",
117 		"Mesh",
118 		"Model",
119 		"Semantic",
120 		"Space",
121 		"Type",
122 		"UserInterface",
123 		"Utility",
124 		"Version",
125 		"WebGL"
126 	];
127 
128 	for (var x in modules) {
129 		collect(SpiderGL[modules[x]]);
130 	}
131 
132 	for (var x in classes) {
133 		var name = options.classPrefix + initialCap(x);
134 		//log("CLASS    : " + name);
135 		options.globalObject[name] = classes[x];
136 	}
137 
138 	for (var x in functions) {
139 		var name = options.functionPrefix + initialCap(x);
140 		//log("FUNCTION : " + name);
141 		options.globalObject[name] = functions[x];
142 	}
143 
144 	for (var x in constants) {
145 		var name = options.constantPrefix + initialCap(x);
146 		//log("CONSTANT : " + name);
147 		options.globalObject[name] = constants[x];
148 	}
149 };
150 
151 /**
152  * Default publishing global object.
153  *
154  * @type object
155  *
156  * @default window
157  */
158 SpiderGL.openNamespace.DEFAULT_GLOBAL_OBJECT = window;
159 
160 /**
161  * Default publishing prefix for constant symbols.
162  *
163  * @type string
164  *
165  * @default "SGL_"
166  */
167 SpiderGL.openNamespace.DEFAULT_CONSTANT_PREFIX = "SGL_";
168 
169 /**
170  * Default publishing prefix for function symbols.
171  *
172  * @type string
173  *
174  * @default "sgl"
175  */
176 SpiderGL.openNamespace.DEFAULT_FUNCTION_PREFIX = "sgl";
177 
178 /**
179  * Default publishing prefix for classes and sub-modules symbols.
180  *
181  * @type string
182  *
183  * @default "Sgl"
184  */
185 SpiderGL.openNamespace.DEFAULT_CLASS_PREFIX = "Sgl";
186