1 // Copyright 2013 Google Inc. All Rights Reserved.
4 * @fileoverview Uses ChromeVox API to access the search tools menu.
5 * @author peterxiao@google.com (Peter Xiao)
8 goog.provide('cvox.SearchTool');
10 goog.require('cvox.ChromeVox');
11 goog.require('cvox.DomUtil');
12 goog.require('cvox.Search');
13 goog.require('cvox.SearchConstants');
14 goog.require('cvox.SearchUtil');
19 cvox.SearchTool = function() {
23 * Index of the current menu in focus.
26 cvox.SearchTool.menuIndex;
30 * @type {Array.<Node>}
32 cvox.SearchTool.menus = [];
35 * Index of the current menu item in focus.
38 cvox.SearchTool.menuItemIndex;
41 * Array of menu items for the current menu.
42 * @type {Array.<Node>}
44 cvox.SearchTool.menuItems = [];
47 * Id of the clear button.
50 cvox.SearchTool.CLEAR_ID = 'hdtb_rst';
53 * Toggles a menu open / close by simulating a click.
55 cvox.SearchTool.toggleMenu = function() {
56 var menu = cvox.SearchTool.menus[cvox.SearchTool.menuIndex];
57 var menuDiv = menu.previousSibling;
58 cvox.DomUtil.clickElem(menuDiv, false, false, false);
62 * Syncs the first item in the current menu to ChromeVox.
64 cvox.SearchTool.syncToMenu = function() {
65 cvox.SearchTool.menuItemIndex = 0;
66 cvox.SearchTool.toggleMenu();
67 cvox.SearchTool.populateMenuItems();
68 cvox.SearchTool.syncToMenuItem();
72 * Syncs the current menu item to ChromeVox.
74 cvox.SearchTool.syncToMenuItem = function() {
75 var menuItem = cvox.SearchTool.menuItems[cvox.SearchTool.menuItemIndex];
76 cvox.ChromeVox.syncToNode(menuItem, true);
80 * Fills in menuItems with the current menu's items.
82 cvox.SearchTool.populateMenuItems = function() {
83 var menu = cvox.SearchTool.menus[cvox.SearchTool.menuIndex];
84 cvox.SearchTool.menuItems = [];
85 /* For now, we just special case on the clear button. */
86 if (menu.id !== cvox.SearchTool.CLEAR_ID) {
87 var MENU_ITEM_SELECTOR = '.hdtbItm';
88 var menuItemNodeList = menu.querySelectorAll(MENU_ITEM_SELECTOR);
89 for (var i = 0; i < menuItemNodeList.length; i++) {
90 cvox.SearchTool.menuItems.push(menuItemNodeList.item(i));
93 cvox.SearchTool.menuItems = [];
94 cvox.SearchTool.menuItems.push(menu);
99 * Fills in menus with the menus of the page.
101 cvox.SearchTool.populateMenus = function() {
102 var MENU_SELECTOR = '.hdtbU';
103 var menuDivs = document.querySelectorAll(MENU_SELECTOR);
104 for (var i = 0; i < menuDivs.length; i++) {
105 cvox.SearchTool.menus.push(menuDivs.item(i));
108 var clearDiv = document.getElementById(cvox.SearchTool.CLEAR_ID);
110 cvox.SearchTool.menus.push(clearDiv);
115 * Switches focus to the tools interface, giving keyboard access.
117 cvox.SearchTool.activateTools = function() {
118 var MENU_BAR_SELECTOR = '#hdtbMenus';
119 var menuBar = document.querySelector(MENU_BAR_SELECTOR);
120 var MENUS_OPEN_CLASS = 'hdtb-td-o';
121 menuBar.className = MENUS_OPEN_CLASS;
123 cvox.SearchTool.populateMenus();
124 cvox.SearchTool.menuIndex = 0;
125 cvox.SearchTool.syncToMenu();
129 * Goes to the link of the current menu item action.
131 cvox.SearchTool.gotoMenuItem = function() {
132 var menuItem = cvox.SearchTool.menuItems[cvox.SearchTool.menuItemIndex];
133 var LOCATION_INPUT_ID = '#lc-input';
134 var input = menuItem.querySelector(LOCATION_INPUT_ID);
135 /* Special case for setting location. */
141 /* Custom Date Range. */
142 var CDR_ID = 'cdr_opt';
143 switch (menuItem.id) {
144 case cvox.SearchTool.CLEAR_ID:
145 window.location = menuItem.dataset.url;
148 var CDR_LINK_SELECTOR = '#cdrlnk';
149 var cdrLink = menuItem.querySelector(CDR_LINK_SELECTOR);
150 cvox.DomUtil.clickElem(cdrLink, false, false, false);
151 cvox.SearchTool.toggleMenu();
154 window.location = cvox.SearchUtil.extractURL(menuItem);
160 * Handles key events for the tools interface.
161 * @param {Event} evt Keydown event.
162 * @return {boolean} True if key was handled, false otherwise.
164 cvox.SearchTool.keyhandler = function(evt) {
165 if (cvox.SearchUtil.isSearchWidgetActive()) {
169 switch (evt.keyCode) {
170 case cvox.SearchConstants.KeyCode.UP:
171 cvox.SearchTool.menuItemIndex = cvox.SearchUtil.subOneWrap(
172 cvox.SearchTool.menuItemIndex, cvox.SearchTool.menuItems.length);
173 cvox.SearchTool.syncToMenuItem();
176 case cvox.SearchConstants.KeyCode.DOWN:
177 cvox.SearchTool.menuItemIndex = cvox.SearchUtil.addOneWrap(
178 cvox.SearchTool.menuItemIndex, cvox.SearchTool.menuItems.length);
179 cvox.SearchTool.syncToMenuItem();
182 case cvox.SearchConstants.KeyCode.LEFT:
183 cvox.SearchTool.toggleMenu();
184 cvox.SearchTool.menuIndex = cvox.SearchUtil.subOneWrap(
185 cvox.SearchTool.menuIndex, cvox.SearchTool.menus.length);
186 cvox.SearchTool.syncToMenu();
189 case cvox.SearchConstants.KeyCode.RIGHT:
190 cvox.SearchTool.toggleMenu();
191 cvox.SearchTool.menuIndex = cvox.SearchUtil.addOneWrap(
192 cvox.SearchTool.menuIndex, cvox.SearchTool.menus.length);
193 cvox.SearchTool.syncToMenu();
196 case cvox.SearchConstants.KeyCode.ENTER:
197 cvox.SearchTool.gotoMenuItem();
203 evt.preventDefault();
204 evt.stopPropagation();