DirectoryContentView.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @constructor
  32. * @extends {WebInspector.DataGrid}
  33. */
  34. WebInspector.DirectoryContentView = function()
  35. {
  36. const indexes = WebInspector.DirectoryContentView.columnIndexes;
  37. var columns = [
  38. {id: indexes.Name, title: WebInspector.UIString("Name"), sortable: true, sort: WebInspector.DataGrid.Order.Ascending, width: "20%"},
  39. {id: indexes.URL, title: WebInspector.UIString("URL"), sortable: true, width: "20%"},
  40. {id: indexes.Type, title: WebInspector.UIString("Type"), sortable: true, width: "15%"},
  41. {id: indexes.Size, title: WebInspector.UIString("Size"), sortable: true, width: "10%"},
  42. {id: indexes.ModificationTime, title: WebInspector.UIString("Modification Time"), sortable: true, width: "25%"}
  43. ];
  44. WebInspector.DataGrid.call(this, columns);
  45. this.addEventListener(WebInspector.DataGrid.Events.SortingChanged, this._sort, this);
  46. }
  47. WebInspector.DirectoryContentView.columnIndexes = {
  48. Name: "0",
  49. URL: "1",
  50. Type: "2",
  51. Size: "3",
  52. ModificationTime: "4"
  53. }
  54. WebInspector.DirectoryContentView.prototype = {
  55. /**
  56. * @param {Array.<WebInspector.FileSystemModel.Directory>} entries
  57. */
  58. showEntries: function(entries)
  59. {
  60. const indexes = WebInspector.DirectoryContentView.columnIndexes;
  61. this.rootNode().removeChildren();
  62. for (var i = 0; i < entries.length; ++i)
  63. this.rootNode().appendChild(new WebInspector.DirectoryContentView.Node(entries[i]));
  64. },
  65. _sort: function()
  66. {
  67. var column = /** @type {string} */ (this.sortColumnIdentifier());
  68. this.sortNodes(WebInspector.DirectoryContentView.Node.comparator(column, !this.isSortOrderAscending()), false);
  69. },
  70. __proto__: WebInspector.DataGrid.prototype
  71. }
  72. /**
  73. * @constructor
  74. * @extends {WebInspector.DataGridNode}
  75. * @param {WebInspector.FileSystemModel.Entry} entry
  76. */
  77. WebInspector.DirectoryContentView.Node = function(entry)
  78. {
  79. const indexes = WebInspector.DirectoryContentView.columnIndexes;
  80. var data = {};
  81. data[indexes.Name] = entry.name;
  82. data[indexes.URL] = entry.url;
  83. data[indexes.Type] = entry.isDirectory ? WebInspector.UIString("Directory") : entry.mimeType;
  84. data[indexes.Size] = "";
  85. data[indexes.ModificationTime] = "";
  86. WebInspector.DataGridNode.call(this, data);
  87. this._entry = entry;
  88. this._metadata = null;
  89. this._entry.requestMetadata(this._metadataReceived.bind(this));
  90. }
  91. /**
  92. * @param {string} column
  93. * @param {boolean} reverse
  94. */
  95. WebInspector.DirectoryContentView.Node.comparator = function(column, reverse)
  96. {
  97. var reverseFactor = reverse ? -1 : 1;
  98. const indexes = WebInspector.DirectoryContentView.columnIndexes;
  99. switch (column) {
  100. case indexes.Name:
  101. case indexes.URL:
  102. return function(x, y)
  103. {
  104. return isDirectoryCompare(x, y) || nameCompare(x, y);
  105. };
  106. case indexes.Type:
  107. return function(x, y)
  108. {
  109. return isDirectoryCompare(x ,y) || typeCompare(x, y) || nameCompare(x, y);
  110. };
  111. case indexes.Size:
  112. return function(x, y)
  113. {
  114. return isDirectoryCompare(x, y) || sizeCompare(x, y) || nameCompare(x, y);
  115. };
  116. case indexes.ModificationTime:
  117. return function(x, y)
  118. {
  119. return isDirectoryCompare(x, y) || modificationTimeCompare(x, y) || nameCompare(x, y);
  120. };
  121. }
  122. function isDirectoryCompare(x, y)
  123. {
  124. if (x._entry.isDirectory != y._entry.isDirectory)
  125. return y._entry.isDirectory ? 1 : -1;
  126. return 0;
  127. }
  128. function nameCompare(x, y)
  129. {
  130. return reverseFactor * x._entry.name.compareTo(y._entry.name);
  131. }
  132. function typeCompare(x, y)
  133. {
  134. return reverseFactor * (x._entry.mimeType || "").compareTo(y._entry.mimeType || "");
  135. }
  136. function sizeCompare(x, y)
  137. {
  138. return reverseFactor * ((x._metadata ? x._metadata.size : 0) - (y._metadata ? y._metadata.size : 0));
  139. }
  140. function modificationTimeCompare(x, y)
  141. {
  142. return reverseFactor * ((x._metadata ? x._metadata.modificationTime : 0) - (y._metadata ? y._metadata.modificationTime : 0));
  143. }
  144. }
  145. WebInspector.DirectoryContentView.Node.prototype = {
  146. /**
  147. * @param {number} errorCode
  148. * @param {FileSystemAgent.Metadata} metadata
  149. */
  150. _metadataReceived: function(errorCode, metadata)
  151. {
  152. const indexes = WebInspector.DirectoryContentView.columnIndexes;
  153. if (errorCode !== 0)
  154. return;
  155. this._metadata = metadata;
  156. var data = this.data;
  157. if (this._entry.isDirectory)
  158. data[indexes.Size] = WebInspector.UIString("-");
  159. else
  160. data[indexes.Size] = Number.bytesToString(metadata.size);
  161. data[indexes.ModificationTime] = new Date(metadata.modificationTime).toGMTString();
  162. this.data = data;
  163. },
  164. __proto__: WebInspector.DataGridNode.prototype
  165. }