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 define(['core', 'jquery', 'commonUtils', 'module', 'engineConfig', 'logger' ],
 28 	/**
 29 	 * The InputManager handles input events.
 30 	 * 
 31 	 * <p>
 32 	 * On initialization, the InputManager also creates the {@link mmir.InputEngine},
 33 	 * and returns it as the second argument of the {@link #init}() function's callback
 34 	 * (or the Promise's triggered callbacks).
 35 	 * 
 36 	 * In addition, the InputEngine is exported as module <code>"inputEngine"</code> via
 37 	 * RequireJS' <code>define()</code> function.
 38 	 * 
 39 	 * @example
 40 	 * //initialization of inputManager
 41 	 * require('inputManager').init().then( function(inputManagerInstance, inputEngineInstance){
 42 	 * 		//do something
 43 	 * });
 44 	 * 
 45 	 * 
 46 	 * @name mmir.InputManager
 47 	 * @static
 48 	 * @class
 49 	 * 
 50 	 * 
 51      *  @requires jQuery.Deferred
 52      *  @requires jQuery.extend
 53 	 * 
 54 	 */
 55 	function(
 56 		mmir, $, commonUtils,module, engineConfig, Logger
 57 ){
 58 
 59 	//the next comment enables JSDoc2 to map all functions etc. to the correct class description
 60 	/** @scope mmir.InputManager.prototype */
 61 	
 62 	/**
 63 	 * @memberOf mmir.InputManager#
 64 	 */
 65 	var _instance = {
 66 
 67 		/** @scope mmir.InputManager.prototype */
 68 		
 69 		/** 
 70 		 * @deprecated instead: use mmir.InputManager object directly.
 71 		 * 
 72 		 * @memberOf mmir.InputManager.prototype
 73 		 */
 74 		getInstance : function() {
 75 			return this;
 76 		},
 77 		
 78 		/**
 79 		 * This function raises an event. 
 80 		 * 
 81 		 * @function
 82 		 * @param {String} eventName
 83 		 * 				The name of the event which is to be raised
 84 		 * @param {Object} [eventData] OPTIONAL
 85 		 * 				Data belonging to the event
 86 		 * @throws {Error} if this function is invoked while the internal
 87 		 * 				   event/state engine (i.e. {@link mmir.InputEngine}
 88 		 * 				   is not initialized yet
 89 		 * @public
 90 		 */
 91 		raise : function(eventName, eventData) {
 92 			//NOTE the functional implementation will be set during initialization (see below #init())
 93 			throw new Error('InputEngine not initialized yet: '
 94 					+'call mmir.InputManager.init(callback) and wait for the callback.'
 95 			);
 96 		}
 97 
 98 	};
 99 
100 	return $.extend(true, _instance, {
101 
102 		/**
103 		 * @function
104 		 * @name init
105 		 * @returns {Deferred}
106 		 * 
107 		 * @memberOf mmir.InputManager.prototype
108 		 */
109 		init : function() {
110 			delete this.init;
111 			
112 			//"read" settings from requirejs' config (see mainConfig.js):
113 			var url = module.config().scxmlDoc;
114 			var mode = module.config().mode;
115 			
116 			//create a SCION engine:
117 			var engine = engineConfig(url, mode);
118 			
119 			this._log = Logger.create(module);
120 			engine._log = Logger.create(module.id+'Engine', module.config().logLevel);
121 
122 //			var _self = this;
123 
124 			return $.Deferred(function(theDeferredObj) {
125 				
126 				engine.load().done(function(_engine) {
127 					
128 					_instance.raise = function raise(){
129 						_engine.raise.apply(_engine, arguments);
130 					};
131 					
132 //					mmir.InputEngine = _engine;
133 					delete _engine.gen;
134 
135 					//register the InputEngine with requirejs as module "inputEngine":
136 					define("inputEngine", function(){
137 						return _engine;
138 					});
139 					//immediately load the module-definition:
140 					require(['inputEngine'], function(){
141 						//signal end of initialization process:
142 						theDeferredObj.resolve(_instance, _engine);	
143 					});
144 					
145 				});
146 				
147 			}).promise();
148 		}
149 	});
150 
151 });
152