mirror of
https://github.com/silverstripe/silverstripe-widgets
synced 2024-10-22 17:05:54 +02:00
Fix: #127 - generate unique ID upon creation
Fixes front end components that require a unique ID. Duplicate IDs in the DOM is invalid HTML. Fix: #127 - code formatting Check validation before writing BUG fixes duplicate ID issue Fixes #127 BUG fixes duplicate ID issue Fixes #127 remove unneeded write() Fix: #127 - generate unique ID upon creation
This commit is contained in:
parent
90d6402185
commit
987d29341e
@ -209,22 +209,25 @@ class Widget extends DataObject
|
|||||||
/**
|
/**
|
||||||
* @return FieldList
|
* @return FieldList
|
||||||
*/
|
*/
|
||||||
public function CMSEditor()
|
public function CMSEditor()
|
||||||
{
|
{
|
||||||
$fields = $this->getCMSFields();
|
$fields = $this->getCMSFields();
|
||||||
$outputFields = new FieldList();
|
$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) {
|
foreach ($fields as $field) {
|
||||||
$name = $field->getName();
|
$name = $field->getName();
|
||||||
$value = $this->getField($name);
|
$value = $this->getField($name);
|
||||||
if ($value) {
|
if ($value) {
|
||||||
$field->setValue($value);
|
$field->setValue($value);
|
||||||
}
|
}
|
||||||
$name = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->ID . "][\\1]", $name);
|
$namefiltered = preg_replace("/([A-Za-z0-9\-_]+)/", "Widget[" . $this->FormID . "][\\1]", $name);
|
||||||
$field->setName($name);
|
|
||||||
|
$field->setName($namefiltered);
|
||||||
$outputFields->push($field);
|
$outputFields->push($field);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $outputFields;
|
return $outputFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,22 +24,7 @@
|
|||||||
placeholder: 'ui-state-highlight',
|
placeholder: 'ui-state-highlight',
|
||||||
forcePlaceholderSize: true
|
forcePlaceholderSize: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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
|
// Ensure correct sort values are written when page is saved
|
||||||
// TODO Adjust to new event listeners
|
// TODO Adjust to new event listeners
|
||||||
$('.cms-container').bind('submitform', function(e) {parentRef.beforeSave(e)});
|
$('.cms-container').bind('submitform', function(e) {parentRef.beforeSave(e)});
|
||||||
@ -58,17 +43,13 @@
|
|||||||
if (!widget.rewritten && (widget.id || widget.name)) {
|
if (!widget.rewritten && (widget.id || widget.name)) {
|
||||||
if (widget.id && widget.id.indexOf('Widget[') === 0) {
|
if (widget.id && widget.id.indexOf('Widget[') === 0) {
|
||||||
var newValue = widget.id.replace(/Widget\[/, 'Widget['+name+'][');
|
var newValue = widget.id.replace(/Widget\[/, 'Widget['+name+'][');
|
||||||
//console.log('Renaming '+widget.tagName+' ID '+widget.id+' to '+newValue);
|
|
||||||
widget.id = newValue;
|
widget.id = newValue;
|
||||||
}
|
}
|
||||||
if (widget.name && widget.name.indexOf('Widget[') === 0) {
|
if (widget.name && widget.name.indexOf('Widget[') === 0) {
|
||||||
var newValue = widget.name.replace(/Widget\[/, 'Widget['+name+'][');
|
var newValue = widget.name.replace(/Widget\[/, 'Widget['+name+'][');
|
||||||
//console.log('Renaming '+widget.tagName+' Name '+widget.name+' to '+newValue);
|
|
||||||
widget.name=newValue;
|
widget.name=newValue;
|
||||||
}
|
}
|
||||||
widget.rewritten='yes';
|
widget.rewritten='yes';
|
||||||
}else {
|
|
||||||
//console.log('Skipping '+(widget.id ? widget.id : (widget.name ? widget.name : 'unknown '+widget.tagName)));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -130,13 +111,8 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
insertWidgetEditor: function(response) {
|
insertWidgetEditor: function(response) {
|
||||||
var usedWidgets = $('#usedWidgets-'+$(this).attr('name')).children();
|
var newID = $(response).find('.formid').val();
|
||||||
|
var widgetContent = response.replace(/Widget\[0\]/gi, "Widget[" + (newID) + "]");
|
||||||
// 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) + "]");
|
|
||||||
$('#usedWidgets-'+$(this).attr('name')).append(widgetContent);
|
$('#usedWidgets-'+$(this).attr('name')).append(widgetContent);
|
||||||
|
|
||||||
this.rewriteWidgetAreaAttributes();
|
this.rewriteWidgetAreaAttributes();
|
||||||
|
Loading…
Reference in New Issue
Block a user