1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5 6/** 7 * @fileoverview Context Menu for Search. 8 */ 9 10goog.provide('cvox.SearchContextMenu'); 11 12goog.require('cvox.ChromeVoxKbHandler'); 13goog.require('cvox.KeySequence'); 14goog.require('cvox.Search'); 15goog.require('cvox.SearchTool'); 16 17/** 18 * @constructor 19 */ 20cvox.SearchContextMenu = function() { 21}; 22 23/* Globals */ 24var Command = { 25 TOOLS: 'tools', 26 ADS: 'ads', 27 MAIN: 'main' 28}; 29 30/** 31 * Current focus Search is in. 32 */ 33cvox.SearchContextMenu.currState = Command.MAIN; 34 35/** 36 * Handles context menu events. 37 * @param {Event} evt Event received. 38 */ 39cvox.SearchContextMenu.contextMenuHandler = function(evt) { 40 var cmd = evt.detail['customCommand']; 41 switch (cmd) { 42 case Command.TOOLS: 43 cvox.SearchContextMenu.focusTools(); 44 break; 45 46 case Command.ADS: 47 cvox.SearchContextMenu.focusAds(); 48 break; 49 50 case Command.MAIN: 51 cvox.SearchContextMenu.focusMain(); 52 break; 53 } 54}; 55 56/** 57 * Handles key events. 58 * @param {Event} evt Event received. 59 * @return {boolean} True if key was handled, false otherwise. 60 */ 61cvox.SearchContextMenu.keyhandler = function(evt) { 62 var ret = false; 63 var keySeq = new cvox.KeySequence(evt); 64 var command = cvox.ChromeVoxKbHandler.handlerKeyMap.commandForKey(keySeq); 65 /* Handle if just default action. */ 66 if (!command || command === 'performDefaultAction') { 67 switch (cvox.SearchContextMenu.currState) { 68 case Command.TOOLS: 69 ret = cvox.SearchTool.keyhandler(evt); 70 break; 71 case Command.ADS: 72 case Command.MAIN: 73 ret = cvox.Search.keyhandler(evt); 74 break; 75 } 76 } 77 return ret; 78}; 79 80/** 81 * Switch to main search results focus. 82 */ 83cvox.SearchContextMenu.focusMain = function() { 84 if (cvox.SearchContextMenu.currState === Command.TOOLS) { 85 cvox.SearchTool.toggleMenu(); 86 } 87 cvox.Search.populateResults(); 88 cvox.Search.index = 0; 89 cvox.Search.syncToIndex(); 90 cvox.SearchContextMenu.currState = Command.MAIN; 91}; 92 93/** 94 * Switch to ads focus. 95 */ 96cvox.SearchContextMenu.focusAds = function() { 97 cvox.Search.populateAdResults(); 98 if (cvox.Search.results.length === 0) { 99 cvox.SearchContextMenu.focusMain(); 100 return; 101 } 102 cvox.Search.index = 0; 103 cvox.Search.syncToIndex(); 104 105 if (cvox.SearchContextMenu.currState === Command.TOOLS) { 106 cvox.SearchTool.toggleMenu(); 107 } 108 109 cvox.SearchContextMenu.currState = Command.ADS; 110}; 111 112/** 113 * Switch to tools focus. 114 */ 115cvox.SearchContextMenu.focusTools = function() { 116 if (cvox.SearchContextMenu.currState !== Command.TOOLS) { 117 cvox.SearchTool.activateTools(); 118 cvox.SearchContextMenu.currState = Command.TOOLS; 119 } 120}; 121 122/** 123 * Initializes the context menu. 124 */ 125cvox.SearchContextMenu.init = function() { 126 var ACTIONS = [ 127 { desc: 'Main Results', cmd: Command.MAIN }, 128 { desc: 'Search Tools', cmd: Command.TOOLS }, 129 { desc: 'Ads', cmd: Command.ADS } 130 ]; 131 /* Attach ContextMenuActions. */ 132 var body = document.querySelector('body'); 133 body.setAttribute('contextMenuActions', JSON.stringify(ACTIONS)); 134 135 /* Listen for ContextMenu events. */ 136 body.addEventListener('ATCustomEvent', 137 cvox.SearchContextMenu.contextMenuHandler, true); 138 139 window.addEventListener('keydown', cvox.SearchContextMenu.keyhandler, true); 140 cvox.Search.init(); 141}; 142