Home | History | Annotate | Download | only in front_end
      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  */
     35 WebInspector.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 
     50 WebInspector.DirectoryContentView.columnIndexes = {
     51     Name: "0",
     52     URL: "1",
     53     Type: "2",
     54     Size: "3",
     55     ModificationTime: "4"
     56 }
     57 
     58 WebInspector.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  */
     84 WebInspector.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  */
    105 WebInspector.DirectoryContentView.Node.comparator = function(column, reverse)
    106 {
    107     var reverseFactor = reverse ? -1 : 1;
    108     const indexes = WebInspector.DirectoryContentView.columnIndexes;
    109 
    110     switch (column) {
    111     case indexes.Name:
    112     case indexes.URL:
    113         return function(x, y)
    114         {
    115             return isDirectoryCompare(x, y) || nameCompare(x, y);
    116         };
    117     case indexes.Type:
    118         return function(x, y)
    119         {
    120             return isDirectoryCompare(x ,y) || typeCompare(x, y) || nameCompare(x, y);
    121         };
    122     case indexes.Size:
    123         return function(x, y)
    124         {
    125             return isDirectoryCompare(x, y) || sizeCompare(x, y) || nameCompare(x, y);
    126         };
    127     case indexes.ModificationTime:
    128         return function(x, y)
    129         {
    130             return isDirectoryCompare(x, y) || modificationTimeCompare(x, y) || nameCompare(x, y);
    131         };
    132     }
    133 
    134     function isDirectoryCompare(x, y)
    135     {
    136         if (x._entry.isDirectory != y._entry.isDirectory)
    137             return y._entry.isDirectory ? 1 : -1;
    138         return 0;
    139     }
    140 
    141     function nameCompare(x, y)
    142     {
    143         return reverseFactor * x._entry.name.compareTo(y._entry.name);
    144     }
    145 
    146     function typeCompare(x, y)
    147     {
    148         return reverseFactor * (x._entry.mimeType || "").compareTo(y._entry.mimeType || "");
    149     }
    150 
    151     function sizeCompare(x, y)
    152     {
    153         return reverseFactor * ((x._metadata ? x._metadata.size : 0) - (y._metadata ? y._metadata.size : 0));
    154     }
    155 
    156     function modificationTimeCompare(x, y)
    157     {
    158         return reverseFactor * ((x._metadata ? x._metadata.modificationTime : 0) - (y._metadata ? y._metadata.modificationTime : 0));
    159     }
    160 }
    161 
    162 WebInspector.DirectoryContentView.Node.prototype = {
    163     /**
    164      * @param {number} errorCode
    165      * @param {FileSystemAgent.Metadata} metadata
    166      */
    167     _metadataReceived: function(errorCode, metadata)
    168     {
    169         const indexes = WebInspector.DirectoryContentView.columnIndexes;
    170         if (errorCode !== 0)
    171             return;
    172 
    173         this._metadata = metadata;
    174         var data = this.data;
    175         if (this._entry.isDirectory)
    176             data[indexes.Size] = WebInspector.UIString("-");
    177         else
    178             data[indexes.Size] = Number.bytesToString(metadata.size);
    179         data[indexes.ModificationTime] = new Date(metadata.modificationTime).toGMTString();
    180         this.data = data;
    181     },
    182 
    183     __proto__: WebInspector.DataGridNode.prototype
    184 }
    185