diff options
Diffstat (limited to 'apps/files/js/filelist.js')
-rw-r--r-- | apps/files/js/filelist.js | 114 |
1 files changed, 77 insertions, 37 deletions
diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index de34d932b11..38766e2b801 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -8,21 +8,20 @@ * */ -/* global Files */ -/* global dragOptions, folderDropOptions */ (function() { /** * The FileList class manages a file list view. * A file list view consists of a controls bar and * a file list table. */ - var FileList = function($el) { - this.initialize($el); + var FileList = function($el, options) { + this.initialize($el, options); }; FileList.prototype = { SORT_INDICATOR_ASC_CLASS: 'icon-triangle-s', SORT_INDICATOR_DESC_CLASS: 'icon-triangle-n', + id: 'files', appName: t('files', 'Files'), isEmpty: true, useUndo:true, @@ -95,16 +94,35 @@ */ _currentDirectory: null, + _dragOptions: null, + _folderDropOptions: null, + /** * Initialize the file list and its components + * + * @param $el container element with existing markup for the #controls + * and a table + * @param options map of options, see other parameters + * @param scrollContainer scrollable container, defaults to $(window) + * @param dragOptions drag options, disabled by default + * @param folderDropOptions folder drop options, disabled by default */ - initialize: function($el) { + initialize: function($el, options) { var self = this; + options = options || {}; if (this.initialized) { return; } + if (options.dragOptions) { + this._dragOptions = options.dragOptions; + } + if (options.folderDropOptions) { + this._folderDropOptions = options.folderDropOptions; + } + this.$el = $el; + this.$container = options.scrollContainer || $(window); this.$table = $el.find('table:first'); this.$fileList = $el.find('#fileList'); this.fileActions = OCA.Files.FileActions; @@ -116,13 +134,17 @@ this.setSort('name', 'asc'); - this.breadcrumb = new OCA.Files.BreadCrumb({ + var breadcrumbOptions = { onClick: _.bind(this._onClickBreadCrumb, this), - onDrop: _.bind(this._onDropOnBreadCrumb, this), - getCrumbUrl: function(part, index) { + getCrumbUrl: function(part) { return self.linkTo(part.dir); } - }); + }; + // if dropping on folders is allowed, then also allow on breadcrumbs + if (this._folderDropOptions) { + breadcrumbOptions.onDrop = _.bind(this._onDropOnBreadCrumb, this); + } + this.breadcrumb = new OCA.Files.BreadCrumb(breadcrumbOptions); this.$el.find('#controls').prepend(this.breadcrumb.$el); @@ -137,14 +159,13 @@ this.$fileList.on('click','td.filename>a.name', _.bind(this._onClickFile, this)); this.$fileList.on('change', 'td.filename>input:checkbox', _.bind(this._onClickFileCheckbox, this)); this.$el.on('urlChanged', _.bind(this._onUrlChanged, this)); - this.$el.find('#select_all').click(_.bind(this._onClickSelectAll, this)); + this.$el.find('.select-all').click(_.bind(this._onClickSelectAll, this)); this.$el.find('.download').click(_.bind(this._onClickDownloadSelected, this)); this.$el.find('.delete-selected').click(_.bind(this._onClickDeleteSelected, this)); this.setupUploadEvents(); - // FIXME: only do this when visible - $(window).scroll(function(e) {self._onScroll(e);}); + this.$container.on('scroll', _.bind(this._onScroll, this)); }, /** @@ -182,7 +203,7 @@ delete this._selectedFiles[$tr.data('id')]; this._selectionSummary.remove(data); } - this.$el.find('#select_all').prop('checked', this._selectionSummary.getTotal() === this.files.length); + this.$el.find('.select-all').prop('checked', this._selectionSummary.getTotal() === this.files.length); }, /** @@ -327,8 +348,12 @@ } }, + /** + * Event handler for when scrolling the list container. + * This appends/renders the next page of entries when reaching the bottom. + */ _onScroll: function(e) { - if ($(window).scrollTop() + $(window).height() > $(document).height() - 500) { + if (this.$container.scrollTop() + this.$container.height() > this.$el.height() - 100) { this._nextPage(true); } }, @@ -460,7 +485,7 @@ this.$fileList.empty(); // clear "Select all" checkbox - this.$el.find('#select_all').prop('checked', false); + this.$el.find('.select-all').prop('checked', false); this.isEmpty = this.files.length === 0; this._nextPage(); @@ -469,10 +494,6 @@ this.updateEmptyContent(); this.$fileList.trigger(jQuery.Event("fileActionsReady")); - // "Files" might not be loaded in extending apps - if (window.Files) { - Files.setupDragAndDrop(); - } this.fileSummary.calculate(filesArray); @@ -540,7 +561,8 @@ else { linkUrl = this.getDownloadUrl(name, this.getCurrentDirectory()); } - td.append('<input id="select-' + fileData.id + '" type="checkbox" /><label for="select-' + fileData.id + '"></label>'); + td.append('<input id="select-' + this.id + '-' + fileData.id + + '" type="checkbox" /><label for="select-' + this.id + '-' + fileData.id + '"></label>'); var linkElem = $('<a></a>').attr({ "class": "name", "href": linkUrl @@ -694,12 +716,12 @@ // TODO: move dragging to FileActions ? // enable drag only for deletable files - if (permissions & OC.PERMISSION_DELETE) { - filenameTd.draggable(dragOptions); + if (this._dragOptions && permissions & OC.PERMISSION_DELETE) { + filenameTd.draggable(this._dragOptions); } // allow dropping on folders - if (fileData.type === 'dir') { - filenameTd.droppable(folderDropOptions); + if (this._folderDropOptions && fileData.type === 'dir') { + filenameTd.droppable(this._folderDropOptions); } if (options.hidden) { @@ -780,8 +802,7 @@ * @param changeUrl true to also update the URL, false otherwise (default) */ _setCurrentDir: function(targetDir, changeUrl) { - var url, - previousDir = this.getCurrentDirectory(), + var previousDir = this.getCurrentDirectory(), baseDir = OC.basename(targetDir); if (baseDir !== '') { @@ -832,7 +853,7 @@ var self = this; this._selectedFiles = {}; this._selectionSummary.clear(); - this.$el.find('#select_all').prop('checked', false); + this.$el.find('.select-all').prop('checked', false); this.showMask(); if (this._reloadCall) { this._reloadCall.abort(); @@ -873,7 +894,7 @@ // TODO: should rather return upload file size through // the files list ajax call - Files.updateStorageStatistics(true); + this.updateStorageStatistics(true); if (result.data.permissions) { this.setDirectoryPermissions(result.data.permissions); @@ -882,12 +903,16 @@ this.setFiles(result.data.files); }, + updateStorageStatistics: function(force) { + OCA.Files.Files.updateStorageStatistics(this.getCurrentDirectory(), force); + }, + getAjaxUrl: function(action, params) { - return Files.getAjaxUrl(action, params); + return OCA.Files.Files.getAjaxUrl(action, params); }, getDownloadUrl: function(files, dir) { - return Files.getDownloadUrl(files, dir || this.getCurrentDirectory()); + return OCA.Files.Files.getDownloadUrl(files, dir || this.getCurrentDirectory()); }, /** @@ -994,6 +1019,7 @@ setViewerMode: function(show){ this.showActions(!show); this.$el.find('#filestable').toggleClass('hidden', show); + this.$el.trigger(new $.Event('changeViewerMode', {viewerModeEnabled: show})); }, /** * Removes a file entry from the list @@ -1014,7 +1040,7 @@ this._selectFileEl(fileEl, false); this.updateSelectionSummary(); } - if (fileEl.data('permissions') & OC.PERMISSION_DELETE) { + if (this._dragOptions && (fileEl.data('permissions') & OC.PERMISSION_DELETE)) { // file is only draggable when delete permissions are set fileEl.find('td.filename').draggable('destroy'); } @@ -1109,7 +1135,8 @@ OC.dialogs.alert(t('files', 'Error moving file'), t('files', 'Error')); } $td.css('background-image', oldBackgroundImage); - }); + } + ); }); }, @@ -1144,7 +1171,7 @@ var filename = input.val(); if (filename !== oldname) { // Files.isFileNameValid(filename) throws an exception itself - Files.isFileNameValid(filename); + OCA.Files.Files.isFileNameValid(filename); if (self.inList(filename)) { throw t('files', '{new_name} already exists', {new_name: filename}); } @@ -1299,7 +1326,7 @@ self.updateEmptyContent(); self.fileSummary.update(); self.updateSelectionSummary(); - Files.updateStorageStatistics(); + self.updateStorageStatistics(); } else { if (result.status === 'error' && result.data.message) { OC.Notification.show(result.data.message); @@ -1406,6 +1433,7 @@ */ updateSelectionSummary: function() { var summary = this._selectionSummary.summary; + var canDelete; if (summary.totalFiles === 0 && summary.totalDirs === 0) { this.$el.find('#headerName a.name>span:first').text(t('files','Name')); this.$el.find('#headerSize a>span:first').text(t('files','Size')); @@ -1414,6 +1442,7 @@ this.$el.find('.selectedActions').addClass('hidden'); } else { + canDelete = (this.getDirectoryPermissions() & OC.PERMISSION_DELETE); this.$el.find('.selectedActions').removeClass('hidden'); this.$el.find('#headerSize a>span:first').text(OC.Util.humanFileSize(summary.totalSize)); var selection = ''; @@ -1429,6 +1458,7 @@ this.$el.find('#headerName a.name>span:first').text(selection); this.$el.find('#modified a>span:first').text(''); this.$el.find('table').addClass('multiselect'); + this.$el.find('.delete-selected').toggleClass('hidden', !canDelete); } }, @@ -1437,7 +1467,7 @@ * @return true if all files are selected, false otherwise */ isAllSelected: function() { - return this.$el.find('#select_all').prop('checked'); + return this.$el.find('.select-all').prop('checked'); }, /** @@ -1475,7 +1505,10 @@ } return name; }, - + + /** + * Setup file upload events related to the file-upload plugin + */ setupUploadEvents: function() { var self = this; @@ -1486,6 +1519,11 @@ OC.Upload.log('filelist handle fileuploaddrop', e, data); var dropTarget = $(e.originalEvent.target).closest('tr, .crumb'); + // check if dropped inside this list at all + if (dropTarget && !self.$el.has(dropTarget).length) { + return false; + } + if (dropTarget && (dropTarget.data('type') === 'dir' || dropTarget.hasClass('crumb'))) { // drag&drop upload to folder // remember as context @@ -1515,7 +1553,7 @@ }; } else { // cancel uploads to current dir if no permission - var isCreatable = (this.getDirectoryPermissions() & OC.PERMISSION_CREATE) !== 0; + var isCreatable = (self.getDirectoryPermissions() & OC.PERMISSION_CREATE) !== 0; if (!isCreatable) { return false; } @@ -1655,6 +1693,7 @@ uploadText.fadeOut(); uploadText.attr('currentUploads', 0); } + self.updateStorageStatistics(); }); fileUploadStart.on('fileuploadfail', function(e, data) { OC.Upload.log('filelist handle fileuploadfail', e, data); @@ -1668,6 +1707,7 @@ uploadText.fadeOut(); uploadText.attr('currentUploads', 0); } + self.updateStorageStatistics(); }); } |