Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Download file action to file meatballs button #1011

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

50 changes: 35 additions & 15 deletions client/src/containers/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,30 @@ class Editor extends Component {
}

handleAction(event) {
const name = event.currentTarget.name;
switch (event.currentTarget.name) {
// intercept the Add to Campaign submit and open the modal dialog instead
case 'action_addtocampaign':
this.openModal();
event.preventDefault();

// intercept the Add to Campaign submit and open the modal dialog instead
if (name === 'action_addtocampaign') {
this.openModal();
event.preventDefault();
return;
}
break;
case 'action_replacefile':
this.replaceFile();
event.preventDefault();

if (name === 'action_replacefile') {
this.replaceFile();
event.preventDefault();
return;
}
break;
case 'action_downloadfile':
this.downloadFile();
event.preventDefault();

if (name === 'action_delete') {
this.props.actions.confirmDeletion.confirm([this.props.file]);
event.preventDefault();
break;
case 'action_delete':
this.props.actions.confirmDeletion.confirm([this.props.file]);
event.preventDefault();

break;
default:
break;
}
}

Expand Down Expand Up @@ -111,6 +117,20 @@ class Editor extends Component {
}
}

downloadFile() {
maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved
function downloadURI(uri, name) {
const link = document.createElement('a');
link.download = name;
link.href = uri;
document.body.appendChild(link);
bergice marked this conversation as resolved.
Show resolved Hide resolved
link.click();
document.body.removeChild(link);
bergice marked this conversation as resolved.
Show resolved Hide resolved
}

downloadURI(this.props.file.url, this.props.file.name);
document.getElementById('Form_fileEditForm_PopoverActions').focus();
}

handleLoadingError(exception) {
this.setState({
loadingForm: false,
Expand Down
20 changes: 20 additions & 0 deletions code/Forms/FileFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,25 @@ protected function getReplaceFileAction($record)
return $action;
}

/**
* Get Download file action
*
* @param File $record
* @return FormAction
*/
protected function getDownloadFileAction($record)
{
// Check if record exists and user has correct permissions
if (!$record || !$record->isInDB() || !$record->canEdit()) {
bergice marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

$action = FormAction::create('downloadfile', _t(__CLASS__ . '.DOWNLOAD_FILE', 'Download file'))
->setIcon('down-circled');

return $action;
}

/**
* Get actions that go into the Popover menu
*
Expand All @@ -411,6 +430,7 @@ protected function getPopoverActions($record)
$this->beforeExtending('updatePopoverActions', function (&$actions, $record) {
// add the unpublish and replace file actions to the start of the array
array_unshift($actions, $this->getUnpublishAction($record));
array_unshift($actions, $this->getDownloadFileAction($record));
bergice marked this conversation as resolved.
Show resolved Hide resolved
array_unshift($actions, $this->getReplaceFileAction($record));
});

Expand Down