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