mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
API bulk action front-end data are configurable
Bulk action front-end behaviour and style can now be configured via array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false ) passed to addBulkAction()
This commit is contained in:
parent
2ee566a99e
commit
d01b27c589
@ -41,15 +41,30 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
|
|||||||
$this->config['actions'] = array(
|
$this->config['actions'] = array(
|
||||||
'edit' => array(
|
'edit' => array(
|
||||||
'label' => _t('GridFieldBulkTools.EDIT_SELECT_LABEL', 'Edit'),
|
'label' => _t('GridFieldBulkTools.EDIT_SELECT_LABEL', 'Edit'),
|
||||||
'handler' => 'GridFieldBulkManager_Request'
|
'handler' => 'GridFieldBulkManager_Request',
|
||||||
|
'config' => array(
|
||||||
|
'isAjax' => false,
|
||||||
|
'icon' => 'pencil',
|
||||||
|
'isDestructive' => false
|
||||||
|
)
|
||||||
),
|
),
|
||||||
'unlink' => array(
|
'unlink' => array(
|
||||||
'label' => _t('GridFieldBulkTools.UNLINK_SELECT_LABEL', 'UnLink'),
|
'label' => _t('GridFieldBulkTools.UNLINK_SELECT_LABEL', 'UnLink'),
|
||||||
'handler' => 'GridFieldBulkManager_Request'
|
'handler' => 'GridFieldBulkManager_Request',
|
||||||
|
'config' => array(
|
||||||
|
'isAjax' => true,
|
||||||
|
'icon' => 'chain--minus',
|
||||||
|
'isDestructive' => false
|
||||||
|
)
|
||||||
),
|
),
|
||||||
'delete' => array(
|
'delete' => array(
|
||||||
'label' => _t('GridFieldBulkTools.DELETE_SELECT_LABEL', 'Delete'),
|
'label' => _t('GridFieldBulkTools.DELETE_SELECT_LABEL', 'Delete'),
|
||||||
'handler' => 'GridFieldBulkManager_Request'
|
'handler' => 'GridFieldBulkManager_Request',
|
||||||
|
'config' => array(
|
||||||
|
'isAjax' => true,
|
||||||
|
'icon' => 'decline',
|
||||||
|
'isDestructive' => true
|
||||||
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -162,9 +177,10 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
|
|||||||
* @param string $name Bulk action's name. Used by RequestHandler.
|
* @param string $name Bulk action's name. Used by RequestHandler.
|
||||||
* @param string $label Dropdown menu action's label. Default to ucfirst($name).
|
* @param string $label Dropdown menu action's label. Default to ucfirst($name).
|
||||||
* @param string $handler RequestHandler class name for this action. Default to 'GridFieldBulkAction'.ucfirst($name).'Handler'
|
* @param string $handler RequestHandler class name for this action. Default to 'GridFieldBulkAction'.ucfirst($name).'Handler'
|
||||||
|
* @param array $config Front-end configuration array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )
|
||||||
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
||||||
*/
|
*/
|
||||||
function addBulkAction(string $name, $label = null, $handler = null)
|
function addBulkAction(string $name, $label = null, $handler = null, $config = null)
|
||||||
{
|
{
|
||||||
if ( array_key_exists($name, $this->config['actions']) )
|
if ( array_key_exists($name, $this->config['actions']) )
|
||||||
{
|
{
|
||||||
@ -188,9 +204,22 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
|
|||||||
user_error("Bulk action handler for $name not found: $handler", E_USER_ERROR);
|
user_error("Bulk action handler for $name not found: $handler", E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $config && !is_array($config) )
|
||||||
|
{
|
||||||
|
user_error("Bulk action front-end config should be an array of key => value pairs.", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$config = array(
|
||||||
|
'isAjax' => true,
|
||||||
|
'icon' => 'accept',
|
||||||
|
'isDestructive' => false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$this->config['actions'][$name] = array(
|
$this->config['actions'][$name] = array(
|
||||||
'label' => $label,
|
'label' => $label,
|
||||||
'handler' => $handler
|
'handler' => $handler,
|
||||||
|
'config' => $config
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -265,31 +294,45 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
|
|||||||
}
|
}
|
||||||
|
|
||||||
$actionsListSource = array();
|
$actionsListSource = array();
|
||||||
|
$actionsConfig = array();
|
||||||
|
|
||||||
foreach ($this->config['actions'] as $action => $actionData)
|
foreach ($this->config['actions'] as $action => $actionData)
|
||||||
{
|
{
|
||||||
$actionsListSource[$action] = $actionData['label'];
|
$actionsListSource[$action] = $actionData['label'];
|
||||||
|
$actionsConfig[$action] = $actionData['config'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset($this->config['actions']);
|
||||||
|
$firstAction = key($this->config['actions']);
|
||||||
|
|
||||||
$dropDownActionsList = DropdownField::create('bulkActionName', '')
|
$dropDownActionsList = DropdownField::create('bulkActionName', '')
|
||||||
->setSource( $actionsListSource )
|
->setSource( $actionsListSource )
|
||||||
->setAttribute('class', 'bulkActionName no-change-track')
|
->setAttribute('class', 'bulkActionName no-change-track')
|
||||||
->setAttribute('id', '');
|
->setAttribute('id', '');
|
||||||
|
|
||||||
$templateData = new ArrayData(array(
|
$templateData = array(
|
||||||
'Menu' => $dropDownActionsList->FieldHolder(),
|
'Menu' => $dropDownActionsList->FieldHolder(),
|
||||||
'Button' => array(
|
'Button' => array(
|
||||||
'Label' => _t('GridFieldBulkTools.ACTION_BTN_LABEL', 'Go'),
|
'Label' => _t('GridFieldBulkTools.ACTION_BTN_LABEL', 'Go'),
|
||||||
'DataURL' => $gridField->Link('bulkaction')
|
'DataURL' => $gridField->Link('bulkaction'),
|
||||||
|
'Icon' => $this->config['actions'][$firstAction]['config']['icon'],
|
||||||
|
'DataConfig' => htmlspecialchars(json_encode($actionsConfig), ENT_QUOTES, 'UTF-8')
|
||||||
),
|
),
|
||||||
'Select' => array(
|
'Select' => array(
|
||||||
'Label' => _t('GridFieldBulkTools.SELECT_ALL_LABEL', 'Select all')
|
'Label' => _t('GridFieldBulkTools.SELECT_ALL_LABEL', 'Select all')
|
||||||
)
|
),
|
||||||
));
|
'Colspan' => (count($gridField->getColumns()) - 1)
|
||||||
|
);
|
||||||
$args = array('Colspan' => count($gridField->getColumns())-1);
|
|
||||||
|
if ( !$this->config['actions'][$firstAction]['config']['isAjax'] )
|
||||||
|
{
|
||||||
|
$templateData['Button']['href'] = $gridField->Link('bulkaction') . '/' . $firstAction;
|
||||||
|
}
|
||||||
|
|
||||||
|
$templateData = new ArrayData($templateData);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'header' => $templateData->renderWith('BulkManagerButtons', $args)
|
'header' => $templateData->renderWith('BulkManagerButtons')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,45 +48,41 @@
|
|||||||
},
|
},
|
||||||
onunmatch: function(){
|
onunmatch: function(){
|
||||||
},
|
},
|
||||||
onchange: function(e) {
|
onchange: function(e)
|
||||||
var value, btn, icon;
|
{
|
||||||
value = $(this).val();
|
var value = $(this).val(),
|
||||||
btn = $(this).parents('.bulkManagerOptions').find('.doBulkActionButton');
|
$parent = $(this).parents('.bulkManagerOptions'),
|
||||||
icon = $(this).parents('.bulkManagerOptions').find('.doBulkActionButton .ui-icon');
|
$btn = $parent.find('.doBulkActionButton'),
|
||||||
|
config = $btn.data('config'),
|
||||||
switch (value) {
|
$icon = $parent.find('.doBulkActionButton .ui-icon')
|
||||||
case 'edit':
|
;
|
||||||
$(btn).removeClass('ss-ui-action-destructive');
|
|
||||||
$(btn).attr('data-icon', 'pencil');
|
if ( config[value]['isAjax'] )
|
||||||
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-pencil');
|
{
|
||||||
|
$btn.removeAttr('href');
|
||||||
$(btn).attr('href', $(btn).data('url')+'/edit');
|
}
|
||||||
break;
|
else{
|
||||||
|
$btn.attr('href', $btn.data('url')+'/'+value);
|
||||||
case 'unlink':
|
|
||||||
$(btn).removeClass('ss-ui-action-destructive');
|
|
||||||
$(btn).attr('data-icon', 'chain--minus');
|
|
||||||
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-chain--minus');
|
|
||||||
$(btn).removeAttr('href');
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'delete':
|
|
||||||
$(btn).addClass('ss-ui-action-destructive');
|
|
||||||
$(btn).attr('data-icon', 'decline');
|
|
||||||
$(icon).removeClass('btn-icon-decline btn-icon-pencil').addClass('btn-icon-decline');
|
|
||||||
$(btn).removeAttr('href');
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
$(icon).removeClass('btn-icon-decline btn-icon-pencil btn-icon-chain--minus')
|
$.each( config, function( configKey, configData )
|
||||||
$(btn).removeClass('ss-ui-action-destructive');
|
{
|
||||||
$(btn).attr('data-icon', 'decline');
|
if ( configKey != value )
|
||||||
$(btn).attr('data-icon', 'chain--minus');
|
{
|
||||||
$(btn).attr('data-icon', 'pencil');
|
$icon.removeClass('btn-icon-'+configData['icon']);
|
||||||
$(btn).removeAttr('href');
|
}
|
||||||
$(btn).attr('href', $(btn).data('url')+'/edit');
|
});
|
||||||
*/
|
$icon.addClass('btn-icon-'+config[value]['icon']);
|
||||||
|
|
||||||
|
|
||||||
|
if ( config[value]['isDestructive'] )
|
||||||
|
{
|
||||||
|
$btn.addClass('ss-ui-action-destructive');
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$btn.removeClass('ss-ui-action-destructive');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -2,10 +2,9 @@
|
|||||||
<th class="extra bulkmanagerheading" colspan="$Colspan">
|
<th class="extra bulkmanagerheading" colspan="$Colspan">
|
||||||
|
|
||||||
$Menu
|
$Menu
|
||||||
<a data-url="$Button.DataURL" class="doBulkActionButton action ss-ui-button cms-panel-link" data-icon="pencil">
|
<a <% if $Button.href %>href="$Button.href"<% end_if %> data-url="$Button.DataURL" data-config="$Button.DataConfig" class="doBulkActionButton action ss-ui-button cms-panel-link" data-icon="$Button.Icon">
|
||||||
$Button.Label
|
$Button.Label
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
</th>
|
</th>
|
||||||
<th class="extra bulkmanagerselect">
|
<th class="extra bulkmanagerselect">
|
||||||
|
Loading…
Reference in New Issue
Block a user