diff options
Diffstat (limited to 'apps/files/js')
-rw-r--r-- | apps/files/js/filelist.js | 136 | ||||
-rw-r--r-- | apps/files/js/filesummary.js | 176 |
2 files changed, 200 insertions, 112 deletions
diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 21ce1418210..223f4bb4409 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -8,7 +8,7 @@ * */ -/* global OC, t, n, FileList, FileActions, Files, BreadCrumb */ +/* global OC, t, n, FileList, FileActions, Files, FileSummary, BreadCrumb */ /* global procesSelection, dragOptions, folderDropOptions */ window.FileList = { appName: t('files', 'Files'), @@ -17,6 +17,11 @@ window.FileList = { $el: $('#filestable'), $fileList: $('#fileList'), breadcrumb: null, + + /** + * Instance of FileSummary + */ + fileSummary: null, initialized: false, // number of files per page @@ -38,6 +43,8 @@ window.FileList = { this.$el = $('#filestable'); this.$fileList = $('#fileList'); + this.fileSummary = this._createSummary(); + this.breadcrumb = new BreadCrumb({ onClick: this._onClickBreadCrumb, onDrop: this._onDropOnBreadCrumb, @@ -72,7 +79,6 @@ window.FileList = { if (this.pageNumber + 1 >= this.totalPages) { return; } - var target = $(document); if ($(window).scrollTop() + $(window).height() > $(document).height() - 20) { this._nextPage(true); } @@ -206,7 +212,9 @@ window.FileList = { if (window.Files) { Files.setupDragAndDrop(); } - this.updateFileSummary(); + + this.fileSummary.calculate(filesArray); + procesSelection(); $(window).scrollTop(0); @@ -404,7 +412,7 @@ window.FileList = { // defaults to true if not defined if (typeof(options.updateSummary) === 'undefined' || !!options.updateSummary) { - this.updateFileSummary(); + this.fileSummary.add(fileData, true); this.updateEmptyContent(); } return tr; @@ -582,10 +590,10 @@ window.FileList = { } fileEl.remove(); // TODO: improve performance on batch update - FileList.isEmpty = !this.$fileList.find('tr:not(.summary)').length; + FileList.isEmpty = !this.$fileList.find('tr').length; if (typeof(options.updateSummary) === 'undefined' || !!options.updateSummary) { FileList.updateEmptyContent(); - FileList.updateFileSummary(); + this.fileSummary.remove({type: fileEl.attr('data-type'), size: fileEl.attr('data-size')}, true); } return fileEl; }, @@ -621,7 +629,6 @@ window.FileList = { } FileList.isEmpty = false; FileList.updateEmptyContent(); - FileList.updateFileSummary(); }, rename: function(oldname) { var tr, td, input, form; @@ -825,12 +832,13 @@ window.FileList = { var fileEl = FileList.remove(file, {updateSummary: false}); fileEl.find('input[type="checkbox"]').prop('checked', false); fileEl.removeClass('selected'); + FileList.fileSummary.remove({type: fileEl.attr('data-type'), size: fileEl.attr('data-size')}); }); } procesSelection(); checkTrashStatus(); - FileList.updateFileSummary(); FileList.updateEmptyContent(); + FileList.fileSummary.update(); Files.updateStorageStatistics(); } else { if (result.status === 'error' && result.data.message) { @@ -857,108 +865,14 @@ window.FileList = { } }); }, - createFileSummary: function() { - if ( !FileList.isEmpty ) { - var summary = this._calculateFileSummary(); - - // Get translations - var directoryInfo = n('files', '%n folder', '%n folders', summary.totalDirs); - var fileInfo = n('files', '%n file', '%n files', summary.totalFiles); - - var infoVars = { - dirs: '<span class="dirinfo">'+directoryInfo+'</span><span class="connector">', - files: '</span><span class="fileinfo">'+fileInfo+'</span>' - }; - - var info = t('files', '{dirs} and {files}', infoVars); - - // don't show the filesize column, if filesize is NaN (e.g. in trashbin) - var fileSize = ''; - if (!isNaN(summary.totalSize)) { - fileSize = '<td class="filesize">'+humanFileSize(summary.totalSize)+'</td>'; - } - - var $summary = $('<tr class="summary" data-file="undefined"><td><span class="info">'+info+'</span></td>'+fileSize+'<td></td></tr>'); - this.$fileList.append($summary); - - var $dirInfo = $summary.find('.dirinfo'); - var $fileInfo = $summary.find('.fileinfo'); - var $connector = $summary.find('.connector'); + /** + * Creates the file summary section + */ + _createSummary: function() { + var $tr = $('<tr class="summary"></tr>'); + this.$el.find('tfoot').append($tr); - // Show only what's necessary, e.g.: no files: don't show "0 files" - if (summary.totalDirs === 0) { - $dirInfo.addClass('hidden'); - $connector.addClass('hidden'); - } - if (summary.totalFiles === 0) { - $fileInfo.addClass('hidden'); - $connector.addClass('hidden'); - } - } - }, - _calculateFileSummary: function() { - var result = { - totalDirs: 0, - totalFiles: 0, - totalSize: 0 - }; - $.each($('tr[data-file]'), function(index, value) { - var $value = $(value); - if ($value.data('type') === 'dir') { - result.totalDirs++; - } else if ($value.data('type') === 'file') { - result.totalFiles++; - } - if ($value.data('size') !== undefined && $value.data('id') !== -1) { - //Skip shared as it does not count toward quota - result.totalSize += parseInt($value.data('size')); - } - }); - return result; - }, - updateFileSummary: function() { - var $summary = this.$el.find('.summary'); - - // always make it the last element - this.$fileList.append($summary.detach()); - - // Check if we should remove the summary to show "Upload something" - if (this.isEmpty && $summary.length === 1) { - $summary.remove(); - } - // If there's no summary create one (createFileSummary checks if there's data) - else if ($summary.length === 0) { - FileList.createFileSummary(); - } - // There's a summary and data -> Update the summary - else if (!this.isEmpty && $summary.length === 1) { - var fileSummary = this._calculateFileSummary(); - var $dirInfo = $('.summary .dirinfo'); - var $fileInfo = $('.summary .fileinfo'); - var $connector = $('.summary .connector'); - - // Substitute old content with new translations - $dirInfo.html(n('files', '%n folder', '%n folders', fileSummary.totalDirs)); - $fileInfo.html(n('files', '%n file', '%n files', fileSummary.totalFiles)); - $('.summary .filesize').html(humanFileSize(fileSummary.totalSize)); - - // Show only what's necessary (may be hidden) - if (fileSummary.totalDirs === 0) { - $dirInfo.addClass('hidden'); - $connector.addClass('hidden'); - } else { - $dirInfo.removeClass('hidden'); - } - if (fileSummary.totalFiles === 0) { - $fileInfo.addClass('hidden'); - $connector.addClass('hidden'); - } else { - $fileInfo.removeClass('hidden'); - } - if (fileSummary.totalDirs > 0 && fileSummary.totalFiles > 0) { - $connector.removeClass('hidden'); - } - } + return new FileSummary($tr); }, updateEmptyContent: function() { var permissions = $('#permissions').val(); @@ -1009,7 +923,7 @@ window.FileList = { } }, filter:function(query) { - $('#fileList tr:not(.summary)').each(function(i,e) { + $('#fileList tr').each(function(i,e) { if ($(e).data('file').toString().toLowerCase().indexOf(query.toLowerCase()) !== -1) { $(e).addClass("searchresult"); } else { @@ -1315,7 +1229,5 @@ $(document).ready(function() { setTimeout(function() { FileList.changeDirectory(dir, false, true); }, 0); - - FileList.createFileSummary(); }); diff --git a/apps/files/js/filesummary.js b/apps/files/js/filesummary.js new file mode 100644 index 00000000000..bbe4d43ba49 --- /dev/null +++ b/apps/files/js/filesummary.js @@ -0,0 +1,176 @@ +/** +* ownCloud +* +* @author Vincent Petry +* @copyright 2014 Vincent Petry <pvince81@owncloud.com> +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/* global OC, n, t */ + +(function() { + /** + * The FileSummary class encapsulates the file summary values and + * the logic to render it in the given container + * @param $tr table row element + * $param summary optional initial summary value + */ + var FileSummary = function($tr, summary) { + this.$el = $tr; + this.render(); + }; + + FileSummary.prototype = { + summary: { + totalFiles: 0, + totalDirs: 0, + totalSize: 0 + }, + + /** + * Adds file + * @param file file to add + * @param update whether to update the display + */ + add: function(file, update) { + if (file.type === 'dir' || file.mime === 'httpd/unix-directory') { + this.summary.totalDirs++; + } + else { + this.summary.totalFiles++; + } + this.summary.totalSize += parseInt(file.size, 10) || 0; + if (!!update) { + this.update(); + } + }, + /** + * Removes file + * @param file file to remove + * @param update whether to update the display + */ + remove: function(file, update) { + if (file.type === 'dir' || file.mime === 'httpd/unix-directory') { + this.summary.totalDirs--; + } + else { + this.summary.totalFiles--; + } + this.summary.totalSize -= parseInt(file.size, 10) || 0; + if (!!update) { + this.update(); + } + }, + /** + * Recalculates the summary based on the given files array + * @param files array of files + */ + calculate: function(files) { + var file; + var summary = { + totalDirs: 0, + totalFiles: 0, + totalSize: 0 + }; + + for (var i = 0; i < files.length; i++) { + file = files[i]; + if (file.type === 'dir' || file.mime === 'httpd/unix-directory') { + summary.totalDirs++; + } + else { + summary.totalFiles++; + } + summary.totalSize += parseInt(file.size, 10) || 0; + } + this.setSummary(summary); + }, + /** + * Sets the current summary values + * @param summary map + */ + setSummary: function(summary) { + this.summary = summary; + this.update(); + }, + + /** + * Renders the file summary element + */ + update: function() { + if (!this.summary.totalFiles && !this.summary.totalDirs) { + this.$el.addClass('hidden'); + return; + } + // There's a summary and data -> Update the summary + this.$el.removeClass('hidden'); + var $dirInfo = this.$el.find('.dirinfo'); + var $fileInfo = this.$el.find('.fileinfo'); + var $connector = this.$el.find('.connector'); + + // Substitute old content with new translations + $dirInfo.html(n('files', '%n folder', '%n folders', this.summary.totalDirs)); + $fileInfo.html(n('files', '%n file', '%n files', this.summary.totalFiles)); + this.$el.find('.filesize').html(OC.Util.humanFileSize(this.summary.totalSize)); + + // Show only what's necessary (may be hidden) + if (this.summary.totalDirs === 0) { + $dirInfo.addClass('hidden'); + $connector.addClass('hidden'); + } else { + $dirInfo.removeClass('hidden'); + } + if (this.summary.totalFiles === 0) { + $fileInfo.addClass('hidden'); + $connector.addClass('hidden'); + } else { + $fileInfo.removeClass('hidden'); + } + if (this.summary.totalDirs > 0 && this.summary.totalFiles > 0) { + $connector.removeClass('hidden'); + } + }, + render: function() { + var summary = this.summary; + var directoryInfo = n('files', '%n folder', '%n folders', summary.totalDirs); + var fileInfo = n('files', '%n file', '%n files', summary.totalFiles); + var fileSize; + + var infoVars = { + dirs: '<span class="dirinfo">'+directoryInfo+'</span><span class="connector">', + files: '</span><span class="fileinfo">'+fileInfo+'</span>' + }; + + // don't show the filesize column, if filesize is NaN (e.g. in trashbin) + var fileSize = ''; + if (!isNaN(summary.totalSize)) { + fileSize = '<td class="filesize">' + OC.Util.humanFileSize(summary.totalSize) + '</td>'; + } + + var info = t('files', '{dirs} and {files}', infoVars); + + var $summary = $('<td><span class="info">'+info+'</span></td>'+fileSize+'<td></td>'); + + if (!this.summary.totalFiles && !this.summary.totalDirs) { + this.$el.addClass('hidden'); + } + + this.$el.append($summary); + } + }; + window.FileSummary = FileSummary; +})(); + |