diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-05-08 22:06:30 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-05-15 17:51:04 +0200 |
commit | 9d38e3602b2faf37d861729c52690ce51b8fee97 (patch) | |
tree | 5da63d26db4a4e8ec356dee45fc8f7804c6fe38a /apps/files_trashbin/js/filelist.js | |
parent | fb10bf4048aaf5b2a9665fc9dff217c790efe005 (diff) | |
download | nextcloud-server-9d38e3602b2faf37d861729c52690ce51b8fee97.tar.gz nextcloud-server-9d38e3602b2faf37d861729c52690ce51b8fee97.zip |
Namespacing for FileList, FileActions and trashbin app
- FileList is now an instantiable class
- FileActions is now in namespace
- added App class for trashbin app
- moved trashbin overrides into classes extending FileList
- replaced many static calls with "this." or "self." to make the classes
reusable/extendable
- new URL parameter "view" to specify which view is shown, for example
"files" or "trashbin"
- added OC.Util.History utility class in core for handling history
- moved URL handling/routing to OCA.Files.App
- popstate will correctly update the current view and notify the view of
the URL change so it can update the current dir
- added JS unitt tests for the trashbin app
- fixed public app to work with the new namespaces
Diffstat (limited to 'apps/files_trashbin/js/filelist.js')
-rw-r--r-- | apps/files_trashbin/js/filelist.js | 365 |
1 files changed, 206 insertions, 159 deletions
diff --git a/apps/files_trashbin/js/filelist.js b/apps/files_trashbin/js/filelist.js index 00fc7e16b88..d3206958e8b 100644 --- a/apps/files_trashbin/js/filelist.js +++ b/apps/files_trashbin/js/filelist.js @@ -1,8 +1,14 @@ -/* global OC, t, FileList */ +/* + * Copyright (c) 2014 + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ (function() { - FileList.appName = t('files_trashbin', 'Deleted files'); - - FileList._deletedRegExp = new RegExp(/^(.+)\.d[0-9]+$/); + var DELETED_REGEXP = new RegExp(/^(.+)\.d[0-9]+$/); /** * Convert a file name in the format filename.d12345 to the real file name. @@ -11,195 +17,236 @@ * @param name file name * @return converted file name */ - FileList.getDeletedFileName = function(name) { + function getDeletedFileName(name) { name = OC.basename(name); - var match = FileList._deletedRegExp.exec(name); + var match = DELETED_REGEXP.exec(name); if (match && match.length > 1) { name = match[1]; } return name; - }; - - var oldSetCurrentDir = FileList._setCurrentDir; - FileList._setCurrentDir = function(targetDir) { - oldSetCurrentDir.apply(this, arguments); + } - var baseDir = OC.basename(targetDir); - if (baseDir !== '') { - FileList.setPageTitle(FileList.getDeletedFileName(baseDir)); - } + var FileList = function($el) { + this.initialize($el); }; + FileList.prototype = _.extend({}, OCA.Files.FileList.prototype, { + appName: t('files_trashbin', 'Deleted files'), - var oldCreateRow = FileList._createRow; - FileList._createRow = function() { - // FIXME: MEGAHACK until we find a better solution - var tr = oldCreateRow.apply(this, arguments); - tr.find('td.filesize').remove(); - return tr; - }; + initialize: function() { + var result = OCA.Files.FileList.prototype.initialize.apply(this, arguments); + this.$el.find('.undelete').click('click', _.bind(this._onClickRestoreSelected, this)); - FileList._onClickBreadCrumb = function(e) { - var $el = $(e.target).closest('.crumb'), - index = $el.index(), - $targetDir = $el.data('dir'); - // first one is home, let the link makes it default action - if (index !== 0) { - e.preventDefault(); - FileList.changeDirectory($targetDir); - } - }; + this.setSort('mtime', 'desc'); - var oldRenderRow = FileList._renderRow; - FileList._renderRow = function(fileData, options) { - options = options || {}; - var dir = FileList.getCurrentDirectory(); - var dirListing = dir !== '' && dir !== '/'; - // show deleted time as mtime - if (fileData.mtime) { - fileData.mtime = parseInt(fileData.mtime, 10); - } - if (!dirListing) { - fileData.displayName = fileData.name; - fileData.name = fileData.name + '.d' + Math.floor(fileData.mtime / 1000); - } - return oldRenderRow.call(this, fileData, options); - }; + // override crumb URL maker + this.breadcrumb.getCrumbUrl = function(part, index) { + return OC.linkTo('files_trashbin', 'index.php')+"?view=trashbin&dir=" + encodeURIComponent(part.dir); + }; + /** + * Override crumb making to add "Deleted Files" entry + * and convert files with ".d" extensions to a more + * user friendly name. + */ + this.breadcrumb._makeCrumbs = function() { + var parts = OCA.Files.BreadCrumb.prototype._makeCrumbs.apply(this, arguments); + for (var i = 1; i < parts.length; i++) { + parts[i].name = getDeletedFileName(parts[i].name); + } + return parts; + }; - FileList.linkTo = function(dir){ - return OC.linkTo('files_trashbin', 'index.php')+"?dir="+ encodeURIComponent(dir).replace(/%2F/g, '/'); - }; + return result; + }, - FileList.updateEmptyContent = function(){ - var $fileList = $('#fileList'); - var exists = $fileList.find('tr:first').exists(); - $('#emptycontent').toggleClass('hidden', exists); - $('#filestable th').toggleClass('hidden', !exists); - }; + _setCurrentDir: function(targetDir) { + OCA.Files.FileList.prototype._setCurrentDir.apply(this, arguments); - var oldInit = FileList.initialize; - FileList.initialize = function() { - var result = oldInit.apply(this, arguments); - $('.undelete').click('click', FileList._onClickRestoreSelected); - this.setSort('mtime', 'desc'); - return result; - }; - - FileList._removeCallback = function(result) { - if (result.status !== 'success') { - OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); - } + var baseDir = OC.basename(targetDir); + if (baseDir !== '') { + this.setPageTitle(getDeletedFileName(baseDir)); + } + }, + + _createRow: function() { + // FIXME: MEGAHACK until we find a better solution + var tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments); + tr.find('td.filesize').remove(); + return tr; + }, + + _renderRow: function(fileData, options) { + options = options || {}; + var dir = this.getCurrentDirectory(); + var dirListing = dir !== '' && dir !== '/'; + // show deleted time as mtime + if (fileData.mtime) { + fileData.mtime = parseInt(fileData.mtime, 10); + } + if (!dirListing) { + fileData.displayName = fileData.name; + fileData.name = fileData.name + '.d' + Math.floor(fileData.mtime / 1000); + } + return OCA.Files.FileList.prototype._renderRow.call(this, fileData, options); + }, - var files = result.data.success; - var $el; - for (var i = 0; i < files.length; i++) { - $el = FileList.remove(OC.basename(files[i].filename), {updateSummary: false}); - FileList.fileSummary.remove({type: $el.attr('data-type'), size: $el.attr('data-size')}); - } - FileList.fileSummary.update(); - FileList.updateEmptyContent(); - enableActions(); - } + getAjaxUrl: function(action, params) { + var q = ''; + if (params) { + q = '?' + OC.buildQueryString(params); + } + return OC.filePath('files_trashbin', 'ajax', action + '.php') + q; + }, + + linkTo: function(dir){ + return OC.linkTo('files_trashbin', 'index.php')+"?dir="+ encodeURIComponent(dir).replace(/%2F/g, '/'); + }, + + updateEmptyContent: function(){ + var exists = this.$fileList.find('tr:first').exists(); + this.$el.find('#emptycontent').toggleClass('hidden', exists); + this.$el.find('#filestable th').toggleClass('hidden', !exists); + }, + + _removeCallback: function(result) { + if (result.status !== 'success') { + OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); + } - FileList._onClickRestoreSelected = function(event) { - event.preventDefault(); - var allFiles = $('#select_all').is(':checked'); - var files = []; - var params = {}; - disableActions(); - if (allFiles) { - FileList.showMask(); - params = { - allfiles: true, - dir: FileList.getCurrentDirectory() - }; - } - else { - files = _.pluck(FileList.getSelectedFiles(), 'name'); + var files = result.data.success; + var $el; for (var i = 0; i < files.length; i++) { - var deleteAction = FileList.findFileEl(files[i]).children("td.date").children(".action.delete"); - deleteAction.removeClass('delete-icon').addClass('progress-icon'); + $el = this.remove(OC.basename(files[i].filename), {updateSummary: false}); + this.fileSummary.remove({type: $el.attr('data-type'), size: $el.attr('data-size')}); } - params = { - files: JSON.stringify(files), - dir: FileList.getCurrentDirectory() - }; - } + this.fileSummary.update(); + this.updateEmptyContent(); + this.enableActions(); + }, - $.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'), - params, - function(result) { - if (allFiles) { - if (result.status !== 'success') { - OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); - } - FileList.hideMask(); - // simply remove all files - FileList.setFiles([]); - enableActions(); - } - else { - FileList._removeCallback(result); - } + _onClickRestoreSelected: function(event) { + event.preventDefault(); + var self = this; + var allFiles = this.$el.find('#select_all').is(':checked'); + var files = []; + var params = {}; + this.disableActions(); + if (allFiles) { + this.showMask(); + params = { + allfiles: true, + dir: this.getCurrentDirectory() + }; } - ); - }; - - FileList._onClickDeleteSelected = function(event) { - event.preventDefault(); - var allFiles = $('#select_all').is(':checked'); - var files = []; - var params = {}; - if (allFiles) { - params = { - allfiles: true, - dir: FileList.getCurrentDirectory() - }; - } - else { - files = _.pluck(FileList.getSelectedFiles(), 'name'); - params = { - files: JSON.stringify(files), - dir: FileList.getCurrentDirectory() - }; - } - - disableActions(); - if (allFiles) { - FileList.showMask(); - } - else { - for (var i = 0; i < files.length; i++) { - var deleteAction = FileList.findFileEl(files[i]).children("td.date").children(".action.delete"); - deleteAction.removeClass('delete-icon').addClass('progress-icon'); + else { + files = _.pluck(this.getSelectedFiles(), 'name'); + for (var i = 0; i < files.length; i++) { + var deleteAction = this.findFileEl(files[i]).children("td.date").children(".action.delete"); + deleteAction.removeClass('delete-icon').addClass('progress-icon'); + } + params = { + files: JSON.stringify(files), + dir: this.getCurrentDirectory() + }; } - } - $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), + $.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'), params, function(result) { if (allFiles) { if (result.status !== 'success') { OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); } - FileList.hideMask(); + self.hideMask(); // simply remove all files - FileList.setFiles([]); - enableActions(); + self.setFiles([]); + self.enableActions(); } else { - FileList._removeCallback(result); + self._removeCallback(result); } } - ); - }; + ); + }, - var oldClickFile = FileList._onClickFile; - FileList._onClickFile = function(event) { - var mime = $(this).parent().parent().data('mime'); - if (mime !== 'httpd/unix-directory') { + _onClickDeleteSelected: function(event) { event.preventDefault(); + var self = this; + var allFiles = this.$el.find('#select_all').is(':checked'); + var files = []; + var params = {}; + if (allFiles) { + params = { + allfiles: true, + dir: this.getCurrentDirectory() + }; + } + else { + files = _.pluck(this.getSelectedFiles(), 'name'); + params = { + files: JSON.stringify(files), + dir: this.getCurrentDirectory() + }; + } + + this.disableActions(); + if (allFiles) { + this.showMask(); + } + else { + for (var i = 0; i < files.length; i++) { + var deleteAction = this.findFileEl(files[i]).children("td.date").children(".action.delete"); + deleteAction.removeClass('delete-icon').addClass('progress-icon'); + } + } + + $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), + params, + function(result) { + if (allFiles) { + if (result.status !== 'success') { + OC.dialogs.alert(result.data.message, t('files_trashbin', 'Error')); + } + self.hideMask(); + // simply remove all files + self.setFiles([]); + self.enableActions(); + } + else { + self._removeCallback(result); + } + } + ); + }, + + _onClickFile: function(event) { + var mime = $(this).parent().parent().data('mime'); + if (mime !== 'httpd/unix-directory') { + event.preventDefault(); + } + return OCA.Files.FileList.prototype._onClickFile.apply(this, arguments); + }, + + generatePreviewUrl: function(urlSpec) { + return OC.generateUrl('/apps/files_trashbin/ajax/preview.php?') + $.param(urlSpec); + }, + + getDownloadUrl: function(action, params) { + // no downloads + return '#'; + }, + + enableActions: function() { + this.$el.find('.action').css('display', 'inline'); + this.$el.find(':input:checkbox').css('display', 'inline'); + }, + + disableActions: function() { + this.$el.find('.action').css('display', 'none'); + this.$el.find(':input:checkbox').css('display', 'none'); } - return oldClickFile.apply(this, arguments); - }; + }); + + OCA.Trashbin.FileList = FileList; })(); + |