From 04c42273cfc045ad4f3e241cfed1d9283b55b64b Mon Sep 17 00:00:00 2001 From: Lee Bradley Date: Thu, 12 Dec 2019 16:10:11 +0000 Subject: [PATCH 1/2] Add Multi Class to inline add button Raised in Issue #300 --- _config/gridfieldextensions.yml | 6 +- css/GridFieldExtensions.css | 22 ++++++ javascript/GridFieldExtensions.js | 18 +++++ .../GridFieldDetailForm_ItemRequest.php | 75 +++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/Extensions/GridFieldDetailForm_ItemRequest.php diff --git a/_config/gridfieldextensions.yml b/_config/gridfieldextensions.yml index 837c474f..34e1647b 100644 --- a/_config/gridfieldextensions.yml +++ b/_config/gridfieldextensions.yml @@ -2,4 +2,8 @@ name: gridfieldextensions --- GridFieldAddNewMultiClass: - showEmptyString: true \ No newline at end of file + showEmptyString: true + +SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest: + extensions: + - Symbiote\GridFieldExtensions\Extensions\GridFieldDetailForm_ItemRequest diff --git a/css/GridFieldExtensions.css b/css/GridFieldExtensions.css index f56db7eb..f36ff2f9 100644 --- a/css/GridFieldExtensions.css +++ b/css/GridFieldExtensions.css @@ -201,3 +201,25 @@ .ss-gridfield-configurable-paginator .pagination-page-number input { text-align: center; } + +.grid-field-inline-new--multi-class-list { + display: none; + background-color: #008a00; + border-radius: 5px; + bottom: 3em; + list-style: none; + margin: 0; + right: 3em; + padding: 10px; + position: fixed; + +} +.grid-field-inline-new--multi-class-list a { + color: #FFF; + display: block; + margin: 5px -10px; + padding: 0 10px; +} +.grid-field-inline-new--multi-class-list__visible { + display: block; +} diff --git a/javascript/GridFieldExtensions.js b/javascript/GridFieldExtensions.js index 33e3115c..60529d70 100644 --- a/javascript/GridFieldExtensions.js +++ b/javascript/GridFieldExtensions.js @@ -237,6 +237,24 @@ } }); + $(".action--new__multi-class").entwine({ + onmatch: function () { + const hrefTemplate = this.data('hrefTemplate'); + const classes = this.data('classes'); + const liHtml = Object.keys(classes).map(className => { + const link = hrefTemplate.replace('{class}', className); + return `
  • Add: ${classes[className]}
  • `; + }); + + const listElement = $(``); + listElement.insertBefore(this); + + this.on('click', function () { + listElement.toggleClass('grid-field-inline-new--multi-class-list__visible'); + }); + }, + }); + $(".ss-gridfield-add-new-multi-class select").entwine({ onadd: function() { this.update(); diff --git a/src/Extensions/GridFieldDetailForm_ItemRequest.php b/src/Extensions/GridFieldDetailForm_ItemRequest.php new file mode 100644 index 00000000..4a099c25 --- /dev/null +++ b/src/Extensions/GridFieldDetailForm_ItemRequest.php @@ -0,0 +1,75 @@ +owner->getGridField(); + $gridFieldConfig = $grid->getConfig(); + $addMultiClassComponent = $gridFieldConfig->getComponentByType(GridFieldAddNewMultiClass::class); + if ($addMultiClassComponent) { + $newRecordField = static::get_new_record_field_from_actions($actions); + if ($newRecordField) { + $newRecordField->getContainerFieldList()->removeByName('new-record'); + $newRecordField->getContainerFieldList()->push( + LiteralField::create('new-record', $this->getHTMLFragment($addMultiClassComponent)) + ); + GridFieldExtensions::include_requirements(); + } + } + } + + /** + * {@inheritDoc} + */ + private function getHTMLFragment(GridFieldAddNewMultiClass $component) + { + $grid = $this->owner->getGridField(); + + $classes = $component->getClasses($grid); + + if (!count($classes)) { + return false; + } + + return HTML::createTag('a', [ + 'data-href-template' => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'), + 'title' => _t(__CLASS__ . '.NEW', 'Add new record'), + 'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'), + 'class' => 'btn btn-primary font-icon-plus-thin btn--circular action--new discard-confirmation action--new__multi-class', + 'data-classes' => Convert::array2json($classes), + ]); + } + + /** + * @param FieldList $actions + * @return LiteralField OR NULL + */ + private static function get_new_record_field_from_actions(FieldList &$actions) + { + $rightGroup = $actions->fieldByName('RightGroup'); + if (!$rightGroup) { + return null; + } + return $rightGroup->getChildren()->fieldByName('new-record'); + } +} \ No newline at end of file From 4afe5dcfd378d5060e300e3f0c139c0c3bac5efa Mon Sep 17 00:00:00 2001 From: Lee Bradley Date: Thu, 12 Dec 2019 16:18:09 +0000 Subject: [PATCH 2/2] Tweaks to pass tests --- _config/gridfieldextensions.yml | 2 +- ... GridFieldDetailFormItemRequestExtension.php} | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) rename src/Extensions/{GridFieldDetailForm_ItemRequest.php => GridFieldDetailFormItemRequestExtension.php} (85%) diff --git a/_config/gridfieldextensions.yml b/_config/gridfieldextensions.yml index 34e1647b..f8727c39 100644 --- a/_config/gridfieldextensions.yml +++ b/_config/gridfieldextensions.yml @@ -6,4 +6,4 @@ GridFieldAddNewMultiClass: SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest: extensions: - - Symbiote\GridFieldExtensions\Extensions\GridFieldDetailForm_ItemRequest + - Symbiote\GridFieldExtensions\Extensions\GridFieldDetailFormItemRequestExtension diff --git a/src/Extensions/GridFieldDetailForm_ItemRequest.php b/src/Extensions/GridFieldDetailFormItemRequestExtension.php similarity index 85% rename from src/Extensions/GridFieldDetailForm_ItemRequest.php rename to src/Extensions/GridFieldDetailFormItemRequestExtension.php index 4a099c25..46af26af 100644 --- a/src/Extensions/GridFieldDetailForm_ItemRequest.php +++ b/src/Extensions/GridFieldDetailFormItemRequestExtension.php @@ -6,7 +6,7 @@ use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; use SilverStripe\Forms\FieldList; -use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest As CoreGridFieldDetailForm_ItemRequest; +use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest as CoreGridFieldDetailForm_ItemRequest; use SilverStripe\Forms\LiteralField; use SilverStripe\View\ArrayData; use SilverStripe\View\HTML; @@ -16,7 +16,7 @@ /** * @property CoreGridFieldDetailForm_ItemRequest $owner */ -class GridFieldDetailForm_ItemRequest extends Extension +class GridFieldDetailFormItemRequestExtension extends Extension { /** * @param FieldList $actions @@ -55,7 +55,15 @@ private function getHTMLFragment(GridFieldAddNewMultiClass $component) 'data-href-template' => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'), 'title' => _t(__CLASS__ . '.NEW', 'Add new record'), 'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'), - 'class' => 'btn btn-primary font-icon-plus-thin btn--circular action--new discard-confirmation action--new__multi-class', + 'class' => implode(' ', array( + 'btn', + 'btn-primary', + 'font-icon-plus-thin', + 'btn--circular', + 'action--new', + 'discard-confirmation', + 'action--new__multi-class', + )), 'data-classes' => Convert::array2json($classes), ]); } @@ -72,4 +80,4 @@ private static function get_new_record_field_from_actions(FieldList &$actions) } return $rightGroup->getChildren()->fieldByName('new-record'); } -} \ No newline at end of file +}