Skip to content

Commit

Permalink
NEW Add Download file action to file meatballs button (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice authored and Maxime Rainville committed Oct 9, 2019
1 parent 7656807 commit 74c350a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
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() {
function downloadURI(uri, name) {
const link = document.createElement('a');
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

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()) {
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));
array_unshift($actions, $this->getReplaceFileAction($record));
});

Expand Down

0 comments on commit 74c350a

Please sign in to comment.