Use new plus icon (#8619)

* Use new plus icon and refactor

* MINOR: Add ability to customise the visibility of the `Previous`, `Next` and `Add` buttons at a `GridField` level

* - Merge `showPrevious` and `showNext` to `showPagination` for grid fields
- Update documentation
- Improve performance for next/previous buttons by not fetching all list records
- Refactoring

* Refactor to fail gracefully on GridFieldPaginator

* Fix merge
This commit is contained in:
Andre Kiste 2018-11-23 12:40:18 +13:00 committed by Aaron Carlino
parent 661d2567fa
commit 1d9e40ba26
2 changed files with 56 additions and 51 deletions

View File

@ -202,7 +202,7 @@ class GridFieldDetailForm implements GridField_URLHandler
protected function getDefaultShowPagination()
{
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
return isset($formActionsConfig['showPagination']) ? (boolean) $formActionsConfig['showPagination'] : false;
return isset($formActionsConfig['showPagination']) ? (bool) $formActionsConfig['showPagination'] : false;
}
/**
@ -214,7 +214,7 @@ class GridFieldDetailForm implements GridField_URLHandler
return $this->getDefaultShowPagination();
}
return (boolean) $this->showPagination;
return (bool) $this->showPagination;
}
/**
@ -233,7 +233,7 @@ class GridFieldDetailForm implements GridField_URLHandler
protected function getDefaultShowAdd()
{
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
return isset($formActionsConfig['showAdd']) ? (boolean) $formActionsConfig['showAdd'] : false;
return isset($formActionsConfig['showAdd']) ? (bool) $formActionsConfig['showAdd'] : false;
}
/**
@ -245,7 +245,7 @@ class GridFieldDetailForm implements GridField_URLHandler
return $this->getDefaultShowAdd();
}
return (boolean) $this->showAdd;
return (bool) $this->showAdd;
}
/**
@ -313,9 +313,8 @@ class GridFieldDetailForm implements GridField_URLHandler
return $this->itemRequestClass;
} elseif (ClassInfo::exists(static::class . '_ItemRequest')) {
return static::class . '_ItemRequest';
} else {
return GridFieldDetailForm_ItemRequest::class;
}
return GridFieldDetailForm_ItemRequest::class;
}
/**

View File

@ -271,6 +271,47 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
return $form;
}
/**
* @return CompositeField Returns the right aligned toolbar group field along with its FormAction's
*/
private function getRightGroupField()
{
$rightGroup = CompositeField::create()->setName('RightGroup');
$rightGroup->addExtraClass('ml-auto');
$rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup');
$previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup');
$previousAndNextGroup->addExtraClass('circular-group mr-2');
$previousAndNextGroup->setFieldHolderTemplate(get_class($previousAndNextGroup) . '_holder_buttongroup');
$component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
$gridState = $this->getRequest()->requestVar('gridState');
if ($component->getShowPagination()) {
$previousAndNextGroup->push(FormAction::create('doPrevious')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridState)
->setDisabled(!$this->getPreviousRecordID())
->addExtraClass('btn btn-secondary font-icon-left-open action--previous discard-confirmation'));
$previousAndNextGroup->push(FormAction::create('doNext')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridState)
->setDisabled(!$this->getNextRecordID())
->addExtraClass('btn btn-secondary font-icon-right-open action--next discard-confirmation'));
}
$rightGroup->push($previousAndNextGroup);
if ($component->getShowAdd()) {
$rightGroup->push(FormAction::create('doNew')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $this->getRequest()->getVar('gridState'))
->addExtraClass('btn btn-primary font-icon-plus-thin circular action--new discard-confirmation'));
}
return $rightGroup;
}
/**
* Build the set of form field actions for this DataObject
*
@ -278,66 +319,31 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
*/
protected function getFormActions()
{
$canEdit = $this->record->canEdit();
$canDelete = $this->record->canDelete();
$actions = new FieldList();
if ($this->record->ID !== 0) {
if ($canEdit) {
if ($this->record->ID !== 0) { // existing record
if ($this->record->canEdit()) {
$actions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save'))
->setUseButtonTag(true)
->addExtraClass('btn-primary font-icon-save'));
}
if ($canDelete) {
if ($this->record->canDelete()) {
$actions->push(FormAction::create('doDelete', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete'))
->setUseButtonTag(true)
->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete'));
}
$gridStateStr = $this->getRequest()->requestVar('gridState');
$gridState = $this->getRequest()->requestVar('gridState');
$this->gridField->getState(false)->setValue($gridState);
$actions->push(HiddenField::create('gridState', null, $gridState));
$this->gridField->getState(false)->setValue($gridStateStr);
$actions->push(HiddenField::create('gridState', null, $gridStateStr));
$rightGroup = CompositeField::create()->setName('RightGroup');
$rightGroup->addExtraClass('right');
$rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup');
$previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup');
$previousAndNextGroup->addExtraClass('rounded');
$previousAndNextGroup->setFieldHolderTemplate(get_class($previousAndNextGroup) . '_holder_buttongroup');
$component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
if ($component->getShowPagination()) {
$previousAndNextGroup->push(FormAction::create('doPrevious')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridStateStr)
->setDisabled(!$this->getPreviousRecordID())
->addExtraClass('btn btn-secondary font-icon-left-open action--previous discard-confirmation'));
$previousAndNextGroup->push(FormAction::create('doNext')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $gridStateStr)
->setDisabled(!$this->getNextRecordID())
->addExtraClass('btn btn-secondary font-icon-right-open action--next discard-confirmation'));
}
$rightGroup->push($previousAndNextGroup);
if ($component->getShowAdd()) {
$rightGroup->push(FormAction::create('doNew')
->setUseButtonTag(true)
->setAttribute('data-grid-state', $this->getRequest()->getVar('gridState'))
->addExtraClass('btn btn-primary font-icon-plus rounded action--new discard-confirmation'));
}
$actions->push($this->getRightGroupField());
} else { // adding new record
//Change the Save label to 'Create'
$actions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create'))
->setUseButtonTag(true)
->addExtraClass('btn-primary font-icon-plus'));
->addExtraClass('btn-primary font-icon-plus-thin'));
// Add a Cancel link which is a button-like link and link back to one level up.
$crumbs = $this->Breadcrumbs();
@ -530,8 +536,8 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
return false;
}
$currentPage = $data->getData('GridFieldPaginator')->getData('currentPage');
$itemsPerPage = $data->getData('GridFieldPaginator')->getData('itemsPerPage');
$currentPage = $paginator->getData('currentPage');
$itemsPerPage = $paginator->getData('itemsPerPage');
$limit = $itemsPerPage + 2;
$limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1);