Commit 50609a53 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

Update JS code for Switch Access Menu Panel to use ES2016.

This is a pure refactor, with no change to code behavior.

Bug: None
Change-Id: Ib864a858b2de4aca6619ad8b002d59d075f94fa1
Reviewed-on: https://chromium-review.googlesource.com/c/1428602
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Auto-Submit: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarKatie Dektar <katie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#625322}
parent b19e8afc
...@@ -3,113 +3,123 @@ ...@@ -3,113 +3,123 @@
// found in the LICENSE file. // found in the LICENSE file.
/** /**
* Class to handle the Switch Access menu panel. * Handles the Switch Access menu panel.
* @constructor
*/ */
function Panel() {} const Panel = {
/**
// This must be kept in sync with the div ID in menu_panel.html * This must be kept in sync with the div ID in menu_panel.html.
Panel.MENU_ID = 'switchaccess_menu_actions'; * @type {string}
// This must be kept in sync with the class name in menu_panel.css */
Panel.FOCUS_CLASS = 'focus'; MENU_ID: 'switchaccess_menu_actions',
/**
* This must be kept in sync with the class name in menu_panel.css.
* @type {string}
*/
FOCUS_CLASS: 'focus',
Panel.readyReceived = false; /**
Panel.loaded = false; * Keeps track of whether we have received a 'ready' from the background page.
window.addEventListener('message', Panel.preMessageHandler); * @type {boolean}
*/
readyReceived: false,
/**
* Keeps track of whether the context menu is loaded.
* @type {boolean}
*/
loaded: false,
/** /**
* Captures messages before the Panel is initialized. * Captures messages before the Panel is initialized.
*/ */
Panel.preMessageHandler = function() { preMessageHandler: () => {
Panel.readyReceived = true; Panel.readyReceived = true;
if (Panel.loaded) if (Panel.loaded)
Panel.sendReady(); Panel.sendReady();
window.removeEventListener('message', Panel.preMessageHandler); window.removeEventListener('message', Panel.preMessageHandler);
}; },
/** /**
* Initialize the panel and buttons. * Initialize the panel and buttons.
*/ */
Panel.init = function() { init: () => {
Panel.loaded = true; Panel.loaded = true;
if (Panel.readyReceived) if (Panel.readyReceived)
Panel.sendReady(); Panel.sendReady();
let div = document.getElementById(Panel.MENU_ID); const div = document.getElementById(Panel.MENU_ID);
for (let button of div.children) for (const button of div.children)
Panel.setupButton(button); Panel.setupButton(button);
window.addEventListener('message', Panel.matchMessage); window.addEventListener('message', Panel.matchMessage);
}; },
/** /**
* Sends a message to the background when both pages are loaded. * Sends a message to the background when both pages are loaded.
*/ */
Panel.sendReady = function() { sendReady: () => {
MessageHandler.sendMessage(MessageHandler.Destination.BACKGROUND, 'ready'); MessageHandler.sendMessage(
}; MessageHandler.Destination.BACKGROUND, MessageHandler.READY);
},
/** /**
* Adds an event listener to the given button to send a message when clicked. * Adds an event listener to the given button to send a message when clicked.
* @param {!HTMLElement} button * @param {!HTMLElement} button
*/ */
Panel.setupButton = function(button) { setupButton: (button) => {
let id = button.id; let id = button.id;
button.addEventListener('click', function() { button.addEventListener('click', function() {
MessageHandler.sendMessage(MessageHandler.Destination.BACKGROUND, id); MessageHandler.sendMessage(MessageHandler.Destination.BACKGROUND, id);
}.bind(id)); }.bind(id));
}; },
/** /**
* Takes the given message and sees if it matches any expected pattern. If * Takes the given message and sees if it matches any expected pattern. If
* it does, extract the parameters and pass them to the appropriate handler. * it does, extract the parameters and pass them to the appropriate handler.
* If not, log as an unrecognized action. * If not, log as an unrecognized action.
* *
* @param {Object} message * @param {Object} message
*/ */
Panel.matchMessage = function(message) { matchMessage: (message) => {
if (message.data === 'ready') {
clearInterval(Panel.handshakeInterval);
return;
}
let matches = message.data.match(MessageHandler.SET_ACTIONS_REGEX); let matches = message.data.match(MessageHandler.SET_ACTIONS_REGEX);
if (matches && matches.length === 2) { if (matches && matches.length === 2) {
let actions = matches[1].split(','); const actions = matches[1].split(',');
Panel.setActions(actions); Panel.setActions(actions);
return; return;
} }
matches = message.data.match(MessageHandler.SET_FOCUS_RING_REGEX); matches = message.data.match(MessageHandler.SET_FOCUS_RING_REGEX);
if (matches && matches.length === 3) { if (matches && matches.length === 3) {
let id = matches[1]; const id = matches[1];
let shouldAdd = matches[2] === 'on'; const shouldAdd = matches[2] === 'on';
Panel.updateStyle(id, Panel.FOCUS_CLASS, shouldAdd); Panel.updateClass(id, Panel.FOCUS_CLASS, shouldAdd);
return; return;
} }
console.log('Action not recognized: ' + message.data); console.log('Action not recognized: ' + message.data);
}; },
/** /**
* Sets which buttons are enabled/disabled, based on |actions|. * Sets which buttons are enabled/disabled, based on |actions|.
* @param {!Array<string>} actions * @param {!Array<string>} actions
*/ */
Panel.setActions = function(actions) { setActions: (actions) => {
let div = document.getElementById(Panel.MENU_ID); const div = document.getElementById(Panel.MENU_ID);
for (let button of div.children) for (const button of div.children)
button.hidden = !actions.includes(button.id); button.hidden = !actions.includes(button.id);
}; },
/** /**
* Sets the style for the element with the given |id| by toggling * Either adds or removes the class |className| for the element with the given
* |className|. * |id|.
* @param {string} id * @param {string} id
* @param {string} className * @param {string} className
* @param {bool} shouldAdd * @param {bool} shouldAdd
*/ */
Panel.updateStyle = function(id, className, shouldAdd) { updateClass: (id, className, shouldAdd) => {
let htmlNode = document.getElementById(id); const htmlNode = document.getElementById(id);
if (shouldAdd) if (shouldAdd)
htmlNode.classList.add(className); htmlNode.classList.add(className);
else else
htmlNode.classList.remove(className); htmlNode.classList.remove(className);
}
}; };
window.addEventListener('message', Panel.preMessageHandler);
window.addEventListener('load', Panel.init); window.addEventListener('load', Panel.init);
...@@ -26,6 +26,12 @@ const MessageHandler = { ...@@ -26,6 +26,12 @@ const MessageHandler = {
*/ */
Destination: {BACKGROUND: 'background', MENU_PANEL: 'menu_panel.html'}, Destination: {BACKGROUND: 'background', MENU_PANEL: 'menu_panel.html'},
/**
* Constant that signals readiness.
* @type {string}
*/
READY: 'ready',
/** /**
* Sends a message to the Switch Access menu panel with the given name and * Sends a message to the Switch Access menu panel with the given name and
* parameters. * parameters.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment