Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / options / cookies_view.js
blobc2cb530e1b63317021a9b8e6564b0d4422513a16
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() {
7   var Page = cr.ui.pageManager.Page;
8   var PageManager = cr.ui.pageManager.PageManager;
10   /////////////////////////////////////////////////////////////////////////////
11   // CookiesView class:
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   }
24   cr.addSingletonGetter(CookiesView);
26   CookiesView.prototype = {
27     __proto__: Page.prototype,
29     /**
30      * The timer id of the timer set on search query change events.
31      * @type {number}
32      * @private
33      */
34     queryDelayTimerId_: 0,
36     /**
37      * The most recent search query, empty string if the query is empty.
38      * @type {string}
39      * @private
40      */
41     lastQuery_: '',
43     /** @override */
44     initializePage: function() {
45       Page.prototype.initializePage.call(this);
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       };
56       this.pageDiv.querySelector('.remove-all-cookies-button').onclick =
57           function(e) {
58             chrome.send('removeAllCookies');
59           };
61       var cookiesList = this.pageDiv.querySelector('.cookies-list');
62       options.CookiesList.decorate(cookiesList);
64       this.addEventListener('visibleChange', this.handleVisibleChange_);
66       this.pageDiv.querySelector('.cookies-view-overlay-confirm').onclick =
67           PageManager.closeOverlay.bind(PageManager);
68     },
70     /** @override */
71     didShowPage: function() {
72       this.pageDiv.querySelector('.cookies-search-box').value = '';
73       this.lastQuery_ = '';
74     },
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     },
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_);
101       this.queryDelayTimerId_ = window.setTimeout(
102           this.searchCookie.bind(this), 500);
103     },
105     initialized_: false,
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;
116       chrome.send('reloadCookies');
118       if (!this.initialized_) {
119         this.initialized_ = true;
120         this.searchCookie();
121       } else {
122         this.pageDiv.querySelector('.cookies-list').redraw();
123       }
125       this.pageDiv.querySelector('.cookies-search-box').focus();
126     },
127   };
129   // CookiesViewHandler callbacks.
130   CookiesView.onTreeItemAdded = function(args) {
131     $('cookies-list').addByParentId(args[0], args[1], args[2]);
132   };
134   CookiesView.onTreeItemRemoved = function(args) {
135     $('cookies-list').removeByParentId(args[0], args[1], args[2]);
136   };
138   CookiesView.loadChildren = function(args) {
139     $('cookies-list').loadChildren(args[0], args[1]);
140   };
142   // Export
143   return {
144     CookiesView: CookiesView
145   };