Introduces accessor methods for all cached HTML nodes.

This commit is contained in:
zorkow 2015-08-25 13:48:00 +01:00
parent e29e3c9e99
commit 816259d6ff

View File

@ -297,7 +297,7 @@
if (event) { if (event) {
var x = event.pageX, y = event.pageY; var x = event.pageX, y = event.pageY;
} }
var node = MENU.node || event.target; var node = MENU.GetNode() || event.target;
if (!x && !y && node) { if (!x && !y && node) {
var rect = node.getBoundingClientRect(); var rect = node.getBoundingClientRect();
x = rect.right; x = rect.right;
@ -480,8 +480,8 @@
jaxs: [], // List of all MathJax nodes. jaxs: [], // List of all MathJax nodes.
hasJaxs: false, // Flag to indicate if the MathJax node list has already hasJaxs: false, // Flag to indicate if the MathJax node list has already
// been computed. // been computed.
node: null, // The node the menu was activated on. node: null, // The node the menu was activated on. HTML!
active: null, // The currently focused item. There can only be one! active: null, // The currently focused item. There can only be one! HTML!
posted: false, // Is a menu open? posted: false, // Is a menu open?
GetJaxs: function() { GetJaxs: function() {
@ -490,6 +490,18 @@
MENU.jaxs.push(node); MENU.jaxs.push(node);
} }
}, },
GetNode: function() {
return MENU.node;
},
SetNode: function(node) {
MENU.node = node;
},
GetActive: function() {
return MENU.active;
},
SetActive: function(node) {
MENU.active = node;
},
// //
// Focus is a global affair, since we only ever want a single focused item. // Focus is a global affair, since we only ever want a single focused item.
// //
@ -497,19 +509,19 @@
if (!MENU.posted) { if (!MENU.posted) {
MENU.Activate(menu); MENU.Activate(menu);
} }
if (MENU.active) { if (MENU.GetActive()) {
MENU.active.tabIndex = -1; MENU.GetActive().tabIndex = -1;
} }
MENU.active = menu; MENU.SetActive(menu);
MENU.active.tabIndex = 0; MENU.GetActive().tabIndex = 0;
MENU.active.focus(); MENU.GetActive().focus();
}, },
Activate: function(event, menu) { Activate: function(event, menu) {
if (!MENU.hasJaxs) { if (!MENU.hasJaxs) {
MENU.GetJaxs(); MENU.GetJaxs();
} }
if (!MENU.node) { if (!MENU.GetNode()) {
MENU.node = document.getElementById(MENU.jax.inputID + '-Frame'); MENU.SetNode(document.getElementById(MENU.jax.inputID + '-Frame'));
} }
for (var j = 0, jax; jax = MENU.jaxs[j]; j++) { for (var j = 0, jax; jax = MENU.jaxs[j]; j++) {
jax.tabIndex = -1; jax.tabIndex = -1;
@ -517,13 +529,13 @@
MENU.posted = true; MENU.posted = true;
}, },
Unfocus: function() { Unfocus: function() {
MENU.active.tabIndex = -1; MENU.GetActive().tabIndex = -1;
MENU.active = null; MENU.SetActive(null);
for (var j = 0, jax; jax = MENU.jaxs[j]; j++) { for (var j = 0, jax; jax = MENU.jaxs[j]; j++) {
jax.tabIndex = 0; jax.tabIndex = 0;
} }
MENU.node.focus(); MENU.GetNode().focus();
MENU.node = null; MENU.SetNode(null);
MENU.posted = false; MENU.posted = false;
}, },
//TODO: A toggle focus method on the top level would avoid having to //TODO: A toggle focus method on the top level would avoid having to
@ -533,13 +545,13 @@
if (len === 0) { if (len === 0) {
return; return;
} }
var next = MENU.jaxs[MENU.Mod(move(MENU.jaxs.indexOf(MENU.node)), len)]; var next = MENU.jaxs[MENU.Mod(move(MENU.jaxs.indexOf(MENU.GetNode())), len)];
if (next === MENU.node) { if (next === MENU.GetNode()) {
return; return;
} }
MENU.menu.Remove(event, menu); MENU.menu.Remove(event, menu);
MENU.jax = MathJax.Hub.getJaxFor(next); MENU.jax = MathJax.Hub.getJaxFor(next);
MENU.node = next; MENU.SetNode(next);
MENU.menu.Post(null); MENU.menu.Post(null);
}, },
Right: function(event, menu) { Right: function(event, menu) {
@ -584,6 +596,12 @@
SetNode: function(node) { SetNode: function(node) {
this.node = node; this.node = node;
}, },
GetMenuNode: function() {
return this.menu;
},
SetMenuNode: function(menu) {
this.menu = menu;
},
Attributes: function(def) { Attributes: function(def) {
return HUB.Insert( return HUB.Insert(
@ -600,14 +618,14 @@
var label = this.Label(def,menu); var label = this.Label(def,menu);
var node = HTML.addElement(menu, "div", def, label); var node = HTML.addElement(menu, "div", def, label);
this.SetNode(node); this.SetNode(node);
this.menu = menu; this.SetMenuNode(menu);
} }
}, },
Name: function () {return _(this.name[0],this.name[1])}, Name: function () {return _(this.name[0],this.name[1])},
Mouseover: function (event,menu) { Mouseover: function (event,menu) {
if (menu.parentNode === MENU.active.parentNode) { if (menu.parentNode === MENU.GetActive().parentNode) {
this.Deactivate(MENU.active); this.Deactivate(MENU.GetActive());
} }
this.Activate(event, menu); this.Activate(event, menu);
}, },
@ -619,7 +637,7 @@
DeactivateSubmenus: function(menu) { DeactivateSubmenus: function(menu) {
var menus = document.getElementById("MathJax_MenuFrame").childNodes, var menus = document.getElementById("MathJax_MenuFrame").childNodes,
items = this.menu.childNodes; items = this.GetMenuNode().childNodes;
for (var i = 0, m = items.length; i < m; i++) { for (var i = 0, m = items.length; i < m; i++) {
var item = items[i].menuItem; var item = items[i].menuItem;
// Deactivates submenu items. // Deactivates submenu items.
@ -633,7 +651,7 @@
RemoveSubmenus: function(menu, menus) { RemoveSubmenus: function(menu, menus) {
menus = menus || document.getElementById("MathJax_MenuFrame").childNodes; menus = menus || document.getElementById("MathJax_MenuFrame").childNodes;
var m = menus.length-1; var m = menus.length-1;
while (m >= 0 && this.menu.menuItem !== menus[m].menuItem) { while (m >= 0 && this.GetMenuNode().menuItem !== menus[m].menuItem) {
menus[m].menuItem.posted = false; menus[m].menuItem.posted = false;
menus[m].parentNode.removeChild(menus[m]); menus[m].parentNode.removeChild(menus[m]);
m--; m--;
@ -698,7 +716,7 @@
return def; return def;
}, },
Move: function(event, item, move) { Move: function(event, item, move) {
var items = this.menu.menuItem.items; var items = this.GetMenuNode().menuItem.items;
var len = items.length; var len = items.length;
var index = items.indexOf(this); var index = items.indexOf(this);
if (index === -1) { if (index === -1) {
@ -718,12 +736,12 @@
this.Move(event, item, function(x) { return x + 1; }); this.Move(event, item, function(x) { return x + 1; });
}, },
Right: function(event, item) { Right: function(event, item) {
if (this.menu.menuItem === MENU.menu) { if (this.GetMenuNode().menuItem === MENU.menu) {
MENU.Right(event, item); MENU.Right(event, item);
} }
}, },
Left: function(event, item) { Left: function(event, item) {
if (this.menu.menuItem === MENU.menu) { if (this.GetMenuNode().menuItem === MENU.menu) {
MENU.Left(event, item); MENU.Left(event, item);
} else { } else {
this.Deactivate(item); this.Deactivate(item);