filesummary.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. (function() {
  22. /**
  23. * The FileSummary class encapsulates the file summary values and
  24. * the logic to render it in the given container
  25. *
  26. * @constructs FileSummary
  27. * @memberof OCA.Files
  28. *
  29. * @param $tr table row element
  30. */
  31. var FileSummary = function($tr) {
  32. this.$el = $tr;
  33. this.clear();
  34. this.render();
  35. };
  36. FileSummary.prototype = {
  37. summary: {
  38. totalFiles: 0,
  39. totalDirs: 0,
  40. totalSize: 0,
  41. filter:'',
  42. sumIsPending:false
  43. },
  44. /**
  45. * Adds file
  46. * @param file file to add
  47. * @param update whether to update the display
  48. */
  49. add: function(file, update) {
  50. if (file.name && file.name.toLowerCase().indexOf(this.summary.filter) === -1) {
  51. return;
  52. }
  53. if (file.type === 'dir' || file.mime === 'httpd/unix-directory') {
  54. this.summary.totalDirs++;
  55. }
  56. else {
  57. this.summary.totalFiles++;
  58. }
  59. var size = parseInt(file.size, 10) || 0;
  60. if (size >=0) {
  61. this.summary.totalSize += size;
  62. } else {
  63. this.summary.sumIsPending = true;
  64. }
  65. if (!!update) {
  66. this.update();
  67. }
  68. },
  69. /**
  70. * Removes file
  71. * @param file file to remove
  72. * @param update whether to update the display
  73. */
  74. remove: function(file, update) {
  75. if (file.name && file.name.toLowerCase().indexOf(this.summary.filter) === -1) {
  76. return;
  77. }
  78. if (file.type === 'dir' || file.mime === 'httpd/unix-directory') {
  79. this.summary.totalDirs--;
  80. }
  81. else {
  82. this.summary.totalFiles--;
  83. }
  84. var size = parseInt(file.size, 10) || 0;
  85. if (size >=0) {
  86. this.summary.totalSize -= size;
  87. }
  88. if (!!update) {
  89. this.update();
  90. }
  91. },
  92. setFilter: function(filter, files){
  93. this.summary.filter = filter.toLowerCase();
  94. this.calculate(files);
  95. },
  96. /**
  97. * Returns the total of files and directories
  98. */
  99. getTotal: function() {
  100. return this.summary.totalDirs + this.summary.totalFiles;
  101. },
  102. /**
  103. * Recalculates the summary based on the given files array
  104. * @param files array of files
  105. */
  106. calculate: function(files) {
  107. var file;
  108. var summary = {
  109. totalDirs: 0,
  110. totalFiles: 0,
  111. totalSize: 0,
  112. filter: this.summary.filter,
  113. sumIsPending: false
  114. };
  115. for (var i = 0; i < files.length; i++) {
  116. file = files[i];
  117. if (file.name && file.name.toLowerCase().indexOf(this.summary.filter) === -1) {
  118. continue;
  119. }
  120. if (file.type === 'dir' || file.mime === 'httpd/unix-directory') {
  121. summary.totalDirs++;
  122. }
  123. else {
  124. summary.totalFiles++;
  125. }
  126. var size = parseInt(file.size, 10) || 0;
  127. if (size >=0) {
  128. summary.totalSize += size;
  129. } else {
  130. summary.sumIsPending = true;
  131. }
  132. }
  133. this.setSummary(summary);
  134. },
  135. /**
  136. * Clears the summary
  137. */
  138. clear: function() {
  139. this.calculate([]);
  140. },
  141. /**
  142. * Sets the current summary values
  143. * @param summary map
  144. */
  145. setSummary: function(summary) {
  146. this.summary = summary;
  147. if (typeof this.summary.filter === 'undefined') {
  148. this.summary.filter = '';
  149. }
  150. this.update();
  151. },
  152. /**
  153. * Renders the file summary element
  154. */
  155. update: function() {
  156. if (!this.$el) {
  157. return;
  158. }
  159. if (!this.summary.totalFiles && !this.summary.totalDirs) {
  160. this.$el.addClass('hidden');
  161. return;
  162. }
  163. // There's a summary and data -> Update the summary
  164. this.$el.removeClass('hidden');
  165. var $dirInfo = this.$el.find('.dirinfo');
  166. var $fileInfo = this.$el.find('.fileinfo');
  167. var $connector = this.$el.find('.connector');
  168. var $filterInfo = this.$el.find('.filter');
  169. // Substitute old content with new translations
  170. $dirInfo.html(n('files', '%n folder', '%n folders', this.summary.totalDirs));
  171. $fileInfo.html(n('files', '%n file', '%n files', this.summary.totalFiles));
  172. var fileSize = this.summary.sumIsPending ? t('files', 'Pending') : OC.Util.humanFileSize(this.summary.totalSize);
  173. this.$el.find('.filesize').html(fileSize);
  174. // Show only what's necessary (may be hidden)
  175. if (this.summary.totalDirs === 0) {
  176. $dirInfo.addClass('hidden');
  177. $connector.addClass('hidden');
  178. } else {
  179. $dirInfo.removeClass('hidden');
  180. }
  181. if (this.summary.totalFiles === 0) {
  182. $fileInfo.addClass('hidden');
  183. $connector.addClass('hidden');
  184. } else {
  185. $fileInfo.removeClass('hidden');
  186. }
  187. if (this.summary.totalDirs > 0 && this.summary.totalFiles > 0) {
  188. $connector.removeClass('hidden');
  189. }
  190. if (this.summary.filter === '') {
  191. $filterInfo.html('');
  192. $filterInfo.addClass('hidden');
  193. } else {
  194. $filterInfo.html(' ' + n('files', 'matches \'{filter}\'', 'match \'{filter}\'', this.summary.totalDirs + this.summary.totalFiles, {filter: this.summary.filter}));
  195. $filterInfo.removeClass('hidden');
  196. }
  197. },
  198. render: function() {
  199. if (!this.$el) {
  200. return;
  201. }
  202. // TODO: ideally this should be separate to a template or something
  203. var summary = this.summary;
  204. var directoryInfo = n('files', '%n folder', '%n folders', summary.totalDirs);
  205. var fileInfo = n('files', '%n file', '%n files', summary.totalFiles);
  206. var filterInfo = '';
  207. if (this.summary.filter !== '') {
  208. filterInfo = ' ' + n('files', 'matches \'{filter}\'', 'match \'{filter}\'', summary.totalFiles + summary.totalDirs, {filter: summary.filter});
  209. }
  210. var infoVars = {
  211. dirs: '<span class="dirinfo">'+directoryInfo+'</span><span class="connector">',
  212. files: '</span><span class="fileinfo">'+fileInfo+'</span>'
  213. };
  214. // don't show the filesize column, if filesize is NaN (e.g. in trashbin)
  215. var fileSize = '';
  216. if (!isNaN(summary.totalSize)) {
  217. fileSize = summary.sumIsPending ? t('files', 'Pending') : OC.Util.humanFileSize(summary.totalSize);
  218. fileSize = '<td class="filesize">' + fileSize + '</td>';
  219. }
  220. var info = t('files', '{dirs} and {files}', infoVars, null, {'escape': false});
  221. var $summary = $('<td><span class="info">'+info+'<span class="filter">'+filterInfo+'</span></span></td>'+fileSize+'<td class="date"></td>');
  222. if (!this.summary.totalFiles && !this.summary.totalDirs) {
  223. this.$el.addClass('hidden');
  224. }
  225. this.$el.append($summary);
  226. }
  227. };
  228. OCA.Files.FileSummary = FileSummary;
  229. })();