// %fv: udsp.js-13 % %dc: Wed Jul 08 15:02:41 2009 %

/******************************************************************************
COPYRIGHT
    (C) 2008 Compuware Corporation.  All rights reserved.
    Unpublished - rights reserved under the Copyright Laws of the United States.

    U.S. GOVERNMENT RIGHTS-Use, duplication, or disclosure by the
    U.S. Government is subject to restrictions as set forth in
    Compuware Corporation license agreement and as
    provided in DFARS 227.7202-1(a) and 227.7202-3(a) (1995),
    DFARS 252.227-7013(c)(1)(ii)(OCT 1988), FAR 12.212(a) (1995),
    FAR 52.227-19, or FAR 52.227-14 (ALT III), as applicable.
    Compuware Corporation

    This product contains confidential information and trade
    secrets of Compuware Corporation.  Use disclosure, or
    reproduction is prohibited
    without the prior express written permission of Compuware Corporation.
******************************************************************************/

/*global UNIFACE document */

/*******************************************************************************
date   refnum    version who description
090528 c27361    9401c1  mzu RIA: Introduce this new widget for DSP mashup.
date   refnum    version who description
*******************************************************************************/


///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.elem2
// The plain HTML widget base class.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.elem2 = function(fieldID) {
    // Call base class
    UNIFACE.widget.AbstractWidget.apply(this, arguments);

    this.controlType = "text";
    this.element = null;
    this.parent = null;
    this.tagName = "span";
};

UNIFACE.widget.elem2.prototype = new UNIFACE.widget.AbstractWidget();
    
UNIFACE.widget.elem2.prototype.getElement = function() {
    return this.element;
};

UNIFACE.widget.elem2.prototype.doRender = function(a_placeHolder) {
    this.parent = a_placeHolder.parentNode;
    this.element = this.createElement(a_placeHolder);
    this.fillAttributes();
    this.element.id = this.callBack.getId();
    this.setElementValue(this.callBack.getValue());
    this.element.style.cssText = a_placeHolder.style.cssText;
    this.parent.replaceChild(this.element, a_placeHolder);
    this.fillStyles();
    this.setElementResource(this.resource);
    this.wrapperNode = this.element;
};

UNIFACE.widget.elem2.prototype.setElementResource = function(aRes) {
};

UNIFACE.widget.elem2.prototype.setElementValue = function(aVal) {
    this.element.value = aVal;
};

UNIFACE.widget.elem2.prototype.setValue = function(aVal) {
    // Cache the value if element was not rendered yet....
    if (this.element) {
        this.setElementValue(aVal);
    } 
};

UNIFACE.widget.elem2.prototype.getValue = function() {
    return this.element ? this.element.value : this.callBack.getValue();
};

UNIFACE.widget.elem2.prototype.fillAttributes = function() {
};

UNIFACE.widget.elem2.prototype.fillStyles = function() {
};

UNIFACE.widget.elem2.prototype.createElement = function(a_placeHolder) {
    if (a_placeHolder.tagName.toLowerCase() == this.tagName) {
        return a_placeHolder.cloneNode(true);
    } else {
        return document.createElement(this.tagName);
    }
};

UNIFACE.widget.elem2.prototype.getDomNode = function() {
    return this.element;
};

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.embeddedDSP
// The generic HTML widget.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.checkIf = function(b) {
	if ( b ) {
		if ( !(true && b) ) {
			alert("Boolean error!");
		}
	} else {
		if ( true && b ) {
			alert("Boolean error!");
		}
	}
	return (true && b);
};

UNIFACE.widget.embeddedDSP = function(fieldID) {
    UNIFACE.widget.elem2.apply(this, arguments);
    this._value = null;    // Private by convention (not by nature).
};

UNIFACE.widget.embeddedDSP.prototype = new UNIFACE.widget.elem2();

UNIFACE.widget.embeddedDSP.prototype.setValue = function(aVal) {
	var v = aVal.toUpperCase();
	
	if ( UNIFACE.checkIf (this._value !== v) ) {
		var oldV = this._value;
	    this._value = v;
	    if ( this.element != null ) { // pragma(allow-loose-compare)
	    	this.unbindingDSP(oldV);
	    	this.bindingDSP(v);
            if (typeof(this.onChange) === "function") {
                this.onChange();
            }                       
	    }
	}
};

