math_map.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.loadLocale = loadLocale;
  13. exports.standardLoader = standardLoader;
  14. const BrowserUtil = require("../common/browser_util.js");
  15. const engine_js_1 = require("../common/engine.js");
  16. const EngineConst = require("../common/engine_const.js");
  17. const FileUtil = require("../common/file_util.js");
  18. const system_external_js_1 = require("../common/system_external.js");
  19. const dynamic_cstr_js_1 = require("../rule_engine/dynamic_cstr.js");
  20. const MathCompoundStore = require("../rule_engine/math_compound_store.js");
  21. const speech_rule_engine_js_1 = require("../rule_engine/speech_rule_engine.js");
  22. const l10n_js_1 = require("../l10n/l10n.js");
  23. const AlphabetGenerator = require("./alphabet_generator.js");
  24. const addSymbols = {
  25. functions: MathCompoundStore.addFunctionRules,
  26. symbols: MathCompoundStore.addSymbolRules,
  27. units: MathCompoundStore.addUnitRules,
  28. si: (x) => x.forEach(MathCompoundStore.setSiPrefixes),
  29. messages: l10n_js_1.completeLocale,
  30. rules: speech_rule_engine_js_1.SpeechRuleEngine.addStore,
  31. characters: MathCompoundStore.addCharacterRules
  32. };
  33. let _init = false;
  34. function loadLocale() {
  35. return __awaiter(this, arguments, void 0, function* (locale = engine_js_1.Engine.getInstance().locale) {
  36. if (!_init) {
  37. AlphabetGenerator.generateBase();
  38. _loadLocale(dynamic_cstr_js_1.DynamicCstr.BASE_LOCALE);
  39. _init = true;
  40. }
  41. return engine_js_1.EnginePromise.promises[dynamic_cstr_js_1.DynamicCstr.BASE_LOCALE].then(() => __awaiter(this, void 0, void 0, function* () {
  42. const defLoc = engine_js_1.Engine.getInstance().defaultLocale;
  43. if (defLoc) {
  44. _loadLocale(defLoc);
  45. return engine_js_1.EnginePromise.promises[defLoc].then(() => __awaiter(this, void 0, void 0, function* () {
  46. _loadLocale(locale);
  47. return engine_js_1.EnginePromise.promises[locale];
  48. }));
  49. }
  50. _loadLocale(locale);
  51. return engine_js_1.EnginePromise.promises[locale];
  52. }));
  53. });
  54. }
  55. function _loadLocale(locale = engine_js_1.Engine.getInstance().locale) {
  56. if (!engine_js_1.EnginePromise.loaded[locale]) {
  57. engine_js_1.EnginePromise.loaded[locale] = [false, false];
  58. MathCompoundStore.reset();
  59. retrieveMaps(locale);
  60. }
  61. }
  62. function loadMethod() {
  63. if (engine_js_1.Engine.getInstance().customLoader) {
  64. return engine_js_1.Engine.getInstance().customLoader;
  65. }
  66. return standardLoader();
  67. }
  68. function standardLoader() {
  69. switch (engine_js_1.Engine.getInstance().mode) {
  70. case EngineConst.Mode.ASYNC:
  71. return loadFile;
  72. case EngineConst.Mode.HTTP:
  73. return loadAjax;
  74. case EngineConst.Mode.SYNC:
  75. default:
  76. return loadFileSync;
  77. }
  78. }
  79. function retrieveFiles(locale) {
  80. const loader = loadMethod();
  81. const promise = new Promise((res) => {
  82. const inner = loader(locale);
  83. inner.then((str) => {
  84. parseMaps(str);
  85. engine_js_1.EnginePromise.loaded[locale] = [true, true];
  86. res(locale);
  87. }, (_err) => {
  88. engine_js_1.EnginePromise.loaded[locale] = [true, false];
  89. console.error(`Unable to load locale: ${locale}`);
  90. engine_js_1.Engine.getInstance().locale = engine_js_1.Engine.getInstance().defaultLocale;
  91. res(locale);
  92. });
  93. });
  94. engine_js_1.EnginePromise.promises[locale] = promise;
  95. }
  96. function parseMaps(json) {
  97. const js = typeof json === 'string'
  98. ? JSON.parse(json)
  99. : json;
  100. addMaps(js);
  101. }
  102. function addMaps(json, opt_locale) {
  103. let generate = true;
  104. for (let i = 0, key; (key = Object.keys(json)[i]); i++) {
  105. const info = key.split('/');
  106. if (opt_locale && opt_locale !== info[0]) {
  107. continue;
  108. }
  109. if (generate && info[1] === 'symbols' && info[0] !== 'base') {
  110. AlphabetGenerator.generate(info[0]);
  111. generate = false;
  112. }
  113. addSymbols[info[1]](json[key]);
  114. }
  115. }
  116. function retrieveMaps(locale) {
  117. if (engine_js_1.Engine.getInstance().isIE &&
  118. engine_js_1.Engine.getInstance().mode === EngineConst.Mode.HTTP) {
  119. getJsonIE_(locale);
  120. return;
  121. }
  122. retrieveFiles(locale);
  123. }
  124. function getJsonIE_(locale, opt_count) {
  125. let count = opt_count || 1;
  126. if (!BrowserUtil.mapsForIE) {
  127. if (count <= 5) {
  128. setTimeout((() => getJsonIE_(locale, count++)).bind(this), 300);
  129. }
  130. return;
  131. }
  132. addMaps(BrowserUtil.mapsForIE, locale);
  133. }
  134. function loadFile(locale) {
  135. const file = FileUtil.localePath(locale);
  136. return new Promise((res, rej) => {
  137. system_external_js_1.SystemExternal.fs.readFile(file, 'utf8', (err, json) => {
  138. if (err) {
  139. return rej(err);
  140. }
  141. res(json);
  142. });
  143. });
  144. }
  145. function loadFileSync(locale) {
  146. const file = FileUtil.localePath(locale);
  147. return new Promise((res, rej) => {
  148. let str = '{}';
  149. try {
  150. str = system_external_js_1.SystemExternal.fs.readFileSync(file, 'utf8');
  151. }
  152. catch (err) {
  153. return rej(err);
  154. }
  155. res(str);
  156. });
  157. }
  158. function loadAjax(locale) {
  159. const file = FileUtil.localePath(locale);
  160. const httpRequest = new XMLHttpRequest();
  161. return new Promise((res, rej) => {
  162. httpRequest.onreadystatechange = function () {
  163. if (httpRequest.readyState === 4) {
  164. const status = httpRequest.status;
  165. if (status === 0 || (status >= 200 && status < 400)) {
  166. res(httpRequest.responseText);
  167. }
  168. else {
  169. rej(status);
  170. }
  171. }
  172. };
  173. httpRequest.open('GET', file, true);
  174. httpRequest.send();
  175. });
  176. }