1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
29 */
30
31/**
32 * @constructor
33 * @extends {WebInspector.SortableDataGrid}
34 */
35WebInspector.DirectoryContentView = function()
36{
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%"}
44    ];
45
46    WebInspector.SortableDataGrid.call(this, columns);
47    this.addEventListener(WebInspector.DataGrid.Events.SortingChanged, this._sort, this);
48}
49
50WebInspector.DirectoryContentView.columnIndexes = {
51    Name: "0",
52    URL: "1",
53    Type: "2",
54    Size: "3",
55    ModificationTime: "4"
56}
57
58WebInspector.DirectoryContentView.prototype = {
59    /**
60     * @param {!Array.<!WebInspector.FileSystemModel.Directory>} entries
61     */
62    showEntries: function(entries)
63    {
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]));
68    },
69
70    _sort: function()
71    {
72        var column = this.sortColumnIdentifier();
73        if (!column)
74            return;
75        this.sortNodes(WebInspector.DirectoryContentView.Node.comparator(column), !this.isSortOrderAscending());
76    },
77
78    __proto__: WebInspector.SortableDataGrid.prototype
79}
80
81/**
82 * @constructor
83 * @extends {WebInspector.SortableDataGridNode}
84 * @param {!WebInspector.FileSystemModel.Entry} entry
85 */
86WebInspector.DirectoryContentView.Node = function(entry)
87{
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] = "";
95
96    WebInspector.SortableDataGridNode.call(this, data);
97    this._entry = entry;
98    this._metadata = null;
99
100    this._entry.requestMetadata(this._metadataReceived.bind(this));
101}
102
103/**
104 * @param {string} column
105 * @return {function(!WebInspector.DataGridNode, !WebInspector.DataGridNode):number}
106 */
107WebInspector.DirectoryContentView.Node.comparator = function(column)
108{
109    const indexes = WebInspector.DirectoryContentView.columnIndexes;
110
111    switch (column) {
112    case indexes.Name:
113    case indexes.URL:
114        return function(x, y)
115        {
116            return isDirectoryCompare(x, y) || nameCompare(x, y);
117        };
118    case indexes.Type:
119        return function(x, y)
120        {
121            return isDirectoryCompare(x ,y) || typeCompare(x, y) || nameCompare(x, y);
122        };
123    case indexes.Size:
124        return function(x, y)
125        {
126            return isDirectoryCompare(x, y) || sizeCompare(x, y) || nameCompare(x, y);
127        };
128    case indexes.ModificationTime:
129        return function(x, y)
130        {
131            return isDirectoryCompare(x, y) || modificationTimeCompare(x, y) || nameCompare(x, y);
132        };
133    default:
134        return WebInspector.SortableDataGrid.TrivialComparator;
135    }
136
137    function isDirectoryCompare(x, y)
138    {
139        if (x._entry.isDirectory != y._entry.isDirectory)
140            return y._entry.isDirectory ? 1 : -1;
141        return 0;
142    }
143
144    function nameCompare(x, y)
145    {
146        return x._entry.name.compareTo(y._entry.name);
147    }
148
149    function typeCompare(x, y)
150    {
151        return (x._entry.mimeType || "").compareTo(y._entry.mimeType || "");
152    }
153
154    function sizeCompare(x, y)
155    {
156        return ((x._metadata ? x._metadata.size : 0) - (y._metadata ? y._metadata.size : 0));
157    }
158
159    function modificationTimeCompare(x, y)
160    {
161        return ((x._metadata ? x._metadata.modificationTime : 0) - (y._metadata ? y._metadata.modificationTime : 0));
162    }
163}
164
165WebInspector.DirectoryContentView.Node.prototype = {
166    /**
167     * @param {number} errorCode
168     * @param {!FileSystemAgent.Metadata} metadata
169     */
170    _metadataReceived: function(errorCode, metadata)
171    {
172        const indexes = WebInspector.DirectoryContentView.columnIndexes;
173        if (errorCode !== 0)
174            return;
175
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;
184    },
185
186    __proto__: WebInspector.SortableDataGridNode.prototype
187}
188