KeyExplorer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  18. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  19. return new (P || (P = Promise))(function (resolve, reject) {
  20. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  21. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  22. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  23. step((generator = generator.apply(thisArg, _arguments || [])).next());
  24. });
  25. };
  26. var __generator = (this && this.__generator) || function (thisArg, body) {
  27. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  28. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  29. function verb(n) { return function (v) { return step([n, v]); }; }
  30. function step(op) {
  31. if (f) throw new TypeError("Generator is already executing.");
  32. while (_) try {
  33. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  34. if (y = 0, t) op = [op[0] & 2, t.value];
  35. switch (op[0]) {
  36. case 0: case 1: t = op; break;
  37. case 4: _.label++; return { value: op[1], done: false };
  38. case 5: _.label++; y = op[1]; op = [0]; continue;
  39. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  40. default:
  41. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  42. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  43. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  44. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  45. if (t[2]) _.ops.pop();
  46. _.trys.pop(); continue;
  47. }
  48. op = body.call(thisArg, _);
  49. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  50. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  51. }
  52. };
  53. var __importDefault = (this && this.__importDefault) || function (mod) {
  54. return (mod && mod.__esModule) ? mod : { "default": mod };
  55. };
  56. Object.defineProperty(exports, "__esModule", { value: true });
  57. exports.Magnifier = exports.SpeechExplorer = exports.AbstractKeyExplorer = void 0;
  58. var Explorer_js_1 = require("./Explorer.js");
  59. var sre_js_1 = __importDefault(require("../sre.js"));
  60. var AbstractKeyExplorer = (function (_super) {
  61. __extends(AbstractKeyExplorer, _super);
  62. function AbstractKeyExplorer() {
  63. var _this = _super !== null && _super.apply(this, arguments) || this;
  64. _this.attached = false;
  65. _this.eventsAttached = false;
  66. _this.events = _super.prototype.Events.call(_this).concat([['keydown', _this.KeyDown.bind(_this)],
  67. ['focusin', _this.FocusIn.bind(_this)],
  68. ['focusout', _this.FocusOut.bind(_this)]]);
  69. _this.oldIndex = null;
  70. return _this;
  71. }
  72. AbstractKeyExplorer.prototype.FocusIn = function (_event) {
  73. };
  74. AbstractKeyExplorer.prototype.FocusOut = function (_event) {
  75. this.Stop();
  76. };
  77. AbstractKeyExplorer.prototype.Update = function (force) {
  78. if (force === void 0) { force = false; }
  79. if (!this.active && !force)
  80. return;
  81. this.highlighter.unhighlight();
  82. var nodes = this.walker.getFocus(true).getNodes();
  83. if (!nodes.length) {
  84. this.walker.refocus();
  85. nodes = this.walker.getFocus().getNodes();
  86. }
  87. this.highlighter.highlight(nodes);
  88. };
  89. AbstractKeyExplorer.prototype.Attach = function () {
  90. _super.prototype.Attach.call(this);
  91. this.attached = true;
  92. this.oldIndex = this.node.tabIndex;
  93. this.node.tabIndex = 1;
  94. this.node.setAttribute('role', 'application');
  95. };
  96. AbstractKeyExplorer.prototype.AddEvents = function () {
  97. if (!this.eventsAttached) {
  98. _super.prototype.AddEvents.call(this);
  99. this.eventsAttached = true;
  100. }
  101. };
  102. AbstractKeyExplorer.prototype.Detach = function () {
  103. if (this.active) {
  104. this.node.tabIndex = this.oldIndex;
  105. this.oldIndex = null;
  106. this.node.removeAttribute('role');
  107. }
  108. this.attached = false;
  109. };
  110. AbstractKeyExplorer.prototype.Stop = function () {
  111. if (this.active) {
  112. this.highlighter.unhighlight();
  113. this.walker.deactivate();
  114. }
  115. _super.prototype.Stop.call(this);
  116. };
  117. return AbstractKeyExplorer;
  118. }(Explorer_js_1.AbstractExplorer));
  119. exports.AbstractKeyExplorer = AbstractKeyExplorer;
  120. var SpeechExplorer = (function (_super) {
  121. __extends(SpeechExplorer, _super);
  122. function SpeechExplorer(document, region, node, mml) {
  123. var _this = _super.call(this, document, region, node) || this;
  124. _this.document = document;
  125. _this.region = region;
  126. _this.node = node;
  127. _this.mml = mml;
  128. _this.showRegion = 'subtitles';
  129. _this.init = false;
  130. _this.restarted = false;
  131. _this.initWalker();
  132. return _this;
  133. }
  134. SpeechExplorer.prototype.Start = function () {
  135. var _this = this;
  136. if (!this.attached)
  137. return;
  138. var options = this.getOptions();
  139. if (!this.init) {
  140. this.init = true;
  141. SpeechExplorer.updatePromise = SpeechExplorer.updatePromise.then(function () { return __awaiter(_this, void 0, void 0, function () {
  142. var _this = this;
  143. return __generator(this, function (_a) {
  144. return [2, sre_js_1.default.sreReady()
  145. .then(function () { return sre_js_1.default.setupEngine({ locale: options.locale }); })
  146. .then(function () {
  147. _this.Speech(_this.walker);
  148. _this.Start();
  149. })];
  150. });
  151. }); })
  152. .catch(function (error) { return console.log(error.message); });
  153. return;
  154. }
  155. _super.prototype.Start.call(this);
  156. this.speechGenerator = sre_js_1.default.getSpeechGenerator('Direct');
  157. this.speechGenerator.setOptions(options);
  158. this.walker = sre_js_1.default.getWalker('table', this.node, this.speechGenerator, this.highlighter, this.mml);
  159. this.walker.activate();
  160. this.Update();
  161. if (this.document.options.a11y[this.showRegion]) {
  162. SpeechExplorer.updatePromise.then(function () { return _this.region.Show(_this.node, _this.highlighter); });
  163. }
  164. this.restarted = true;
  165. };
  166. SpeechExplorer.prototype.Update = function (force) {
  167. var _this = this;
  168. if (force === void 0) { force = false; }
  169. _super.prototype.Update.call(this, force);
  170. var options = this.speechGenerator.getOptions();
  171. if (options.modality === 'speech') {
  172. this.document.options.sre.domain = options.domain;
  173. this.document.options.sre.style = options.style;
  174. this.document.options.a11y.speechRules =
  175. options.domain + '-' + options.style;
  176. }
  177. SpeechExplorer.updatePromise = SpeechExplorer.updatePromise.then(function () { return __awaiter(_this, void 0, void 0, function () {
  178. var _this = this;
  179. return __generator(this, function (_a) {
  180. return [2, sre_js_1.default.sreReady()
  181. .then(function () { return sre_js_1.default.setupEngine({ modality: options.modality,
  182. locale: options.locale }); })
  183. .then(function () { return _this.region.Update(_this.walker.speech()); })];
  184. });
  185. }); });
  186. };
  187. SpeechExplorer.prototype.Speech = function (walker) {
  188. var _this = this;
  189. SpeechExplorer.updatePromise.then(function () {
  190. walker.speech();
  191. _this.node.setAttribute('hasspeech', 'true');
  192. _this.Update();
  193. if (_this.restarted && _this.document.options.a11y[_this.showRegion]) {
  194. _this.region.Show(_this.node, _this.highlighter);
  195. }
  196. });
  197. };
  198. SpeechExplorer.prototype.KeyDown = function (event) {
  199. var code = event.keyCode;
  200. this.walker.modifier = event.shiftKey;
  201. if (code === 27) {
  202. this.Stop();
  203. this.stopEvent(event);
  204. return;
  205. }
  206. if (this.active) {
  207. this.Move(code);
  208. if (this.triggerLink(code))
  209. return;
  210. this.stopEvent(event);
  211. return;
  212. }
  213. if (code === 32 && event.shiftKey || code === 13) {
  214. this.Start();
  215. this.stopEvent(event);
  216. }
  217. };
  218. SpeechExplorer.prototype.triggerLink = function (code) {
  219. var _a, _b;
  220. if (code !== 13) {
  221. return false;
  222. }
  223. var node = (_a = this.walker.getFocus().getNodes()) === null || _a === void 0 ? void 0 : _a[0];
  224. var focus = (_b = node === null || node === void 0 ? void 0 : node.getAttribute('data-semantic-postfix')) === null || _b === void 0 ? void 0 : _b.match(/(^| )link($| )/);
  225. if (focus) {
  226. node.parentNode.dispatchEvent(new MouseEvent('click'));
  227. return true;
  228. }
  229. return false;
  230. };
  231. SpeechExplorer.prototype.Move = function (key) {
  232. this.walker.move(key);
  233. this.Update();
  234. };
  235. SpeechExplorer.prototype.initWalker = function () {
  236. this.speechGenerator = sre_js_1.default.getSpeechGenerator('Tree');
  237. var dummy = sre_js_1.default.getWalker('dummy', this.node, this.speechGenerator, this.highlighter, this.mml);
  238. this.walker = dummy;
  239. };
  240. SpeechExplorer.prototype.getOptions = function () {
  241. var options = this.speechGenerator.getOptions();
  242. var sreOptions = this.document.options.sre;
  243. if (options.modality === 'speech' &&
  244. (options.locale !== sreOptions.locale ||
  245. options.domain !== sreOptions.domain ||
  246. options.style !== sreOptions.style)) {
  247. options.domain = sreOptions.domain;
  248. options.style = sreOptions.style;
  249. options.locale = sreOptions.locale;
  250. this.walker.update(options);
  251. }
  252. return options;
  253. };
  254. SpeechExplorer.updatePromise = Promise.resolve();
  255. return SpeechExplorer;
  256. }(AbstractKeyExplorer));
  257. exports.SpeechExplorer = SpeechExplorer;
  258. var Magnifier = (function (_super) {
  259. __extends(Magnifier, _super);
  260. function Magnifier(document, region, node, mml) {
  261. var _this = _super.call(this, document, region, node) || this;
  262. _this.document = document;
  263. _this.region = region;
  264. _this.node = node;
  265. _this.mml = mml;
  266. _this.walker = sre_js_1.default.getWalker('table', _this.node, sre_js_1.default.getSpeechGenerator('Dummy'), _this.highlighter, _this.mml);
  267. return _this;
  268. }
  269. Magnifier.prototype.Update = function (force) {
  270. if (force === void 0) { force = false; }
  271. _super.prototype.Update.call(this, force);
  272. this.showFocus();
  273. };
  274. Magnifier.prototype.Start = function () {
  275. _super.prototype.Start.call(this);
  276. if (!this.attached)
  277. return;
  278. this.region.Show(this.node, this.highlighter);
  279. this.walker.activate();
  280. this.Update();
  281. };
  282. Magnifier.prototype.showFocus = function () {
  283. var node = this.walker.getFocus().getNodes()[0];
  284. this.region.Show(node, this.highlighter);
  285. };
  286. Magnifier.prototype.Move = function (key) {
  287. var result = this.walker.move(key);
  288. if (result) {
  289. this.Update();
  290. }
  291. };
  292. Magnifier.prototype.KeyDown = function (event) {
  293. var code = event.keyCode;
  294. this.walker.modifier = event.shiftKey;
  295. if (code === 27) {
  296. this.Stop();
  297. this.stopEvent(event);
  298. return;
  299. }
  300. if (this.active && code !== 13) {
  301. this.Move(code);
  302. this.stopEvent(event);
  303. return;
  304. }
  305. if (code === 32 && event.shiftKey || code === 13) {
  306. this.Start();
  307. this.stopEvent(event);
  308. }
  309. };
  310. return Magnifier;
  311. }(AbstractKeyExplorer));
  312. exports.Magnifier = Magnifier;
  313. //# sourceMappingURL=KeyExplorer.js.map