Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / resources / DirectoryContentView.js
blobd1ba80bcf7a4b85f7d3b1967a0710799414c7feb
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /**
32 * @constructor
33 * @extends {WebInspector.SortableDataGrid}
35 WebInspector.DirectoryContentView = function()
37 const indexes = WebInspector.DirectoryContentView.columnIndexes;
38 var columns = [
39 {id: indexes.Name, title: WebInspector.UIString("Name"), sortable: true, sort: WebInspector.DataGrid.Order.Ascending, width: "20%"},
40 {id: indexes.URL, title: WebInspector.UIString("URL"), sortable: true, width: "20%"},
41 {id: indexes.Type, title: WebInspector.UIString("Type"), sortable: true, width: "15%"},
42 {id: indexes.Size, title: WebInspector.UIString("Size"), sortable: true, width: "10%"},
43 {id: indexes.ModificationTime, title: WebInspector.UIString("Modification Time"), sortable: true, width: "25%"}
46 WebInspector.SortableDataGrid.call(this, columns);
47 this.addEventListener(WebInspector.DataGrid.Events.SortingChanged, this._sort, this);
50 WebInspector.DirectoryContentView.columnIndexes = {
51 Name: "0",
52 URL: "1",
53 Type: "2",
54 Size: "3",
55 ModificationTime: "4"
58 WebInspector.DirectoryContentView.prototype = {
59 /**
60 * @param {!Array.<!WebInspector.FileSystemModel.Directory>} entries
62 showEntries: function(entries)
64 const indexes = WebInspector.DirectoryContentView.columnIndexes;
65 this.rootNode().removeChildren();
66 for (var i = 0; i < entries.length; ++i)
67 this.rootNode().appendChild(new WebInspector.DirectoryContentView.Node(entries[i]));
70 _sort: function()
72 var column = this.sortColumnIdentifier();
73 if (!column)
74 return;
75 this.sortNodes(WebInspector.DirectoryContentView.Node.comparator(column), !this.isSortOrderAscending());
78 __proto__: WebInspector.SortableDataGrid.prototype
81 /**
82 * @constructor
83 * @extends {WebInspector.SortableDataGridNode}
84 * @param {!WebInspector.FileSystemModel.Entry} entry
86 WebInspector.DirectoryContentView.Node = function(entry)
88 const indexes = WebInspector.DirectoryContentView.columnIndexes;
89 var data = {};
90 data[indexes.Name] = entry.name;
91 data[indexes.URL] = entry.url;
92 data[indexes.Type] = entry.isDirectory ? WebInspector.UIString("Directory") : entry.mimeType;
93 data[indexes.Size] = "";
94 data[indexes.ModificationTime] = "";
96 WebInspector.SortableDataGridNode.call(this, data);
97 this._entry = entry;
98 this._metadata = null;
100 this._entry.requestMetadata(this._metadataReceived.bind(this));
104 * @param {string} column
105 * @return {function(!WebInspector.DataGridNode, !WebInspector.DataGridNode):number}
107 WebInspector.DirectoryContentView.Node.comparator = function(column)
109 const indexes = WebInspector.DirectoryContentView.columnIndexes;
111 switch (column) {
112 case indexes.Name:
113 case indexes.URL:
114 return function(x, y)
116 return isDirectoryCompare(x, y) || nameCompare(x, y);
118 case indexes.Type:
119 return function(x, y)
121 return isDirectoryCompare(x ,y) || typeCompare(x, y) || nameCompare(x, y);
123 case indexes.Size:
124 return function(x, y)
126 return isDirectoryCompare(x, y) || sizeCompare(x, y) || nameCompare(x, y);
128 case indexes.ModificationTime:
129 return function(x, y)
131 return isDirectoryCompare(x, y) || modificationTimeCompare(x, y) || nameCompare(x, y);
133 default:
134 return WebInspector.SortableDataGrid.TrivialComparator;
137 function isDirectoryCompare(x, y)
139 if (x._entry.isDirectory != y._entry.isDirectory)
140 return y._entry.isDirectory ? 1 : -1;
141 return 0;
144 function nameCompare(x, y)
146 return x._entry.name.compareTo(y._entry.name);
149 function typeCompare(x, y)
151 return (x._entry.mimeType || "").compareTo(y._entry.mimeType || "");
154 function sizeCompare(x, y)
156 return ((x._metadata ? x._metadata.size : 0) - (y._metadata ? y._metadata.size : 0));
159 function modificationTimeCompare(x, y)
161 return ((x._metadata ? x._metadata.modificationTime : 0) - (y._metadata ? y._metadata.modificationTime : 0));
165 WebInspector.DirectoryContentView.Node.prototype = {
167 * @param {number} errorCode
168 * @param {!FileSystemAgent.Metadata} metadata
170 _metadataReceived: function(errorCode, metadata)
172 const indexes = WebInspector.DirectoryContentView.columnIndexes;
173 if (errorCode !== 0)
174 return;
176 this._metadata = metadata;
177 var data = this.data;
178 if (this._entry.isDirectory)
179 data[indexes.Size] = WebInspector.UIString("-");
180 else
181 data[indexes.Size] = Number.bytesToString(metadata.size);
182 data[indexes.ModificationTime] = new Date(metadata.modificationTime).toISOString();
183 this.data = data;
186 __proto__: WebInspector.SortableDataGridNode.prototype