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