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