Web Inspector: [Resources] Prefactorings in DataGrid and CookieTable
[WebKit-https.git] / Source / WebCore / inspector / front-end / CookieItemsView.js
1 /*
2  * Copyright (C) 2009 Apple Inc.  All rights reserved.
3  * Copyright (C) 2009 Joseph Pecoraro
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /**
31  * @constructor
32  * @extends {WebInspector.View}
33  */
34 WebInspector.CookieItemsView = function(treeElement, cookieDomain)
35 {
36     WebInspector.View.call(this);
37
38     this.element.addStyleClass("storage-view");
39
40     this._deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
41     this._deleteButton.visible = false;
42     this._deleteButton.addEventListener("click", this._deleteButtonClicked, this);
43
44     this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
45     this._refreshButton.addEventListener("click", this._refreshButtonClicked, this);
46
47     this._treeElement = treeElement;
48     this._cookieDomain = cookieDomain;
49
50     this._emptyView = new WebInspector.EmptyView(WebInspector.UIString("This site has no cookies."));
51     this._emptyView.show(this.element);
52
53     this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
54 }
55
56 WebInspector.CookieItemsView.prototype = {
57     get statusBarItems()
58     {
59         return [this._refreshButton.element, this._deleteButton.element];
60     },
61
62     wasShown: function()
63     {
64         this._update();
65     },
66
67     willHide: function()
68     {
69         this._deleteButton.visible = false;
70     },
71
72     _update: function()
73     {
74         WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this));
75     },
76
77     /**
78      * @param {Array.<WebInspector.Cookie>} allCookies
79      * @param {boolean} isAdvanced
80      */
81     _updateWithCookies: function(allCookies, isAdvanced)
82     {
83         this._cookies = isAdvanced ? this._filterCookiesForDomain(allCookies) : allCookies;
84
85         if (!this._cookies.length) {
86             // Nothing to show.
87             this._emptyView.show(this.element);
88             this._deleteButton.visible = false;
89             if (this._cookiesTable)
90                 this._cookiesTable.detach();
91             return;
92         }
93
94         if (!this._cookiesTable)
95             this._cookiesTable = isAdvanced ? new WebInspector.CookiesTable(false, this._update.bind(this)) : new WebInspector.SimpleCookiesTable();
96
97         this._cookiesTable.setCookies(this._cookies);
98         this._emptyView.detach();
99         this._cookiesTable.show(this.element);
100         if (isAdvanced) {
101             this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length,
102                 Number.bytesToString(this._totalSize));
103             this._deleteButton.visible = true;
104         }
105     },
106
107     /**
108      * @param {Array.<WebInspector.Cookie>} allCookies
109      */
110     _filterCookiesForDomain: function(allCookies)
111     {
112         var cookies = [];
113         var resourceURLsForDocumentURL = [];
114         this._totalSize = 0;
115
116         function populateResourcesForDocuments(resource)
117         {
118             var url = resource.documentURL.asParsedURL();
119             if (url && url.host == this._cookieDomain)
120                 resourceURLsForDocumentURL.push(resource.url);
121         }
122         WebInspector.forAllResources(populateResourcesForDocuments.bind(this));
123
124         for (var i = 0; i < allCookies.length; ++i) {
125             var pushed = false;
126             var size = allCookies[i].size();
127             for (var j = 0; j < resourceURLsForDocumentURL.length; ++j) {
128                 var resourceURL = resourceURLsForDocumentURL[j];
129                 if (WebInspector.Cookies.cookieMatchesResourceURL(allCookies[i], resourceURL)) {
130                     this._totalSize += size;
131                     if (!pushed) {
132                         pushed = true;
133                         cookies.push(allCookies[i]);
134                     }
135                 }
136             }
137         }
138         return cookies;
139     },
140
141     _deleteButtonClicked: function()
142     {
143         var selectedCookie = this._cookiesTable.selectedCookie();
144         if (selectedCookie) {
145             selectedCookie.remove();
146             this._update();
147         }
148     },
149
150     _refreshButtonClicked: function(event)
151     {
152         this._update();
153     },
154
155     _contextMenu: function(event)
156     {
157         if (!this._cookies.length) {
158             var contextMenu = new WebInspector.ContextMenu(event);
159             contextMenu.appendItem(WebInspector.UIString("Refresh"), this._update.bind(this));
160             contextMenu.show();
161         }
162     },
163
164     __proto__: WebInspector.View.prototype
165 }
166
167 /**
168  * @constructor
169  * @extends {WebInspector.View}
170  */
171 WebInspector.SimpleCookiesTable = function()
172 {
173     WebInspector.View.call(this);
174
175     var columns = {};
176     columns[0] = {};
177     columns[1] = {};
178     columns[0].title = WebInspector.UIString("Name");
179     columns[1].title = WebInspector.UIString("Value");
180
181     this._dataGrid = new WebInspector.DataGrid(columns);
182     this._dataGrid.autoSizeColumns(20, 80);
183     this._dataGrid.show(this.element);
184 }
185
186 WebInspector.SimpleCookiesTable.prototype = {
187     /**
188      * @param {Array.<WebInspector.Cookie>} cookies
189      */
190     setCookies: function(cookies)
191     {
192         this._dataGrid.rootNode().removeChildren();
193         var addedCookies = {};
194         for (var i = 0; i < cookies.length; ++i) {
195             if (addedCookies[cookies[i].name()])
196                 continue;
197             addedCookies[cookies[i].name()] = true;
198             var data = {};
199             data[0] = cookies[i].name();
200             data[1] = cookies[i].value();
201
202             var node = new WebInspector.DataGridNode(data, false);
203             node.selectable = true;
204             this._dataGrid.rootNode().appendChild(node);
205         }
206         this._dataGrid.rootNode().children[0].selected = true;
207     },
208
209     __proto__: WebInspector.View.prototype
210 }