2009-05-14 08:11:18 +02:00
|
|
|
WidgetAreaEditorClass = Class.create();
|
|
|
|
WidgetAreaEditorClass.applyTo('div.WidgetAreaEditor');
|
2007-08-03 00:07:11 +02:00
|
|
|
|
2009-05-14 08:11:18 +02:00
|
|
|
WidgetAreaEditorClass.prototype = {
|
2007-08-03 00:07:11 +02:00
|
|
|
initialize: function() {
|
|
|
|
UsedWidget.applyToChildren($('WidgetAreaEditor_usedWidgets'), 'div.Widget');
|
|
|
|
|
|
|
|
// Make available widgets draggable
|
|
|
|
var availableWidgets = $('WidgetAreaEditor_availableWidgets').childNodes;
|
|
|
|
for(var i = 0; i < availableWidgets.length; i++) {
|
|
|
|
var widget = availableWidgets[i];
|
|
|
|
if(widget.id)
|
|
|
|
new Draggable(widget.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create dummy sortable to prevent javascript errors
|
2009-05-06 04:54:59 +02:00
|
|
|
Sortable.create('WidgetAreaEditor_availableWidgets', {
|
|
|
|
tag: 'li',
|
|
|
|
handle: 'handle',
|
|
|
|
containment: []
|
|
|
|
});
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// Used widgets are sortable
|
2009-05-06 04:54:59 +02:00
|
|
|
Sortable.create('WidgetAreaEditor_usedWidgets', {
|
|
|
|
tag: 'div',
|
|
|
|
handle: 'handle',
|
|
|
|
containment: ['WidgetAreaEditor_availableWidgets', 'WidgetAreaEditor_usedWidgets'],
|
|
|
|
onUpdate: this.updateWidgets
|
|
|
|
});
|
2007-08-03 00:07:11 +02:00
|
|
|
|
|
|
|
// Figure out maxid, this is used when creating new widgets
|
|
|
|
this.maxid = 0;
|
|
|
|
|
|
|
|
var usedWidgets = $('WidgetAreaEditor_usedWidgets').childNodes;
|
|
|
|
for(var i = 0; i < usedWidgets.length; i++) {
|
|
|
|
var widget = usedWidgets[i];
|
|
|
|
if(widget.id) {
|
|
|
|
widgetid = widget.id.match(/Widget\[([0-9]+)\]/i);
|
|
|
|
if(widgetid && parseInt(widgetid[1]) > this.maxid)
|
|
|
|
this.maxid = parseInt(widgetid[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure correct sort values are written when page is saved
|
|
|
|
$('Form_EditForm').observeMethod('BeforeSave', this.beforeSave.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeSave: function() {
|
|
|
|
// Ensure correct sort values are written when page is saved
|
|
|
|
var usedWidgets = $('WidgetAreaEditor_usedWidgets');
|
|
|
|
|
|
|
|
if(usedWidgets) {
|
|
|
|
this.sortWidgets();
|
|
|
|
|
|
|
|
var children = usedWidgets.childNodes;
|
|
|
|
|
|
|
|
for( var i = 0; i < children.length; ++i ) {
|
|
|
|
var child = children[i];
|
|
|
|
|
|
|
|
if(child.beforeSave) {
|
|
|
|
child.beforeSave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateWidgets: function() {
|
|
|
|
// This is called when an available widgets is dragged over to used widgets.
|
|
|
|
// It inserts the editor form into the new used widget
|
|
|
|
var usedWidgets = $('WidgetAreaEditor_usedWidgets').childNodes;
|
|
|
|
for(var i = 0; i < usedWidgets.length; i++) {
|
|
|
|
var widget = usedWidgets[i];
|
2009-05-06 04:54:59 +02:00
|
|
|
if(widget.id && (widget.id.indexOf("Widget[") != 0) && (widget.id != 'NoWidgets')) {
|
|
|
|
new Ajax.Request('Widget_Controller/EditableSegment/' + widget.id, {
|
|
|
|
onSuccess : $('WidgetAreaEditor_usedWidgets').parentNode.parentNode.insertWidgetEditor.bind(this)
|
|
|
|
});
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
insertWidgetEditor: function(response) {
|
|
|
|
// Remove placeholder text
|
|
|
|
if($('NoWidgets')) {
|
|
|
|
$('WidgetAreaEditor_usedWidgets').removeChild($('NoWidgets'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the new widget
|
|
|
|
var usedWidgets = $('WidgetAreaEditor_usedWidgets').childNodes;
|
|
|
|
for(var i = 0; i < usedWidgets.length; i++) {
|
|
|
|
var widget = usedWidgets[i];
|
|
|
|
if(widget.id && (widget.id.indexOf("Widget[") != 0)) {
|
|
|
|
// Clone the widget so we can put it back in the available widgets column
|
|
|
|
clone = widget.cloneNode(true);
|
|
|
|
|
|
|
|
// Give the widget a unique id
|
|
|
|
widget.innerHTML = response.responseText.replace(/Widget\[0\]/gi, "Widget[new-" + (++$('WidgetAreaEditor_usedWidgets').parentNode.parentNode.maxid) + "]");
|
|
|
|
|
|
|
|
// Replace the available widget with the used widget with editor form
|
2009-01-05 07:17:59 +01:00
|
|
|
widget.parentNode.insertBefore($(widget).getElementsByClassName('Widget')[0], widget);
|
2007-08-03 00:07:11 +02:00
|
|
|
widget.parentNode.removeChild(widget);
|
|
|
|
|
|
|
|
// Put the clone into the available widgets column
|
|
|
|
$('WidgetAreaEditor_availableWidgets').appendChild(clone);
|
|
|
|
|
|
|
|
// Reapply behaviour
|
|
|
|
new Draggable(clone.id);
|
2009-05-06 04:54:59 +02:00
|
|
|
|
|
|
|
Sortable.create('WidgetAreaEditor_usedWidgets', {
|
|
|
|
tag: 'div',
|
|
|
|
handle: 'handle',
|
|
|
|
containment: ['WidgetAreaEditor_availableWidgets', 'WidgetAreaEditor_usedWidgets'],
|
|
|
|
onUpdate: $('WidgetAreaEditor_usedWidgets').parentNode.parentNode.updateWidgets
|
|
|
|
});
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
UsedWidget.applyToChildren($('WidgetAreaEditor_usedWidgets'), 'div.Widget');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sortWidgets: function() {
|
|
|
|
// Order the sort by the order the widgets are in the list
|
|
|
|
var usedWidgets = $('WidgetAreaEditor_usedWidgets');
|
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
if(usedWidgets) {
|
2007-08-03 00:07:11 +02:00
|
|
|
widgets = usedWidgets.childNodes;
|
|
|
|
|
2008-12-04 23:38:58 +01:00
|
|
|
for(i = 0; i < widgets.length; i++) {
|
|
|
|
var div = widgets[i];
|
|
|
|
|
|
|
|
if(div.nodeName != '#comment') {
|
|
|
|
var fields = div.getElementsByTagName('input');
|
|
|
|
|
|
|
|
for(j = 0; field = fields.item(j); j++) {
|
|
|
|
if(field.name == div.id + '[Sort]') {
|
|
|
|
field.value = i;
|
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:38:58 +01:00
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteWidget: function(widgetToRemove) {
|
|
|
|
// Remove a widget from the used widgets column
|
|
|
|
$('WidgetAreaEditor_usedWidgets').removeChild(widgetToRemove);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UsedWidget = Class.create();
|
|
|
|
|
|
|
|
UsedWidget.prototype = {
|
|
|
|
initialize: function() {
|
|
|
|
// Call deleteWidget when delete button is pushed
|
|
|
|
this.deleteButton = this.findDescendant('span', 'widgetDelete');
|
|
|
|
if(this.deleteButton)
|
|
|
|
this.deleteButton.onclick = this.deleteWidget.bind(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Taken from FieldEditor
|
|
|
|
findDescendant: function(tag, clsName, element) {
|
|
|
|
if(!element)
|
|
|
|
element = this;
|
|
|
|
|
|
|
|
var descendants = element.getElementsByTagName(tag);
|
|
|
|
|
|
|
|
for(var i = 0; i < descendants.length; i++) {
|
|
|
|
var el = descendants[i];
|
|
|
|
|
|
|
|
if(tag.toUpperCase() == el.tagName && el.className.indexOf( clsName ) != -1)
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteWidget: function() {
|
|
|
|
this.parentNode.parentNode.parentNode.deleteWidget(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|