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