summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/js
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-05-08 22:06:30 +0200
committerVincent Petry <pvince81@owncloud.com>2014-05-15 17:51:04 +0200
commit9d38e3602b2faf37d861729c52690ce51b8fee97 (patch)
tree5da63d26db4a4e8ec356dee45fc8f7804c6fe38a /apps/files_trashbin/js
parentfb10bf4048aaf5b2a9665fc9dff217c790efe005 (diff)
downloadnextcloud-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')
-rw-r--r--apps/files_trashbin/js/app.js78
-rw-r--r--apps/files_trashbin/js/disableDefaultActions.js3
-rw-r--r--apps/files_trashbin/js/filelist.js365
-rw-r--r--apps/files_trashbin/js/files.js23
-rw-r--r--apps/files_trashbin/js/trash.js130
5 files changed, 307 insertions, 292 deletions
diff --git a/apps/files_trashbin/js/app.js b/apps/files_trashbin/js/app.js
new file mode 100644
index 00000000000..9ab78e7cbb3
--- /dev/null
+++ b/apps/files_trashbin/js/app.js
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2014
+ *
+ * This file is licensed under the Affero General Public License version 3
+ * or later.
+ *
+ * See the COPYING-README file.
+ *
+ */
+
+OCA.Trashbin = {};
+OCA.Trashbin.App = {
+ _initialized: false,
+
+ initialize: function($el) {
+ if (this._initialized) {
+ return;
+ }
+ this._initialized = true;
+ this.fileList = new OCA.Trashbin.FileList($el);
+ this.registerFileActions(this.fileList);
+ },
+
+ registerFileActions: function(fileList) {
+ var self = this;
+ var fileActions = _.extend({}, OCA.Files.FileActions);
+ fileActions.clear();
+ fileActions.register('dir', 'Open', OC.PERMISSION_READ, '', function (filename) {
+ var dir = fileList.getCurrentDirectory();
+ if (dir !== '/') {
+ dir = dir + '/';
+ }
+ fileList.changeDirectory(dir + filename);
+ });
+
+ fileActions.setDefault('dir', 'Open');
+
+ fileActions.register('all', 'Restore', OC.PERMISSION_READ, OC.imagePath('core', 'actions/history'), function(filename) {
+ var tr = fileList.findFileEl(filename);
+ var deleteAction = tr.children("td.date").children(".action.delete");
+ deleteAction.removeClass('delete-icon').addClass('progress-icon');
+ fileList.disableActions();
+ $.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'), {
+ files: JSON.stringify([filename]),
+ dir: fileList.getCurrentDirectory()
+ },
+ _.bind(fileList._removeCallback, fileList)
+ );
+ }, t('files_trashbin', 'Restore'));
+
+ fileActions.register('all', 'Delete', OC.PERMISSION_READ, function() {
+ return OC.imagePath('core', 'actions/delete');
+ }, function(filename) {
+ $('.tipsy').remove();
+ var tr = fileList.findFileEl(filename);
+ var deleteAction = tr.children("td.date").children(".action.delete");
+ deleteAction.removeClass('delete-icon').addClass('progress-icon');
+ fileList.disableActions();
+ $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), {
+ files: JSON.stringify([filename]),
+ dir: fileList.getCurrentDirectory()
+ },
+ _.bind(fileList._removeCallback, fileList)
+ );
+ });
+ fileList.setFileActions(fileActions);
+ }
+};
+
+$(document).ready(function() {
+ $('#app-content-trashbin').on('show', function() {
+ var App = OCA.Trashbin.App;
+ App.initialize($('#app-content-trashbin'));
+ // force breadcrumb init
+ // App.fileList.changeDirectory(App.fileList.getCurrentDirectory(), false, true);
+ });
+});
+
diff --git a/apps/files_trashbin/js/disableDefaultActions.js b/apps/files_trashbin/js/disableDefaultActions.js
deleted file mode 100644
index 50ceaf4696f..00000000000
--- a/apps/files_trashbin/js/disableDefaultActions.js
+++ /dev/null
@@ -1,3 +0,0 @@
-/* disable download and sharing actions */
-var disableDownloadActions = true;
-var trashBinApp = true;
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;
})();
+
diff --git a/apps/files_trashbin/js/files.js b/apps/files_trashbin/js/files.js
new file mode 100644
index 00000000000..f46b96a40b3
--- /dev/null
+++ b/apps/files_trashbin/js/files.js
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2014
+ *
+ * This file is licensed under the Affero General Public License version 3
+ * or later.
+ *
+ * See the COPYING-README file.
+ *
+ */
+
+(function() {
+
+ var Files = _.extend({}, OCA.Files.Files, {
+ updateStorageStatistics: function() {
+ // no op because the trashbin doesn't have
+ // storage info like free space / used space
+ }
+
+ });
+
+ OCA.Trashbin.Files = Files;
+})();
+
diff --git a/apps/files_trashbin/js/trash.js b/apps/files_trashbin/js/trash.js
deleted file mode 100644
index 5f2436de809..00000000000
--- a/apps/files_trashbin/js/trash.js
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Copyright (c) 2014
- *
- * This file is licensed under the Affero General Public License version 3
- * or later.
- *
- * See the COPYING-README file.
- *
- */
-
-/* global OC, t, BreadCrumb, FileActions, FileList, Files */
-$(document).ready(function() {
- var deletedRegExp = new RegExp(/^(.+)\.d[0-9]+$/);
-
- /**
- * Convert a file name in the format filename.d12345 to the real file name.
- * This will use basename.
- * The name will not be changed if it has no ".d12345" suffix.
- * @param name file name
- * @return converted file name
- */
- function getDeletedFileName(name) {
- name = OC.basename(name);
- var match = deletedRegExp.exec(name);
- if (match && match.length > 1) {
- name = match[1];
- }
- return name;
- }
-
- Files.updateStorageStatistics = function() {
- // no op because the trashbin doesn't have
- // storage info like free space / used space
- };
-
- if (typeof FileActions !== 'undefined') {
- FileActions.register('all', 'Restore', OC.PERMISSION_READ, OC.imagePath('core', 'actions/history'), function(filename) {
- var tr = FileList.findFileEl(filename);
- var deleteAction = tr.children("td.date").children(".action.delete");
- deleteAction.removeClass('delete-icon').addClass('progress-icon');
- disableActions();
- $.post(OC.filePath('files_trashbin', 'ajax', 'undelete.php'), {
- files: JSON.stringify([filename]),
- dir: FileList.getCurrentDirectory()
- },
- FileList._removeCallback
- );
- }, t('files_trashbin', 'Restore'));
- };
-
- FileActions.register('all', 'Delete', OC.PERMISSION_READ, function() {
- return OC.imagePath('core', 'actions/delete');
- }, function(filename) {
- $('.tipsy').remove();
- var tr = FileList.findFileEl(filename);
- var deleteAction = tr.children("td.date").children(".action.delete");
- deleteAction.removeClass('delete-icon').addClass('progress-icon');
- disableActions();
- $.post(OC.filePath('files_trashbin', 'ajax', 'delete.php'), {
- files: JSON.stringify([filename]),
- dir: FileList.getCurrentDirectory()
- },
- FileList._removeCallback
- );
- });
-
- /**
- * Override crumb URL maker (hacky!)
- */
- FileList.breadcrumb.getCrumbUrl = function(part, index) {
- if (index === 0) {
- return OC.linkTo('files', 'index.php');
- }
- return OC.linkTo('files_trashbin', 'index.php')+"?dir=" + encodeURIComponent(part.dir);
- };
-
- Files.generatePreviewUrl = function(urlSpec) {
- return OC.generateUrl('/apps/files_trashbin/ajax/preview.php?') + $.param(urlSpec);
- };
-
- Files.getDownloadUrl = function(action, params) {
- // no downloads
- return '#';
- };
-
- Files.getAjaxUrl = function(action, params) {
- var q = '';
- if (params) {
- q = '?' + OC.buildQueryString(params);
- }
- return OC.filePath('files_trashbin', 'ajax', action + '.php') + q;
- };
-
-
- /**
- * Override crumb making to add "Deleted Files" entry
- * and convert files with ".d" extensions to a more
- * user friendly name.
- */
- var oldMakeCrumbs = BreadCrumb.prototype._makeCrumbs;
- BreadCrumb.prototype._makeCrumbs = function() {
- var parts = oldMakeCrumbs.apply(this, arguments);
- // duplicate first part
- parts.unshift(parts[0]);
- parts[1] = {
- dir: '/',
- name: t('files_trashbin', 'Deleted Files')
- };
- for (var i = 2; i < parts.length; i++) {
- parts[i].name = getDeletedFileName(parts[i].name);
- }
- return parts;
- };
-
- FileActions.actions.dir = {
- // only keep 'Open' action for navigation
- 'Open': FileActions.actions.dir.Open
- };
-});
-
-function enableActions() {
- $(".action").css("display", "inline");
- $(":input:checkbox").css("display", "inline");
-}
-
-function disableActions() {
- $(".action").css("display", "none");
- $(":input:checkbox").css("display", "none");
-}
-