Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / home_page_overlay.js
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 cr.define('options', function() {
6   /** @const */ var Page = cr.ui.pageManager.Page;
7   /** @const */ var SettingsDialog = options.SettingsDialog;
8
9   /**
10    * HomePageOverlay class
11    * Dialog that allows users to set the home page.
12    * @extends {SettingsDialog}
13    */
14   function HomePageOverlay() {
15     SettingsDialog.call(this, 'homePageOverlay',
16                         loadTimeData.getString('homePageOverlayTabTitle'),
17                         'home-page-overlay',
18                         $('home-page-confirm'), $('home-page-cancel'));
19   }
20
21   cr.addSingletonGetter(HomePageOverlay);
22
23   HomePageOverlay.prototype = {
24     __proto__: SettingsDialog.prototype,
25
26     /**
27      * An autocomplete list that can be attached to the home page URL field.
28      * @type {cr.ui.AutocompleteList}
29      * @private
30      */
31     autocompleteList_: null,
32
33     /** @override */
34     initializePage: function() {
35       SettingsDialog.prototype.initializePage.call(this);
36
37       var self = this;
38       options.Preferences.getInstance().addEventListener(
39           'homepage_is_newtabpage',
40           this.handleHomepageIsNTPPrefChange.bind(this));
41
42       var urlField = $('homepage-url-field');
43       urlField.addEventListener('keydown', function(event) {
44         // Don't auto-submit when the user selects something from the
45         // auto-complete list.
46         if (event.keyIdentifier == 'Enter' && !self.autocompleteList_.hidden)
47           event.stopPropagation();
48       });
49       urlField.addEventListener('change', this.updateFavicon_.bind(this));
50
51       var suggestionList = new cr.ui.AutocompleteList();
52       suggestionList.autoExpands = true;
53       suggestionList.requestSuggestions =
54           this.requestAutocompleteSuggestions_.bind(this);
55       $('home-page-overlay').appendChild(suggestionList);
56       this.autocompleteList_ = suggestionList;
57
58       urlField.addEventListener('focus', function(event) {
59         self.autocompleteList_.attachToInput(urlField);
60       });
61       urlField.addEventListener('blur', function(event) {
62         self.autocompleteList_.detach();
63       });
64     },
65
66     /** @override */
67     didShowPage: function() {
68       this.updateFavicon_();
69     },
70
71     /**
72      * Updates the state of the homepage text input and its controlled setting
73      * indicator when the |homepage_is_newtabpage| pref changes. The input is
74      * enabled only if the homepage is not the NTP. The indicator is always
75      * enabled but treats the input's value as read-only if the homepage is the
76      * NTP.
77      * @param {Event} Pref change event.
78      */
79     handleHomepageIsNTPPrefChange: function(event) {
80       var urlField = $('homepage-url-field');
81       var urlFieldIndicator = $('homepage-url-field-indicator');
82       urlField.setDisabled('homepage-is-ntp', event.value.value);
83       urlFieldIndicator.readOnly = event.value.value;
84     },
85
86     /**
87      * Updates the background of the url field to show the favicon for the
88      * URL that is currently typed in.
89      * @private
90      */
91     updateFavicon_: function() {
92       var urlField = $('homepage-url-field');
93       urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
94     },
95
96     /**
97      * Sends an asynchronous request for new autocompletion suggestions for the
98      * the given query. When new suggestions are available, the C++ handler will
99      * call updateAutocompleteSuggestions_.
100      * @param {string} query List of autocomplete suggestions.
101      * @private
102      */
103     requestAutocompleteSuggestions_: function(query) {
104       chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
105     },
106
107     /**
108      * Updates the autocomplete suggestion list with the given entries.
109      * @param {Array} pages List of autocomplete suggestions.
110      * @private
111      */
112     updateAutocompleteSuggestions_: function(suggestions) {
113       var list = this.autocompleteList_;
114       // If the trigger for this update was a value being selected from the
115       // current list, do nothing.
116       if (list.targetInput && list.selectedItem &&
117           list.selectedItem.url == list.targetInput.value) {
118         return;
119       }
120       list.suggestions = suggestions;
121     },
122
123     /**
124      * Sets the 'show home button' and 'home page is new tab page' preferences.
125      * (The home page url preference is set automatically by the SettingsDialog
126      * code.)
127      */
128     handleConfirm: function() {
129       // Strip whitespace.
130       var urlField = $('homepage-url-field');
131       var homePageValue = urlField.value.replace(/\s*/g, '');
132       urlField.value = homePageValue;
133
134       // Don't save an empty URL for the home page. If the user left the field
135       // empty, switch to the New Tab page.
136       if (!homePageValue)
137         $('homepage-use-ntp').checked = true;
138
139       SettingsDialog.prototype.handleConfirm.call(this);
140     },
141   };
142
143   HomePageOverlay.updateAutocompleteSuggestions = function() {
144     var instance = HomePageOverlay.getInstance();
145     instance.updateAutocompleteSuggestions_.apply(instance, arguments);
146   };
147
148   // Export
149   return {
150     HomePageOverlay: HomePageOverlay
151   };
152 });