<commit_before>WidgetAreaEditorClass = Class.create();

WidgetAreaEditorClass.prototype = {
	initialize: function() {
		this.name = this.getAttribute('name');
		this.rewriteWidgetAreaAttributes();
		UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
	
		// Make available widgets draggable
		var availableWidgets = $('availableWidgets-'+this.name).childNodes;
		
		for(var i = 0; i < availableWidgets.length; i++) {
			var widget = availableWidgets[i];
			// 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);
					}
				}
			}
		}
	
	
		// Create dummy sortable to prevent javascript errors
		Sortable.create('availableWidgets-'+this.name, {
			tag: 'li',
			handle: 'handle',
			containment: []
		});
		
		// Used widgets are sortable
		Sortable.create('usedWidgets-'+this.name, {
			tag: 'div',
			handle: 'handle',
			containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
			onUpdate: this.updateWidgets
		});
		
		// Figure out maxid, this is used when creating new widgets
		this.maxid = 0;
		
		var usedWidgets = $('usedWidgets-'+this.name).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[2]) > this.maxid) {
					this.maxid = parseInt(widgetid[2]);
				}
			}
		}

		// Ensure correct sort values are written when page is saved
		// TODO Adjust to new event listeners
		jQuery('#Form_EditForm').bind('ajaxsubmit', this.beforeSave.bind(this));
	},
	
	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);
	},
	
	beforeSave: function() {
		// Ensure correct sort values are written when page is saved
		var usedWidgets = $('usedWidgets-'+this.name);
		
		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();
				}
			}
		}
	},
	
	addWidget: function(className, holder) {
		this.name = holder;
		new Ajax.Request('Widget_Controller/EditableSegment/' + className, {
			onSuccess : $('usedWidgets-'+holder).parentNode.parentNode.insertWidgetEditor.bind(this)
		});
	},

	updateWidgets: function() {

		// 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();
		

		// 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 = $('usedWidgets-'+this.name).childNodes;
		for(var i = 0; i < usedWidgets.length; i++) {
			var widget = usedWidgets[i];
			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)
				});
			}
		}
	},
	
	insertWidgetEditor: function(response) {
		// Remove placeholder text
		if($('NoWidgets-'+this.name)) {
			$('usedWidgets-'+this.name).removeChild($('NoWidgets-'+this.name));
		}

		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) + "]");
	
		$('usedWidgets-'+this.name).appendChild(widget.childNodes[0]);
		$('usedWidgets-'+this.name).parentNode.parentNode.rewriteWidgetAreaAttributes();
		UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
		
		// Repply some common form controls
		WidgetTreeDropdownField.applyTo('div.usedWidgets .TreeDropdownField');
		
		
		Sortable.create('usedWidgets-SideBar', {
			tag: 'div',
			handle: 'handle',
			containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
			onUpdate: $('usedWidgets-'+this.name).parentNode.parentNode.updateWidgets
		});
	},<commit_msg>MINOR fixed widget area MINOR added select box listing anchors in text (from r94198)<commit_after>WidgetAreaEditorClass = Class.create();

WidgetAreaEditorClass.prototype = {
	initialize: function() {
		this.name = this.getAttribute('name');
		this.rewriteWidgetAreaAttributes();
		UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
	
		// Make available widgets draggable
		var availableWidgets = $('availableWidgets-'+this.name).childNodes;
		
		for(var i = 0; i < availableWidgets.length; i++) {
			var widget = availableWidgets[i];
			// 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);
					}
				}
			}
		}
	
	
		// Create dummy sortable to prevent javascript errors
		Sortable.create('availableWidgets-'+this.name, {
			tag: 'li',
			handle: 'handle',
			containment: []
		});
		
		// Used widgets are sortable
		Sortable.create('usedWidgets-'+this.name, {
			tag: 'div',
			handle: 'handle',
			containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
			onUpdate: this.updateWidgets
		});
		
		// Figure out maxid, this is used when creating new widgets
		this.maxid = 0;
		
		var usedWidgets = $('usedWidgets-'+this.name).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[2]) > this.maxid) {
					this.maxid = parseInt(widgetid[2]);
				}
			}
		}

		// Ensure correct sort values are written when page is saved
		// TODO Adjust to new event listeners
		jQuery('#Form_EditForm').bind('ajaxsubmit', this.beforeSave.bind(this));
	},
	
	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);
	},
	
	beforeSave: function() {
		// Ensure correct sort values are written when page is saved
		var usedWidgets = $('usedWidgets-'+this.name);
		
		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();
				}
			}
		}
	},
	
	addWidget: function(className, holder) {
		
		if ($('WidgetAreaEditor-'+holder).getAttribute('maxwidgets')) {
			var maxCount = $('WidgetAreaEditor-'+holder).getAttribute('maxwidgets');
			var count = $$('#usedWidgets-'+holder+' .Widget').length;
			if (count > maxCount) {
				alert('Sorry, you have reached the maximum number of widgets in this area');
				return;
			}
		}
		
		
		this.name = holder;
		new Ajax.Request('Widget_Controller/EditableSegment/' + className, {
			onSuccess : $('usedWidgets-'+holder).parentNode.parentNode.insertWidgetEditor.bind(this)
		});
	},

	updateWidgets: function() {

		// 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();
		

		// 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 = $('usedWidgets-'+this.name).childNodes;
		for(var i = 0; i < usedWidgets.length; i++) {
			var widget = usedWidgets[i];
			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)
				});
			}
		}
	},
	
	insertWidgetEditor: function(response) {
		// Remove placeholder text
		if($('NoWidgets-'+this.name)) {
			$('usedWidgets-'+this.name).removeChild($('NoWidgets-'+this.name));
		}

		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) + "]");
	
		$('usedWidgets-'+this.name).appendChild(widget.childNodes[0]);
		$('usedWidgets-'+this.name).parentNode.parentNode.rewriteWidgetAreaAttributes();
		UsedWidget.applyToChildren($('usedWidgets-'+this.name), 'div.Widget');
		
		// Repply some common form controls
		WidgetTreeDropdownField.applyTo('div.usedWidgets .TreeDropdownField');
		
		
		Sortable.create('usedWidgets-'+this.name, {
			tag: 'div',
			handle: 'handle',
			containment: ['availableWidgets-'+this.name, 'usedWidgets-'+this.name],
			onUpdate: $('usedWidgets-'+this.name).parentNode.parentNode.updateWidgets
		});
	},
{'fraction_non_alphanumeric': 0.17281047946930891, 'fraction_numerical': 0.003106893945755311, 'mean_word_length': 3.554493307839388, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792971', 'n_tokens_mistral': 4567, 'n_tokens_neox': 3845, 'n_words': 802}