UNIFACE.widget.embeddedDSP.prototype.getValue = function() {
    return this._value;
};

UNIFACE.widget.embeddedDSP.prototype.getDspName = function(anInstanceName) {
	var lProperties = this.callBack.getProperties();
    var componentName = "";
  	if ( lProperties.uniface && lProperties.uniface.componentname ) {
   		componentName = lProperties.uniface.componentname;
   	} else {
   		componentName = anInstanceName;
   	}
	return componentName;
};

UNIFACE.widget.embeddedDSP.prototype.unbindingDSP = function(anInstanceName) {
	if ( typeof anInstanceName !== "string" || anInstanceName === "" ) {
		return;
	}
	
	var cp = UNIFACE.luv.getComponent(anInstanceName);
	cp.hideTargetNode();
	this.detachOperation(anInstanceName);
	this.dsp = undefined;
    cp.widget = undefined;
};

UNIFACE.widget.embeddedDSP.prototype.bindingDSP = function(anInstanceName) {
	var cp = null;
    if ( typeof anInstanceName === "string" && anInstanceName !== "" ) {
	    cp = UNIFACE.luv.getComponent(anInstanceName, this.getDspName(anInstanceName));
        if (true && cp.widget) { 
            var l_w = cp.widget;
            l_w.setValue("");
        }
        this.loadDSP(cp);
    } 
};

UNIFACE.widget.embeddedDSP.prototype.preRender = function(a_placeholder) {
};

UNIFACE.widget.embeddedDSP.prototype.postRender = function(a_placeholder) {
    if (this._value && this.dsp === undefined)
    {
        this.bindingDSP(this._value);
	}
};

UNIFACE.widget.elem2.prototype.createElement = function(a_placeHolder) {
        return document.createElement(this.tagName);

};

UNIFACE.widget.embeddedDSP.prototype.unrender = function() {
	this.setValue("");
};

UNIFACE.widget.embeddedDSP.prototype.loadDSP = function(anInstance) {
    anInstance.widget = this;
    this.dsp = anInstance;
    if ( anInstance.targetNode === undefined ) { // pragma(allow-loose-compare)
        UNIFACE.dl.loadDSP(anInstance, "embed");
    }
    else
    {
        this.doAttach(anInstance);
    }
};

UNIFACE.widget.embeddedDSP.prototype.setLayout = function(anInstance) {
        anInstance.showTargetNode(this.element);
};
	
UNIFACE.widget.embeddedDSP.prototype.doAttach = function(anInstance) {
        this.setLayout(anInstance);
        this.attachOperation();    
};
    	
UNIFACE.widget.embeddedDSP.prototype.attachOperation = function()
{
    UNIFACE.dl.initialCommand( { macro: {  instancename : this._value, params: {}, operationName:"attach"} });
    UNIFACE.dl.commands.run();
};
        
UNIFACE.widget.embeddedDSP.prototype.detachOperation = function(anInstanceName)
{
    UNIFACE.dl.initialCommand( { macro: {  instancename : anInstanceName, params: {}, operationName:"detach"} });
    UNIFACE.dl.commands.run();
};

UNIFACE.widget.embeddedDSP.prototype.mapEvents = function() {
    this.onChange = this.callBack.bindEvent("onchange", true);
    UNIFACE.eventMapper.map(this.element, "ondblclick", this, "ondblclick");
    UNIFACE.eventMapper.map(this.element, "onclick", this, "onclick");
    UNIFACE.eventMapper.map(this.element, "onfocus", this, "onfocus");
    UNIFACE.eventMapper.map(this.element, "onblur", this, "onblur");
    UNIFACE.eventMapper.map(this.element, "onkeypress", this, "onkeypress");
};

UNIFACE.addWidgetCreator("UNIFACE.widget.embeddedDSP",
                         function(fieldID, properties) {
                            return new UNIFACE.widget.embeddedDSP(fieldID);
                         });


