mirror of
https://github.com/symbiote/silverstripe-gridfieldextensions.git
synced 2024-06-13 16:29:26 +02:00
parent
b93b32b3f9
commit
974c981721
|
@ -101,7 +101,14 @@ class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_S
|
||||||
|
|
||||||
$content = $field->Field();
|
$content = $field->Field();
|
||||||
} else {
|
} else {
|
||||||
$content = null;
|
$content = $grid->getColumnContent($record, $column);
|
||||||
|
|
||||||
|
// Convert GridFieldEditableColumns to the template format
|
||||||
|
$content = str_replace(
|
||||||
|
'[GridFieldEditableColumns][0]',
|
||||||
|
'[GridFieldAddNewInlineButton][{%=o.num%}]',
|
||||||
|
$content
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$attrs = '';
|
$attrs = '';
|
||||||
|
@ -129,7 +136,10 @@ class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_S
|
||||||
}
|
}
|
||||||
|
|
||||||
$class = $grid->getModelClass();
|
$class = $grid->getModelClass();
|
||||||
|
/** @var GridFieldEditableColumns $editable */
|
||||||
$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns');
|
$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns');
|
||||||
|
/** @var GridFieldOrderableRows $sortable */
|
||||||
|
$sortable = $grid->getConfig()->getComponentByType('GridFieldOrderableRows');
|
||||||
$form = $editable->getForm($grid, $record);
|
$form = $editable->getForm($grid, $record);
|
||||||
|
|
||||||
if(!singleton($class)->canCreate()) {
|
if(!singleton($class)->canCreate()) {
|
||||||
|
@ -143,6 +153,12 @@ class GridFieldAddNewInlineButton implements GridField_HTMLProvider, GridField_S
|
||||||
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
||||||
$form->saveInto($item);
|
$form->saveInto($item);
|
||||||
|
|
||||||
|
// Check if we are also sorting these records
|
||||||
|
if ($sortable) {
|
||||||
|
$sortField = $sortable->getSortField();
|
||||||
|
$item->setField($sortField, $fields[$sortField]);
|
||||||
|
}
|
||||||
|
|
||||||
if($list instanceof ManyManyList) {
|
if($list instanceof ManyManyList) {
|
||||||
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
|
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
|
||||||
// (ie. Maybe its readonly due to certain circumstances, or removed and not editable)
|
// (ie. Maybe its readonly due to certain circumstances, or removed and not editable)
|
||||||
$cmsFields = $record->getCMSFields();
|
$cmsFields = $record->getCMSFields();
|
||||||
$cmsField = $cmsFields->dataFieldByName($colRelation[0]);
|
$cmsField = $cmsFields->dataFieldByName($colRelation[0]);
|
||||||
if (!$cmsField || $cmsField->isReadonly() || $cmsField->isDisabled())
|
if (!$cmsField || $cmsField->isReadonly() || $cmsField->isDisabled())
|
||||||
{
|
{
|
||||||
return parent::getColumnContent($grid, $record, $col);
|
return parent::getColumnContent($grid, $record, $col);
|
||||||
}
|
}
|
||||||
|
@ -70,9 +70,9 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
|
||||||
|
|
||||||
$field->setName($this->getFieldName($field->getName(), $grid, $record));
|
$field->setName($this->getFieldName($field->getName(), $grid, $record));
|
||||||
$field->setValue($value);
|
$field->setValue($value);
|
||||||
|
|
||||||
if ($field instanceof HtmlEditorField) {
|
if ($field instanceof HtmlEditorField) {
|
||||||
return $field->FieldHolder();
|
return $field->FieldHolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $field->forTemplate();
|
return $field->forTemplate();
|
||||||
|
@ -91,6 +91,9 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var GridFieldOrderableRows $sortable */
|
||||||
|
$sortable = $grid->getConfig()->getComponentByType('GridFieldOrderableRows');
|
||||||
|
|
||||||
$form = $this->getForm($grid, $record);
|
$form = $this->getForm($grid, $record);
|
||||||
|
|
||||||
foreach($value[__CLASS__] as $id => $fields) {
|
foreach($value[__CLASS__] as $id => $fields) {
|
||||||
|
@ -109,6 +112,12 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
|
||||||
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
||||||
$form->saveInto($item);
|
$form->saveInto($item);
|
||||||
|
|
||||||
|
// Check if we are also sorting these records
|
||||||
|
if ($sortable) {
|
||||||
|
$sortField = $sortable->getSortField();
|
||||||
|
$item->setField($sortField, $fields[$sortField]);
|
||||||
|
}
|
||||||
|
|
||||||
if($list instanceof ManyManyList) {
|
if($list instanceof ManyManyList) {
|
||||||
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
|
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
|
||||||
}
|
}
|
||||||
|
@ -199,7 +208,7 @@ class GridFieldEditableColumns extends GridFieldDataColumns implements
|
||||||
//
|
//
|
||||||
// Allows use of 'MyBool.Nice' and 'MyHTML.NoHTML' so that
|
// Allows use of 'MyBool.Nice' and 'MyHTML.NoHTML' so that
|
||||||
// GridFields not using inline editing still look good or
|
// GridFields not using inline editing still look good or
|
||||||
// revert to looking good in cases where the field isn't
|
// revert to looking good in cases where the field isn't
|
||||||
// available or is readonly
|
// available or is readonly
|
||||||
//
|
//
|
||||||
$colRelation = explode('.', $col);
|
$colRelation = explode('.', $col);
|
||||||
|
|
|
@ -33,9 +33,9 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
protected $sortField;
|
protected $sortField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If set to true, when an item is re-ordered, it will update on the
|
* If set to true, when an item is re-ordered, it will update on the
|
||||||
* database and refresh the gridfield. When set to false, it will only
|
* database and refresh the gridfield. When set to false, it will only
|
||||||
* update the sort order when the record is saved.
|
* update the sort order when the record is saved.
|
||||||
*
|
*
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
|
@ -196,7 +196,22 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getColumnContent($grid, $record, $col) {
|
public function getColumnContent($grid, $record, $col) {
|
||||||
return ViewableData::create()->renderWith('GridFieldOrderableRowsDragHandle');
|
// In case you are using GridFieldEditableColumns, this ensures that
|
||||||
|
// the correct sort order is saved. If you are not using that component,
|
||||||
|
// this will be ignored by other components, but will still work for this.
|
||||||
|
$sortFieldName = sprintf(
|
||||||
|
'%s[GridFieldEditableColumns][%s][%s]',
|
||||||
|
$grid->getName(),
|
||||||
|
$record->ID,
|
||||||
|
$this->getSortField()
|
||||||
|
);
|
||||||
|
$sortField = new HiddenField($sortFieldName, false, $record->getField($this->getSortField()));
|
||||||
|
$sortField->addExtraClass('ss-orderable-hidden-sort');
|
||||||
|
$sortField->setForm($grid->getForm());
|
||||||
|
|
||||||
|
return ViewableData::create()->customise(array(
|
||||||
|
'SortField' => $sortField
|
||||||
|
))->renderWith('GridFieldOrderableRowsDragHandle');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getColumnAttributes($grid, $record, $col) {
|
public function getColumnAttributes($grid, $record, $col) {
|
||||||
|
@ -262,14 +277,16 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
$this->httpError(403);
|
$this->httpError(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save any un-comitted changes to the gridfield
|
// Save any un-committed changes to the gridfield
|
||||||
if(($form = $grid->getForm()) && ($record = $form->getRecord()) ) {
|
if(($form = $grid->getForm()) && ($record = $form->getRecord()) ) {
|
||||||
$form->loadDataFrom($request->requestVars(), true);
|
$form->loadDataFrom($request->requestVars(), true);
|
||||||
$grid->saveInto($record);
|
$grid->saveInto($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ids = $request->postVar('order');
|
// Get records from the `GridFieldEditableColumns` column
|
||||||
if (!$this->executeReorder($grid, $ids))
|
$data = $request->postVar($grid->getName());
|
||||||
|
$sortedIDs = $this->getSortedIDs($data);
|
||||||
|
if (!$this->executeReorder($grid, $sortedIDs))
|
||||||
{
|
{
|
||||||
$this->httpError(400);
|
$this->httpError(400);
|
||||||
}
|
}
|
||||||
|
@ -277,6 +294,26 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
return $grid->FieldHolder();
|
return $grid->FieldHolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get mapping of sort value to ID from posted data
|
||||||
|
*
|
||||||
|
* @param array $data Raw posted data
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getSortedIDs($data) {
|
||||||
|
if (empty($data['GridFieldEditableColumns'])) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$sortedIDs = array();
|
||||||
|
foreach($data['GridFieldEditableColumns'] as $id => $recordData) {
|
||||||
|
$sortValue = $recordData[$this->sortField];
|
||||||
|
$sortedIDs[$sortValue] = $id;
|
||||||
|
}
|
||||||
|
ksort($sortedIDs);
|
||||||
|
return $sortedIDs;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles requests to move an item to the previous or next page.
|
* Handles requests to move an item to the previous or next page.
|
||||||
*/
|
*/
|
||||||
|
@ -343,23 +380,21 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
public function handleSave(GridField $grid, DataObjectInterface $record) {
|
public function handleSave(GridField $grid, DataObjectInterface $record) {
|
||||||
if (!$this->immediateUpdate)
|
if (!$this->immediateUpdate)
|
||||||
{
|
{
|
||||||
$list = $grid->getList();
|
|
||||||
$value = $grid->Value();
|
$value = $grid->Value();
|
||||||
if (isset($value['GridFieldEditableColumns']) && $value['GridFieldEditableColumns'])
|
$sortedIDs = $this->getSortedIDs($value);
|
||||||
{
|
if ($sortedIDs) {
|
||||||
$rows = $value['GridFieldEditableColumns'];
|
$this->executeReorder($grid, $sortedIDs);
|
||||||
$ids = array();
|
|
||||||
foreach ($rows as $id => $data)
|
|
||||||
{
|
|
||||||
$ids[] = $id;
|
|
||||||
}
|
|
||||||
$this->executeReorder($grid, $ids);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function executeReorder(GridField $grid, $ids) {
|
/**
|
||||||
if(!is_array($ids)) {
|
* @param GridField $grid
|
||||||
|
* @param array $sortedIDs List of IDS, where the key is the sort field value to save
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function executeReorder(GridField $grid, $sortedIDs) {
|
||||||
|
if(!is_array($sortedIDs)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$field = $this->getSortField();
|
$field = $this->getSortField();
|
||||||
|
@ -376,10 +411,10 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
}
|
}
|
||||||
$list = $grid->getList();
|
$list = $grid->getList();
|
||||||
$sortterm .= '"'.$this->getSortTable($list).'"."'.$field.'"';
|
$sortterm .= '"'.$this->getSortTable($list).'"."'.$field.'"';
|
||||||
$items = $list->filter('ID', $ids)->sort($sortterm);
|
$items = $list->filter('ID', $sortedIDs)->sort($sortterm);
|
||||||
|
|
||||||
// Ensure that each provided ID corresponded to an actual object.
|
// Ensure that each provided ID corresponded to an actual object.
|
||||||
if(count($items) != count($ids)) {
|
if(count($items) != count($sortedIDs)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,11 +443,11 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform the actual re-ordering.
|
// Perform the actual re-ordering.
|
||||||
$this->reorderItems($list, $current, $ids);
|
$this->reorderItems($list, $current, $sortedIDs);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reorderItems($list, array $values, array $order) {
|
protected function reorderItems($list, array $values, array $sortedIDs) {
|
||||||
$sortField = $this->getSortField();
|
$sortField = $this->getSortField();
|
||||||
/** @var SS_List $map */
|
/** @var SS_List $map */
|
||||||
$map = $list->map('ID', $sortField);
|
$map = $list->map('ID', $sortField);
|
||||||
|
@ -433,13 +468,13 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
if (!$isVersioned) {
|
if (!$isVersioned) {
|
||||||
$sortTable = $this->getSortTable($list);
|
$sortTable = $this->getSortTable($list);
|
||||||
$additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : '';
|
$additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : '';
|
||||||
foreach(array_values($order) as $pos => $id) {
|
foreach($sortedIDs as $sortValue => $id) {
|
||||||
if($map[$id] != $pos) {
|
if($map[$id] != $sortValue) {
|
||||||
DB::query(sprintf(
|
DB::query(sprintf(
|
||||||
'UPDATE "%s" SET "%s" = %d%s WHERE %s',
|
'UPDATE "%s" SET "%s" = %d%s WHERE %s',
|
||||||
$sortTable,
|
$sortTable,
|
||||||
$sortField,
|
$sortField,
|
||||||
$pos,
|
$sortValue,
|
||||||
$additionalSQL,
|
$additionalSQL,
|
||||||
$this->getSortTableClauseForIds($list, $id)
|
$this->getSortTableClauseForIds($list, $id)
|
||||||
));
|
));
|
||||||
|
@ -450,10 +485,10 @@ class GridFieldOrderableRows extends RequestHandler implements
|
||||||
// *_versions table is updated. This ensures re-ordering works
|
// *_versions table is updated. This ensures re-ordering works
|
||||||
// similar to the SiteTree where you change the position, and then
|
// similar to the SiteTree where you change the position, and then
|
||||||
// you go into the record and publish it.
|
// you go into the record and publish it.
|
||||||
foreach(array_values($order) as $pos => $id) {
|
foreach($sortedIDs as $sortValue => $id) {
|
||||||
if($map[$id] != $pos) {
|
if($map[$id] != $sortValue) {
|
||||||
$record = $class::get()->byID($id);
|
$record = $class::get()->byID($id);
|
||||||
$record->$sortField = $pos;
|
$record->$sortField = $sortValue;
|
||||||
$record->write();
|
$record->write();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,327 +1,391 @@
|
||||||
(function($) {
|
(function($) {
|
||||||
$.entwine("ss", function($) {
|
$.entwine("ss", function($) {
|
||||||
/**
|
/**
|
||||||
* GridFieldAddExistingSearchButton
|
* GridFieldAddExistingSearchButton
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$(".add-existing-search-dialog").entwine({
|
$(".add-existing-search-dialog").entwine({
|
||||||
loadDialog: function(deferred) {
|
loadDialog: function(deferred) {
|
||||||
var dialog = this.addClass("loading").children(".ui-dialog-content").empty();
|
var dialog = this.addClass("loading").children(".ui-dialog-content").empty();
|
||||||
|
|
||||||
deferred.done(function(data) {
|
deferred.done(function(data) {
|
||||||
dialog.html(data).parent().removeClass("loading");
|
dialog.html(data).parent().removeClass("loading");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".ss-gridfield .add-existing-search").entwine({
|
$(".ss-gridfield .add-existing-search").entwine({
|
||||||
onclick: function() {
|
onclick: function() {
|
||||||
var dialog = $("<div></div>").appendTo("body").dialog({
|
var dialog = $("<div></div>").appendTo("body").dialog({
|
||||||
modal: true,
|
modal: true,
|
||||||
resizable: false,
|
resizable: false,
|
||||||
width: 500,
|
width: 500,
|
||||||
height: 600,
|
height: 600,
|
||||||
close: function() {
|
close: function() {
|
||||||
$(this).dialog("destroy").remove();
|
$(this).dialog("destroy").remove();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
dialog.parent().addClass("add-existing-search-dialog").loadDialog(
|
dialog.parent().addClass("add-existing-search-dialog").loadDialog(
|
||||||
$.get(this.prop("href"))
|
$.get(this.prop("href"))
|
||||||
);
|
);
|
||||||
dialog.data("grid", this.closest(".ss-gridfield"));
|
dialog.data("grid", this.closest(".ss-gridfield"));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".add-existing-search-dialog .add-existing-search-form").entwine({
|
$(".add-existing-search-dialog .add-existing-search-form").entwine({
|
||||||
onsubmit: function() {
|
onsubmit: function() {
|
||||||
this.closest(".add-existing-search-dialog").loadDialog($.get(
|
this.closest(".add-existing-search-dialog").loadDialog($.get(
|
||||||
this.prop("action"), this.serialize()
|
this.prop("action"), this.serialize()
|
||||||
));
|
));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".add-existing-search-dialog .add-existing-search-items a").entwine({
|
$(".add-existing-search-dialog .add-existing-search-items a").entwine({
|
||||||
onclick: function() {
|
onclick: function() {
|
||||||
var link = this.closest(".add-existing-search-items").data("add-link");
|
var link = this.closest(".add-existing-search-items").data("add-link");
|
||||||
var id = this.data("id");
|
var id = this.data("id");
|
||||||
|
|
||||||
var dialog = this.closest(".add-existing-search-dialog")
|
var dialog = this.closest(".add-existing-search-dialog")
|
||||||
.addClass("loading")
|
.addClass("loading")
|
||||||
.children(".ui-dialog-content")
|
.children(".ui-dialog-content")
|
||||||
.empty()
|
.empty();
|
||||||
|
|
||||||
$.post(link, { id: id }, function() {
|
$.post(link, { id: id }, function() {
|
||||||
dialog.data("grid").reload();
|
dialog.data("grid").reload();
|
||||||
dialog.dialog("close");
|
dialog.dialog("close");
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".add-existing-search-dialog .add-existing-search-pagination a").entwine({
|
$(".add-existing-search-dialog .add-existing-search-pagination a").entwine({
|
||||||
onclick: function() {
|
onclick: function() {
|
||||||
this.closest(".add-existing-search-dialog").loadDialog($.get(
|
this.closest(".add-existing-search-dialog").loadDialog($.get(
|
||||||
this.prop("href")
|
this.prop("href")
|
||||||
));
|
));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GridFieldAddNewInlineButton
|
* GridFieldAddNewInlineButton
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$(".ss-gridfield.ss-gridfield-editable").entwine({
|
$(".ss-gridfield.ss-gridfield-editable").entwine({
|
||||||
reload: function(opts, success) {
|
reload: function(opts, success) {
|
||||||
var grid = this;
|
var grid = this;
|
||||||
var added = grid.find("tbody:first").find(".ss-gridfield-inline-new").detach();
|
// Record position of all items
|
||||||
|
var added = [];
|
||||||
this._super(opts, function() {
|
var index = 0; // 0-based index
|
||||||
if(added.length) {
|
grid.find("tbody:first .ss-gridfield-item").each(function() {
|
||||||
added.appendTo(grid.find("tbody:first"));
|
// Record inline items with their original positions
|
||||||
grid.find("tbody:first").children(".ss-gridfield-no-items").hide();
|
if ($(this).is(".ss-gridfield-inline-new")) {
|
||||||
}
|
added.push({
|
||||||
|
'index': index,
|
||||||
if(success) success.apply(grid, arguments);
|
'row': $(this).detach()
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
onpaste: function(e) {
|
index++;
|
||||||
// The following was used as a basis for clipboard data access:
|
});
|
||||||
// http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser
|
|
||||||
var clipboardData = typeof e.originalEvent.clipboardData !== "undefined" ? e.originalEvent.clipboardData : null;
|
this._super(opts, function() {
|
||||||
if (clipboardData) {
|
var body = grid.find("tbody:first");
|
||||||
// Get current input wrapper div class (ie. 'col-Title')
|
$.each(added, function(i, item) {
|
||||||
var input = $(e.target);
|
var row = item['row'],
|
||||||
var inputType = input.attr('type');
|
index = item['index'],
|
||||||
if (inputType === 'text' || inputType === 'email')
|
replaces;
|
||||||
{
|
// Insert at index position
|
||||||
var lastInput = this.find(".ss-gridfield-inline-new:last").find("input");
|
if (index === 0) {
|
||||||
if (input.attr('type') === 'text' && input.is(lastInput) && input.val() === '')
|
body.prepend(row);
|
||||||
{
|
} else {
|
||||||
var inputWrapperDivClass = input.parent().attr('class');
|
// Find item that we could potentially insert this row after
|
||||||
// Split clipboard data into lines
|
replaces = body.find('.ss-gridfield-item:nth-child(' + index + ')');
|
||||||
var lines = clipboardData.getData("text/plain").match(/[^\r\n]+/g);
|
if (replaces.length) {
|
||||||
var linesLength = lines.length;
|
replaces.after(row);
|
||||||
// If there are multiple newlines detected, split the data into new rows automatically
|
} else {
|
||||||
if (linesLength > 1)
|
body.append(row);
|
||||||
{
|
}
|
||||||
var elementsChanged = [];
|
}
|
||||||
for (var i = 1; i < linesLength; ++i)
|
grid.find("tbody:first").children(".ss-gridfield-no-items").hide();
|
||||||
{
|
});
|
||||||
this.trigger("addnewinline");
|
|
||||||
var row = this.find(".ss-gridfield-inline-new:last");
|
if(success) success.apply(grid, arguments);
|
||||||
var rowInput = row.find("."+inputWrapperDivClass).find("input");
|
});
|
||||||
rowInput.val(lines[i]);
|
},
|
||||||
elementsChanged.push(rowInput);
|
onpaste: function(e) {
|
||||||
}
|
// The following was used as a basis for clipboard data access:
|
||||||
// Store the rows added via this method so they can be undo'd.
|
// http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser
|
||||||
input.data('pasteManipulatedElements', elementsChanged);
|
var clipboardData = typeof e.originalEvent.clipboardData !== "undefined" ? e.originalEvent.clipboardData : null;
|
||||||
// To set the current row to not just be all the clipboard data, must wait a frame
|
if (clipboardData) {
|
||||||
setTimeout(function() {
|
// Get current input wrapper div class (ie. 'col-Title')
|
||||||
input.val(lines[0]);
|
var input = $(e.target);
|
||||||
}, 0);
|
var inputType = input.attr('type');
|
||||||
}
|
if (inputType === 'text' || inputType === 'email')
|
||||||
}
|
{
|
||||||
}
|
var lastInput = this.find(".ss-gridfield-inline-new:last").find("input");
|
||||||
}
|
if (input.attr('type') === 'text' && input.is(lastInput) && input.val() === '')
|
||||||
},
|
{
|
||||||
onkeyup: function(e) {
|
var inputWrapperDivClass = input.parent().attr('class');
|
||||||
if (e.keyCode == 90 && e.ctrlKey)
|
// Split clipboard data into lines
|
||||||
{
|
var lines = clipboardData.getData("text/plain").match(/[^\r\n]+/g);
|
||||||
var target = $(e.target);
|
var linesLength = lines.length;
|
||||||
var elementsChanged = target.data("pasteManipulatedElements");
|
// If there are multiple newlines detected, split the data into new rows automatically
|
||||||
if (typeof elementsChanged !== "undefined" && elementsChanged && elementsChanged.length)
|
if (linesLength > 1)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < elementsChanged.length; ++i)
|
var elementsChanged = [];
|
||||||
{
|
for (var i = 1; i < linesLength; ++i)
|
||||||
elementsChanged[i].closest('tr').remove();
|
{
|
||||||
}
|
this.trigger("addnewinline");
|
||||||
target.data("pasteManipulatedElements", []);
|
var row = this.find(".ss-gridfield-inline-new:last");
|
||||||
}
|
var rowInput = row.find("."+inputWrapperDivClass).find("input");
|
||||||
}
|
rowInput.val(lines[i]);
|
||||||
},
|
elementsChanged.push(rowInput);
|
||||||
onaddnewinline: function(e) {
|
}
|
||||||
if(e.target != this[0]) {
|
// Store the rows added via this method so they can be undo'd.
|
||||||
return;
|
input.data('pasteManipulatedElements', elementsChanged);
|
||||||
}
|
// To set the current row to not just be all the clipboard data, must wait a frame
|
||||||
|
setTimeout(function() {
|
||||||
var tmpl = window.tmpl;
|
input.val(lines[0]);
|
||||||
var row = this.find(".ss-gridfield-add-inline-template:last");
|
}, 0);
|
||||||
var num = this.data("add-inline-num") || 1;
|
}
|
||||||
|
}
|
||||||
tmpl.cache[this[0].id + "ss-gridfield-add-inline-template"] = tmpl(row.html());
|
}
|
||||||
|
}
|
||||||
this.find("tbody:first").append(tmpl(this[0].id + "ss-gridfield-add-inline-template", { num: num }));
|
},
|
||||||
this.find("tbody:first").children(".ss-gridfield-no-items").hide();
|
onkeyup: function(e) {
|
||||||
this.data("add-inline-num", num + 1);
|
if (e.keyCode == 90 && e.ctrlKey)
|
||||||
}
|
{
|
||||||
});
|
var target = $(e.target);
|
||||||
|
var elementsChanged = target.data("pasteManipulatedElements");
|
||||||
$(".ss-gridfield-add-new-inline").entwine({
|
if (typeof elementsChanged !== "undefined" && elementsChanged && elementsChanged.length)
|
||||||
onclick: function() {
|
{
|
||||||
this.getGridField().trigger("addnewinline");
|
for (var i = 0; i < elementsChanged.length; ++i)
|
||||||
return false;
|
{
|
||||||
}
|
elementsChanged[i].closest('tr').remove();
|
||||||
});
|
}
|
||||||
|
target.data("pasteManipulatedElements", []);
|
||||||
$(".ss-gridfield-delete-inline").entwine({
|
}
|
||||||
onclick: function() {
|
}
|
||||||
var msg = ss.i18n._t("GridFieldExtensions.CONFIRMDEL", "Are you sure you want to delete this?");
|
},
|
||||||
|
onaddnewinline: function(e) {
|
||||||
if(confirm(msg)) {
|
if(e.target != this[0]) {
|
||||||
this.parents("tr.ss-gridfield-inline-new:first").remove();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
var tmpl = window.tmpl;
|
||||||
}
|
var row = this.find(".ss-gridfield-add-inline-template:last");
|
||||||
});
|
var num = this.data("add-inline-num") || 1;
|
||||||
|
|
||||||
/**
|
tmpl.cache[this[0].id + "ss-gridfield-add-inline-template"] = tmpl(row.html());
|
||||||
* GridFieldAddNewMultiClass
|
|
||||||
*/
|
this.find("tbody:first").append(tmpl(this[0].id + "ss-gridfield-add-inline-template", { num: num }));
|
||||||
|
this.find("tbody:first").children(".ss-gridfield-no-items").hide();
|
||||||
$(".ss-gridfield-add-new-multi-class .ss-ui-button").entwine({
|
this.data("add-inline-num", num + 1);
|
||||||
onclick: function() {
|
|
||||||
var link = this.data("href");
|
// Rebuild sort order fields
|
||||||
var cls = this.parents(".ss-gridfield-add-new-multi-class").find("select").val();
|
$(".ss-gridfield-orderable tbody").rebuildSort();
|
||||||
|
}
|
||||||
if(cls && cls.length) {
|
});
|
||||||
this.getGridField().showDetailView(link.replace("{class}", cls));
|
|
||||||
}
|
$(".ss-gridfield-add-new-inline").entwine({
|
||||||
|
onclick: function() {
|
||||||
return false;
|
this.getGridField().trigger("addnewinline");
|
||||||
}
|
return false;
|
||||||
});
|
}
|
||||||
|
});
|
||||||
$(".ss-gridfield-add-new-multi-class select").entwine({
|
|
||||||
onadd: function() {
|
$(".ss-gridfield-delete-inline").entwine({
|
||||||
this.update();
|
onclick: function() {
|
||||||
},
|
var msg = ss.i18n._t("GridFieldExtensions.CONFIRMDEL", "Are you sure you want to delete this?");
|
||||||
onchange: function() {
|
|
||||||
this.update();
|
if(confirm(msg)) {
|
||||||
},
|
this.parents("tr.ss-gridfield-inline-new:first").remove();
|
||||||
update: function() {
|
}
|
||||||
var btn = this.parents(".ss-gridfield-add-new-multi-class").find(".ss-ui-button");
|
|
||||||
|
return false;
|
||||||
if(this.val() && this.val().length) {
|
}
|
||||||
btn.button("enable");
|
});
|
||||||
} else {
|
|
||||||
btn.button("disable");
|
/**
|
||||||
}
|
* GridFieldAddNewMultiClass
|
||||||
}
|
*/
|
||||||
});
|
|
||||||
|
$(".ss-gridfield-add-new-multi-class .ss-ui-button").entwine({
|
||||||
/**
|
onclick: function() {
|
||||||
* GridFieldEditableColumns
|
var link = this.data("href");
|
||||||
*/
|
var cls = this.parents(".ss-gridfield-add-new-multi-class").find("select").val();
|
||||||
|
|
||||||
$('.ss-gridfield.ss-gridfield-editable .ss-gridfield-item td').entwine({
|
if(cls && cls.length) {
|
||||||
onclick: function(e) {
|
this.getGridField().showDetailView(link.replace("{class}", cls));
|
||||||
// Prevent the default row click action when clicking a cell that contains a field
|
}
|
||||||
if (this.find('.editable-column-field').length) {
|
|
||||||
e.stopPropagation();
|
return false;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
$(".ss-gridfield-add-new-multi-class select").entwine({
|
||||||
/**
|
onadd: function() {
|
||||||
* GridFieldOrderableRows
|
this.update();
|
||||||
*/
|
},
|
||||||
|
onchange: function() {
|
||||||
$(".ss-gridfield-orderable tbody").entwine({
|
this.update();
|
||||||
onadd: function() {
|
},
|
||||||
var self = this;
|
update: function() {
|
||||||
|
var btn = this.parents(".ss-gridfield-add-new-multi-class").find(".ss-ui-button");
|
||||||
var helper = function(e, row) {
|
|
||||||
return row.clone()
|
if(this.val() && this.val().length) {
|
||||||
.addClass("ss-gridfield-orderhelper")
|
btn.button("enable");
|
||||||
.width("auto")
|
} else {
|
||||||
.find(".col-buttons")
|
btn.button("disable");
|
||||||
.remove()
|
}
|
||||||
.end();
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
var update = function() {
|
/**
|
||||||
var grid = self.getGridField();
|
* GridFieldEditableColumns
|
||||||
|
*/
|
||||||
var data = grid.getItems().map(function() {
|
|
||||||
return { name: "order[]", value: $(this).data("id") };
|
$('.ss-gridfield.ss-gridfield-editable .ss-gridfield-item td').entwine({
|
||||||
});
|
onclick: function(e) {
|
||||||
|
// Prevent the default row click action when clicking a cell that contains a field
|
||||||
if (grid.data("immediate-update"))
|
if (this.find('.editable-column-field').length) {
|
||||||
{
|
e.stopPropagation();
|
||||||
grid.reload({
|
}
|
||||||
url: grid.data("url-reorder"),
|
}
|
||||||
data: data.get()
|
});
|
||||||
});
|
|
||||||
}
|
/**
|
||||||
else
|
* GridFieldOrderableRows
|
||||||
{
|
*/
|
||||||
// Tells the user they have unsaved changes when they
|
|
||||||
// try and leave the page after sorting, also updates the
|
$(".ss-gridfield-orderable tbody").entwine({
|
||||||
// save buttons to show the user they've made a change.
|
rebuildSort: function() {
|
||||||
var form = $('.cms-edit-form');
|
var grid = this.getGridField();
|
||||||
form.addClass('changed');
|
|
||||||
}
|
// Get lowest sort value in this list (respects pagination)
|
||||||
};
|
var minSort = null;
|
||||||
|
grid.getItems().each(function() {
|
||||||
this.sortable({
|
// get sort field
|
||||||
handle: ".handle",
|
var sortField = $(this).find('.ss-orderable-hidden-sort');
|
||||||
helper: helper,
|
if (sortField.length) {
|
||||||
opacity: .7,
|
var thisSort = sortField.val();
|
||||||
update: update
|
if (minSort === null && thisSort > 0) {
|
||||||
});
|
minSort = thisSort;
|
||||||
},
|
} else if (thisSort > 0) {
|
||||||
onremove: function() {
|
minSort = Math.min(minSort, thisSort);
|
||||||
if(this.data('sortable')) {
|
}
|
||||||
this.sortable("destroy");
|
}
|
||||||
}
|
});
|
||||||
}
|
minSort = Math.max(1, minSort);
|
||||||
});
|
|
||||||
|
// With the min sort found, loop through all records and re-arrange
|
||||||
$(".ss-gridfield-orderable .ss-gridfield-previouspage, .ss-gridfield-orderable .ss-gridfield-nextpage").entwine({
|
var sort = minSort;
|
||||||
onadd: function() {
|
grid.getItems().each(function() {
|
||||||
var grid = this.getGridField();
|
// get sort field
|
||||||
|
var sortField = $(this).find('.ss-orderable-hidden-sort');
|
||||||
if(this.is(":disabled")) {
|
if (sortField.length) {
|
||||||
return false;
|
sortField.val(sort);
|
||||||
}
|
sort++;
|
||||||
|
}
|
||||||
var drop = function(e, ui) {
|
});
|
||||||
var page;
|
},
|
||||||
|
onadd: function() {
|
||||||
if($(this).hasClass("ss-gridfield-previouspage")) {
|
var self = this;
|
||||||
page = "prev";
|
|
||||||
} else {
|
var helper = function(e, row) {
|
||||||
page = "next";
|
return row.clone()
|
||||||
}
|
.addClass("ss-gridfield-orderhelper")
|
||||||
|
.width("auto")
|
||||||
grid.find("tbody").sortable("cancel");
|
.find(".col-buttons")
|
||||||
grid.reload({
|
.remove()
|
||||||
url: grid.data("url-movetopage"),
|
.end();
|
||||||
data: [
|
};
|
||||||
{ name: "move[id]", value: ui.draggable.data("id") },
|
|
||||||
{ name: "move[page]", value: page }
|
var update = function(event, ui) {
|
||||||
]
|
// If the item being dragged is unsaved, don't do anything
|
||||||
});
|
var postback = true;
|
||||||
};
|
if (ui.item.hasClass('ss-gridfield-inline-new')) {
|
||||||
|
postback = false;
|
||||||
this.droppable({
|
}
|
||||||
accept: ".ss-gridfield-item",
|
|
||||||
activeClass: "ui-droppable-active ui-state-highlight",
|
// Rebuild all sort hidden fields
|
||||||
disabled: this.prop("disabled"),
|
self.rebuildSort();
|
||||||
drop: drop,
|
|
||||||
tolerance: "pointer"
|
// Check if we are allowed to postback
|
||||||
});
|
var grid = self.getGridField();
|
||||||
},
|
if (grid.data("immediate-update") && postback)
|
||||||
onremove: function() {
|
{
|
||||||
if(this.hasClass("ui-droppable")) this.droppable("destroy");
|
grid.reload({
|
||||||
}
|
url: grid.data("url-reorder")
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
})(jQuery);
|
else
|
||||||
|
{
|
||||||
|
// Tells the user they have unsaved changes when they
|
||||||
|
// try and leave the page after sorting, also updates the
|
||||||
|
// save buttons to show the user they've made a change.
|
||||||
|
var form = $('.cms-edit-form');
|
||||||
|
form.addClass('changed');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.sortable({
|
||||||
|
handle: ".handle",
|
||||||
|
helper: helper,
|
||||||
|
opacity: .7,
|
||||||
|
update: update
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onremove: function() {
|
||||||
|
if(this.data('sortable')) {
|
||||||
|
this.sortable("destroy");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$(".ss-gridfield-orderable .ss-gridfield-previouspage, .ss-gridfield-orderable .ss-gridfield-nextpage").entwine({
|
||||||
|
onadd: function() {
|
||||||
|
var grid = this.getGridField();
|
||||||
|
|
||||||
|
if(this.is(":disabled")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var drop = function(e, ui) {
|
||||||
|
var page;
|
||||||
|
|
||||||
|
if($(this).hasClass("ss-gridfield-previouspage")) {
|
||||||
|
page = "prev";
|
||||||
|
} else {
|
||||||
|
page = "next";
|
||||||
|
}
|
||||||
|
|
||||||
|
grid.find("tbody").sortable("cancel");
|
||||||
|
grid.reload({
|
||||||
|
url: grid.data("url-movetopage"),
|
||||||
|
data: [
|
||||||
|
{ name: "move[id]", value: ui.draggable.data("id") },
|
||||||
|
{ name: "move[page]", value: page }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.droppable({
|
||||||
|
accept: ".ss-gridfield-item",
|
||||||
|
activeClass: "ui-droppable-active ui-state-highlight",
|
||||||
|
disabled: this.prop("disabled"),
|
||||||
|
drop: drop,
|
||||||
|
tolerance: "pointer"
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onremove: function() {
|
||||||
|
if(this.hasClass("ui-droppable")) this.droppable("destroy");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})(jQuery);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script type="text/x-tmpl" class="ss-gridfield-add-inline-template">
|
<script type="text/x-tmpl" class="ss-gridfield-add-inline-template">
|
||||||
<tr class="ss-gridfield-inline-new">
|
<tr class="ss-gridfield-item ss-gridfield-inline-new">
|
||||||
<% loop $Me %>
|
<% loop $Me %>
|
||||||
<% if $IsActions %>
|
<% if $IsActions %>
|
||||||
<td$Attributes>
|
<td$Attributes>
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
<span class="handle"><span class="icon"></span></span>
|
<span class="handle"><span class="icon"></span></span>
|
||||||
|
$SortField
|
||||||
|
|
|
@ -32,13 +32,18 @@ class GridFieldOrderableRowsTest extends SapphireTest {
|
||||||
);
|
);
|
||||||
|
|
||||||
$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
|
$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
|
||||||
$desiredOrder = array_reverse($originalOrder);
|
$desiredOrder = array();
|
||||||
|
|
||||||
|
// Make order non-contiguous, and 1-based
|
||||||
|
foreach(array_reverse($originalOrder) as $index => $id) {
|
||||||
|
$desiredOrder[$index * 2 + 1] = $id;
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertNotEquals($originalOrder, $desiredOrder);
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
||||||
|
|
||||||
$reflection->invoke($orderable, $grid, $desiredOrder);
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
||||||
|
|
||||||
$newOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
|
$newOrder = $parent->MyManyMany()->sort('ManyManySort')->map('ManyManySort', 'ID')->toArray();
|
||||||
|
|
||||||
$this->assertEquals($desiredOrder, $newOrder);
|
$this->assertEquals($desiredOrder, $newOrder);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user