1// Copyright (c) 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 * Requests the database from the backend.
7 */
8function requestAutocompleteActionPredictorDb() {
9  chrome.send('requestAutocompleteActionPredictorDb');
10}
11
12/**
13 * Callback from backend with the database contents. Sets up some globals and
14 * calls to create the UI.
15 * @param {Dictionary} database Information about AutocompleteActionPredictor
16 *     including the database as a flattened list, a boolean indicating if the
17 *     system is enabled and the current hit weight.
18 */
19function updateAutocompleteActionPredictorDb(database) {
20  console.debug('Updating Table NAP DB');
21
22  var filter = $('filter');
23  filter.disabled = false;
24  filter.onchange = function() {
25    updateAutocompleteActionPredictorDbView(database);
26  };
27
28  updateAutocompleteActionPredictorDbView(database);
29}
30
31/**
32 * Updates the table from the database.
33 * @param {Dictionary} database Information about AutocompleteActionPredictor
34 *     including the database as a flattened list, a boolean indicating if the
35 *     system is enabled and the current hit weight.
36 */
37function updateAutocompleteActionPredictorDbView(database) {
38  var databaseSection = $('databaseTableBody');
39  var showEnabled = database.enabled && database.db;
40
41  $('autocompleteActionPredictorEnabledMode').hidden = !showEnabled;
42  $('autocompleteActionPredictorDisabledMode').hidden = showEnabled;
43
44  if (!showEnabled)
45    return;
46
47  var filter = $('filter');
48
49  // Clear any previous list.
50  databaseSection.textContent = '';
51
52  for (var i = 0; i < database.db.length; ++i) {
53    var entry = database.db[i];
54
55    if (!filter.checked || entry.confidence > 0) {
56      var row = document.createElement('tr');
57      row.className = (entry.confidence > 0.8 ? 'action-prerender' :
58                          (entry.confidence > 0.5 ? 'action-preconnect' :
59                              'action-none'));
60
61      row.appendChild(document.createElement('td')).textContent =
62          entry.user_text;
63      row.appendChild(document.createElement('td')).textContent = entry.url;
64      row.appendChild(document.createElement('td')).textContent =
65          entry.hit_count;
66      row.appendChild(document.createElement('td')).textContent =
67          entry.miss_count;
68      row.appendChild(document.createElement('td')).textContent =
69          entry.confidence;
70
71      databaseSection.appendChild(row);
72    }
73  }
74  $('countBanner').textContent = 'Entries: ' + databaseSection.children.length;
75}
76
77document.addEventListener('DOMContentLoaded',
78                          requestAutocompleteActionPredictorDb);
79