accessibility-menu.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************
  2. *
  3. * [Contrib]/a11y/accessibility-menu.js
  4. *
  5. * A thin extension to add opt-in menu items for the accessibility
  6. * extensions in the a11y contributed directory.
  7. *
  8. * ---------------------------------------------------------------------
  9. *
  10. * Copyright (c) 2016-2017 The MathJax Consortium
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. (function(HUB,EXTENSIONS) {
  25. var SETTINGS = HUB.config.menuSettings;
  26. var ITEM, MENU; // filled in when MathMenu extension loads
  27. var BIND = (Function.prototype.bind ? function (f,t) {return f.bind(t)} :
  28. function (f,t) {return function () {f.apply(t,arguments)}});
  29. var KEYS = Object.keys || function (obj) {
  30. var keys = [];
  31. for (var id in obj) {if (obj.hasOwnProperty(id)) keys.push(id)}
  32. return keys;
  33. };
  34. //
  35. // Set up the a11y path,if it isn't already in place
  36. //
  37. var PATH = MathJax.Ajax.config.path;
  38. if (!PATH.a11y) PATH.a11y = HUB.config.root + "/extensions/a11y";
  39. var Accessibility = EXTENSIONS["accessibility-menu"] = {
  40. version: '1.2.3',
  41. prefix: '', //'Accessibility-',
  42. defaults: {},
  43. modules: [],
  44. MakeOption: function(name) {
  45. return Accessibility.prefix + name;
  46. },
  47. GetOption: function(option) {
  48. return SETTINGS[Accessibility.MakeOption(option)];
  49. },
  50. AddDefaults: function() {
  51. var keys = KEYS(Accessibility.defaults);
  52. for (var i = 0, key; key = keys[i]; i++) {
  53. var option = Accessibility.MakeOption(key);
  54. if (typeof(SETTINGS[option]) === 'undefined') {
  55. SETTINGS[option] = Accessibility.defaults[key];
  56. }
  57. }
  58. },
  59. // Attaches the menu items;
  60. AddMenu: function() {
  61. var items = Array(this.modules.length);
  62. for (var i = 0, module; module = this.modules[i]; i++) items[i] = module.placeHolder;
  63. var menu = MENU.FindId('Accessibility');
  64. if (menu) {
  65. items.unshift(ITEM.RULE());
  66. menu.submenu.items.push.apply(menu.submenu.items,items);
  67. } else {
  68. var renderer = (MENU.FindId("Settings","Renderer")||{}).submenu;
  69. if (renderer) {
  70. // move AssitiveMML and InTabOrder from Renderer to Accessibility menu
  71. items.unshift(ITEM.RULE());
  72. items.unshift(renderer.items.pop());
  73. items.unshift(renderer.items.pop());
  74. }
  75. items.unshift("Accessibility");
  76. var menu = ITEM.SUBMENU.apply(ITEM.SUBMENU,items);
  77. var locale = MENU.IndexOfId('Locale');
  78. if (locale) {
  79. MENU.items.splice(locale,0,menu);
  80. } else {
  81. MENU.items.push(ITEM.RULE(), menu);
  82. }
  83. }
  84. },
  85. Register: function(module) {
  86. Accessibility.defaults[module.option] = false;
  87. Accessibility.modules.push(module);
  88. },
  89. Startup: function() {
  90. ITEM = MathJax.Menu.ITEM;
  91. MENU = MathJax.Menu.menu;
  92. for (var i = 0, module; module = this.modules[i]; i++) module.CreateMenu();
  93. this.AddMenu();
  94. },
  95. LoadExtensions: function () {
  96. var extensions = [];
  97. for (var i = 0, module; module = this.modules[i]; i++) {
  98. if (SETTINGS[module.option]) extensions.push(module.module);
  99. }
  100. return (extensions.length ? HUB.Startup.loadArray(extensions) : null);
  101. }
  102. };
  103. var ModuleLoader = MathJax.Extension.ModuleLoader = MathJax.Object.Subclass({
  104. option: '',
  105. name: ['',''],
  106. module: '',
  107. placeHolder: null,
  108. submenu: false,
  109. extension: null,
  110. Init: function(option, name, module, extension, submenu) {
  111. this.option = option;
  112. this.name = [name.replace(/ /g,''),name];
  113. this.module = module;
  114. this.extension = extension;
  115. this.submenu = (submenu || false);
  116. },
  117. CreateMenu: function() {
  118. var load = BIND(this.Load,this);
  119. if (this.submenu) {
  120. this.placeHolder =
  121. ITEM.SUBMENU(this.name,
  122. ITEM.CHECKBOX(["Activate","Activate"],
  123. Accessibility.MakeOption(this.option), {action: load}),
  124. ITEM.RULE(),
  125. ITEM.COMMAND(["OptionsWhenActive","(Options when Active)"],null,{disabled:true})
  126. );
  127. } else {
  128. this.placeHolder = ITEM.CHECKBOX(
  129. this.name, Accessibility.MakeOption(this.option), {action: load}
  130. );
  131. }
  132. },
  133. Load: function() {
  134. HUB.Queue(["Require",MathJax.Ajax,this.module,["Enable",this]]);
  135. },
  136. Enable: function(menu) {
  137. var extension = MathJax.Extension[this.extension];
  138. if (extension) {
  139. extension.Enable(true,true);
  140. MathJax.Menu.saveCookie();
  141. }
  142. }
  143. });
  144. Accessibility.Register(
  145. ModuleLoader(
  146. 'collapsible', 'Collapsible Math', '[a11y]/collapsible.js', 'collapsible'
  147. )
  148. );
  149. Accessibility.Register(
  150. ModuleLoader(
  151. 'autocollapse', 'Auto Collapse', '[a11y]/auto-collapse.js', 'auto-collapse'
  152. )
  153. );
  154. Accessibility.Register(
  155. ModuleLoader(
  156. 'explorer', 'Explorer', '[a11y]/explorer.js', 'explorer', true
  157. )
  158. );
  159. Accessibility.AddDefaults();
  160. HUB.Register.StartupHook('End Extensions', function () {
  161. HUB.Register.StartupHook('MathMenu Ready', function () {
  162. Accessibility.Startup();
  163. HUB.Startup.signal.Post('Accessibility Menu Ready');
  164. },5); // run before other extensions' menu hooks even if they are loaded first
  165. },5);
  166. MathJax.Hub.Register.StartupHook("End Cookie", function () {
  167. MathJax.Callback.Queue(
  168. ["LoadExtensions",Accessibility],
  169. ["loadComplete",MathJax.Ajax,"[a11y]/accessibility-menu.js"]
  170. );
  171. });
  172. })(MathJax.Hub,MathJax.Extension);