Skip to content

Commit

Permalink
NEW Move the replace file into the more options action set (#848)
Browse files Browse the repository at this point in the history
* NEW Move the replace file into the more options action set

* Add behat test
  • Loading branch information
sachajudd authored and Aaron Carlino committed Oct 29, 2018
1 parent 4600d30 commit 2440432
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 22 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion client/src/components/AssetDropzone/AssetDropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AssetDropzone extends Component {
));

// attach the name as a class to the hidden input for easier identification
const name = this.props.name;
const { name } = this.props;
if (name) {
this.dropzone.hiddenFileInput.classList.add(`dz-input-${name}`);
}
Expand Down Expand Up @@ -70,6 +70,15 @@ class AssetDropzone extends Component {
}
}

componentDidUpdate() {
// Reattach name to hiddenFileInput as dropzone recreates this element after each upload
const { name } = this.props;

if (name) {
this.dropzone.hiddenFileInput.classList.add(`dz-input-${name}`);
}
}

componentWillUnmount() {
// Remove all dropzone event listeners.
this.dropzone.disable();
Expand Down
20 changes: 1 addition & 19 deletions client/src/components/PreviewImageField/PreviewImageField.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PreviewImageField extends Component {
url: endpoint && endpoint.url,
method: endpoint && endpoint.method,
paramName: 'Upload',
clickable: '#preview-replace-button',
clickable: true,
maxFiles: 1,
};
const preview = {
Expand Down Expand Up @@ -293,24 +293,6 @@ class PreviewImageField extends Component {
}
return (
<div className="preview-image-field__toolbar fill-height">
{ (this.props.data.url) ? (
<a
href={this.props.data.url}
target="_blank"
rel="noopener noreferrer"
className={this.getButtonClasses('link')}
>Open</a>
)
: null }
{ (canEdit) ? (
<button
id="preview-replace-button"
onClick={this.preventDefault}
className={this.getButtonClasses('replace')}
type="button"
>Replace</button>
)
: null }
{ (this.props.upload.progress || this.props.upload.message) ? (
<button
onClick={this.handleCancelUpload}
Expand Down
15 changes: 15 additions & 0 deletions client/src/containers/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class Editor extends Component {
return;
}

if (name === 'action_replacefile') {
this.replaceFile();
event.preventDefault();
return;
}

if (name === 'action_delete') {
// Customise message based on usage
let message = i18n._t('AssetAdmin.CONFIRMDELETE', 'Are you sure you want to delete this record?');
Expand Down Expand Up @@ -115,6 +121,15 @@ class Editor extends Component {
});
}

replaceFile() {
const hiddenFileInput = document.querySelector('.dz-input-PreviewImage');

// Trigger a click on Dropzone's hidden file input in order to upload an image
if (hiddenFileInput) {
hiddenFileInput.click();
}
}

handleLoadingError(exception) {
this.setState({
loadingForm: false,
Expand Down
22 changes: 21 additions & 1 deletion code/Forms/FileFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ protected function getUnpublishAction($record)
return $action;
}

/**
* Get Replace file action
*
* @param File $record
* @return FormAction
*/
protected function getReplaceFileAction($record)
{
// Check if record exists and user has correct permissions
if (!$record || !$record->isInDB() || !$record->canEdit()) {
return null;
}

$action = FormAction::create('replacefile', _t(__CLASS__ . '.REPLACE_FILE', 'Replace file'))
->setIcon('upload');

return $action;
}

/**
* Get actions that go into the Popover menu
*
Expand All @@ -391,8 +410,9 @@ protected function getUnpublishAction($record)
protected function getPopoverActions($record)
{
$this->beforeExtending('updatePopoverActions', function (&$actions, $record) {
// add the unpublish action to the start of the array
// add the unpublish and replace file actions to the start of the array
array_unshift($actions, $this->getUnpublishAction($record));
array_unshift($actions, $this->getReplaceFileAction($record));
});

return parent::getPopoverActions($record);
Expand Down
1 change: 1 addition & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ en:
LINKOPTIONS: 'Link options'
LINKTEXT: 'Link text'
OTHER_ACTIONS: 'Other actions'
REPLACE_FILE: 'Replace file'
UNEMEDABLE_MESSAGE: '<p class="alert alert-info alert--no-border editor__top-message">This file type can only be inserted as a link. You can edit the link once it is inserted.</p>'
USAGE: 'Used on'
SilverStripe\AssetAdmin\Forms\FileHistoryFormFactory:
Expand Down
2 changes: 2 additions & 0 deletions tests/behat/features/replace-file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Feature: Replace a file with a new file
When I press the "Save" button
Then I should not see a ".preview-image-field__message--success" element
And I should not see a ".preview-image-field__toolbar-button--remove" element
When I press the "Other actions" button
Then I should see a "Replace file" button

@javascript
Scenario: Replacing a file with the same file detects and avoids duplication
Expand Down
20 changes: 20 additions & 0 deletions tests/php/Forms/FileFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testEditFileForm()
// Test actions exist
$this->assertNotNull($form->Actions()->fieldByName('Actions.action_save'));
$this->assertNotNull($form->Actions()->fieldByName('Actions.action_publish'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

Expand Down Expand Up @@ -130,32 +131,50 @@ public function testEditFileFormWithPermissions()

FileExtension::$canDelete = false;
FileExtension::$canPublish = false;
FileExtension::$canEdit = false;
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]);
$this->assertNull($form->Actions()->fieldByName('PopoverActions'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));

$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

FileExtension::$canDelete = false;
FileExtension::$canPublish = true;
FileExtension::$canEdit = false;
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]);
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

FileExtension::$canDelete = true;
FileExtension::$canPublish = false;
FileExtension::$canEdit = false;
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

FileExtension::$canDelete = false;
FileExtension::$canPublish = false;
FileExtension::$canEdit = true;
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file]);
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

FileExtension::$canDelete = true;
FileExtension::$canPublish = true;
FileExtension::$canUnpublish = true;
FileExtension::$canEdit = true;
$file->publishSingle();
$form = $builder->getForm($controller, 'EditForm', ['Record' => $file, 'RequireLinkText' => false]);
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_delete'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_addtocampaign'));
$this->assertNotNull($form->Actions()->fieldByName('PopoverActions.action_unpublish'));

Expand Down Expand Up @@ -323,6 +342,7 @@ public function testFolderForm()
$this->assertNull($form->Actions()->fieldByName('action_publish'));
$this->assertNull($form->Actions()->dataFieldByName('action_publish'));
$this->assertNull($form->Actions()->dataFieldByName('action_unpublish'));
$this->assertNull($form->Actions()->fieldByName('PopoverActions.action_replacefile'));
}

public function testScaffolderFactory()
Expand Down
6 changes: 6 additions & 0 deletions tests/php/Forms/FileFormBuilderTest/FileExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FileExtension extends DataExtension implements TestOnly
public static $canDelete = false;
public static $canPublish = true;
public static $canUnpublish = true;
public static $canEdit = true;

public function canDelete($member)
{
Expand All @@ -30,4 +31,9 @@ public function canUnpublish($member = null)
{
return self::$canUnpublish;
}

public function canEdit($member = null)
{
return self::$canEdit;
}
}

0 comments on commit 2440432

Please sign in to comment.