1 /*
  2  * 	Copyright (C) 2012-2013 DFKI GmbH
  3  * 	Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4  * 	German Research Center for Artificial Intelligence
  5  * 	http://www.dfki.de
  6  * 
  7  * 	Permission is hereby granted, free of charge, to any person obtaining a 
  8  * 	copy of this software and associated documentation files (the 
  9  * 	"Software"), to deal in the Software without restriction, including 
 10  * 	without limitation the rights to use, copy, modify, merge, publish, 
 11  * 	distribute, sublicense, and/or sell copies of the Software, and to 
 12  * 	permit persons to whom the Software is furnished to do so, subject to 
 13  * 	the following conditions:
 14  * 
 15  * 	The above copyright notice and this permission notice shall be included 
 16  * 	in all copies or substantial portions of the Software.
 17  * 
 18  * 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 19  * 	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 20  * 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 21  * 	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 22  * 	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 23  * 	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 24  * 	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 25  */
 26 
 27 
 28 //define(['module'], function(module){//TODO remove module-dependency? -> would need different mechanism for querying env-configuration...
 29 define(['env'], 
 30 /**
 31  * A Utility class that provides various <i>constants</i>.<br>
 32  * 
 33  * <p>
 34  * Note that the actual values depend on the execution environment (e.g. ANDROID
 35  * vs. BROWSER). As a consequence the constants object has 2 modes, that can be
 36  * switchted via the getInstance()-method, e.g. <code>getInstance(false)</code>
 37  * 
 38  * 
 39  * @name Constants
 40  * @memberOf mmir
 41  * @static
 42  * @class
 43  * 
 44  * @requires org.apache.cordova.device: cordova plugin add org.apache.cordova.device
 45  * 
 46  * @example var appBase = mmir.Constants.getBasePath();
 47  */
 48 function(
 49 		env
 50 ){
 51 	
 52 	/**
 53 	 * Object containing the instance of the class constants 
 54 	 * 
 55 	 * @type mmir.Constants
 56 	 * @private
 57 	 * @memberOf Constants#
 58 	 */
 59 	var instance = null;
 60 	
 61 	/**
 62 	 * @memberOf Constants#
 63 	 */
 64 	var isBrowserEnv = false;
 65 
 66 	// needed basepath
 67 	/**
 68 	 * the base path for the "invoking" app (i.e. where the main HTML file is located)
 69 	 * @private
 70 	 * @memberOf Constants#
 71 	 */
 72     var basePath = "";
 73 	
 74     ///////////////////////////////////////////////////// Paths /////////////////////////////////////////////////////
 75     
 76     /**
 77 	 * the base path of the (i.e. this) library
 78 	 * @private
 79 	 * @memberOf Constants#
 80 	 */
 81 	var frameworkBasePath = "mmirf/";
 82 	
 83 	/**
 84 	 * the path for WebWorkers
 85 	 * @private
 86 	 * @memberOf Constants#
 87 	 */
 88 	var workerPath = frameworkBasePath + "workers/";
 89 	/**
 90 	 * the path for plugins
 91 	 *  
 92 	 * TODO deprecated? since Cordova 3.x brings its own loading mechanism now...
 93 	 * 
 94 	 * @private
 95 	 * @memberOf Constants#
 96 	 */
 97 	var pluginsPath = frameworkBasePath + "plugins/";
 98 	/**
 99 	 * the path for Extensions (i.e. extending JavaScript base classes)
100 	 * @private
101 	 * @memberOf Constants#
102 	 */
103 	var extensionsPath = frameworkBasePath + "tools/extensions/";
104 	/**
105 	 * the path to the audio file containing the default beep sound.
106 	 * @private
107 	 * @memberOf Constants#
108 	 */
109 	var beepURL = frameworkBasePath + "vendor/sounds/beep-notification.mp3";
110 	/**
111 	 * the path to the app's controllers
112 	 * @private
113 	 * @memberOf Constants#
114 	 */
115 	var controllerPath = "controllers/";
116 	/**
117 	 * the path to the app's controller helpers
118 	 * @private
119 	 * @memberOf Constants#
120 	 */
121 	var helperPath = "helpers/";
122 	/**
123 	 * the path to the language resources root directory
124 	 * @private
125 	 * @memberOf Constants#
126 	 */
127 	var languagePath = "config/languages/";
128 	/**
129 	 * the path to the app's models
130 	 * @private
131 	 * @memberOf Constants#
132 	 */
133 	var modelPath = "models/";
134 	/**
135 	 * the path to the app's layouts
136 	 * @private
137 	 * @memberOf Constants#
138 	 */
139 	var layoutPath = "views/layouts/";//before changing this: see also use of 'layouts' sub-dir-name in build/lib/mmir-build/ant/StandaloneTemplateParserExec.js
140 	/**
141 	 * the path to the app's view root directory
142 	 * @private
143 	 * @memberOf Constants#
144 	 */
145 	var viewPath = "views/";
146 	/**
147 	 * the path to the app's generated (compiled JS) views
148 	 * @private
149 	 * @memberOf Constants#
150 	 */
151 	var genViewPath = "gen/views/";
152 	/**
153 	 * the path to the app's generated (compiled JS) layouts
154 	 * @private
155 	 * @memberOf Constants#
156 	 */
157 	var genLayoutPath = "gen/views/layouts/";//before changing this: see also use of 'layouts' sub-dir-name in build/lib/mmir-build/ant/StandaloneTemplateParserExec.js
158 	/**
159 	 * the path to the app's generated (compiled JS) grammars
160 	 * @private
161 	 * @memberOf Constants#
162 	 */
163 	var genGrammarsPath = "gen/grammar/";
164 
165 	/**
166 	 * the path to media plugins / modules
167 	 * @private
168 	 * @memberOf Constants#
169 	 */
170 	var mediaPluginPath = frameworkBasePath + "env/media/";
171 	/**
172 	 * the path to grammar engine implementations / modules
173 	 * @private
174 	 * @memberOf Constants#
175 	 */
176 	var grammarPluginPath = frameworkBasePath + "env/grammar/";
177 	
178 	///////////////////////////////////////////////////// Resource Names /////////////////////////////////////////////////////
179     
180 	/**
181 	 * the name of speech (output) configuration files
182 	 * @private
183 	 * @memberOf Constants#
184 	 */
185 	var speechConfigFileName = "speech.json";
186 	/**
187 	 * the name of (JSON) grammar files, i.e. "grammar definitions"
188 	 * @private
189 	 * @memberOf Constants#
190 	 */
191 	var grammarFileName = "grammar.json";
192 	/**
193 	 * the name of language dictionary files
194 	 * @private
195 	 * @memberOf Constants#
196 	 */
197 	var dictionaryFileName = "dictionary.json";
198 	/**
199 	 * the name of the app's configuration file
200 	 * @private
201 	 * @memberOf Constants#
202 	 */
203 	var configurationFileUrl = "config/configuration.json";
204 	/**
205 	 * the name of the app's directory-/file-information file
206 	 * @private
207 	 * @memberOf Constants#
208 	 */
209 	var directoriesFileUrl = "config/directories.json";
210 	
211 	
212 	////////////////////////////////////////////////// General Constant Values///////////////////////////////////////////////////
213     
214 	/**
215 	 * the default language setting
216 	 * @private
217 	 * @memberOf Constants#
218 	 */
219 	var language = "en";
220 	
221 	// Prefixes
222 	
223 	/**
224 	 * the prefix for partial-view file-names
225 	 * @private
226 	 * @memberOf Constants#
227 	 */
228 	var partialsPrefix = '~';
229 	/**
230 	 * the postfix for controller-helper file-names
231 	 * @private
232 	 * @memberOf Constants#
233 	 */
234 	var helperSuffix = "Helper";
235 	
236 	/**
237 	 * Object that holds information about the execution
238 	 * environment / platform.
239 	 * 
240 	 * (set on initialization)
241 	 * 
242 	 * @private
243 	 * @type env
244 	 * @memberOf Constants#
245 	 */
246 	var envInfo = void(0);
247 	
248 	/**
249 	 * @private
250 	 * @memberOf Constants#
251 	 */
252 	function setBasePath(isBrowserEnvParam){
253 		// if not on browser: basepath must be different
254 		if(typeof isBrowserEnvParam === 'string'){
255 			basePath = isBrowserEnvParam;
256 		}
257 		else if (isBrowserEnvParam && isBrowserEnvParam.isCordovaEnv){
258 
259 			//if cordova env, try to use the specific platform
260 			var env = isBrowserEnvParam.envSetting;
261 			if(env === 'cordova'){
262 				
263 				env = isBrowserEnvParam.platform;
264 				
265 				if(env === 'default'){
266 					console.warn('Unknown cordova platform "'+env+'", using default base path /');
267 				}
268 				
269 			}
270 			
271 			switch(env){
272 				case 'android':
273 					basePath = "file:///android_asset/www/";
274 					break;
275 //				case 'cordova':
276 				case 'ios':
277 				case 'default':
278 				default:
279 					basePath = "";
280 			}
281 			
282 		}
283 		else if (isBrowserEnvParam && isBrowserEnvParam.isBrowserEnv){
284 			basePath = "";
285 		}
286 		else if (isBrowserEnvParam === false || typeof isBrowserEnvParam === 'undefined'){
287 			//BACKWARD COMPATIBILITY: false and omitted argument are interpreted as Android env
288 			//TODO remove this?
289 			basePath = "file:///android_asset/www/";
290 		}
291 		else {
292 			//default:
293 			basePath = "";
294 		}
295 		
296 	}
297 	
298 	/**
299 	 * Constructor-Method of Class {@link Constants}<br>
300 	 * 
301 	 * @constructs Constants#
302 	 * @memberOf mmir.Constants.prototype
303 	 * @private
304 	 */
305     function constructor(env){
306     	envInfo = env;
307 		isBrowserEnv = envInfo.isBrowserEnv;
308 		setBasePath(env);
309 		
310 		/** @lends Constants.prototype */
311 		return {
312 			/**
313 			 * Returns a string with the base path.
314 			 * @function
315 			 * @public
316 			 * @returns {String} base path
317 			 * 
318 			 * @memberOf mmir.Constants.prototype
319 			 */
320 			getBasePath: function(){
321 				return basePath;
322 			},
323 			/**
324 			 * Returns a string with the path to the plugins.
325 			 * @function
326 			 * @public
327 			 * @returns {String} plugin path
328 			 */
329 			getPluginsPath: function(){
330 				return basePath+pluginsPath;
331 			},
332 			/**
333 			 * Returns a string with the path to the layouts.
334 			 * @function
335 			 * @public
336 			 * @returns {String} layout path
337 			 */
338 			getLayoutPath: function(){
339 				return basePath+layoutPath;
340 			},
341 			getCompiledLayoutPath: function(){
342 				return basePath+genLayoutPath;
343 			},
344 			/**
345 			 * Returns a string with the path to the models.
346 			 * @function
347 			 * @public
348 			 * @returns {String} model path
349 			 */
350 			getModelPath: function(){
351 				return basePath+modelPath;
352 			},
353 			/**
354 			 * Returns a string with the path to the views.
355 			 * @function
356 			 * @public
357 			 * @returns {String} view path
358 			 */
359 			getViewPath: function(){
360 				return basePath+viewPath;
361 			},
362 			
363 			getCompiledViewPath: function(){
364 				return basePath + genViewPath;
365 			},
366 			/**
367 			 * Returns a string with the path to the languages.
368 			 * @function
369 			 * @public
370 			 * @returns {String} language path
371 			 */
372 			getLanguagePath: function(){
373 				return basePath+languagePath;
374 			},
375 			/**
376 			 * Returns a string with the path to the controllers.
377 			 * @function
378 			 * @public
379 			 * @returns {String} controller path
380 			 */
381 			getControllerPath: function(){
382 				return basePath+controllerPath;
383 			},
384 			/**
385 			 * Returns a string with the path to the workers.
386 			 * @function
387 			 * @public
388 			 * @returns {String} worker path
389 			 */
390 			getWorkerPath: function(){
391 				return workerPath;
392 			},
393 			/**
394 			 * Returns a string with the path to the helpers.
395 			 * @function
396 			 * @public
397 			 * @returns {String} helper path
398 			 */
399 			getHelperPath: function(){
400 				return basePath+helperPath;
401 			},
402 			/**
403 			 * Returns a string with the path to the extensions.
404 			 * @function
405 			 * @public
406 			 * @returns {String} extensions path
407 			 */
408 			getExtensionsPath: function(){
409 				return basePath + extensionsPath;
410 			},
411 			/**
412 			 * Returns a string with the path to the Media-Plugins.
413 			 * @function
414 			 * @public
415 			 * @returns {String} MediaPlugin path
416 			 */
417 			getMediaPluginPath: function(){
418 				return basePath + mediaPluginPath;
419 			},
420 			/**
421 			 * Returns a string with the path to the Grammar-Plugins
422 			 * (ie. engines for grammar generation).
423 			 * 
424 			 * @function
425 			 * @public
426 			 * @returns {String} Grammar Plugin path (
427 			 */
428 			getGrammarPluginPath: function(){
429 				return basePath + grammarPluginPath;
430 			},
431 			/**
432 			 * Returns a string with the path to the directory that contains the generated/executable grammars.
433 			 * @function
434 			 * @public
435 			 * @returns {String} path for generated grammars (JavaScript files)
436 			 */
437 			getGeneratedGrammarsPath: function(){
438 				return basePath + genGrammarsPath;
439 			},
440 			/**
441 			 * Returns a string with the path to the configuration file.
442 			 * @function
443 			 * @public
444 			 * @returns {String} path to configuration file
445 			 */
446 			getConfigurationFileUrl: function(){
447 				return basePath+configurationFileUrl;
448 			},
449 			/**
450 			 * Returns a string with the path to the directories file (directory-strucure / file-list).
451 			 * @function
452 			 * @public
453 			 * @returns {String} path to directories file
454 			 */
455 			getDirectoriesFileUrl: function(){
456 				return basePath+directoriesFileUrl;
457 			},
458 			/**
459 			 * Returns a string with the path to the beep audio-file.
460 			 * @function
461 			 * @public
462 			 * @returns {String} path to beep wav file
463 			 */
464 			getBeepUrl: function(){
465 				return basePath+beepURL;
466 			},
467 			/**
468 			 * Returns the name of the dictionary filename as string 
469 			 * @function
470 			 * @public
471 			 * @returns {String} dictionary filename
472 			 */
473 			getDictionaryFileName: function(){
474 				return dictionaryFileName;
475 			},
476 			/**
477 			 * Returns the name of the filename for
478 			 * the speech configuration as string 
479 			 * @function
480 			 * @public
481 			 * @returns {String} dictionary filename
482 			 */
483 			getSpeechConfigFileName: function(){
484 				return speechConfigFileName;
485 			},
486 			/**
487 			 * Returns the name of the grammar filename as string 
488 			 * @function
489 			 * @public
490 			 * @returns {String} grammar filename
491 			 */
492 			getGrammarFileName: function(){
493 				return grammarFileName;
494 			},
495 			/**
496 			 * Returns the prefix for partial filenames as string 
497 			 * @function
498 			 * @public
499 			 * @returns {String} prefix for partial filenames
500 			 */
501 			getPartialsPrefix: function(){
502 				return partialsPrefix;
503 			},
504 			/**
505 			 * Returns the suffix for helper filenames as string. A helpers filename looks like: "ControllerName"+"Helper"+".js"
506 			 * @function
507 			 * @public
508 			 * @returns {String} suffix for helper filenames
509 			 */
510 			getHelperSuffix: function(){
511 				return helperSuffix;
512 			},
513 			/**
514 			 * Returns default language as string.
515 			 * @function
516 			 * @public
517 			 * @returns {String} default language
518 			 */
519 			getLanguage: function(){
520 				return language;
521 			},
522 			
523 			/**
524 	         * Initialize the Constants singleton.
525 	         * 
526 	         * @function
527 	         * @param {Boolean} forBrowserParameter <tt>true</tt> for browser-environment, if <tt>false</tt> ANDROID environment
528 	         * @returns {Object} Object containing the instance of the class {@link mmir.Constants}
529 	         * @public
530 	         */
531 			init: function(theForBrowserParameter){
532 				if (theForBrowserParameter && theForBrowserParameter != isBrowserEnv){
533     				setBasePath(theForBrowserParameter);
534     			}
535 				return this;
536 			},
537 			
538 			/**
539 	         * @function
540 	         * @returns {Boolean}
541 	         * @public
542 	         */
543 			isBrowserEnv: function(){//FIXME replace with real environment-setting/-mechanism
544 				return isBrowserEnv;
545 			},
546 			
547 
548 			/**
549 	         * @function
550 	         * @returns {String}
551 	         * @values "browser" | "cordova" | (or: VALUE set in document's query-parameter "?env=VALUE"
552 	         * @public
553 	         */
554 			getEnv: function(){
555 				return envInfo.envSetting? envInfo.envSetting : 'browser';
556 			},
557 			
558 			/**
559 	         * @function
560 	         * @returns {String}
561 	         * @values "android" | "ios" | "browser" | "default"
562 	         * @public
563 	         */
564 			getEnvPlatform: function(){
565 				return envInfo.platform;
566 			},
567 			
568 			/**
569 			 * @deprecated instead, use Constants-object directly: mmir.Constants
570 			 */
571 			getInstance: function(forBrowserParameter){
572 				return this.init(forBrowserParameter);
573 			}
574 		};//END: return{}
575 		
576 	}//END: constructor()
577 	
578 	instance = new constructor(env);
579 	
580 	return instance;
581 
582 });
583