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

  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. // Redefine '$' here rather than including 'cr.js', since this is
  6. // the only function needed.  This allows this file to be loaded
  7. // in a browser directly for layout and some testing purposes.
  8. var $ = function(id) { return document.getElementById(id); };
  9.  
  10. /**
  11.  * WebUI for configuring instant.* preference values used by
  12.  * Chrome's instant search system.
  13.  */
  14. var instantConfig = (function() {
  15.   'use strict';
  16.  
  17.   /** List of fields used to dynamically build form. **/
  18.   var FIELDS = [
  19.     {
  20.       key: 'instant_ui.zero_suggest_url_prefix',
  21.       label: 'Prefix URL for the experimental Instant ZeroSuggest provider',
  22.       type: 'string',
  23.       size: 40,
  24.       units: '',
  25.       default: ''
  26.     },
  27.   ];
  28.  
  29.   /**
  30.    * Returns a DOM element of the given type and class name.
  31.    */
  32.   function createElementWithClass(elementType, className) {
  33.     var element = document.createElement(elementType);
  34.     element.className = className;
  35.     return element;
  36.   }
  37.  
  38.   /**
  39.    * Dynamically builds web-form based on FIELDS list.
  40.    * @return {string} The form's HTML.
  41.    */
  42.   function buildForm() {
  43.     var buf = [];
  44.  
  45.     for (var i = 0; i < FIELDS.length; i++) {
  46.       var field = FIELDS[i];
  47.  
  48.       var row = createElementWithClass('div', 'row');
  49.       row.id = '';
  50.  
  51.       var label = createElementWithClass('label', 'row-label');
  52.       label.setAttribute('for', field.key);
  53.       label.textContent = field.label;
  54.       row.appendChild(label);
  55.  
  56.       var input = createElementWithClass('input', 'row-input');
  57.       input.type = field.type;
  58.       input.id = field.key;
  59.       input.title = "Default Value: " + field.default;
  60.       if (field.size) input.size = field.size;
  61.       input.min = field.min || 0;
  62.       if (field.max) input.max = field.max;
  63.       if (field.step) input.step = field.step;
  64.       row.appendChild(input);
  65.  
  66.       var units = createElementWithClass('div', 'row-units');
  67.       if (field.units)
  68.         units.innerHTML = field.units;
  69.       row.appendChild(units);
  70.  
  71.       $('instant-form').appendChild(row);
  72.     }
  73.   }
  74.  
  75.   /**
  76.    * Initialize the form by adding 'onChange' listeners to all fields.
  77.    */
  78.   function initForm() {
  79.     for (var i = 0; i < FIELDS.length; i++) {
  80.       var field = FIELDS[i];
  81.       $(field.key).onchange = (function(key) {
  82.         setPreferenceValue(key);
  83.       }).bind(null, field.key);
  84.     }
  85.   }
  86.  
  87.   /**
  88.    * Request a preference setting's value.
  89.    * This method is asynchronous; the result is provided by a call to
  90.    * getPreferenceValueResult.
  91.    * @param {string} prefName The name of the preference value being requested.
  92.    */
  93.   function getPreferenceValue(prefName) {
  94.     chrome.send('getPreferenceValue', [prefName]);
  95.   }
  96.  
  97.   /**
  98.    * Handle callback from call to getPreferenceValue.
  99.    * @param {string} prefName The name of the requested preference value.
  100.    * @param {value} value The current value associated with prefName.
  101.    */
  102.   function getPreferenceValueResult(prefName, value) {
  103.     if ($(prefName).type == 'checkbox')
  104.       $(prefName).checked = value;
  105.     else
  106.       $(prefName).value = value;
  107.   }
  108.  
  109.   /**
  110.    * Set a preference setting's value stored in the element with prefName.
  111.    * @param {string} prefName The name of the preference value being set.
  112.    */
  113.   function setPreferenceValue(prefName) {
  114.     var value;
  115.     if ($(prefName).type == 'checkbox')
  116.       value = $(prefName).checked;
  117.     else if ($(prefName).type == 'number')
  118.       value = parseFloat($(prefName).value);
  119.     else
  120.       value = $(prefName).value;
  121.     chrome.send(
  122.         'setPreferenceValue',
  123.         [prefName, value]);
  124.   }
  125.  
  126.   /**
  127.    * Handle processing of "Reset" button.
  128.    * Causes off form values to be updated based on current preference values.
  129.    */
  130.   function onReset() {
  131.     for (var i = 0; i < FIELDS.length; i++) {
  132.       var field = FIELDS[i];
  133.       if ($(field.key).type == 'checkbox')
  134.         $(field.key).checked = field.default;
  135.       else
  136.         $(field.key).value = field.default;
  137.       setPreferenceValue(field.key);
  138.     }
  139.     return false;
  140.   }
  141.  
  142.   /**
  143.    * Saves data back into Chrome preferences.
  144.    */
  145.   function onSave() {
  146.     for (var i = 0; i < FIELDS.length; i++) {
  147.       var field = FIELDS[i];
  148.       setPreferenceValue(field.key);
  149.     }
  150.     return false;
  151.   }
  152.  
  153.  
  154.   function loadForm() {
  155.     for (var i = 0; i < FIELDS.length; i++)
  156.       getPreferenceValue(FIELDS[i].key);
  157.   }
  158.  
  159.   /**
  160.    * Build and initialize the configuration form.
  161.    */
  162.   function initialize() {
  163.     buildForm();
  164.     loadForm();
  165.     initForm();
  166.  
  167.     $('reset-button').onclick = onReset.bind(this);
  168.     $('save-button').onclick = onSave.bind(this);
  169.   }
  170.  
  171.   return {
  172.     initialize: initialize,
  173.     getPreferenceValueResult: getPreferenceValueResult
  174.   };
  175. })();
  176.  
  177. document.addEventListener('DOMContentLoaded', instantConfig.initialize);
  178.