Merge pull request #128 from torleif/patch-2

This commit is contained in:
Daniel Hensby 2016-07-14 00:24:26 +01:00
commit 36ccdf2ebc
No known key found for this signature in database
GPG Key ID: E38EC566FE29EB66
2 changed files with 39 additions and 55 deletions

View File

@ -16,6 +16,7 @@ class Widget extends DataObject
* @var array
*/
private static $db = array(
"Title" => "Varchar(255)",
"Title" => "Varchar(255)",
"Sort" => "Int",
"Enabled" => "Boolean",
@ -102,6 +103,7 @@ class Widget extends DataObject
if ($holder) {
return $this->WidgetHolder();
}
return $this->Content();
}
@ -203,6 +205,7 @@ class Widget extends DataObject
new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
);
$this->extend('updateCMSFields', $fields);
return $fields;
}
@ -214,14 +217,19 @@ class Widget extends DataObject
$fields = $this->getCMSFields();
$outputFields = new FieldList();
$this->FormID = $this->FormID ?: uniqid();
$outputFields->push(HiddenField::create('Widget[' . $this->FormID . '][FormID]', 'FormID',
$this->FormID)->addExtraClass('formid'));
foreach ($fields as $field) {
$name = $field->getName();
$value = $this->getField($name);
if ($value) {
$field->setValue($value);
}
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
$field->setName($name);
$namefiltered = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->FormID . "][\\1]", $name);
$field->setName($namefiltered);
$outputFields->push($field);
}

View File

@ -39,21 +39,6 @@
}
});
// Figure out maxid, this is used when creating new widgets
$(this).data('maxid', 0);
var usedWidgets = $(this).find('.usedWidgets .Widget');
usedWidgets.each(function() {
var widget = $(this)[0];
if(widget.id) {
widgetid = widget.id.match(/Widget\[(.+?)\]\[([0-9]+)\]/i);
if(widgetid && parseInt(widgetid[2]) > parseInt(parentRef.data('maxid'))) {
parentRef.data('maxid', parseInt(widgetid[2]));
}
}
});
// Ensure correct sort values are written when page is saved
// TODO Adjust to new event listeners
$('.cms-container').bind('submitform', function(e) {parentRef.beforeSave(e)});
@ -72,17 +57,13 @@
if (!widget.rewritten && (widget.id || widget.name)) {
if (widget.id && widget.id.indexOf('Widget[') === 0) {
var newValue = widget.id.replace(/Widget\[/, 'Widget['+name+'][');
//console.log('Renaming '+widget.tagName+' ID '+widget.id+' to '+newValue);
widget.id = newValue;
}
if (widget.name && widget.name.indexOf('Widget[') === 0) {
var newValue = widget.name.replace(/Widget\[/, 'Widget['+name+'][');
//console.log('Renaming '+widget.tagName+' Name '+widget.name+' to '+newValue);
widget.name=newValue;
}
widget.rewritten='yes';
}else {
//console.log('Skipping '+(widget.id ? widget.id : (widget.name ? widget.name : 'unknown '+widget.tagName)));
}
});
}
@ -144,13 +125,8 @@
},
insertWidgetEditor: function(response) {
var usedWidgets = $('#usedWidgets-'+$(this).attr('name')).children();
// Give the widget a unique id
var newID=parseInt($(this).data('maxid'))+1;
$(this).data('maxid', newID);
var widgetContent = response.replace(/Widget\[0\]/gi, "Widget[new-" + (newID) + "]");
var newID = $(response).find('.formid').val();
var widgetContent = response.replace(/Widget\[0\]/gi, "Widget[" + (newID) + "]");
$('#usedWidgets-'+$(this).attr('name')).append(widgetContent);
this.rewriteWidgetAreaAttributes();