2009-05-14 08:11:18 +02:00
|
|
|
WidgetAreaEditorClass = Class.create();
|
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() {
|
2009-11-05 01:13:56 +01:00
|
|
|
this.name = this.getAttribute('name');
|
|
|
|
this.rewriteWidgetAreaAttributes();
|
|
|
|
UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
|
2007-08-03 00:07:11 +02:00
|
|
|
|
|
|
|
// Make available widgets draggable
|
2009-11-05 01:13:56 +01:00
|
|
|
var availableWidgets = $('availableWidgets-'+this.name).childNodes;
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
for(var i = 0; i < availableWidgets.length; i++) {
|
|
|
|
var widget = availableWidgets[i];
|
2009-11-05 01:13:56 +01:00
|
|
|
// Don't run on comments, whitespace, etc
|
|
|
|
if (widget.nodeType == 1) {
|
|
|
|
// Gotta change their ID's because otherwise we get clashes between two tabs
|
|
|
|
widget.id = widget.id + '-'+this.name;
|
|
|
|
if(widget.id) {
|
|
|
|
widget.onclick = function(event) {
|
|
|
|
parts = event.currentTarget.id.split('-');
|
|
|
|
var widgetArea = parts.pop();
|
|
|
|
var className = parts.pop();
|
|
|
|
$('WidgetAreaEditor-'+widgetArea).addWidget(className, widgetArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
2009-11-05 01:13:56 +01:00
|
|
|
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// Create dummy sortable to prevent javascript errors
|
2009-11-05 01:13:56 +01:00
|
|
|
Sortable.create('availableWidgets-'+this.name, {
|
2009-05-06 04:54:59 +02:00
|
|
|
tag: 'li',
|
|
|
|
handle: 'handle',
|
|
|
|
containment: []
|
|
|
|
});
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// Used widgets are sortable
|
2009-11-05 01:13:56 +01:00
|
|
|
Sortable.create('usedWidgets-'+this.name, {
|
2009-05-06 04:54:59 +02:00
|
|
|
tag: 'div',
|
|
|
|
handle: 'handle',
|
2009-11-05 01:13:56 +01:00
|
|
|
containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
|
2009-05-06 04:54:59 +02:00
|
|
|
onUpdate: this.updateWidgets
|
|
|
|
});
|
2007-08-03 00:07:11 +02:00
|
|
|
|
|
|
|
// Figure out maxid, this is used when creating new widgets
|
|
|
|
this.maxid = 0;
|
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
var usedWidgets = $('usedWidgets-'+this.name).childNodes;
|
2007-08-03 00:07:11 +02:00
|
|
|
for(var i = 0; i < usedWidgets.length; i++) {
|
|
|
|
var widget = usedWidgets[i];
|
|
|
|
if(widget.id) {
|
2009-11-05 01:13:56 +01:00
|
|
|
widgetid = widget.id.match(/\Widget\[(.+?)\]\[([0-9]+)\]/i);
|
|
|
|
if(widgetid && parseInt(widgetid[2]) > this.maxid) {
|
|
|
|
this.maxid = parseInt(widgetid[2]);
|
|
|
|
}
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 01:13:56 +01:00
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// Ensure correct sort values are written when page is saved
|
|
|
|
$('Form_EditForm').observeMethod('BeforeSave', this.beforeSave.bind(this));
|
|
|
|
},
|
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
rewriteWidgetAreaAttributes: function() {
|
|
|
|
this.name = this.getAttribute('name');
|
|
|
|
|
|
|
|
var monkeyWith = function(widgets, name) {
|
|
|
|
for(var i = 0; i < widgets.length; i++) {
|
|
|
|
widget = widgets[i];
|
|
|
|
if (!widget.getAttribute('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.setAttribute('rewritten', 'yes');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//console.log('Skipping '+(widget.id ? widget.id : (widget.name ? widget.name : 'unknown '+widget.tagName)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monkeyWith($$('#WidgetAreaEditor-'+this.name+' .Widget'), this.name);
|
|
|
|
monkeyWith($$('#WidgetAreaEditor-'+this.name+' .Widget *'), this.name);
|
|
|
|
},
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
beforeSave: function() {
|
|
|
|
// Ensure correct sort values are written when page is saved
|
2009-11-05 01:13:56 +01:00
|
|
|
var usedWidgets = $('usedWidgets-'+this.name);
|
2007-08-03 00:07:11 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
addWidget: function(className, holder) {
|
|
|
|
this.name = holder;
|
|
|
|
new Ajax.Request('Widget_Controller/EditableSegment/' + className, {
|
|
|
|
onSuccess : $('usedWidgets-'+holder).parentNode.parentNode.insertWidgetEditor.bind(this)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
updateWidgets: function() {
|
2009-11-05 01:13:56 +01:00
|
|
|
|
|
|
|
// Gotta get the name of the current dohickey based off the ID
|
|
|
|
this.name = this.element.id.split('-').pop();
|
|
|
|
|
|
|
|
// alert(this.name);
|
|
|
|
|
|
|
|
// Gotta get the name of the current dohickey based off the ID
|
|
|
|
this.name = this.element.id.split('-').pop();
|
|
|
|
|
|
|
|
|
2007-08-03 00:07:11 +02:00
|
|
|
// This is called when an available widgets is dragged over to used widgets.
|
|
|
|
// It inserts the editor form into the new used widget
|
2009-11-05 01:13:56 +01:00
|
|
|
|
|
|
|
var usedWidgets = $('usedWidgets-'+this.name).childNodes;
|
2007-08-03 00:07:11 +02:00
|
|
|
for(var i = 0; i < usedWidgets.length; i++) {
|
|
|
|
var widget = usedWidgets[i];
|
2009-11-05 01:13:56 +01:00
|
|
|
if(widget.id && (widget.id.indexOf("Widget[") != 0) && (widget.id != 'NoWidgets-'+this.name)) {
|
|
|
|
// Need to remove the -$Name part.
|
|
|
|
var wIdArray = widget.id.split('-');
|
|
|
|
wIdArray.pop();
|
|
|
|
|
|
|
|
new Ajax.Request('Widget_Controller/EditableSegment/' + wIdArray.join('-'), {
|
|
|
|
onSuccess : $('usedWidgets-'+this.name).parentNode.parentNode.insertWidgetEditor.bind(this)
|
2009-05-06 04:54:59 +02:00
|
|
|
});
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
insertWidgetEditor: function(response) {
|
|
|
|
// Remove placeholder text
|
2009-11-05 01:13:56 +01:00
|
|
|
if($('NoWidgets-'+this.name)) {
|
|
|
|
$('usedWidgets-'+this.name).removeChild($('NoWidgets-'+this.name));
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
2009-11-05 01:13:56 +01:00
|
|
|
|
|
|
|
var usedWidgets = $('usedWidgets-'+this.name).childNodes;
|
|
|
|
|
|
|
|
// Give the widget a unique id
|
|
|
|
widget = document.createElement('div');
|
|
|
|
widget.innerHTML = response.responseText.replace(/Widget\[0\]/gi, "Widget[new-" + (++$('usedWidgets-'+this.name).parentNode.parentNode.maxid) + "]");
|
2007-08-03 00:07:11 +02:00
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
$('usedWidgets-'+this.name).appendChild(widget.childNodes[0]);
|
|
|
|
$('usedWidgets-'+this.name).parentNode.parentNode.rewriteWidgetAreaAttributes();
|
|
|
|
UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
|
|
|
|
|
|
|
|
Sortable.create('usedWidgets-SideBar', {
|
|
|
|
tag: 'div',
|
|
|
|
handle: 'handle',
|
|
|
|
containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
|
|
|
|
onUpdate: $('usedWidgets-'+this.name).parentNode.parentNode.updateWidgets
|
|
|
|
});
|
2007-08-03 00:07:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sortWidgets: function() {
|
|
|
|
// Order the sort by the order the widgets are in the list
|
2009-11-05 01:13:56 +01:00
|
|
|
var usedWidgets = $('usedWidgets-'+this.name);
|
2007-08-03 00:07:11 +02:00
|
|
|
|
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
|
2009-11-05 01:13:56 +01:00
|
|
|
$('usedWidgets-'+this.name).removeChild(widgetToRemove);
|
|
|
|
// TODO ... re-create NoWidgets div?
|
2007-08-03 00:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-05 01:13:56 +01:00
|
|
|
// Loop over all WidgetAreas and fire 'em up
|
|
|
|
var wAs = $$('.WidgetAreaEditor');
|
|
|
|
for(var i = 0; i < wAs.length; i++) {
|
|
|
|
WidgetAreaEditorClass.applyTo('div#'+wAs[i].id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|