Skip to content

Commit

Permalink
MINOR More consistent overloading in GridField_Action, using standard…
Browse files Browse the repository at this point in the history
… getAttributes() method rather than custom template placeholders, removing setButtonIcon() and setButtonLabel() as those features already exist in the base implementation
  • Loading branch information
chillu committed Mar 6, 2012
1 parent 0b42282 commit 1c344db
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 82 deletions.
83 changes: 26 additions & 57 deletions forms/gridfield/GridField.php
Expand Up @@ -290,6 +290,17 @@ public function getState($getData=true) {
* @return string
*/
public function FieldHolder() {
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(SAPPHIRE_DIR . '/css/GridField.css');

Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
Requirements::javascript(THIRDPARTY_DIR . '/json-js/json2.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/i18n.js');
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/GridField.js');

// Get columns
$columns = $this->getColumns();

Expand Down Expand Up @@ -668,13 +679,6 @@ class GridField_Action extends FormAction {
*/
protected $gridField;

/**
*
* @var string
*/
protected $buttonLabel;
protected $buttonIcon;

/**
*
* @var array
Expand All @@ -688,8 +692,11 @@ class GridField_Action extends FormAction {
//protected $stateFields = array();

protected $actionName;

protected $args = array();

public $useButtonTag = true;

/**
*
* @param GridField $gridField
Expand All @@ -698,12 +705,11 @@ class GridField_Action extends FormAction {
* @param type $actionName
* @param type $args
*/
public function __construct(GridField $gridField, $name, $label, $actionName, $args) {
public function __construct(GridField $gridField, $name, $title, $actionName, $args) {
$this->gridField = $gridField;
$this->buttonLabel = $label;
$this->actionName = $actionName;
$this->args = $args;
parent::__construct($name);
parent::__construct($name, $title);
}

/**
Expand All @@ -723,64 +729,27 @@ public function nameEncode($val) {
public function _nameEncode($match) {
return '%'.dechex(ord($match[0]));
}

/**
* buttonIcon setter
*/
public function setButtonIcon($iconName) {
$this->buttonIcon = $iconName;
}

/**
* buttonIcon getter
*/
public function getButtonIcon(){
return $this->buttonIcon;
}

/**
* Default method used by Templates to render the form
*
* @return string HTML tag
*/
public function Field() {
Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(SAPPHIRE_DIR . '/css/GridField.css');

Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
Requirements::javascript(THIRDPARTY_DIR . '/json-js/json2.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/i18n.js');
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/GridField.js');

public function getAttributes() {
// Store state in session, and pass ID to client side
$state = array(
'grid' => $this->getNameFromParent(),
'actionName' => $this->actionName,
'args' => $this->args,
);

$id = preg_replace('/[^\w]+/', '_', uniqid('', true));
Session::set($id, $state);

$actionData['StateID'] = $id;

// And generate field
$data = new ArrayData(array(
'Class' => ($this->extraClass() ? $this->extraClass() : '') . ($this->isReadonly() ? ' disabled' : ''),
'ID' => $this->id(),
// Note: This field needs to be less than 65 chars, otherwise Suhosin security patch
// will strip it from the requests
'Name' => 'action_gridFieldAlterAction'. '?' . http_build_query($actionData),
'Disabled' => $this->isReadonly(),
'Label' => $this->buttonLabel,
'Icon' => $this->buttonIcon,
'DataURL' => $this->gridField->Link(),
));

return $data->renderWith('GridField_Action');
return array_merge(
parent::getAttributes(),
array(
// Note: This field needs to be less than 65 chars, otherwise Suhosin security patch
// will strip it from the requests
'name' => 'action_gridFieldAlterAction'. '?' . http_build_query($actionData),
'data-url' => $this->gridField->Link(),
)
);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions forms/gridfield/GridFieldAction.php
Expand Up @@ -163,16 +163,17 @@ public function getActions($gridField) {
* @return string - the HTML for the column
*/
public function getColumnContent($gridField, $record, $columnName) {
$field = new GridField_Action(
$field = Object::create('GridField_Action',
$gridField,
'DeleteRecord'.$record->ID,
_t('GridAction.Delete', "delete"),
false,
"deleterecord",
array('RecordID' => $record->ID)
);
$field->addExtraClass('gridfield-button-delete');
$output = $field->Field();
return $output;
)
->addExtraClass('gridfield-button-delete')
->setAttribute('title', _t('GridAction.Delete', "delete"))
->setAttribute('data-icon', 'decline');
return $field->Field();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion forms/gridfield/GridFieldExporter.php
Expand Up @@ -46,7 +46,7 @@ public function getHTMLFragments($gridField) {
'export',
null
);
$button->setButtonIcon('exportlist');
$button->setAttribute('data-icon', 'exportlist');
$button->addExtraClass('no-ajax');
return array(
'after' => '<p>' . $button->Field() . '</p>',
Expand Down
12 changes: 7 additions & 5 deletions forms/gridfield/GridFieldFilter.php
Expand Up @@ -75,12 +75,14 @@ public function getHTMLFragments($gridField) {

$field = new FieldGroup(
$field,
$filterbutton = new GridField_Action($gridField, 'filter', _t('GridField.Filter', "Filter"), 'filter', null),
$resetbutton = new GridField_Action($gridField, 'reset', _t('GridField.ResetFilter', "Reset"), 'reset', null)
Object::create('GridField_Action', $gridField, 'filter', false, 'filter', null)
->addExtraClass('ss-gridfield-button-filter')
->setAttribute('title', _t('GridField.Filter', "Filter"))
,
Object::create('GridField_Action', $gridField, 'reset', false, 'reset', null)
->addExtraClass('ss-gridfield-button-reset')
->setAttribute('title', _t('GridField.ResetFilter', "Reset"))
);

$filterbutton->addExtraClass('ss-gridfield-button-filter');
$resetbutton->addExtraClass('ss-gridfield-button-reset');
} else {
$field = new LiteralField('', '');
}
Expand Down
4 changes: 2 additions & 2 deletions forms/gridfield/GridFieldPopupForms.php
Expand Up @@ -224,9 +224,9 @@ function ItemEditForm() {
$this->record->getCMSFields($this->popupController, $this->popupFormName),
new FieldList(
FormAction::create('doSave', _t('GridFieldDetailsForm.Save', 'Save'))
->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'),
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'),
FormAction::create('doDelete', _t('GridFieldDetailsForm.Delete', 'Delete'))
->setUseButtonTag(true)->addExtraClass('ss-ui-action-destructive')
->addExtraClass('ss-ui-action-destructive')
),
$this->component->getValidator()
);
Expand Down
4 changes: 2 additions & 2 deletions forms/gridfield/GridFieldRelationAdd.php
Expand Up @@ -66,9 +66,9 @@ public function getHTMLFragments($gridField) {
$searchField->addExtraClass('relation-search');

$findAction = new GridField_Action($gridField, 'gridfield_relationfind', _t('GridField.Find', "Find"), 'find', 'find');
$findAction->setButtonIcon('relationfind');
$findAction->setAttribute('data-icon', 'relationfind');
$addAction = new GridField_Action($gridField, 'gridfield_relationadd', _t('GridField.LinkExisting', "Link Exisiting"), 'addto', 'addto');
$addAction->setButtonIcon('linkexisting');
$addAction->setAttribute('data-icon', 'linkexisting');

// If an object is not found, disable the action
if(!is_int($gridField->State->GridFieldAddRelation)) {
Expand Down
10 changes: 5 additions & 5 deletions forms/gridfield/GridFieldRelationDelete.php
Expand Up @@ -73,15 +73,15 @@ public function getActions($gridField) {
* @return string - the HTML for the column
*/
public function getColumnContent($gridField, $record, $columnName) {
$field = new GridField_Action(
$field = Object::create('GridField_Action',
$gridField,
'UnlinkRelation'.$record->ID,
_t('GridAction.UnlinkRelation', "Unlink"),
false,
"unlinkrelation",
array('RecordID' => $record->ID)
);
$output = $field->Field();
return $output;
)
->setAttribute('title', _t('GridAction.UnlinkRelation', "Unlink"));
return $field->Field();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions forms/gridfield/GridFieldSortableHeader.php
Expand Up @@ -28,9 +28,11 @@ public function getHTMLFragments($gridField) {
$dir = 'desc';
}

$field = new GridField_Action($gridField, 'SetOrder'.$columnField, $title, "sort$dir", array('SortColumn' => $columnField));
$field = Object::create(
'GridField_Action', $gridField, 'SetOrder'.$columnField, $title,
"sort$dir", array('SortColumn' => $columnField)
)->addExtraClass('ss-gridfield-sort');

$field->addExtraClass('ss-gridfield-sort');
if($state->SortColumn == $columnField){
$field->addExtraClass('ss-gridfield-sorted');

Expand Down
4 changes: 2 additions & 2 deletions templates/Includes/GridField_Action.ss
@@ -1,3 +1,3 @@
<button id="$ID" class="$Class" name="$Name" type="submit" data-url="$DataURL"<% if Disabled %> disabled="disabled"<% end_if %><% if Icon %> data-icon="$Icon"<% end_if %>>
$Label
<button $AttributesHTML>
<% if ButtonContent %>$ButtonContent<% else %>$Title<% end_if %>
</button>

0 comments on commit 1c344db

Please sign in to comment.