• 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
7  var Page = cr.ui.pageManager.Page;
8  var PageManager = cr.ui.pageManager.PageManager;
9
10  /////////////////////////////////////////////////////////////////////////////
11  // CookiesView class:
12
13  /**
14   * Encapsulated handling of the cookies and other site data page.
15   * @constructor
16   * @extends {cr.ui.pageManager.Page}
17   */
18  function CookiesView(model) {
19    Page.call(this, 'cookies',
20              loadTimeData.getString('cookiesViewPageTabTitle'),
21              'cookies-view-page');
22  }
23
24  cr.addSingletonGetter(CookiesView);
25
26  CookiesView.prototype = {
27    __proto__: Page.prototype,
28
29    /**
30     * The timer id of the timer set on search query change events.
31     * @type {number}
32     * @private
33     */
34    queryDelayTimerId_: 0,
35
36    /**
37     * The most recent search query, empty string if the query is empty.
38     * @type {string}
39     * @private
40     */
41    lastQuery_: '',
42
43    /** @override */
44    initializePage: function() {
45      Page.prototype.initializePage.call(this);
46
47      var searchBox = this.pageDiv.querySelector('.cookies-search-box');
48      searchBox.addEventListener(
49          'search', this.handleSearchQueryChange_.bind(this));
50      searchBox.onkeydown = function(e) {
51        // Prevent the overlay from handling this event.
52        if (e.keyIdentifier == 'Enter')
53          e.stopPropagation();
54      };
55
56      this.pageDiv.querySelector('.remove-all-cookies-button').onclick =
57          function(e) {
58            chrome.send('removeAllCookies');
59          };
60
61      var cookiesList = this.pageDiv.querySelector('.cookies-list');
62      options.CookiesList.decorate(cookiesList);
63
64      this.addEventListener('visibleChange', this.handleVisibleChange_);
65
66      this.pageDiv.querySelector('.cookies-view-overlay-confirm').onclick =
67          PageManager.closeOverlay.bind(PageManager);
68    },
69
70    /** @override */
71    didShowPage: function() {
72      this.pageDiv.querySelector('.cookies-search-box').value = '';
73      this.lastQuery_ = '';
74    },
75
76    /**
77     * Search cookie using text in |cookies-search-box|.
78     */
79    searchCookie: function() {
80      this.queryDelayTimerId_ = 0;
81      var filter = this.pageDiv.querySelector('.cookies-search-box').value;
82      if (this.lastQuery_ != filter) {
83        this.lastQuery_ = filter;
84        chrome.send('updateCookieSearchResults', [filter]);
85      }
86    },
87
88    /**
89     * Handles search query changes.
90     * @param {!Event} e The event object.
91     * @private
92     */
93    handleSearchQueryChange_: function(e) {
94      var stringId = document.querySelector('.cookies-search-box').value ?
95          'remove_all_shown_cookie' : 'remove_all_cookie';
96      document.querySelector('.remove-all-cookies-button').innerHTML =
97          loadTimeData.getString(stringId);
98      if (this.queryDelayTimerId_)
99        window.clearTimeout(this.queryDelayTimerId_);
100
101      this.queryDelayTimerId_ = window.setTimeout(
102          this.searchCookie.bind(this), 500);
103    },
104
105    initialized_: false,
106
107    /**
108     * Handler for Page's visible property change event.
109     * @param {Event} e Property change event.
110     * @private
111     */
112    handleVisibleChange_: function(e) {
113      if (!this.visible)
114        return;
115
116      chrome.send('reloadCookies');
117
118      if (!this.initialized_) {
119        this.initialized_ = true;
120        this.searchCookie();
121      } else {
122        this.pageDiv.querySelector('.cookies-list').redraw();
123      }
124
125      this.pageDiv.querySelector('.cookies-search-box').focus();
126    },
127  };
128
129  // CookiesViewHandler callbacks.
130  CookiesView.onTreeItemAdded = function(args) {
131    $('cookies-list').addByParentId(args[0], args[1], args[2]);
132  };
133
134  CookiesView.onTreeItemRemoved = function(args) {
135    $('cookies-list').removeByParentId(args[0], args[1], args[2]);
136  };
137
138  CookiesView.loadChildren = function(args) {
139    $('cookies-list').loadChildren(args[0], args[1]);
140  };
141
142  // Export
143  return {
144    CookiesView: CookiesView
145  };
146
147});
148