| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 Polymer({ | 5 Polymer({ |
| 6 is: 'bookmarks-folder-node', | 6 is: 'bookmarks-folder-node', |
| 7 | 7 |
| 8 behaviors: [ | 8 behaviors: [ |
| 9 bookmarks.StoreClient, | 9 bookmarks.StoreClient, |
| 10 ], | 10 ], |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 /** @return {boolean} */ | 76 /** @return {boolean} */ |
| 77 isTopLevelFolder_: function() { | 77 isTopLevelFolder_: function() { |
| 78 return this.depth == 0; | 78 return this.depth == 0; |
| 79 }, | 79 }, |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * @private | 82 * @private |
| 83 * @param {!Event} e | 83 * @param {!Event} e |
| 84 */ | 84 */ |
| 85 onKeydown_: function(e) { | 85 onKeydown_: function(e) { |
| 86 var direction = 0; | 86 var yDirection = 0; |
| 87 var xDirection = 0; |
| 87 var handled = true; | 88 var handled = true; |
| 88 // TODO(calamity): Handle left/right arrow keys. | |
| 89 if (e.key == 'ArrowUp') { | 89 if (e.key == 'ArrowUp') { |
| 90 direction = -1; | 90 yDirection = -1; |
| 91 } else if (e.key == 'ArrowDown') { | 91 } else if (e.key == 'ArrowDown') { |
| 92 direction = 1; | 92 yDirection = 1; |
| 93 } else if (e.key == 'ArrowLeft') { |
| 94 xDirection = -1; |
| 95 } else if (e.key == 'ArrowRight') { |
| 96 xDirection = 1; |
| 93 } else { | 97 } else { |
| 94 handled = false; | 98 handled = false; |
| 95 } | 99 } |
| 96 | 100 |
| 97 if (direction) | 101 if (this.getComputedStyleValue('direction') == 'rtl') |
| 98 this.changeKeyboardSelection_(direction, this.root.activeElement); | 102 xDirection *= -1; |
| 103 |
| 104 this.changeKeyboardSelection_( |
| 105 xDirection, yDirection, this.root.activeElement); |
| 99 | 106 |
| 100 if (!handled) | 107 if (!handled) |
| 101 return; | 108 return; |
| 102 | 109 |
| 103 e.preventDefault(); | 110 e.preventDefault(); |
| 104 e.stopPropagation(); | 111 e.stopPropagation(); |
| 105 }, | 112 }, |
| 106 | 113 |
| 107 /** | 114 /** |
| 108 * @private | 115 * @private |
| 109 * @param {number} direction | 116 * @param {number} xDirection |
| 117 * @param {number} yDirection |
| 110 * @param {!HTMLElement} currentFocus | 118 * @param {!HTMLElement} currentFocus |
| 111 */ | 119 */ |
| 112 changeKeyboardSelection_: function(direction, currentFocus) { | 120 changeKeyboardSelection_: function(xDirection, yDirection, currentFocus) { |
| 113 var newFocusFolderNode = null; | 121 var newFocusFolderNode = null; |
| 114 var isChildFolderNodeFocused = | 122 var isChildFolderNodeFocused = |
| 115 currentFocus.tagName == 'BOOKMARKS-FOLDER-NODE'; | 123 currentFocus.tagName == 'BOOKMARKS-FOLDER-NODE'; |
| 116 var reverse = direction == -1; | 124 |
| 125 if (xDirection == 1) { |
| 126 // The right arrow opens a folder if closed and goes to the first child |
| 127 // otherwise. |
| 128 if (this.hasChildFolder_()) { |
| 129 if (this.isClosed_) { |
| 130 this.dispatch( |
| 131 bookmarks.actions.changeFolderOpen(this.item_.id, true)); |
| 132 } else { |
| 133 yDirection = 1; |
| 134 } |
| 135 } |
| 136 } else if (xDirection == -1) { |
| 137 // The left arrow closes a folder if open and goes to the parent |
| 138 // otherwise. |
| 139 if (this.hasChildFolder_() && !this.isClosed_) { |
| 140 this.dispatch(bookmarks.actions.changeFolderOpen(this.item_.id, false)); |
| 141 } else { |
| 142 var parentFolderNode = this.getParentFolderNode_(); |
| 143 if (parentFolderNode.itemId != ROOT_NODE_ID) { |
| 144 parentFolderNode.selectFolder_(); |
| 145 parentFolderNode.getFocusTarget().focus(); |
| 146 } |
| 147 } |
| 148 } |
| 149 |
| 150 if (!yDirection) |
| 151 return; |
| 117 | 152 |
| 118 // The current node's successor is its first child when open. | 153 // The current node's successor is its first child when open. |
| 119 if (!isChildFolderNodeFocused && !reverse && !this.isClosed_) { | 154 if (!isChildFolderNodeFocused && yDirection == 1 && !this.isClosed_) { |
| 120 var children = this.getChildFolderNodes_(); | 155 var children = this.getChildFolderNodes_(); |
| 121 if (children.length) | 156 if (children.length) |
| 122 newFocusFolderNode = children[0]; | 157 newFocusFolderNode = children[0]; |
| 123 } | 158 } |
| 124 | 159 |
| 125 if (isChildFolderNodeFocused) { | 160 if (isChildFolderNodeFocused) { |
| 126 // Get the next child folder node if a child is focused. | 161 // Get the next child folder node if a child is focused. |
| 127 if (!newFocusFolderNode) { | 162 if (!newFocusFolderNode) { |
| 128 newFocusFolderNode = this.getNextChild_( | 163 newFocusFolderNode = this.getNextChild_( |
| 129 reverse, | 164 yDirection == -1, |
| 130 /** @type {!BookmarksFolderNodeElement} */ (currentFocus)); | 165 /** @type {!BookmarksFolderNodeElement} */ (currentFocus)); |
| 131 } | 166 } |
| 132 | 167 |
| 133 // The first child's predecessor is this node. | 168 // The first child's predecessor is this node. |
| 134 if (!newFocusFolderNode && reverse) | 169 if (!newFocusFolderNode && yDirection == -1) |
| 135 newFocusFolderNode = this; | 170 newFocusFolderNode = this; |
| 136 } | 171 } |
| 137 | 172 |
| 138 // If there is no newly focused node, allow the parent to handle the change. | 173 // If there is no newly focused node, allow the parent to handle the change. |
| 139 if (!newFocusFolderNode) { | 174 if (!newFocusFolderNode) { |
| 140 if (this.itemId != ROOT_NODE_ID) | 175 if (this.itemId != ROOT_NODE_ID) |
| 141 this.getParentFolderNode_().changeKeyboardSelection_(direction, this); | 176 this.getParentFolderNode_().changeKeyboardSelection_( |
| 177 0, yDirection, this); |
| 142 | 178 |
| 143 return; | 179 return; |
| 144 } | 180 } |
| 145 | 181 |
| 146 // The root node is not navigable. | 182 // The root node is not navigable. |
| 147 if (newFocusFolderNode.itemId != ROOT_NODE_ID) { | 183 if (newFocusFolderNode.itemId != ROOT_NODE_ID) { |
| 148 newFocusFolderNode.selectFolder_(); | 184 newFocusFolderNode.selectFolder_(); |
| 149 newFocusFolderNode.getFocusTarget().focus(); | 185 newFocusFolderNode.getFocusTarget().focus(); |
| 150 } | 186 } |
| 151 }, | 187 }, |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 }, | 328 }, |
| 293 | 329 |
| 294 /** | 330 /** |
| 295 * @private | 331 * @private |
| 296 * @return {string} | 332 * @return {string} |
| 297 */ | 333 */ |
| 298 getTabIndex_: function() { | 334 getTabIndex_: function() { |
| 299 return this.isSelectedFolder_ ? '0' : ''; | 335 return this.isSelectedFolder_ ? '0' : ''; |
| 300 }, | 336 }, |
| 301 }); | 337 }); |
| OLD | NEW |