Extract code handling PrinterProviderAPI from PrintPreviewHandler
[chromium-blink-merge.git] / chrome / browser / resources / options / language_dictionary_overlay.js
blob61e351afa662938907dbde59da3fd3d44355a16d
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.
5 cr.define('options', function() {
6   /** @const */ var DictionaryWordsList =
7       options.dictionary_words.DictionaryWordsList;
8   /** @const */ var Page = cr.ui.pageManager.Page;
9   /** @const */ var PageManager = cr.ui.pageManager.PageManager;
11   /**
12    * Adding and removing words in custom spelling dictionary.
13    * @constructor
14    * @extends {cr.ui.pageManager.Page}
15    */
16   function EditDictionaryOverlay() {
17     Page.call(this, 'editDictionary',
18               loadTimeData.getString('languageDictionaryOverlayPage'),
19               'language-dictionary-overlay-page');
20   }
22   cr.addSingletonGetter(EditDictionaryOverlay);
24   EditDictionaryOverlay.prototype = {
25     __proto__: Page.prototype,
27     /**
28      * A list of words in the dictionary.
29      * @type {options.dictionary_words.DictionaryWordsList}
30      * @private
31      */
32     wordList_: null,
34     /**
35      * The input field for searching for words in the dictionary.
36      * @type {HTMLElement}
37      * @private
38      */
39     searchField_: null,
41     /**
42      * The paragraph of text that indicates that search returned no results.
43      * @type {HTMLElement}
44      * @private
45      */
46     noMatchesLabel_: null,
48     /** @override */
49     initializePage: function() {
50       Page.prototype.initializePage.call(this);
52       var wordList = $('language-dictionary-overlay-word-list');
53       DictionaryWordsList.decorate(wordList);
54       this.wordList_ = assertInstanceof(wordList, DictionaryWordsList);
55       this.wordList_.onWordListChanged = function() {
56         this.onWordListChanged_();
57       }.bind(this);
59       this.searchField_ = $('language-dictionary-overlay-search-field');
60       this.searchField_.onsearch = function(e) {
61         this.wordList_.search(e.currentTarget.value);
62       }.bind(this);
63       this.searchField_.onkeydown = function(e) {
64         // Don't propagate enter key events. Otherwise the default button will
65         // activate.
66         if (e.keyIdentifier == 'Enter')
67           e.stopPropagation();
68       };
70       this.noMatchesLabel_ = getRequiredElement(
71           'language-dictionary-overlay-no-matches');
73       $('language-dictionary-overlay-done-button').onclick = function(e) {
74         PageManager.closeOverlay();
75       };
76     },
78     /**
79      * Refresh the dictionary words when the page is displayed.
80      * @override
81      */
82     didShowPage: function() {
83       chrome.send('refreshDictionaryWords');
84     },
86     /**
87      * Update the view based on the changes in the word list.
88      * @private
89      */
90     onWordListChanged_: function() {
91       if (this.searchField_.value.length > 0 && this.wordList_.empty) {
92         this.noMatchesLabel_.hidden = false;
93         this.wordList_.classList.add('no-search-matches');
94       } else {
95         this.noMatchesLabel_.hidden = true;
96         this.wordList_.classList.remove('no-search-matches');
97       }
98     },
99   };
101   EditDictionaryOverlay.setWordList = function(entries) {
102     EditDictionaryOverlay.getInstance().wordList_.setWordList(entries);
103   };
105   EditDictionaryOverlay.updateWords = function(add_words, remove_words) {
106     EditDictionaryOverlay.getInstance().wordList_.addWords(add_words);
107     EditDictionaryOverlay.getInstance().wordList_.removeWords(remove_words);
108   };
110   EditDictionaryOverlay.getWordListForTesting = function() {
111     return EditDictionaryOverlay.getInstance().wordList_;
112   };
114   return {
115     EditDictionaryOverlay: EditDictionaryOverlay
116   };