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.DataGrid} 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.DataGrid.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 = /** @type {string} */ (this.sortColumnIdentifier()); 73 this.sortNodes(WebInspector.DirectoryContentView.Node.comparator(column, !this.isSortOrderAscending()), false); 74 }, 75 76 __proto__: WebInspector.DataGrid.prototype 77} 78 79/** 80 * @constructor 81 * @extends {WebInspector.DataGridNode} 82 * @param {!WebInspector.FileSystemModel.Entry} entry 83 */ 84WebInspector.DirectoryContentView.Node = function(entry) 85{ 86 const indexes = WebInspector.DirectoryContentView.columnIndexes; 87 var data = {}; 88 data[indexes.Name] = entry.name; 89 data[indexes.URL] = entry.url; 90 data[indexes.Type] = entry.isDirectory ? WebInspector.UIString("Directory") : entry.mimeType; 91 data[indexes.Size] = ""; 92 data[indexes.ModificationTime] = ""; 93 94 WebInspector.DataGridNode.call(this, data); 95 this._entry = entry; 96 this._metadata = null; 97 98 this._entry.requestMetadata(this._metadataReceived.bind(this)); 99} 100 101/** 102 * @param {string} column 103 * @param {boolean} reverse 104 * @return {function(!WebInspector.DirectoryContentView.Node, !WebInspector.DirectoryContentView.Node):number|undefined} 105 */ 106WebInspector.DirectoryContentView.Node.comparator = function(column, reverse) 107{ 108 var reverseFactor = reverse ? -1 : 1; 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 } 134 135 function isDirectoryCompare(x, y) 136 { 137 if (x._entry.isDirectory != y._entry.isDirectory) 138 return y._entry.isDirectory ? 1 : -1; 139 return 0; 140 } 141 142 function nameCompare(x, y) 143 { 144 return reverseFactor * x._entry.name.compareTo(y._entry.name); 145 } 146 147 function typeCompare(x, y) 148 { 149 return reverseFactor * (x._entry.mimeType || "").compareTo(y._entry.mimeType || ""); 150 } 151 152 function sizeCompare(x, y) 153 { 154 return reverseFactor * ((x._metadata ? x._metadata.size : 0) - (y._metadata ? y._metadata.size : 0)); 155 } 156 157 function modificationTimeCompare(x, y) 158 { 159 return reverseFactor * ((x._metadata ? x._metadata.modificationTime : 0) - (y._metadata ? y._metadata.modificationTime : 0)); 160 } 161} 162 163WebInspector.DirectoryContentView.Node.prototype = { 164 /** 165 * @param {number} errorCode 166 * @param {!FileSystemAgent.Metadata} metadata 167 */ 168 _metadataReceived: function(errorCode, metadata) 169 { 170 const indexes = WebInspector.DirectoryContentView.columnIndexes; 171 if (errorCode !== 0) 172 return; 173 174 this._metadata = metadata; 175 var data = this.data; 176 if (this._entry.isDirectory) 177 data[indexes.Size] = WebInspector.UIString("-"); 178 else 179 data[indexes.Size] = Number.bytesToString(metadata.size); 180 data[indexes.ModificationTime] = new Date(metadata.modificationTime).toISOString(); 181 this.data = data; 182 }, 183 184 __proto__: WebInspector.DataGridNode.prototype 185} 186