Source: tools/resources.js

  1. define(['mmirf/env', 'module'],//TODO remove module-dependency? -> would need different mechanism for querying env-configuration...
  2. /**
  3. * A Utility class that provides various <i>resources</i> and <i>constants</i>.<br>
  4. *
  5. * <p>
  6. * Note that the actual values depend on the execution environment (e.g. ANDROID vs. BROWSER).
  7. * As a consequence the resources object has multiple modes, that can be
  8. * switched via the {@link #init} -method, e.g. <code>init(false)</code>.
  9. *
  10. *
  11. * @name Resources
  12. * @memberOf mmir
  13. * @static
  14. * @class
  15. * @hideconstructor
  16. *
  17. * @requires org.apache.cordova.device: cordova plugin add org.apache.cordova.device
  18. *
  19. * @example var appBase = mmir.res.getBasePath();
  20. */
  21. function(
  22. env, module
  23. ){
  24. var _modConf = module.config(module);
  25. if(_modConf.basePath){
  26. env.basePath = _modConf.basePath;
  27. }
  28. /**
  29. * Object containing the instance of the class constants
  30. *
  31. * @type mmir.Resources
  32. * @private
  33. * @memberOf Resources#
  34. */
  35. var instance = null;
  36. /**
  37. * @memberOf Resources#
  38. */
  39. var isBrowserEnv = false;
  40. // needed basepath
  41. /**
  42. * the base path for the "invoking" app (i.e. where the main HTML file is located)
  43. * @private
  44. * @memberOf Resources#
  45. */
  46. var basePath = "";
  47. ///////////////////////////////////////////// Paths of library resources (sub-paths within library) ////////////////////////////////////////////////
  48. /**
  49. * the base path of the (i.e. this) library
  50. * @private
  51. * @memberOf Resources#
  52. */
  53. var frameworkBasePath = typeof _modConf.mmirBasePath === 'string'? _modConf.mmirBasePath : "mmirf/";
  54. /**
  55. * the path for WebWorkers (within {link #frameworkBasePath})
  56. * @private
  57. * @memberOf Resources#
  58. */
  59. var workerPath = "workers/";
  60. /**
  61. * the path for Extensions (i.e. extending JavaScript base classes; within {link #frameworkBasePath})
  62. * @private
  63. * @memberOf Resources#
  64. */
  65. var extensionsPath = "tools/extensions/";
  66. /**
  67. * the path to the audio file containing the default beep sound (within {link #frameworkBasePath}).
  68. * @private
  69. * @memberOf Resources#
  70. */
  71. var beepURL = "vendor/sounds/beep-notification.mp3";
  72. /**
  73. * the path to media plugins / modules (within {link #frameworkBasePath})
  74. * @private
  75. * @memberOf Resources#
  76. */
  77. var mediaPluginPath = "env/media/";
  78. /**
  79. * the path to grammar engine implementations / modules (within {link #frameworkBasePath})
  80. * @private
  81. * @memberOf Resources#
  82. */
  83. var grammarPluginPath = "env/grammar/";
  84. ///////////////////////////////////////////////////// Paths of app resources /////////////////////////////////////////////////////
  85. /**
  86. * the path to the app's controllers
  87. * @private
  88. * @memberOf Resources#
  89. */
  90. var controllerPath = "controllers/";
  91. /**
  92. * the path to the app's controller helpers
  93. * @private
  94. * @memberOf Resources#
  95. */
  96. var helperPath = "helpers/";
  97. /**
  98. * the path to the language resources root directory
  99. * @private
  100. * @memberOf Resources#
  101. */
  102. var languagePath = "config/languages/";
  103. /**
  104. * the path to the app's models
  105. * @private
  106. * @memberOf Resources#
  107. */
  108. var modelPath = "models/";
  109. /**
  110. * the path to the app's layouts
  111. * @private
  112. * @memberOf Resources#
  113. */
  114. var layoutPath = "views/layouts/";//before changing this: see also use of 'layouts' sub-dir-name in build/lib/mmir-build/ant/StandaloneTemplateParserExec.js
  115. /**
  116. * the path to the app's view root directory
  117. * @private
  118. * @memberOf Resources#
  119. */
  120. var viewPath = "views/";
  121. /**
  122. * the path to the app's generated (compiled JS) views
  123. * @private
  124. * @memberOf Resources#
  125. */
  126. var genViewPath = "gen/view/";
  127. /**
  128. * the path to the app's generated (compiled JS) layouts
  129. * @private
  130. * @memberOf Resources#
  131. */
  132. var genLayoutPath = "gen/view/layouts/";
  133. /**
  134. * the path to the app's generated (compiled JS) grammars
  135. * @private
  136. * @memberOf Resources#
  137. */
  138. var genGrammarsPath = "gen/grammar/";
  139. /**
  140. * the path to the app's generated (compiled JS) state models
  141. * @private
  142. * @memberOf Resources#
  143. */
  144. var genStateModelsPath = "gen/state/";
  145. ///////////////////////////////////////////////////// Resource Names /////////////////////////////////////////////////////
  146. /**
  147. * the name of speech (output) configuration files
  148. * @private
  149. * @memberOf Resources#
  150. */
  151. var speechConfigFileName = "speech.json";
  152. /**
  153. * the name of (JSON) grammar files, i.e. "grammar definitions"
  154. * @private
  155. * @memberOf Resources#
  156. */
  157. var grammarFileName = "grammar.json";
  158. /**
  159. * the name of language dictionary files
  160. * @private
  161. * @memberOf Resources#
  162. */
  163. var dictionaryFileName = "dictionary.json";
  164. /**
  165. * the name of the app's configuration file
  166. * @private
  167. * @memberOf Resources#
  168. */
  169. var configurationFileUrl = _modConf["configuration.json"]? _modConf["configuration.json"] : "config/configuration.json";
  170. /**
  171. * the name of the app's directory-/file-information file
  172. * @private
  173. * @memberOf Resources#
  174. */
  175. var directoriesFileUrl = _modConf["directories.json"]? _modConf["directories.json"] : "gen/directories.json";
  176. ////////////////////////////////////////////////// General Constant Values///////////////////////////////////////////////////
  177. /**
  178. * the default language setting
  179. * @private
  180. * @memberOf Resources#
  181. */
  182. var language = "en";
  183. // Prefixes
  184. /**
  185. * the prefix for partial-view file-names
  186. * @private
  187. * @memberOf Resources#
  188. */
  189. var partialsPrefix = '~';
  190. /**
  191. * the postfix for controller-helper file-names
  192. * @private
  193. * @memberOf Resources#
  194. */
  195. var helperSuffix = "Helper";
  196. /**
  197. * Object that holds information about the execution
  198. * environment / platform.
  199. *
  200. * (set on initialization)
  201. *
  202. * @private
  203. * @type env
  204. * @memberOf Resources#
  205. */
  206. var envInfo = void(0);
  207. /**
  208. * @private
  209. * @memberOf Resources#
  210. */
  211. function setBasePath(isBrowserEnvParam){
  212. var frameworkBasePathIndex;
  213. //for adjusting framework path, if it is a sub-path of basePath
  214. if(typeof basePath === 'string' && frameworkBasePath && frameworkBasePath.indexOf(basePath) === 0){
  215. frameworkBasePathIndex = basePath.length;
  216. }
  217. // if not on browser: basepath must be different
  218. if(typeof isBrowserEnvParam === 'string'){
  219. basePath = isBrowserEnvParam;
  220. }
  221. else if (isBrowserEnvParam && isBrowserEnvParam.basePath){
  222. basePath = isBrowserEnvParam.basePath;
  223. } else if (isBrowserEnvParam && isBrowserEnvParam.isCordovaEnv){
  224. //if cordova env, try to use the specific platform
  225. var env = isBrowserEnvParam.envSetting;
  226. if(env === 'cordova'){
  227. env = isBrowserEnvParam.platform;
  228. if(env === 'default'){
  229. console.warn('Unknown cordova platform "'+env+'", using default base path /');
  230. }
  231. }
  232. switch(env){
  233. case 'android':
  234. basePath = "file:///android_asset/www/";
  235. break;
  236. // case 'cordova':
  237. case 'ios':
  238. case 'default':
  239. default:
  240. basePath = "";
  241. }
  242. }
  243. else if (isBrowserEnvParam && isBrowserEnvParam.isBrowserEnv){
  244. basePath = "";
  245. }
  246. else if (isBrowserEnvParam && isBrowserEnvParam.isNodeEnv){
  247. //TODO should this be the absolute path for node-env?
  248. basePath = "";//"file:";
  249. }
  250. else if (isBrowserEnvParam === false || typeof isBrowserEnvParam === 'undefined'){
  251. //BACKWARD COMPATIBILITY: false and omitted argument are interpreted as Android env
  252. //TODO remove this?
  253. basePath = "file:///android_asset/www/";
  254. }
  255. else {
  256. //default:
  257. basePath = "";
  258. }
  259. //adjust framework path if basePath & framework path had been set before AND framework path had been a sub-path of basePath
  260. if(isFinite(frameworkBasePathIndex) && (frameworkBasePathIndex > 0 || basePath.length > 0)){
  261. //if param specifies that base path is absolute or provides an isAbsolutePath function -> do not adjust frameworkBasePath in case of absolute path!
  262. if((isBrowserEnvParam && (isBrowserEnvParam.isAbsolutePath === false || (isBrowserEnvParam.isAbsolutePath && (!isBrowserEnvParam.isAbsolutePath(frameworkBasePath) || !isBrowserEnvParam.isAbsolutePath(basePath)))))){
  263. frameworkBasePath = basePath + frameworkBasePath.substring(frameworkBasePathIndex);
  264. }
  265. }
  266. }
  267. /**
  268. * Constructor-Method of Class {@link Resources}<br>
  269. *
  270. * @constructs Resources#
  271. * @memberOf mmir.Resources.prototype
  272. * @private
  273. */
  274. function constructor(env){
  275. envInfo = env;
  276. isBrowserEnv = envInfo.isBrowserEnv;
  277. setBasePath(env);
  278. /** @lends mmir.Resources.prototype */
  279. return {
  280. /**
  281. * Returns a string with the base path.
  282. * @function
  283. * @public
  284. * @returns {String} base path
  285. *
  286. * @memberOf mmir.Resources.prototype
  287. */
  288. getBasePath: function(){
  289. return basePath;
  290. },
  291. getMmirBasePath: function(){
  292. return frameworkBasePath;
  293. },
  294. /**
  295. * Returns a string with the path to the layouts.
  296. * @function
  297. * @public
  298. * @returns {String} layout path
  299. *
  300. * @memberOf mmir.Resources.prototype
  301. */
  302. getLayoutPath: function(){
  303. return basePath+layoutPath;
  304. },
  305. /**
  306. * @memberOf mmir.Resources.prototype
  307. */
  308. getCompiledLayoutPath: function(){
  309. return basePath+genLayoutPath;
  310. },
  311. /**
  312. * Returns a string with the path to the models.
  313. * @function
  314. * @public
  315. * @returns {String} model path
  316. *
  317. * @memberOf mmir.Resources.prototype
  318. */
  319. getModelPath: function(){
  320. return basePath+modelPath;
  321. },
  322. /**
  323. * Returns a string with the path to the views.
  324. * @function
  325. * @public
  326. * @returns {String} view path
  327. *
  328. * @memberOf mmir.Resources.prototype
  329. */
  330. getViewPath: function(){
  331. return basePath+viewPath;
  332. },
  333. /**
  334. * @memberOf mmir.Resources.prototype
  335. */
  336. getCompiledViewPath: function(){
  337. return basePath + genViewPath;
  338. },
  339. /**
  340. * Returns a string with the path to the languages.
  341. * @function
  342. * @public
  343. * @returns {String} language path
  344. *
  345. * @memberOf mmir.Resources.prototype
  346. */
  347. getLanguagePath: function(){
  348. return basePath+languagePath;
  349. },
  350. /**
  351. * Returns a string with the path to the controllers.
  352. * @function
  353. * @public
  354. * @returns {String} controller path
  355. *
  356. * @memberOf mmir.Resources.prototype
  357. */
  358. getControllerPath: function(){
  359. return basePath+controllerPath;
  360. },
  361. /**
  362. * Returns a string with the path to the workers.
  363. * @function
  364. * @public
  365. * @returns {String} worker path
  366. *
  367. * @memberOf mmir.Resources.prototype
  368. */
  369. getWorkerPath: function(){
  370. return frameworkBasePath + workerPath;
  371. },
  372. /**
  373. * Returns a string with the path to the helpers.
  374. * @function
  375. * @public
  376. * @returns {String} helper path
  377. *
  378. * @memberOf mmir.Resources.prototype
  379. */
  380. getHelperPath: function(){
  381. return basePath+helperPath;
  382. },
  383. /**
  384. * Returns a string with the path to the extensions.
  385. * @function
  386. * @public
  387. * @returns {String} extensions path
  388. *
  389. * @memberOf mmir.Resources.prototype
  390. */
  391. getExtensionsPath: function(){
  392. return frameworkBasePath + extensionsPath;
  393. },
  394. /**
  395. * Returns a string with the path to the Media-Plugins.
  396. * @function
  397. * @public
  398. * @returns {String} MediaPlugin path
  399. *
  400. * @memberOf mmir.Resources.prototype
  401. */
  402. getMediaPluginPath: function(){
  403. return frameworkBasePath + mediaPluginPath;
  404. },
  405. /**
  406. * Returns a string with the path to the Grammar-Plugins
  407. * (ie. engines for grammar generation).
  408. *
  409. * @function
  410. * @public
  411. * @returns {String} Grammar Plugin path
  412. *
  413. * @memberOf mmir.Resources.prototype
  414. */
  415. getGrammarPluginPath: function(){
  416. return frameworkBasePath + grammarPluginPath;
  417. },
  418. /**
  419. * Returns a string with the path to the directory that contains the generated/executable grammars.
  420. * @function
  421. * @public
  422. * @returns {String} path for generated grammars (JavaScript files)
  423. *
  424. * @memberOf mmir.Resources.prototype
  425. */
  426. getGeneratedGrammarsPath: function(){
  427. return basePath + genGrammarsPath;
  428. },
  429. /**
  430. * Returns a string with the path to the directory that contains the generated/executable state models (compiled SCXML).
  431. * @function
  432. * @public
  433. * @returns {String} path for generated state models (JavaScript files)
  434. *
  435. * @memberOf mmir.Resources.prototype
  436. */
  437. getGeneratedStateModelsPath: function(){
  438. return basePath + genStateModelsPath;
  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. * @memberOf mmir.Resources.prototype
  447. */
  448. getConfigurationFileUrl: function(){
  449. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  450. 'mmirf/settings/configuration' :
  451. basePath+configurationFileUrl;
  452. },
  453. /**
  454. * Returns a string with the path to the directories file (directory-strucure / file-list).
  455. * @function
  456. * @public
  457. * @returns {String} path to directories file
  458. *
  459. * @memberOf mmir.Resources.prototype
  460. */
  461. getDirectoriesFileUrl: function(){
  462. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  463. 'mmirf/settings/directories' :
  464. basePath+directoriesFileUrl;
  465. },
  466. /**
  467. * Returns a string with the path to the beep audio-file.
  468. * @function
  469. * @public
  470. * @returns {String} path to beep wav file
  471. *
  472. * @memberOf mmir.Resources.prototype
  473. */
  474. getBeepUrl: function(){
  475. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  476. basePath + require('../vendor/sounds/beep-notification.mp3') :
  477. frameworkBasePath + beepURL;
  478. },
  479. /**
  480. * Returns the name of the dictionary filename as string
  481. * @function
  482. * @public
  483. * @param {String} [langCode] OPTIONAL
  484. * the language code (i.e. the ID) for the dictionary file
  485. * @returns {String} dictionary filename,
  486. * or, if langCode is provided, the path to the dictionary file
  487. *
  488. * @memberOf mmir.Resources.prototype
  489. */
  490. getDictionaryFileUrl: function(langCode){
  491. if(langCode){
  492. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  493. 'mmirf/settings/dictionary/' + langCode :
  494. this.getLanguagePath() + langCode + '/' + dictionaryFileName;
  495. }
  496. return dictionaryFileName;
  497. },
  498. /**
  499. * Returns the name of the filename for
  500. * the speech configuration as string
  501. * @function
  502. * @public
  503. * @returns {String} speech-configuration filename
  504. * @param {String} [langCode] OPTIONAL
  505. * the language code (i.e. the ID) for the speech-configuration file
  506. * @returns {String} speech-configuration filename,
  507. * or, if langCode is provided, the path to the speech-configuration file
  508. *
  509. * @memberOf mmir.Resources.prototype
  510. */
  511. getSpeechConfigFileUrl: function(langCode){
  512. if(langCode){
  513. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  514. 'mmirf/settings/speech/' + langCode :
  515. this.getLanguagePath() + langCode + '/' + speechConfigFileName;
  516. }
  517. return speechConfigFileName;
  518. },
  519. /**
  520. * Returns the name of the grammar filename as string
  521. * @function
  522. * @public
  523. * @param {String} [langCode] OPTIONAL
  524. * the language code (i.e. the ID) for the grammar file
  525. * @returns {String} grammar filename,
  526. * or, if langCode is provided, the path to the grammar file
  527. *
  528. * @memberOf mmir.Resources.prototype
  529. */
  530. getGrammarFileUrl: function(langCode){
  531. if(langCode){
  532. return typeof WEBPACK_BUILD !== 'undefined' && WEBPACK_BUILD?
  533. 'mmirf/settings/grammar/' + langCode :
  534. this.getLanguagePath() + langCode + '/' + grammarFileName;
  535. }
  536. return grammarFileName;
  537. },
  538. /**
  539. * Returns the prefix for partial filenames as string
  540. * @function
  541. * @public
  542. * @returns {String} prefix for partial filenames
  543. *
  544. * @memberOf mmir.Resources.prototype
  545. */
  546. getPartialsPrefix: function(){
  547. return partialsPrefix;
  548. },
  549. /**
  550. * Returns the suffix for helper filenames as string. A helpers filename looks like: "ControllerName"+"Helper"+".js"
  551. * @function
  552. * @public
  553. * @returns {String} suffix for helper filenames
  554. *
  555. * @memberOf mmir.Resources.prototype
  556. */
  557. getHelperSuffix: function(){
  558. return helperSuffix;
  559. },
  560. /**
  561. * Returns default language as string.
  562. * @function
  563. * @public
  564. * @returns {String} default language
  565. *
  566. * @memberOf mmir.Resources.prototype
  567. */
  568. getLanguage: function(){
  569. return language;
  570. },
  571. /**
  572. * Initialize the Resources singleton.
  573. *
  574. * @function
  575. * @param {Boolean|String|EnvInfo|{isAbsolutePath: true|function}} forBrowserParameter <tt>true</tt> for browser-environment, if <tt>false</tt> ANDROID environment
  576. * @returns {Object} Object containing the instance of the class {@link mmir.Resources}
  577. * @public
  578. *
  579. * @memberOf mmir.Resources.prototype
  580. */
  581. init: function(theForBrowserParameter){
  582. if (theForBrowserParameter && theForBrowserParameter != isBrowserEnv){
  583. setBasePath(theForBrowserParameter);
  584. }
  585. return this;
  586. },
  587. /**
  588. * @function
  589. * @returns {Boolean}
  590. * @public
  591. *
  592. * @memberOf mmir.Resources.prototype
  593. */
  594. isBrowserEnv: function(){//FIXME replace with real environment-setting/-mechanism
  595. return isBrowserEnv;
  596. },
  597. /**
  598. * @function
  599. * @returns {Boolean}
  600. * @public
  601. *
  602. * @memberOf mmir.Resources.prototype
  603. */
  604. isCordovaEnv: function(){
  605. return env.isCordovaEnv;
  606. },
  607. /**
  608. * @function
  609. * @returns {String}
  610. * @values "browser" | "cordova" | (or: VALUE set in document's query-parameter "?env=VALUE"
  611. * @public
  612. *
  613. * @memberOf mmir.Resources.prototype
  614. */
  615. getEnv: function(){
  616. return envInfo.envSetting? envInfo.envSetting : 'browser';
  617. },
  618. /**
  619. * @function
  620. * @returns {String}
  621. * @values "android" | "ios" | "browser" | "default"
  622. * @public
  623. *
  624. * @memberOf mmir.Resources.prototype
  625. */
  626. getEnvPlatform: function(){
  627. return envInfo.platform;
  628. }
  629. };//END: return{}
  630. }//END: constructor()
  631. instance = new constructor(env);
  632. return instance;
  633. });