1e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke/*
2e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * Copyright (C) 2010 Apple Inc. All rights reserved.
3e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *
4e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * Redistribution and use in source and binary forms, with or without
5e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * modification, are permitted provided that the following conditions
6e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * are met:
7e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * 1. Redistributions of source code must retain the above copyright
8e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *    notice, this list of conditions and the following disclaimer.
9e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * 2. Redistributions in binary form must reproduce the above copyright
10e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *    notice, this list of conditions and the following disclaimer in the
11e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *    documentation and/or other materials provided with the distribution.
12e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke *
13e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke * THE POSSIBILITY OF SUCH DAMAGE.
24e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke */
25e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
26e458d70a0d18538346f41b503114c9ebe6b2ce12Leon ClarkeWebInspector.ApplicationCacheItemsView = function(treeElement, appcacheDomain)
27e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke{
28e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    WebInspector.View.call(this);
29e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
30e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.element.addStyleClass("storage-view");
31e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.element.addStyleClass("table");
32e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
33e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    // FIXME: Delete Button semantics are not yet defined.
34e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    // FIXME: Needs better tooltip. (Localized)
35e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
36e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.deleteButton.visible = false;
37e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.deleteButton.addEventListener("click", this._deleteButtonClicked.bind(this), false);
38e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
39e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    // FIXME: Refresh Button semantics are not yet defined.
40e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    // FIXME: Needs better tooltip. (Localized)
41e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
42e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.refreshButton.addEventListener("click", this._refreshButtonClicked.bind(this), false);
43e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
44dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch    if (Preferences.onlineDetectionEnabled) {
45dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityIcon = document.createElement("img");
46dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityIcon.className = "storage-application-cache-connectivity-icon";
47dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityIcon.src = "";
48dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityMessage = document.createElement("span");
49dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityMessage.className = "storage-application-cache-connectivity";
50dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        this.connectivityMessage.textContent = "";
51dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch    }
52e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
53e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.divider = document.createElement("span");
54e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.divider.className = "status-bar-item status-bar-divider";
55e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
56e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusIcon = document.createElement("img");
57e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusIcon.className = "storage-application-cache-status-icon";
58e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusIcon.src = "";
59e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusMessage = document.createElement("span");
60e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusMessage.className = "storage-application-cache-status";
61e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.statusMessage.textContent = "";
62e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
63e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this._treeElement = treeElement;
64e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this._appcacheDomain = appcacheDomain;
65e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
66e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this._emptyMsgElement = document.createElement("div");
67e14391e94c850b8bd03680c23b38978db68687a8John Reck    this._emptyMsgElement.className = "storage-empty-view";
68e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this._emptyMsgElement.textContent = WebInspector.UIString("No Application Cache information available.");
69e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.element.appendChild(this._emptyMsgElement);
70e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
71e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    this.updateStatus(applicationCache.UNCACHED);
72e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke}
73e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
74e458d70a0d18538346f41b503114c9ebe6b2ce12Leon ClarkeWebInspector.ApplicationCacheItemsView.prototype = {
75e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    get statusBarItems()
76e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
77dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        if (Preferences.onlineDetectionEnabled) {
78dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            return [
79dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.refreshButton.element, this.deleteButton.element,
80dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.connectivityIcon, this.connectivityMessage, this.divider,
81dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.statusIcon, this.statusMessage
82dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            ];
83dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        } else {
84dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            return [
85dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.refreshButton.element, this.deleteButton.element, this.divider,
86dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.statusIcon, this.statusMessage
87dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            ];
88dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        }
89e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
90e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
91e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    show: function(parentElement)
92e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
93e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        WebInspector.View.prototype.show.call(this, parentElement);
94e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.updateNetworkState(navigator.onLine);
95e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._update();
96e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
97e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
98e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    hide: function()
99e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
100e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        WebInspector.View.prototype.hide.call(this);
101e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.deleteButton.visible = false;
102e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
103e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
104e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    updateStatus: function(status)
105e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
106e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var statusInformation = {};
107e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.UNCACHED]    = { src: "Images/warningOrangeDot.png", text: "UNCACHED"    };
108e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.IDLE]        = { src: "Images/warningOrangeDot.png", text: "IDLE"        };
109e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.CHECKING]    = { src: "Images/successGreenDot.png",  text: "CHECKING"    };
110e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.DOWNLOADING] = { src: "Images/successGreenDot.png",  text: "DOWNLOADING" };
111e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.UPDATEREADY] = { src: "Images/successGreenDot.png",  text: "UPDATEREADY" };
112e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        statusInformation[applicationCache.OBSOLETE]    = { src: "Images/errorRedDot.png",      text: "OBSOLETE"    };
113e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
114e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var info = statusInformation[status];
115e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (!info) {
116e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            console.error("Unknown Application Cache Status was Not Handled: %d", status);
117e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            return;
118e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
119e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
120e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.statusIcon.src = info.src;
121e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.statusMessage.textContent = info.text;
122e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
123e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
124e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    updateNetworkState: function(isNowOnline)
125e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
126dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch        if (Preferences.onlineDetectionEnabled) {
127dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            if (isNowOnline) {
128dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.connectivityIcon.src = "Images/successGreenDot.png";
129dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.connectivityMessage.textContent = WebInspector.UIString("Online");
130dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            } else {
131dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.connectivityIcon.src = "Images/errorRedDot.png";
132dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch                this.connectivityMessage.textContent = WebInspector.UIString("Offline");
133dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch            }
134e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
135e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
136e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
137e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _update: function()
138e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
1392fc2651226baac27029e38c9d6ef883fa32084dbSteve Block        WebInspector.ApplicationCacheDispatcher.getApplicationCachesAsync(this._updateCallback.bind(this));
140e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
141e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
142e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _updateCallback: function(applicationCaches)
143e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
144e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: applicationCaches is just one cache.
145e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: are these variables needed anywhere else?
146e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._manifest = applicationCaches.manifest;
147e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._creationTime = applicationCaches.creationTime;
148e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._updateTime = applicationCaches.updateTime;
149e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._size = applicationCaches.size;
150e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._resources = applicationCaches.resources;
151e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var lastPathComponent = applicationCaches.lastPathComponent;
152e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
153e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (!this._manifest) {
154e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this._emptyMsgElement.removeStyleClass("hidden");
155e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this.deleteButton.visible = false;
156e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            if (this._dataGrid)
157e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke                this._dataGrid.element.addStyleClass("hidden");
158e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            return;
159e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
160e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
161e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (!this._dataGrid)
162e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this._createDataGrid();
163e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
164e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._populateDataGrid();
165e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid.autoSizeColumns(20, 80);
166e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid.element.removeStyleClass("hidden");
167e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._emptyMsgElement.addStyleClass("hidden");
168e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.deleteButton.visible = true;
169e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
17065f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch        var totalSizeString = Number.bytesToString(this._size);
171e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._treeElement.subtitle = WebInspector.UIString("%s (%s)", lastPathComponent, totalSizeString);
172e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
173e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: For Chrome, put creationTime and updateTime somewhere.
174e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // NOTE: localizedString has not yet been added.
175e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // WebInspector.UIString("(%s) Created: %s Updated: %s", this._size, this._creationTime, this._updateTime);
176e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
177e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
178e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _createDataGrid: function()
179e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
180e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var columns = { 0: {}, 1: {}, 2: {} };
181e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[0].title = WebInspector.UIString("Resource");
182e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[0].sort = "ascending";
183e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[0].sortable = true;
184e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[1].title = WebInspector.UIString("Type");
185e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[1].sortable = true;
186e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[2].title = WebInspector.UIString("Size");
187e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[2].aligned = "right";
188e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        columns[2].sortable = true;
189e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid = new WebInspector.DataGrid(columns);
190e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this.element.appendChild(this._dataGrid.element);
191e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid.addEventListener("sorting changed", this._populateDataGrid, this);
192e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid.updateWidths();
193e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
194e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
195e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _populateDataGrid: function()
196e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
197e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var selectedResource = this._dataGrid.selectedNode ? this._dataGrid.selectedNode.resource : null;
198e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
199e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
200e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        function numberCompare(field, resource1, resource2)
201e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        {
202e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            return sortDirection * (resource1[field] - resource2[field]);
203e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
204e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        function localeCompare(field, resource1, resource2)
205e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        {
206e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke             return sortDirection * (resource1[field] + "").localeCompare(resource2[field] + "")
207e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
208e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
209e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var comparator;
210e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        switch (parseInt(this._dataGrid.sortColumnIdentifier)) {
211e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            case 0: comparator = localeCompare.bind(this, "name"); break;
212e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            case 1: comparator = localeCompare.bind(this, "type"); break;
213e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            case 2: comparator = numberCompare.bind(this, "size"); break;
214e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            default: localeCompare.bind(this, "resource"); // FIXME: comparator = ?
215e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
216e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
217e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._resources.sort(comparator);
218e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._dataGrid.removeChildren();
219e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
220e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        var nodeToSelect;
221e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        for (var i = 0; i < this._resources.length; ++i) {
222e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            var data = {};
223e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            var resource = this._resources[i];
224e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            data[0] = resource.name;
225e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            data[1] = resource.type;
22665f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch            data[2] = Number.bytesToString(resource.size);
227e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            var node = new WebInspector.DataGridNode(data);
228e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            node.resource = resource;
229e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            node.selectable = true;
230e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this._dataGrid.appendChild(node);
231e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            if (resource === selectedResource) {
232e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke                nodeToSelect = node;
233e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke                nodeToSelect.selected = true;
234e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            }
235e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        }
236e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
237e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (!nodeToSelect)
238e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this._dataGrid.children[0].selected = true;
239e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
240e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
241e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    resize: function()
242e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
243e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (this._dataGrid)
244e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            this._dataGrid.updateWidths();
245e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
246e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
247e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _deleteButtonClicked: function(event)
248e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
249e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        if (!this._dataGrid || !this._dataGrid.selectedNode)
250e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke            return;
251e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
252e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: Delete Button semantics are not yet defined. (Delete a single, or all?)
253e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        this._deleteCallback(this._dataGrid.selectedNode);
254e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
255e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
256e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _deleteCallback: function(node)
257e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
258e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: Should we delete a single (selected) resource or all resources?
259e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // InspectorBackend.deleteCachedResource(...)
260e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // this._update();
261e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    },
262e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
263e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    _refreshButtonClicked: function(event)
264e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    {
265e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // FIXME: Is this a refresh button or a re-fetch manifest button?
266e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke        // this._update();
267e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke    }
268e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke}
269e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke
270e458d70a0d18538346f41b503114c9ebe6b2ce12Leon ClarkeWebInspector.ApplicationCacheItemsView.prototype.__proto__ = WebInspector.View.prototype;
2712bde8e466a4451c7319e3a072d118917957d6554Steve Block
2722bde8e466a4451c7319e3a072d118917957d6554Steve BlockWebInspector.ApplicationCacheDispatcher = function()
2732bde8e466a4451c7319e3a072d118917957d6554Steve Block{
2742bde8e466a4451c7319e3a072d118917957d6554Steve Block}
2752bde8e466a4451c7319e3a072d118917957d6554Steve Block
2762bde8e466a4451c7319e3a072d118917957d6554Steve BlockWebInspector.ApplicationCacheDispatcher.getApplicationCachesAsync = function(callback)
2772bde8e466a4451c7319e3a072d118917957d6554Steve Block{
2782bde8e466a4451c7319e3a072d118917957d6554Steve Block    function mycallback(error, applicationCaches)
2792bde8e466a4451c7319e3a072d118917957d6554Steve Block    {
2802bde8e466a4451c7319e3a072d118917957d6554Steve Block        // FIXME: Currently, this list only returns a single application cache.
2812bde8e466a4451c7319e3a072d118917957d6554Steve Block        if (!error && applicationCaches)
2822bde8e466a4451c7319e3a072d118917957d6554Steve Block            callback(applicationCaches);
2832bde8e466a4451c7319e3a072d118917957d6554Steve Block    }
2842bde8e466a4451c7319e3a072d118917957d6554Steve Block
2852bde8e466a4451c7319e3a072d118917957d6554Steve Block    ApplicationCacheAgent.getApplicationCaches(mycallback);
2862bde8e466a4451c7319e3a072d118917957d6554Steve Block}
2872bde8e466a4451c7319e3a072d118917957d6554Steve Block
2882bde8e466a4451c7319e3a072d118917957d6554Steve BlockWebInspector.ApplicationCacheDispatcher.prototype = {
2892bde8e466a4451c7319e3a072d118917957d6554Steve Block    updateApplicationCacheStatus: function(status)
2902bde8e466a4451c7319e3a072d118917957d6554Steve Block    {
2912bde8e466a4451c7319e3a072d118917957d6554Steve Block        WebInspector.panels.resources.updateApplicationCacheStatus(status);
2922bde8e466a4451c7319e3a072d118917957d6554Steve Block    },
2932bde8e466a4451c7319e3a072d118917957d6554Steve Block
2942bde8e466a4451c7319e3a072d118917957d6554Steve Block    updateNetworkState: function(isNowOnline)
2952bde8e466a4451c7319e3a072d118917957d6554Steve Block    {
2962bde8e466a4451c7319e3a072d118917957d6554Steve Block        WebInspector.panels.resources.updateNetworkState(isNowOnline);
2972bde8e466a4451c7319e3a072d118917957d6554Steve Block    }
2982bde8e466a4451c7319e3a072d118917957d6554Steve Block}
2992bde8e466a4451c7319e3a072d118917957d6554Steve Block
3002bde8e466a4451c7319e3a072d118917957d6554Steve BlockInspectorBackend.registerDomainDispatcher("ApplicationCache", new WebInspector.ApplicationCacheDispatcher());
301