Source: tools/extensions/Core3Compatibility.js

  1. define(['require', 'mmirf/semanticInterpreterCompatibility',
  2. 'mmirf/configurationManager','mmirf/controllerManager','mmirf/dialogManager','mmirf/dialogEngine',
  3. 'mmirf/inputManager','mmirf/inputEngine','mmirf/commonUtils','mmirf/languageManager','mmirf/mediaManager','mmirf/presentationManager',
  4. 'mmirf/semanticInterpreter','mmirf/modelManager','mmirf/resources','mmirf/notificationManager','mmirf/grammarConverter', 'mmirf/util/deferred',
  5. 'mmirf/parseUtils', 'mmirf/renderUtils', 'mmirf/scionEngine',
  6. //only loaded, not used as argument:
  7. 'mmirf/stringExtension'
  8. ],
  9. /**
  10. * Set to "backwards compatibility mode v3" (for pre version 4.0) for module names and method names.
  11. *
  12. * This function adds old names/synonyms for modules names (on <code>mmir</code> object/namespace):
  13. * <ul>
  14. * <li> {@link mmir.CommonUtils} as
  15. * <b><u>mmir.util</u></b>
  16. * </li><li> {@link mmir.ConfigurationManager} as
  17. * <b><u>mmir.conf</u></b>
  18. * </li><li> {@link mmir.ControllerManager} as
  19. * <b><u>mmir.ctrl</u></b>
  20. * </li><li> {@link mmir.Resources } as
  21. * <b><u>mmir.const</u></b>
  22. * </li><li> {@link mmir.DialogManager} as
  23. * <b><u>mmir.dialog</u></b>
  24. * </li><li> {@link mmir.InputManager } as
  25. * <b><u>mmir.input</u></b>
  26. * </li><li> {@link mmir.LanguageManager} as
  27. * <b><u>mmir.lang</u></b>
  28. * </li><li> {@link mmir.MediaManager} as
  29. * <b><u>mmir.media</u></b>
  30. * </li><li> {@link mmir.ModelManager} as
  31. * <b><u>mmir.model</u></b>
  32. * </li><li> {@link mmir.NotificationManager} as
  33. * <b><u>mmir.notifier</u></b>
  34. * </li><li> {@link mmir.PresentationManager} as
  35. * <b><u>mmir.presentation</u></b>
  36. * </li><li> {@link mmir.SemanticInterpreter} as
  37. * <b><u>mmir.semantic</u></b>
  38. * </li>
  39. * </ul>
  40. *
  41. * In addition, old method names will be added as synonyms:
  42. * <ul>
  43. * <li> {@link mmir.ConfigurationManager}
  44. * <ul>
  45. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.ConfigurationManager}</li>
  46. * </ul>
  47. * </li><li> {@link mmir.Resources}
  48. * <ul>
  49. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.Resources}</li>
  50. * </ul>
  51. * </li><li> {@link mmir.ControllerManager}
  52. * <ul>
  53. * <li><b><u>get</u></b> for {@link mmir.ControllerManager#getController}</li>
  54. * <li><b><u>getNames</u></b> for {@link mmir.ControllerManager#getControllerNames}</li>
  55. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.ControllerManager}</li>
  56. * </ul>
  57. * </li><li> {@link mmir.DialogManager}
  58. * <ul>
  59. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.DialogManager}</li>
  60. * </ul>
  61. * </li><li> {@link mmir.InputManager}
  62. * <ul>
  63. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.InputManager}</li>
  64. * </ul>
  65. * </li><li> {@link mmir.LanguageManager}
  66. * <ul>
  67. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.LanguageManager}</li>
  68. * </ul>
  69. * </li><li> {@link mmir.ModelManager}
  70. * <ul>
  71. * <li><b><u>get</u></b> for {@link mmir.ModelManager#getController}</li>
  72. * <li><b><u>getNames</u></b> for {@link mmir.ModelManager#getControllerNames}</li>
  73. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.ModelManager}</li>
  74. * </ul>
  75. * </li><li> {@link mmir.ModelManager}
  76. * <ul>
  77. * <li><b><u>get</u></b> for {@link mmir.ModelManager#getController}</li>
  78. * <li><b><u>getNames</u></b> for {@link mmir.ModelManager#getControllerNames}</li>
  79. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.ModelManager}</li>
  80. * </ul>
  81. * </li>
  82. * <li> {@link mmir.NotificationManager}
  83. * <ul>
  84. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.NotificationManager}</li>
  85. * </ul>
  86. * </li>
  87. * <li> {@link mmir.PresentationManager}
  88. * <ul>
  89. * <li><b><u>renderView</u></b> for {@link mmir.PresentationManager#render}</li>
  90. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.PresentationManager}</li>
  91. * </ul>
  92. * </li><li> {@link mmir.parser.ParserUtils}
  93. * <ul>
  94. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.parser.ParserUtils}</li>
  95. * </ul>
  96. * </li><li> {@link mmir.parser.RenderUtils}
  97. * <ul>
  98. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.parser.RenderUtils}</li>
  99. * </ul>
  100. * </li><li> {@link mmir.SemanticInterpreter}
  101. * <ul>
  102. * <li><b><u>getASRSemantic</u></b> for {@link mmir.SemanticInterpreter#interpret}</li>
  103. * <li><b><u>getInstance</u></b> <em>(removed)</em> for {@link mmir.SemanticInterpreter}</li>
  104. * </ul>
  105. * </li>
  106. * </ul>
  107. *
  108. * Methods with changed signature will be re-mapped to match their old signature
  109. * <ul>
  110. * <li> {@link mmir.ConfigurationManager}
  111. * <ul>
  112. * <li>map <b><u>get(propertyName, defaultValue, useSafeAccess)</u></b> back to <br>
  113. * get(propertyName, useSafeAccess, defaultValue)<br>
  114. * the original function will be available via <em>_get(propertyName, useSafeAccess, defaultValue)</em>
  115. * </li>
  116. * <li>map <b><u>getBoolean(propertyName, defaultValue, useSafeAccess)</u></b> back to <br>
  117. * getBoolean(propertyName, useSafeAccess, defaultValue)<br>
  118. * the original function will be available via <em>_getBoolean(propertyName, useSafeAccess, defaultValue)</em>
  119. * </li>
  120. * <li>map <b><u>getString(propertyName, defaultValue, useSafeAccess)</u></b> back to <br>
  121. * getString(propertyName, useSafeAccess, defaultValue)<br>
  122. * the original function will be available via <em>_getString(propertyName, useSafeAccess, defaultValue)</em>
  123. * </li>
  124. * </ul>
  125. * </li>
  126. * </ul>
  127. *
  128. * Lastly, removed methods will be added:
  129. * <ul>
  130. * <li> {@link mmir.Resources}
  131. * <ul>
  132. * <li><b><u>getPluginsPath</u></b> <em>(removed)</em> to {@link mmir.CommonUtils}</li>
  133. * </ul>
  134. * </li>
  135. * <li> {@link mmir.CommonUtils}
  136. * <ul>
  137. * <li><b><u>loadAllCordovaPlugins</u></b> <em>(removed)</em> to {@link mmir.CommonUtils}</li>
  138. * <li><b><u>setToCompatibilityMode</u></b> <em>(removed)</em> to {@link mmir.CommonUtils}</li>
  139. * </ul>
  140. * </li>
  141. * <li> {@link mmir.LanguageManager}
  142. * <ul>
  143. * <li><b><u>setToCompatibilityMode</u></b> <em>(removed)</em> to {@link mmir.LanguageManager}</li>
  144. * </ul>
  145. * </li>
  146. * </ul>
  147. *
  148. * @param {mmir} mmir
  149. * the (core) instance/namespace for MMIR
  150. *
  151. *
  152. * @class
  153. * @name mmir.compat.v3.CoreCompat
  154. * @static
  155. *
  156. * @see mmir.compat.v3.CommonUtils
  157. * @see mmir.compat.v3.Constants
  158. * @see mmir.compat.v3.LanguageManager
  159. *
  160. * @requires SemanticInterpreterCompatibility
  161. *
  162. * @example
  163. * mmir.require(['mmirf/core3Compatibility', 'mmirf/core'], function(setCompatibility, mmir){
  164. * setCompatibility(mmir);
  165. * });
  166. *
  167. * //OR: if mmir-lib modules were require'd in application code, add v3 module-ID aliases first:
  168. * mmir.require(['mmirf/core3ModuleIdCompatibility', 'mmirf/core3Compatibility', 'mmirf/core'], function(core3ModuleIdCompatibility, setCompatibility, mmir){
  169. * core3ModuleIdCompatibility(mmir.require, mmir);
  170. * setCompatibility(mmir);
  171. * });
  172. *
  173. * @public
  174. */
  175. function(require, semanticInterpreterCompatibility,
  176. configurationManager, controllerManager, dialogManager, dialogEngine,
  177. inputManager, inputEngine, commonUtils, languageManager, mediaManager, presentationManager,
  178. semanticInterpreter, modelManager, constants, notificationManager, GrammarConverter, deferred,
  179. parseUtils, renderUtils, scionEngine
  180. ){
  181. /**
  182. * Set to "backwards compatibility mode" (for pre version 4.0).
  183. *
  184. * This function re-adds deprecated and removed functions and
  185. * properties to the (core) mmir namespace.
  186. *
  187. * NOTE that once set to compatibility mode, it cannot be reset to
  188. * non-compatibility mode.
  189. *
  190. *
  191. * @param {mmir} mmir
  192. * the (core) instance/namespace for MMIR
  193. *
  194. * @constructs mmir.compat.v3.CoreCompat
  195. */
  196. return function setToCompatibilityMode(mmir) {
  197. mmir.ConfigurationManager = configurationManager;
  198. mmir.ControllerManager = controllerManager;
  199. mmir.PresentationManager = presentationManager;
  200. mmir.DialogManager = dialogManager;
  201. mmir.DialogEngine = dialogEngine;
  202. mmir.InputManager = inputManager;
  203. mmir.InputEngine = inputEngine;
  204. mmir.CommonUtils = commonUtils;
  205. mmir.LanguageManager = languageManager;
  206. mmir.MediaManager = mediaManager;
  207. mmir.SemanticInterpreter = semanticInterpreter;
  208. mmir.ModelManager = modelManager;
  209. mmir.Constants = constants;
  210. mmir.NotificationManager = notificationManager;
  211. // mediaManager.textToSpech = mediaManager.tts;
  212. // mediaManager.setTextToSpeechVolume = mediaManager.ttsVolume;
  213. presentationManager.renderView = presentationManager.render;
  214. controllerManager.getController = controllerManager.get;
  215. controllerManager.getControllerNames = controllerManager.getNames;
  216. modelManager.getModel = modelManager.get;
  217. modelManager.getModels = modelManager.getNames;
  218. //set backwards-compatibility mode for semanticInterpreter and GrammarConverter
  219. semanticInterpreterCompatibility(semanticInterpreter, GrammarConverter);
  220. var getInstance = function(){return this;};
  221. var getInstanceAsInit = function(){return this.init.apply(this, arguments);};
  222. constants.getInstance = getInstanceAsInit;
  223. controllerManager.getInstance = getInstance;
  224. dialogManager.getInstance = getInstance;
  225. inputManager.getInstance = getInstance;
  226. languageManager.getInstance = function(lang){
  227. if(lang) {
  228. setLanguage(lang);
  229. }
  230. return this;
  231. };
  232. mediaManager.getInstance = function(){
  233. return this.init(null, null);
  234. };
  235. modelManager.getInstance = getInstanceAsInit;
  236. notificationManager.getInstance = getInstance;
  237. presentationManager.getInstance = getInstance;
  238. parseUtils.getInstance = getInstance;
  239. //DISABLED would not effect scionEngine instances, that were already created
  240. // ... and scionEninge (and created instance) are only internally used, so application code should be not effected...
  241. // requirejs.undef('mmirf/scionEngine');
  242. // define('mmirf/scionEngine', function(){
  243. // return function(configuration , context){
  244. // var instance = scionEngine(configuration , context);
  245. // instance.getInstance = getInstance;
  246. // return instance;
  247. // };
  248. // });
  249. // require(['mmirf/scionEngine']);
  250. renderUtils.getInstance = getInstance;
  251. configurationManager.getLanguage = function(){ return languageManager.getLanguage(); };
  252. configurationManager.setLanguage = function(lang){ languageManager.setLanguage(lang); };
  253. configurationManager._get = configurationManager.get;
  254. configurationManager.get = function(propertyName, useSafeAccess, defaultValue){
  255. return this._get(propertyName, defaultValue, useSafeAccess);
  256. };
  257. configurationManager._getBoolean = configurationManager.getBoolean;
  258. configurationManager.getBoolean = function(propertyName, useSafeAccess, defaultValue){
  259. return this._getBoolean(propertyName, defaultValue, useSafeAccess);
  260. };
  261. configurationManager._getString = configurationManager.getString;
  262. configurationManager.getString = function(propertyName, useSafeAccess, defaultValue){
  263. return this._getString(propertyName, defaultValue, useSafeAccess);
  264. };
  265. /**
  266. * Set to "backwards compatibility mode" (for pre version 2.0).
  267. *
  268. * This function re-adds deprecated and removed functions and
  269. * properties to the CommonUtils instance.
  270. *
  271. * NOTE that once set to compatibility mode, it cannot be reset to
  272. * non-compatibility mode.
  273. *
  274. * NOTE: Requires jQuery to be present.
  275. *
  276. * @deprecated use only for backwards compatibility
  277. *
  278. * @async
  279. * @requires jQuery
  280. * @requires mmir.compat.v1.CommonUtils
  281. *
  282. * @param {Function} [success]
  283. * a callback function that is invoked, after compatibility mode
  284. * was set (alternatively the returned promise can be used).
  285. * @param {Function} [requireFunction]
  286. * the require-function that is configured for loading the compatibility module/file.
  287. * Normally, this would be the function <code>mmir.require</code>.
  288. * If omitted, the default (local dependency) <code>require</code> function will be used.
  289. * NOTE: this argument is positional, i.e. argument <code>success</code> must be present, if
  290. * you want to specify this argument
  291. * @returns {Promise}
  292. * a deferred promise that is resolved, after compatibility mode
  293. * was set
  294. *
  295. * @memberOf mmir.compat.v3.CommonUtils#
  296. */
  297. commonUtils.setToCompatibilityMode = function(success, requireFunction) {
  298. var defer = deferred();
  299. if(success){
  300. defer.then(success, success);
  301. }
  302. requireFunction = requireFunction || require;
  303. requireFunction(['mmirf/commonUtilsCompatibility'],function(setCompatibility){
  304. setCompatibility(commonUtils);
  305. defer.resolve();
  306. });
  307. return defer;
  308. };
  309. /**
  310. * Set to "backwards compatibility mode" (for pre version 2.0).
  311. *
  312. * This function re-adds deprecated and removed functions and
  313. * properties to the CommonUtils instance.
  314. *
  315. * NOTE that once set to compatibility mode, it cannot be reset to
  316. * non-compatibility mode.
  317. *
  318. * NOTE: Requires jQuery to be present.
  319. *
  320. * @deprecated use only for backwards compatibility
  321. *
  322. * @public
  323. * @async
  324. * @requires jQuery
  325. * @requires mmir.compat.v1.LanguageManager
  326. *
  327. * @param {Function} [success]
  328. * a callback function that is invoked, after compatibility mode
  329. * was set (alternatively the returned promise can be used).
  330. * @param {Function} [requireFunction]
  331. * the require-function that is configured for loading the compatibility module/file.
  332. * Normally, this would be the function <code>mmir.require</code>.
  333. * If omitted, the global <code>require</code> function will be used.
  334. * NOTE: this argument is positional, i.e. argument <code>success</code> must be present, if
  335. * you want to specify this argument
  336. * @returns {Promise}
  337. * a deferred promise that is resolved, after compatibility mode
  338. * was set
  339. *
  340. * @memberOf mmir.compat.v3.LanguageManager#
  341. */
  342. languageManager.setToCompatibilityMode = function(success, requireFunction) {
  343. var defer = deferred();
  344. if(success){
  345. defer.then(success, success);
  346. }
  347. requireFunction = requireFunction || require;
  348. requireFunction(['mmirf/languageManagerCompatibility'],function(setCompatibility){
  349. setCompatibility(languageManager);
  350. defer.resolve();
  351. });
  352. return defer;
  353. };
  354. /**
  355. * Cycles through the available languages.
  356. *
  357. * @function
  358. * @returns {String} The (new) current language
  359. * @public
  360. *
  361. * @memberOf mmir.compat.v3.LanguageManager#
  362. */
  363. languageManager.setNextLanguage = function() {
  364. var languages = languageManager.getLanguages();
  365. var currentLanguage = languageManager.getLanguage();
  366. var indexCurrentLanguage = languages.indexOf(currentLanguage);
  367. // if (logger.isVerbose()) logger.v("[LanguageManager] Current language is " + currentLanguage);
  368. if (indexCurrentLanguage > -1) {
  369. indexCurrentLanguage = indexCurrentLanguage + 1;
  370. if (indexCurrentLanguage > languages.length - 1) {
  371. indexCurrentLanguage = 0;
  372. }
  373. currentLanguage = languages[indexCurrentLanguage];
  374. // if (logger.isVerbose()) logger.v("[LanguageManager] Next language is " + currentLanguage);
  375. return languageManager.setLanguage(currentLanguage);
  376. }
  377. return currentLanguage;
  378. };
  379. /**
  380. * Returns a string with the path to the (Cordova v2.x) plugins directory.
  381. * @function
  382. * @public
  383. * @returns {String} plugins directory path
  384. *
  385. * @memberOf mmir.compat.v3.Constants#
  386. */
  387. constants.getPluginsPath = function(){
  388. return constants.getBasePath()+'mmirf/plugins/';
  389. };
  390. /**
  391. * Load all plugins (i.e. JavaScript interfaces for
  392. * Cordova v2 / JavaScript-impl. plugins).
  393. *
  394. * @function
  395. * @param {String} [pluginsPath] OPTIONAL
  396. * Path of the plugins which should be
  397. * loaded, e.g.
  398. * <b>mmirf/plugins/</b>
  399. *
  400. * If omitted: the default plugin-path is used
  401. * (see {@link mmir.compat.v3.Constants#getPluginsPath})
  402. *
  403. * @param {Function} [cbFunction] OPTIONAL
  404. * The function that should be executed after
  405. * the plugins are loaded. If the execution of following
  406. * functions is dependent on the present of plugins, they
  407. * should be triggered from inside the callback-function
  408. *
  409. * @returns {Promise} a Deferred.promise (see loadImpl())
  410. *
  411. * @async
  412. * @public
  413. * @memberOf mmir.compat.v3.CommonUtils#
  414. */
  415. commonUtils.loadAllCordovaPlugins = function(pluginsPath, cbFunction) {
  416. if(typeof pluginsPath === 'function'){
  417. cbFunction = pluginsPath;
  418. pluginsPath = null;
  419. }
  420. if(typeof pluginsPath !== 'string'){
  421. pluginsPath = constants.getPluginsPath();
  422. }
  423. // reads all *.js files in www/mmirf/plugins/
  424. // and loads them dynamically
  425. // IMPORTANT: /assets/www/config/directories.json must be up-to-date!
  426. // (it contains the list of JS-files for the plugins)
  427. // -> use "cordova prepare" for updating
  428. //FIXME: Cordova 2.x mechanism!!! (remove when switching to 3.x ?)
  429. window.plugins = window.plugins || {};
  430. return commonUtils.loadImpl(
  431. pluginsPath,
  432. false,
  433. cbFunction,
  434. function isPluginAlreadyLoaded(pluginFileName) {
  435. if (window.plugins[pluginFileName.replace(/\.[^.]+$/g, "")]) {//<- regexpr for removing file extension
  436. return true;
  437. }
  438. else {
  439. return false;
  440. }
  441. },
  442. function(status, fileName, msg){
  443. if (status === 'info') {
  444. if(logger.isInfo()) logger.info('CommonUtils', 'loadAllCordovaPlugins', 'loaded "'+ fileName + '": ' + msg);
  445. }
  446. else if (status === 'warning') {
  447. if(logger.isWarn()) logger.warn('CommonUtils', 'loadAllCordovaPlugins', 'loading "'+ fileName + '": ' + msg);
  448. }
  449. else if (status === 'error') {
  450. logger.error('CommonUtils', 'loadAllCordovaPlugins', 'loading "'+ fileName + '": ' + msg);
  451. }
  452. else {
  453. logger.error('CommonUtils', 'loadAllCordovaPlugins', status + ' (UNKNOWN STATUS) -> "'+ fileName + '": ' + msg);
  454. }
  455. }
  456. );
  457. }
  458. };
  459. });