/* ===================================================================================================
 * editControl
 * ===================================================================================================
 */
 
/// jsonParameters
/// { fullControlName: <string>,
///   text: <string>,
///   visible: "0" | "1"
///   description: <columnDescriptor>
/// }
editControl_Initialize = function(jsonParameters)
{
	//alert(jsonParameters);
	var P = parseParameters(jsonParameters);
	var c = getControl(P.fullControlName);
	c.description = P.description;
	editControl_setup(c, P.text);
	editControl_updateAttributes(c, P);
};

editControl_Update = function(jsonParameters)
{
	var P = parseParameters(jsonParameters);
	var c = getControl(P.fullControlName);
	editControl_setText(c, P.text);
	editControl_updateAttributes(c, P);
};

editControl_updateAttributes = function(cell, jsonParameters)
{
	var P = parseParameters(jsonParameters);
	// alert(JSON.stringify(P));
	var visible = true;
	var enabled = true;

	if (typeof P.visible != "undefined")
	{
		visible = parseIntEx(P.visible);
		cell.style.visibility = visible ? "inherit" : "hidden";
	}

	if (typeof P.enabled != "undefined")
	{
		enabled = parseIntEx(P.enabled);
		if (enabled)
			cell.setAttribute("contenteditable", "true");
		else
			cell.removeAttribute("contenteditable");
	}

	if (typeof P.backColor != "undefined")
	{
		var winColor = parseIntEx(P.backColor);
		if ((winColor > 0) && (winColor != 16777215)) // rgb(255,255,255)
		{
			var r = winColor & 255;
			var g = (winColor >> 8) & 255;
			var b = (winColor >> 16) & 255;
			var styleColor = "rgb(" + r.toString() + "," + g.toString() + "," + b.toString() + ")";
			cell.style.backgroundColor = styleColor;
		}
	}
};
 
/// cell: html-div-Element
/// descr: <columnDescriptor>
editControl_setup = function(cell, txt)
{
	var descr = cell.description;
	switch(descr.inputType)
	{
		case "Text_Right":
		case "Label_Right":
		case "Integer":
		case "Decimal":
			cell.style.justifyContent = "end";
			break;
		case "Text_Center":
		case "Label_Center":
			cell.style.justifyContent = "center";
			break;
		default:
			cell.style.justifyContent = "start";
			break;
	}

	// im grid ist das onChange-Event auf Cellebene nicht erwuenscht
	var raiseOnChange = cell.getAttribute("class") == "edit";

	cell.setAttribute("contenteditable", "true");
	cell.setAttribute("spellcheck", "false");

	addControlEvent(cell, "focus", function(event) { editControl_onFocus(cell, event); });
	addControlEvent(cell, "blur", function(event) { editControl_onBlur(cell, event); });
	addControlEvent(cell, "paste", function(event) { editControl_onPaste(cell, event, raiseOnChange); });
	addControlEvent(cell, "beforeinput", function(event) { editControl_onBeforeInput(cell, event); });
	addControlEvent(cell, "input", function(event) { editControl_onInput(cell, event, raiseOnChange); });
	addControlEvent(cell, "contextmenu", function(event) { control_onContextMenu(cell, event); });

	if (raiseOnChange)
	{
		// Zeilenhoehe zentrieren
		cell.onAfterResize = function(cell)
		{
			var h = cell.offsetHeight;
			if (h >= 8)	
			{
				h -= 2; //parseIntEx(cell.style.paddingTop) + parseIntEx(cell.style.paddingBottom);
				cell.style.lineHeight = h.toString() + "px";
			}
		};
		cell.onAfterResize(cell);
	}

	cell.enabled = descr.enabled && parseInt(descr.enabled);
	editControl_setText(cell, txt);
};

editControl_setText = function(cell, txt)
{
	cell.innerText = txt;
	cell.title = txt;
};

editControl_getEnabled = function(cell)
{
	return (cell.getAttribute("contenteditable") == "true") && cell.enabled;
};

