Commit 241a9109 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

[Switch Access] Simplify node construction

All calls to connectChildren were immediately followed by a call to
setChildren, so I combined them, along with several other readability
changes.

This change is a pure refactor.

Bug: None
Change-Id: If340c812127d95412939977a12f247203da07d86
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902516
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarChris Hall <chrishall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714970}
parent 6d9ed950
......@@ -84,11 +84,8 @@ class GroupNode extends SAChildNode {
children.push(child);
}
const backButton = new BackButtonNode(root);
children.push(backButton);
SARootNode.connectChildren(children);
root.setChildren(children);
children.push(new BackButtonNode(root));
root.children = children;
return root;
}
......
......@@ -57,15 +57,15 @@ class KeyboardNode extends NodeWrapper {
}
const root = new RootNodeWrapper(node);
KeyboardNode.buildHelper(root);
KeyboardNode.findAndSetChildren(root);
return root;
}
/**
* Builds a tree of KeyboardNodes.
* Helper function to connect tree elements, given the root node.
* @param {!RootNodeWrapper} root
*/
static buildHelper(root) {
static findAndSetChildren(root) {
const childConstructor = (node) => new KeyboardNode(node, root);
/** @type {!Array<!chrome.automation.AutomationNode>} */
......@@ -75,11 +75,8 @@ class KeyboardNode extends NodeWrapper {
children = GroupNode.separateByRow(children);
}
const backButton = new BackButtonNode(root);
children.push(backButton);
SARootNode.connectChildren(children);
root.setChildren(children);
children.push(new BackButtonNode(root));
root.children = children;
}
}
......@@ -126,7 +123,7 @@ class KeyboardRootNode extends RootNodeWrapper {
const root = new KeyboardRootNode(keyboard);
root.onEnter_();
KeyboardNode.buildHelper(root);
KeyboardNode.findAndSetChildren(root);
return root;
}
}
......@@ -208,18 +208,18 @@ class RootNodeWrapper extends SARootNode {
const root = new RootNodeWrapper(rootNode);
const childConstructor = (node) => new NodeWrapper(node, root);
RootNodeWrapper.buildHelper(root, childConstructor);
RootNodeWrapper.findAndSetChildren(root, childConstructor);
return root;
}
/**
* Helper function to connect tree elements, given constructors for the root
* and child types.
* Helper function to connect tree elements, given the root node and a
* constructor for the child type.
* @param {!RootNodeWrapper} root
* @param {function(!AutomationNode): !SAChildNode} childConstructor
* Constructs a child node from an automation node.
*/
static buildHelper(root, childConstructor) {
static findAndSetChildren(root, childConstructor) {
const interestingChildren = RootNodeWrapper.getInterestingChildren(root);
if (interestingChildren.length < 1) {
......@@ -228,12 +228,8 @@ class RootNodeWrapper extends SARootNode {
'Root node must have at least 1 interesting child.');
}
let children = interestingChildren.map(childConstructor);
const backButton = new BackButtonNode(root);
children.push(backButton);
SARootNode.connectChildren(children);
root.setChildren(children);
children.push(new BackButtonNode(root));
root.children = children;
}
/**
......@@ -252,9 +248,7 @@ class RootNodeWrapper extends SARootNode {
const childConstructor = (autoNode) => new NodeWrapper(autoNode, root);
let children = interestingChildren.map(childConstructor);
SARootNode.connectChildren(children);
root.setChildren(children);
root.children = children;
return root;
}
......
......@@ -21,20 +21,14 @@ class SAChildNode {
this.next_ = null;
}
/**
* @param {!SAChildNode} previous
* @protected
*/
setPrevious_(previous) {
this.previous_ = previous;
/** @param {!SAChildNode} newVal */
set previous(newVal) {
this.previous_ = newVal;
}
/**
* @param {!SAChildNode} next
* @protected
*/
setNext_(next) {
this.next_ = next;
/** @param {!SAChildNode} newVal */
set next(newVal) {
this.next_ = newVal;
}
/**
......@@ -136,12 +130,12 @@ class SAChildNode {
asRootNode() {}
/**
* Called when a node becomes the primary highlighted node.
* Called when this node becomes the primary highlighted node.
*/
onFocus() {}
/**
* Called when a node stops being the primary highlighted node.
* Called when this node stops being the primary highlighted node.
*/
onUnfocus() {}
......@@ -187,9 +181,10 @@ class SARootNode {
this.children_ = [];
}
/** @param {!Array<!SAChildNode>} children */
setChildren(children) {
this.children_ = children;
/** @param {!Array<!SAChildNode>} newVal */
set children(newVal) {
this.children_ = newVal;
this.connectChildren_();
}
/**
......@@ -302,21 +297,21 @@ class SARootNode {
/**
* Helper function to connect children.
* @param {!Array<!SAChildNode>} children
* @private
*/
static connectChildren(children) {
if (children.length < 1) {
connectChildren_() {
if (this.children_.length < 1) {
throw SwitchAccess.error(
SAConstants.ErrorType.NO_CHILDREN,
'Root node must have at least 1 interesting child.');
}
let previous = children[children.length - 1];
let previous = this.children_[this.children_.length - 1];
for (let i = 0; i < children.length; i++) {
let current = children[i];
previous.setNext_(current);
current.setPrevious_(previous);
for (let i = 0; i < this.children_.length; i++) {
let current = this.children_[i];
previous.next = current;
current.previous = previous;
previous = current;
}
......
......@@ -29,7 +29,7 @@ class SystemMenuRootNode extends RootNodeWrapper {
const root = new SystemMenuRootNode(menuNode);
const childConstructor = (node) => new NodeWrapper(node, root);
RootNodeWrapper.buildHelper(root, childConstructor);
RootNodeWrapper.findAndSetChildren(root, childConstructor);
return root;
}
}
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