mirror of
https://github.com/UndefinedOffset/SortableGridField.git
synced 2024-10-22 17:05:38 +02:00
Coding convention updates
Moved text labels into translations Added class documentation to the GridFieldSortableRows class
This commit is contained in:
parent
32746c875c
commit
20dfa3c9ea
@ -1,142 +1,156 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
|
* This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
|
||||||
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
*/
|
*/
|
||||||
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
|
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
|
||||||
protected $sortColumn;
|
protected $sortColumn;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} $sortColumn Column that should be used to update the sort information
|
* @param String $sortColumn Column that should be used to update the sort information
|
||||||
*/
|
*/
|
||||||
public function __construct($sortColumn) {
|
public function __construct($sortColumn) {
|
||||||
$this->sortColumn=$sortColumn;
|
$this->sortColumn = $sortColumn;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
* Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @return {array} Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
* @return Array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
||||||
*/
|
|
||||||
public function getHTMLFragments($gridField) {
|
|
||||||
$state=$gridField->State->GridFieldSortableRows;
|
|
||||||
if(!is_bool($state->sortableToggle)) {
|
|
||||||
$state->sortableToggle=false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Sort order toggle
|
|
||||||
$sortOrderToggle=new GridField_FormAction($gridField, 'sortablerows_toggle', 'Allow drag and drop re-ordering', 'saveGridRowSort', null);
|
|
||||||
$sortOrderToggle->addExtraClass('sortablerows_toggle');
|
|
||||||
|
|
||||||
|
|
||||||
//Disable Pagenator
|
|
||||||
$disablePagenator=new GridField_FormAction($gridField, 'sortablerows_disablepagenator', 'Disable Pagenator', 'sortableRowsDisablePaginator', null);
|
|
||||||
$disablePagenator->addExtraClass('sortablerows_disablepagenator');
|
|
||||||
|
|
||||||
|
|
||||||
$forTemplate=new ArrayData(array(
|
|
||||||
'SortableToggle'=>$sortOrderToggle,
|
|
||||||
'PagenatorToggle'=>$disablePagenator,
|
|
||||||
'Checked'=>($state->sortableToggle==true ? ' checked="checked"':'')
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
//Inject Requirements
|
|
||||||
Requirements::css('SortableGridField/css/GridFieldSortableRows.css');
|
|
||||||
Requirements::javascript('SortableGridField/javascript/GridFieldSortableRows.js');
|
|
||||||
|
|
||||||
|
|
||||||
return array(
|
|
||||||
'header'=>$forTemplate->renderWith('GridFieldSortableRows', array('Colspan'=>count($gridField->getColumns())))
|
|
||||||
);
|
|
||||||
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Manipulate the datalist as needed by this grid modifier.
|
|
||||||
* @param {GridField} $gridField Grid Field Reference
|
|
||||||
* @param {SS_List} $dataList Data List to adjust
|
|
||||||
* @return {DataList} Modified Data List
|
|
||||||
*/
|
*/
|
||||||
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
public function getHTMLFragments($gridField) {
|
||||||
$state=$gridField->State->GridFieldSortableHeader;
|
$state = $gridField->State->GridFieldSortableRows;
|
||||||
|
if(!is_bool($state->sortableToggle)) {
|
||||||
|
$state->sortableToggle = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Sort order toggle
|
||||||
|
$sortOrderToggle = Object::create(
|
||||||
|
'GridField_FormAction',
|
||||||
|
$gridField,
|
||||||
|
'sortablerows-toggle',
|
||||||
|
_t('GridFieldSortableRows.ALLOW_DRAG_DROP', '_Allow drag and drop re-ordering'),
|
||||||
|
'saveGridRowSort',
|
||||||
|
null
|
||||||
|
)->addExtraClass('sortablerows-toggle');
|
||||||
|
|
||||||
|
|
||||||
|
//Disable Pagenator
|
||||||
|
$disablePagenator = Object::create(
|
||||||
|
'GridField_FormAction',
|
||||||
|
$gridField,
|
||||||
|
'sortablerows-disablepagenator',
|
||||||
|
_t('GridFieldSortableRows.DISABLE_PAGINATOR', '_Disable Pagenator'),
|
||||||
|
'sortableRowsDisablePaginator',
|
||||||
|
null
|
||||||
|
)->addExtraClass('sortablerows-disablepagenator');
|
||||||
|
|
||||||
|
|
||||||
|
$data = array('SortableToggle' => $sortOrderToggle,
|
||||||
|
'PagenatorToggle' => $disablePagenator,
|
||||||
|
'Checked' => ($state->sortableToggle == true ? ' checked = "checked"':''));
|
||||||
|
|
||||||
|
$forTemplate = new ArrayData($data);
|
||||||
|
|
||||||
|
|
||||||
|
//Inject Requirements
|
||||||
|
Requirements::css('SortableGridField/css/GridFieldSortableRows.css');
|
||||||
|
Requirements::javascript('SortableGridField/javascript/GridFieldSortableRows.js');
|
||||||
|
|
||||||
|
|
||||||
|
$args = array('Colspan' => count($gridField->getColumns()));
|
||||||
|
|
||||||
|
return array('header' => $forTemplate->renderWith('GridFieldSortableRows', $args));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manipulate the datalist as needed by this grid modifier.
|
||||||
|
* @param GridField $gridField Grid Field Reference
|
||||||
|
* @param SS_List $dataList Data List to adjust
|
||||||
|
* @return DataList Modified Data List
|
||||||
|
*/
|
||||||
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
||||||
|
$state = $gridField->State->GridFieldSortableHeader;
|
||||||
if ($state && !empty($state->SortColumn)) {
|
if ($state && !empty($state->SortColumn)) {
|
||||||
return $dataList;
|
return $dataList;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dataList->sort($this->sortColumn);
|
return $dataList->sort($this->sortColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a list of the actions handled by this action provider.
|
* Return a list of the actions handled by this action provider.
|
||||||
* @param GridField $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @return {array} Array with action identifier strings.
|
* @return Array Array with action identifier strings.
|
||||||
*/
|
*/
|
||||||
public function getActions($gridField) {
|
public function getActions($gridField) {
|
||||||
return array('saveGridRowSort', 'sortableRowsDisablePaginator');
|
return array('saveGridRowSort', 'sortableRowsDisablePaginator');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle an action on the given grid field.
|
* Handle an action on the given grid field.
|
||||||
* @param {GridField} $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param {string} $actionName Action identifier, see {@link getActions()}.
|
* @param String $actionName Action identifier, see {@link getActions()}.
|
||||||
* @param {array} $arguments Arguments relevant for this
|
* @param Array $arguments Arguments relevant for this
|
||||||
* @param {array} $data All form data
|
* @param Array $data All form data
|
||||||
*/
|
*/
|
||||||
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
||||||
$state=$gridField->State->GridFieldSortableRows;
|
$state = $gridField->State->GridFieldSortableRows;
|
||||||
if(!is_bool($state->sortableToggle)) {
|
if (!is_bool($state->sortableToggle)) {
|
||||||
$state->sortableToggle=false;
|
$state->sortableToggle = false;
|
||||||
}else if($state->sortableToggle==true) {
|
} else if ($state->sortableToggle == true) {
|
||||||
if($gridField->getConfig()->getComponentsByType('GridFieldPaginator')) {
|
if ($gridField->getConfig()->getComponentsByType('GridFieldPaginator')) {
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldPaginator');
|
$gridField->getConfig()->removeComponentsByType('GridFieldPaginator');
|
||||||
$gridField->getConfig()->addComponent(new GridFieldFooter());
|
$gridField->getConfig()->addComponent(new GridFieldFooter());
|
||||||
}
|
}
|
||||||
|
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
||||||
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if($actionName=='savegridrowsort') {
|
if ($actionName == 'savegridrowsort') {
|
||||||
return $this->saveGridRowSort($gridField, $data);
|
return $this->saveGridRowSort($gridField, $data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles saving of the row sort order
|
* Handles saving of the row sort order
|
||||||
* @param {GridField} $gridField Grid Field Reference
|
* @param GridField $gridField Grid Field Reference
|
||||||
* @param {array} $data Data submitted in the request
|
* @param Array $data Data submitted in the request
|
||||||
*/
|
*/
|
||||||
private function saveGridRowSort(GridField $gridField, $data) {
|
private function saveGridRowSort(GridField $gridField, $data) {
|
||||||
if(empty($data['Items'])) {
|
if (empty($data['Items'])) {
|
||||||
user_error('No items to sort', E_USER_ERROR);
|
user_error('No items to sort', E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
$className=$gridField->getModelClass();
|
$className = $gridField->getModelClass();
|
||||||
$owner=$gridField->Form->getRecord();
|
$owner = $gridField->Form->getRecord();
|
||||||
$items=$gridField->getList();
|
$items = $gridField->getList();
|
||||||
$many_many=($items instanceof ManyManyList);
|
$many_many = ($items instanceof ManyManyList);
|
||||||
$sortColumn=$this->sortColumn;
|
$sortColumn = $this->sortColumn;
|
||||||
|
|
||||||
|
|
||||||
if($many_many) {
|
if ($many_many) {
|
||||||
list($parentClass, $componentClass, $parentField, $componentField, $table)=$owner->many_many($gridField->getName());
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$data['Items']=explode(',', $data['Items']);
|
$data['Items'] = explode(',', $data['Items']);
|
||||||
for($sort=0;$sort<count($data['Items']);$sort++) {
|
for($sort = 0;$sort<count($data['Items']);$sort++) {
|
||||||
$id=intval($data['Items'][$sort]);
|
$id = intval($data['Items'][$sort]);
|
||||||
if($many_many) {
|
if ($many_many) {
|
||||||
DB::query('UPDATE "'.$table.'" SET "'.$sortColumn.'"='.($sort+1).' WHERE "'.$componentField.'"='.$id.' AND "'.$parentField.'"='.$owner->ID);
|
DB::query('UPDATE "' . $table
|
||||||
}else {
|
. '" SET "' . $sortColumn.'" = ' . ($sort+1)
|
||||||
$obj=$items->byID($data['Items'][$sort]);
|
. ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID);
|
||||||
$obj->$sortColumn=$sort+1;
|
} else {
|
||||||
$obj->write();
|
$obj = $items->byID($data['Items'][$sort]);
|
||||||
}
|
$obj->$sortColumn = $sort+1;
|
||||||
}
|
$obj->write();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -1,11 +1,11 @@
|
|||||||
.cms table.ss-gridfield-table thead tr th.sortablerowsheading {
|
.cms table.ss-gridfield-table thead tr th.sortablerowsheading {
|
||||||
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
|
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
.cms table.ss-gridfield-table thead tr th.sortablerowsheading .gridfield-sortablerows button {
|
.cms table.ss-gridfield-table thead tr th.sortablerowsheading .gridfield-sortablerows button {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.cms table.ss-gridfield-table.dragSorting tbody tr td {
|
.cms table.ss-gridfield-table.dragSorting tbody tr td {
|
||||||
cursor: move;
|
cursor: move;
|
||||||
}
|
}
|
@ -1,50 +1,59 @@
|
|||||||
(function($) {
|
(function($) {
|
||||||
$('.ss-gridfield .gridfield-sortablerows input').entwine({
|
$('.ss-gridfield .gridfield-sortablerows input').entwine({
|
||||||
onmatch: function() {
|
onmatch: function() {
|
||||||
var refCheckbox=$(this);
|
var refCheckbox=$(this);
|
||||||
|
|
||||||
var gridField=$(this).getGridField();
|
var gridField=$(this).getGridField();
|
||||||
|
|
||||||
if($(this).is(':checked')) {
|
if($(this).is(':checked')) {
|
||||||
gridField.find('table').addClass('dragSorting');
|
gridField.find('table').addClass('dragSorting');
|
||||||
}else {
|
}else {
|
||||||
gridField.find('table').removeClass('dragSorting');
|
gridField.find('table').removeClass('dragSorting');
|
||||||
}
|
}
|
||||||
|
|
||||||
gridField.find('tbody').sortable({
|
gridField.find('tbody').sortable({
|
||||||
disabled: ($(this).is(':checked')==false),
|
disabled: ($(this).is(':checked')==false),
|
||||||
helper: function(e, ui) {
|
helper: function(e, ui) {
|
||||||
//Maintains width of the columns
|
//Maintains width of the columns
|
||||||
ui.children().each(function() {
|
ui.children().each(function() {
|
||||||
$(this).width($(this).width());
|
$(this).width($(this).width());
|
||||||
});
|
});
|
||||||
|
|
||||||
return ui;
|
return ui;
|
||||||
},
|
},
|
||||||
update: function(event, ui) {
|
update: function(event, ui) {
|
||||||
var dataRows=[];
|
var dataRows=[];
|
||||||
var gridItems=gridField.getItems();
|
var gridItems=gridField.getItems();
|
||||||
var button=refCheckbox.parent().find('.sortablerows_toggle');
|
var button=refCheckbox.parent().find('.sortablerows-toggle');
|
||||||
|
|
||||||
|
|
||||||
for(var i=0;i<gridItems.length;i++) {
|
for(var i=0;i<gridItems.length;i++) {
|
||||||
dataRows[i]=$(gridItems[i]).data('id');
|
dataRows[i]=$(gridItems[i]).data('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
gridField.reload({data: [{name: button.attr('name'), value: button.val()}, {name: 'Items', value: dataRows}]});
|
gridField.reload({data: [
|
||||||
}
|
{
|
||||||
}).disableSelection();
|
name: button.attr('name'),
|
||||||
},
|
value: button.val()},
|
||||||
|
{
|
||||||
|
name: 'Items',
|
||||||
|
value: dataRows
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).disableSelection();
|
||||||
|
},
|
||||||
|
|
||||||
onchange: function(e) {
|
onchange: function(e) {
|
||||||
var gridField=$(this).getGridField();
|
var gridField=$(this).getGridField();
|
||||||
gridField.find('tbody').sortable('option', 'disabled', ($(this).is(':checked')==false));
|
gridField.find('tbody').sortable('option', 'disabled', ($(this).is(':checked')==false));
|
||||||
gridField.setState('GridFieldSortableRows', {sortableToggle: $(this).is(':checked')});
|
gridField.setState('GridFieldSortableRows', {sortableToggle: $(this).is(':checked')});
|
||||||
|
|
||||||
|
|
||||||
var button=$(this).parent().find('.sortablerows_disablepagenator');
|
var button=$(this).parent().find('.sortablerows-disablepagenator');
|
||||||
gridField.reload({data: [{name: button.attr('name'), value: button.val()}]});
|
gridField.reload({data: [{name: button.attr('name'), value: button.val()}]});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})(jQuery);
|
})(jQuery);
|
4
lang/en.yml
Normal file
4
lang/en.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
en:
|
||||||
|
GridFieldSortableRows:
|
||||||
|
ALLOW_DRAG_DROP: "Allow Drag and Drop"
|
||||||
|
DISABLE_PAGINATOR: "Disable Pagenator"
|
@ -1,9 +1,9 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th class="extra sortablerowsheading" colspan="$Colspan">
|
<th class="extra sortablerowsheading" colspan="$Colspan">
|
||||||
<div class="gridfield-sortablerows">
|
<div class="gridfield-sortablerows">
|
||||||
<input type="checkbox" value="1"$Checked/> Allow drag and drop re-ordering
|
<input type="checkbox" value="1"$Checked/> <%t GridFieldSortableRows.ALLOW_DRAG_DROP "_Allow drag and drop re-ordering" %>
|
||||||
$SortableToggle
|
$SortableToggle
|
||||||
$PagenatorToggle
|
$PagenatorToggle
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
Loading…
Reference in New Issue
Block a user