refactor: fileActions.getCurrentDefaultFileAction()

fileActions.getCurrentDefaultFileAction() returns the default file action
for the currently selected file.

There were a number of places querying for the mime, type and permissions
of that file first to then query for the default action.

Signed-off-by: Azul <azul@riseup.net>
This commit is contained in:
Azul 2020-04-06 09:41:36 +02:00
parent cb2b38516f
commit d2728cbdc1
4 changed files with 20 additions and 18 deletions

View File

@ -256,6 +256,19 @@
return undefined; return undefined;
}, },
/**
* Returns the default file action handler for the current file
*
* @return {OCA.Files.FileActions~actionSpec} action spec
* @since 8.2
*/
getCurrentDefaultFileAction: function() {
var mime = this.getCurrentMimeType();
var type = this.getCurrentType();
var permissions = this.getCurrentPermissions();
return this.getDefaultFileAction(mime,type, permissions);
},
/** /**
* Returns the default file action handler for the given conditions * Returns the default file action handler for the given conditions
* *
@ -263,7 +276,7 @@
* @param {string} type "dir" or "file" * @param {string} type "dir" or "file"
* @param {int} permissions permissions * @param {int} permissions permissions
* *
* @return {OCA.Files.FileActions~actionHandler} action handler * @return {OCA.Files.FileActions~actionSpec} action spec
* @since 8.2 * @since 8.2
*/ */
getDefaultFileAction: function(mime, type, permissions) { getDefaultFileAction: function(mime, type, permissions) {

View File

@ -77,11 +77,7 @@
fileActions.getCurrentPermissions() fileActions.getCurrentPermissions()
); );
var defaultAction = fileActions.getDefaultFileAction( var defaultAction = fileActions.getCurrentDefaultFileAction();
fileActions.getCurrentMimeType(),
fileActions.getCurrentType(),
fileActions.getCurrentPermissions()
);
var items = _.filter(actions, function(actionSpec) { var items = _.filter(actions, function(actionSpec) {
return !defaultAction || actionSpec.name !== defaultAction.name; return !defaultAction || actionSpec.name !== defaultAction.name;

View File

@ -892,13 +892,10 @@
event.preventDefault(); event.preventDefault();
} else if (!renaming) { } else if (!renaming) {
this.fileActions.currentFile = $tr.find('td'); this.fileActions.currentFile = $tr.find('td');
var mime = this.fileActions.getCurrentMimeType(); var spec = this.fileActions.getCurrentDefaultFileAction();
var type = this.fileActions.getCurrentType(); if (spec && spec.action) {
var permissions = this.fileActions.getCurrentPermissions();
var action = this.fileActions.getDefault(mime,type, permissions);
if (action) {
event.preventDefault(); event.preventDefault();
action(filename, { spec.action(filename, {
$file: $tr, $file: $tr,
fileList: this, fileList: this,
fileActions: this.fileActions, fileActions: this.fileActions,

View File

@ -113,8 +113,8 @@ OCA.Sharing.PublicApp = {
// Show file preview if previewer is available, images are already handled by the template // Show file preview if previewer is available, images are already handled by the template
if (mimetype.substr(0, mimetype.indexOf('/')) !== 'image' && $('.publicpreview').length === 0) { if (mimetype.substr(0, mimetype.indexOf('/')) !== 'image' && $('.publicpreview').length === 0) {
// Trigger default action if not download TODO // Trigger default action if not download TODO
var action = FileActions.getDefault(mimetype, 'file', OC.PERMISSION_READ); var action = FileActions.getDefaultFileAction(mimetype, 'file', OC.PERMISSION_READ);
if (typeof action !== 'undefined') { if (action && action.action) {
action($('#filename').val()); action($('#filename').val());
} }
} }
@ -204,10 +204,6 @@ OCA.Sharing.PublicApp = {
var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments); var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
if (hideDownload === 'true') { if (hideDownload === 'true') {
this.fileActions.currentFile = $tr.find('td'); this.fileActions.currentFile = $tr.find('td');
var mime = this.fileActions.getCurrentMimeType();
var type = this.fileActions.getCurrentType();
var permissions = this.fileActions.getCurrentPermissions();
var action = this.fileActions.getDefault(mime, type, permissions);
// Remove the link. This means that files without a default action fail hard // Remove the link. This means that files without a default action fail hard
$tr.find('a.name').attr('href', '#'); $tr.find('a.name').attr('href', '#');