editControl_onValidate = function(cell, event)
{
	if (cell.changed)
	{
		cell.changed = false;
		if (getBaseClassName(cell).startsWith("grid"))
			gridControl_cellNotify(cell, "onValidate", cell.innerText);
		else
			raiseControlEvent({ msg: "onValidate", control: cell.id, text: cell.innerText });
	}
};

editControl_onFocus = function(cell, event)
{
	cell.changed = false;
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onSetFocus");
	else
		raiseControlEvent({ msg: "onSetFocus", control: cell.id });
};

editControl_onBlur = function(cell, event)
{
	editControl_onValidate(cell, event);
	if (getBaseClassName(cell).startsWith("grid"))
		gridControl_cellNotify(cell, "onKillFocus");
	else
		raiseControlEvent({ msg: "onKillFocus", control: cell.id });
};

editControl_raiseOnChange = function(cell)
{
	raiseControlEvent({ msg: "onChange", control: cell.id, text: cell.innerText });
};

editControl_onBeforeInput = function(cell, event)
{
	switch(event.inputType)
	{
		case "insertText":
			var totalLength = cell.innerText.length + event.data.length;
			if (totalLength > cell.description.limit)
			{
				event.preventDefault();
				return false;
			}
			break;
		
		case "deleteContentForward":
		case "deleteContentBackward":
			break;
			
		case "insertParagraph":
			event.preventDefault();
			editControl_onValidate(cell, event);
			editControl_selectNext(cell);
			return false;

		case "insertLineBreak":
			event.preventDefault();
			editControl_onValidate(cell, event);
			editControl_selectPrev(cell);
			return false;
			
		default:
			alert("editControl_onBeforeInput " + event.inputType);
			event.preventDefault();
			return false;
	}

	//cell.changed = true;
	return true;
};

editControl_onInput = function(cell, event, raiseOnChange)
{
	cell.changed = true;
	if (raiseOnChange) editControl_raiseOnChange(cell);
	return true;
};

editControl_onPaste = function(cell, event, raiseOnChange)
{
	// paste plain only
	var pastedText = (event.clipboardData || window.clipboardData).getData('text');
	var descr = cell.description;
	var limit = (typeof descr != "undefined") && (typeof descr.limit != "undefined") ? parseIntEx(descr.limit) : -1;
	if (control_replaceSelection(cell, pastedText, limit))
	{
		cell.changed = true;
		if (raiseOnChange) editControl_raiseOnChange(cell);
	}
	event.preventDefault();
};

editControl_findNext = function(cell)
{
	var className = getBaseClassName(cell);
	for (var e = cell.nextElementSibling; e; e = e.nextElementSibling)
		if ((getBaseClassName(e) == className) && editControl_getEnabled(e))
			return e;

	// ersten der naechsten Zeile
	if (className.startsWith("grid"))
	{
		var nextRow = cell.parentElement.nextElementSibling;
		if (nextRow)
			for (e = nextRow.firstElementChild; e; e = e.nextElementSibling)
				if ((getBaseClassName(e) == className) && editControl_getEnabled(e))
					return e;
	}
	return null;
};

editControl_findPrev = function(cell)
{
	var className = getBaseClassName(cell);
	for (var e = cell.previousElementSibling; e; e = e.previousElementSibling)
		if ((getBaseClassName(e) == className) && editControl_getEnabled(e))
			return e;
		
	// ersten der vorherigen Zeile
	if (className.startsWith("grid"))
	{
		var prevRow = cell.parentElement.previousElementSibling;
		if (prevRow)
			for (e = prevRow.firstElementChild; e; e = e.nextElementSibling)
				if ((getBaseClassName(e) == className) && editControl_getEnabled(e))
					return e;
	}
	return null;
};

editControl_selectNext = function(cell)
{
	var next = editControl_findNext(cell);
	if (!next) 
		next = editControl_findPrev(cell);
	if (next) 
		next.focus();
};

editControl_selectPrev = function(cell)
{
	var next = editControl_findPrev(cell);
	if (!next) 
		next = editControl_findNext(cell);
	if (next) 
		next.focus();
};

