Skip to content

Commit

Permalink
Added remove changed class flag on save all button. Fixes #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mellisa Hankins committed May 5, 2017
1 parent d5e73fd commit f53b6cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
10 changes: 10 additions & 0 deletions _assets/js/mwm.gridfield.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,5 +933,15 @@
});
}
});

// Milkyway\SS\GridFieldUtils\SaveAllButton
// @credit SilbinaryWolf
$(".js-mwm-gridfield--saveall").entwine({
onmatch: function(){
// Remove 'changed' state from form anytime a SaveAll button is created.
$(this[0].form).removeClass('changed');
this._super();
}
});
});
})(jQuery);
34 changes: 26 additions & 8 deletions code/Buttons/SaveAllButton.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Milkyway\SS\GridFieldUtils;
/**
* Milkyway Multimedia
* SaveAllButton.php
Expand All @@ -7,9 +7,14 @@
* @author Mellisa Hankins <mell@milkywaymultimedia.com.au>
*/

namespace Milkyway\SS\GridFieldUtils;
use GridField;
use GridField_HTMLProvider;
use GridField_ActionProvider;
use GridField_FormAction;
use GridField_SaveHandler;
use Controller;

class SaveAllButton implements \GridField_HTMLProvider, \GridField_ActionProvider
class SaveAllButton implements GridField_HTMLProvider, GridField_ActionProvider
{
protected $targetFragment;
protected $actionName = 'saveallrecords';
Expand All @@ -20,12 +25,20 @@ class SaveAllButton implements \GridField_HTMLProvider, \GridField_ActionProvide

public $completeMessage;

public $removeChangeFlagOnFormOnSave = false;

public function setButtonName($name)
{
$this->buttonName = $name;
return $this;
}

public function setRemoveChangeFlagOnFormOnSave($flag)
{
$this->removeChangeFlagOnFormOnSave = $flag;
return $this;
}

public function __construct($targetFragment = 'before', $publish = true, $action = 'saveallrecords')
{
$this->targetFragment = $targetFragment;
Expand All @@ -49,15 +62,20 @@ public function getHTMLFragments($gridField)
}
}

$button = \GridField_FormAction::create(
$button = GridField_FormAction::create(
$gridField,
$this->actionName,
$this->buttonName,
$this->actionName,
null
);

$button->setAttribute('data-icon', 'disk')->addExtraClass('new new-link ui-button-text-icon-primary');

if($this->removeChangeFlagOnFormOnSave) {
$button->addExtraClass('js-mwm-gridfield--saveall');
}

return [
$this->targetFragment => $button->Field(),
];
Expand All @@ -68,22 +86,22 @@ public function getActions($gridField)
return [$this->actionName];
}

public function handleAction(\GridField $gridField, $actionName, $arguments, $data)
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
if ($actionName == $this->actionName) {
return $this->saveAllRecords($gridField, $arguments, $data);
}
}

protected function saveAllRecords(\GridField $grid, $arguments, $data)
protected function saveAllRecords(GridField $grid, $arguments, $data)
{
if (isset($data[$grid->Name])) {
$currValue = $grid->Value();
$grid->setValue($data[$grid->Name]);
$model = singleton($grid->List->dataClass());

foreach ($grid->getConfig()->getComponents() as $component) {
if ($component instanceof \GridField_SaveHandler) {
if ($component instanceof GridField_SaveHandler) {
$component->handleSave($grid, $model);
}
}
Expand All @@ -109,7 +127,7 @@ function ($item) {

$grid->setValue($currValue);

if (\Controller::curr() && $response = \Controller::curr()->Response) {
if (Controller::curr() && $response = Controller::curr()->Response) {
if (!$this->completeMessage) {
$this->completeMessage = _t('GridField.DONE', 'Done.');
}
Expand Down
Loading

0 comments on commit f53b6cc

Please sign in to comment.