home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome.dll / 0 / BINDATA / 564 < prev    next >
Encoding:
Text File  |  2013-04-03  |  7.0 KB  |  240 lines

  1. // Copyright 2012 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. //                               Util functions
  7. // =============================================================================
  8.  
  9. /**
  10.  * The maximum number of suggestions to show.
  11.  * @type {number}
  12.  * @const
  13.  */
  14. var MAX_SUGGESTIONS_TO_SHOW = 5;
  15.  
  16. /**
  17.  * The omnibox input value during the last onnativesuggestions event.
  18.  * @type {string}
  19.  */
  20. var lastInputValue = '';
  21.  
  22. /**
  23.  * The ordered restricted ids of the currently displayed suggestions.  Since the
  24.  * suggestions contain the user's personal data (browser history) the searchBox
  25.  * API embeds the content of the suggestion in a shadow dom, and assigns a
  26.  * random restricted id to each suggestion which is accessible to the JS.
  27.  * @type {Array.<number>}
  28.  */
  29.  
  30. var restrictedIds = [];
  31.  
  32. /**
  33.  * The index of the currently selected suggestion or -1 if none are selected.
  34.  * @type {number}
  35.  */
  36. var selectedIndex = -1;
  37.  
  38. /**
  39.  * Displays a suggestion.
  40.  * @param {Object} suggestion The suggestion to render.
  41.  * @param {HTMLElement} box The html element to add the suggestion to.
  42.  * @param {boolean} select True to select the selection.
  43.  */
  44. function addSuggestionToBox(suggestion, box, select) {
  45.   var suggestionDiv = document.createElement('div');
  46.   suggestionDiv.classList.add('suggestion');
  47.   if (select)
  48.     suggestionDiv.classList.add('selected');
  49.  
  50.   var contentsContainer = document.createElement('div');
  51.   contentsContainer.className = 'contents';
  52.   var contents = suggestion.combinedNode;
  53.   contentsContainer.appendChild(contents);
  54.   suggestionDiv.appendChild(contentsContainer);
  55.   var restrictedId = suggestion.rid;
  56.   suggestionDiv.onclick = function() {
  57.     handleSuggestionClick(restrictedId);
  58.   };
  59.  
  60.   restrictedIds.push(restrictedId);
  61.   box.appendChild(suggestionDiv);
  62. }
  63.  
  64. /**
  65.  * Renders the input suggestions.
  66.  * @param {Array} nativeSuggestions An array of native suggestions to render.
  67.  */
  68. function renderSuggestions(nativeSuggestions) {
  69.   var box = document.createElement('div');
  70.   box.id = 'suggestionsBox';
  71.   $('suggestions-box-container').appendChild(box);
  72.  
  73.   for (var i = 0, length = nativeSuggestions.length;
  74.        i < Math.min(MAX_SUGGESTIONS_TO_SHOW, length); ++i) {
  75.     // Select the first suggestion.
  76.     addSuggestionToBox(nativeSuggestions[i], box, i == 0);
  77.   }
  78. }
  79.  
  80. /**
  81.  * Clears the suggestions being displayed.
  82.  */
  83. function clearSuggestions() {
  84.   $('suggestions-box-container').innerHTML = '';
  85.   restrictedIds = [];
  86.   selectedIndex = -1;
  87. }
  88.  
  89. /**
  90.  * @return {integer} The height of the dropdown.
  91.  */
  92. function getDropdownHeight() {
  93.   return $('suggestions-box-container').offsetHeight;
  94. }
  95.  
  96. /**
  97.  * Updates selectedIndex, bounding it between -1 and the total number of
  98.  * of suggestions - 1 (looping as necessary), and selects the corresponding
  99.  * suggestion.
  100.  * @param {boolean} increment True to increment the selected suggestion, false
  101.  *     to decrement.
  102.  */
  103. function updateSelectedSuggestion(increment) {
  104.   var numSuggestions = restrictedIds.length;
  105.   if (!numSuggestions)
  106.     return;
  107.  
  108.   var oldSelection = $('suggestionsBox').querySelector('.selected');
  109.   if (oldSelection)
  110.     oldSelection.classList.remove('selected');
  111.  
  112.   if (increment)
  113.     selectedIndex = ++selectedIndex > numSuggestions - 1 ? -1 : selectedIndex;
  114.   else
  115.     selectedIndex = --selectedIndex < -1 ? numSuggestions - 1 : selectedIndex;
  116.   var apiHandle = getApiObjectHandle();
  117.   if (selectedIndex == -1) {
  118.     apiHandle.setValue(lastInputValue);
  119.   } else {
  120.     var newSelection = $('suggestionsBox').querySelector(
  121.         '.suggestion:nth-of-type(' + (selectedIndex + 1) + ')');
  122.     newSelection.classList.add('selected');
  123.     apiHandle.setRestrictedValue(restrictedIds[selectedIndex]);
  124.   }
  125. }
  126.  
  127. // =============================================================================
  128. //                             Handlers / API stuff
  129. // =============================================================================
  130.  
  131. /**
  132.  * @return {Object} the handle to the searchBox API.
  133.  */
  134.  function getApiObjectHandle() {
  135.   if (window.cideb)
  136.     return window.cideb;
  137.   if (window.navigator && window.navigator.searchBox)
  138.     return window.navigator.searchBox;
  139.   if (window.chrome && window.chrome.searchBox)
  140.     return window.chrome.searchBox;
  141.   return null;
  142. }
  143.  
  144. /**
  145.  * chrome.searchBox.onnativesuggestions implementation.
  146.  */
  147. function handleNativeSuggestions() {
  148.   // This can't be done in setUpApi(), because apiHandle.font/fontSize
  149.   // isn't available yet.
  150.   var suggestionStyleNode = $('suggestionStyle');
  151.   if (!suggestionStyleNode)
  152.     appendSuggestionStyles();
  153.  
  154.   var apiHandle = getApiObjectHandle();
  155.  
  156.   // Used to workaround repeated undesired asynchronous onnativesuggestions
  157.   // events and the fact that when a suggestion is clicked, the omnibox unfocus
  158.   // can cause onnativesuggestions to fire, preventing the suggestion onclick
  159.   // from registering.
  160.   if (lastInputValue == apiHandle.value && $('suggestionsBox')) {
  161.     return;
  162.   }
  163.   lastInputValue = apiHandle.value;
  164.  
  165.   clearSuggestions();
  166.   var nativeSuggestions = apiHandle.nativeSuggestions;
  167.   if (nativeSuggestions.length) {
  168.     nativeSuggestions.sort(function(a, b) {
  169.       return b.rankingData.relevance - a.rankingData.relevance;
  170.     });
  171.     renderSuggestions(nativeSuggestions);
  172.     selectedIndex = 0;
  173.     apiHandle.setRestrictedAutocompleteText(
  174.         nativeSuggestions[selectedIndex].rid);
  175.   }
  176.  
  177.   var height = getDropdownHeight();
  178.   apiHandle.show(2, height);
  179. }
  180.  
  181. /**
  182.  * Appends a style node for suggestion properties that depend on apiHandle.
  183.  */
  184. function appendSuggestionStyles() {
  185.   var apiHandle = getApiObjectHandle();
  186.   var style = document.createElement('style');
  187.   style.type = 'text/css';
  188.   style.id = 'suggestionStyle';
  189.   style.textContent =
  190.       '.suggestion {' +
  191.       '  -webkit-margin-start: ' + apiHandle.startMargin + 'px;' +
  192.       '  -webkit-margin-end: ' + apiHandle.endMargin + 'px;' +
  193.       '  font: ' + apiHandle.fontSize + 'px "' + apiHandle.font + '";' +
  194.       '}';
  195.   document.querySelector('head').appendChild(style);
  196. }
  197.  
  198. /**
  199.  * Handles suggestion clicks.
  200.  * @param {integer} restrictedId The restricted id of the suggestion being
  201.  *     clicked.
  202.  */
  203. function handleSuggestionClick(restrictedId) {
  204.   clearSuggestions();
  205.   getApiObjectHandle().navigateContentWindow(restrictedId);
  206. }
  207.  
  208. /**
  209.  * chrome.searchBox.onkeypress implementation.
  210.  * @param {Object} e The key being pressed.
  211.  */
  212. function handleKeyPress(e) {
  213.   switch (e.keyCode) {
  214.     case 38:  // Up arrow
  215.       updateSelectedSuggestion(false);
  216.       break;
  217.     case 40:  // Down arrow
  218.       updateSelectedSuggestion(true);
  219.       break;
  220.   }
  221. }
  222.  
  223. /**
  224.  * chrome.searchBox.onsubmit implementation.
  225.  */
  226. function onSubmit() {
  227. }
  228.  
  229. /**
  230.  * Sets up the searchBox API.
  231.  */
  232. function setUpApi() {
  233.   var apiHandle = getApiObjectHandle();
  234.   apiHandle.onnativesuggestions = handleNativeSuggestions;
  235.   apiHandle.onkeypress = handleKeyPress;
  236.   apiHandle.onsubmit = onSubmit;
  237. }
  238.  
  239. document.addEventListener('DOMContentLoaded', setUpApi);
  240.