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