// %fv: uhtmlcontrols.js-43 % %dc: Tue Jun 09 12:04:22 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.
******************************************************************************/

/*******************************************************************************
date   refnum    version who description
090205 c27248    9.ajax  jdk acceskeys do not work & labels etc show the %sign
090305 c27328    9.ajax  fd  Move common code up in widget class inheritance tree
date   refnum    version who description
*******************************************************************************/

/*global UNIFACE document */

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.element
// The plain HTML widget base class.
///////////////////////////////////////////////////////////////////////////////
(function() {

UNIFACE.widget.element = function(fieldID) {
    // Call base class
    UNIFACE.widget.AbstractWidget.apply(this, arguments);
    this.ackey = 0; // @access remember for rendering
    this.controlType = "text";
    this.element = null;
    this.parent = null;
    this.tagName = "span";
};

UNIFACE.widget.element.prototype = new UNIFACE.widget.AbstractWidget();


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

UNIFACE.widget.element.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.element.prototype.setElementResource = function(aRes) {
};

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

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

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

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

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

UNIFACE.widget.element.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.element.prototype.getDomNode = function() {
    return this.element;
};

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.genericHTML
// The generic HTML widget.
///////////////////////////////////////////////////////////////////////////////

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

UNIFACE.widget.genericHTML.prototype = new UNIFACE.widget.element();

UNIFACE.widget.genericHTML.prototype.setValue = function(aVal) {
    this._value = aVal;
};

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

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

UNIFACE.widget.genericHTML.prototype.doRender = function(aPlaceholder) {
    this.element = aPlaceholder;   // Already 'rendered'.
    this.wrapperNode = aPlaceholder;
};

UNIFACE.widget.genericHTML.prototype.postRender = function() {
};

UNIFACE.widget.genericHTML.prototype.mapEvents = function() {
    // There is no natural mapping for which event to map to "detail".
    // Therefore onclick and ondblclick are mapped like this:
    UNIFACE.eventMapper.map(this.element, "ondblclick", this, "ondblclick");
    UNIFACE.eventMapper.map(this.element, "onclick", this, "onclick");

    // Do we really want mappings for all of these?
    //UNIFACE.eventMapper.map(this.element, "onmousedown", this, "onmousedown");
    //UNIFACE.eventMapper.map(this.element, "onmouseup", this, "onmouseup");
    //UNIFACE.eventMapper.map(this.element, "onmouseover", this, "onmouseover");
    //UNIFACE.eventMapper.map(this.element, "onmousemove", this, "onmousemove");
    //UNIFACE.eventMapper.map(this.element, "onmouseout", this, "onmouseout");
    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.eventMapper.map(this.element, "onkeydown", this, "onkeydown");
    //UNIFACE.eventMapper.map(this.element, "onkeyup", this, "onkeyup");
    //UNIFACE.eventMapper.map(this.element, "onselect", this, "onselect");
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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


///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.label
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.label = function(labelID) {
    // Call base class
    UNIFACE.widget.element.apply(this, arguments);
    this.associatedId = "ufld:" + labelID.replace(/^ulbl:/, "");
    this.tagName = "label";
};

UNIFACE.widget.label.prototype = new UNIFACE.widget.element();

UNIFACE.widget.label.prototype.setElementValue = function(aValue) {
    this.element.value = aValue;
    // Set the label's value, as defined in the label definition.
    this.element.innerHTML = aValue;
};

UNIFACE.widget.label.prototype.setValue = function(aVal) {
    if (this.element) {
        this.setElementValue(this.accesskey(aVal, true, true));
    }
};

UNIFACE.widget.label.prototype.postRender = function() { //@access
};

UNIFACE.widget.label.prototype.doRender = function(aPlaceholder) {
    this.parent = aPlaceholder.parentNode;
    if (aPlaceholder.tagName.toLowerCase() == "label") {
        this.element = aPlaceholder;
    } else {
        this.element = document.createElement("label");
        this.parent.replaceChild(this.element, aPlaceholder);
    }
    // Associate the label with the appropriate field, if applicable.
    if (this.associatedId != undefined) { // pragma(allow-loose-compare)
        this.element.htmlFor = this.associatedId;
    }
    if (this.ackey !== 0){
    	this.element.accessKey = this.ackey;
    }     
    this.setElementValue(this.accesskey(this.callBack.getValue(), true, true));
    
};

UNIFACE.widget.label.prototype.mapEvents = function() {
    // No events to be mapped!
};

UNIFACE.addWidgetCreator("UNIFACE.widget.label",
                         function(labelID, properties) {
                            return new UNIFACE.widget.label(labelID);
                         });

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.input
// The HTML text box.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.input = function(fieldID) {
    // Call base class
    UNIFACE.widget.element.apply(this, arguments);
    this.controlType = "text";
    this.tagName = "input";
};

UNIFACE.widget.input.prototype = new UNIFACE.widget.element();

UNIFACE.widget.input.prototype.createElement = function(a_placeHolder) {
    if (a_placeHolder.tagName.toLowerCase() == this.tagName && this.placeholder.type == this.controlType) {
        return a_placeHolder.cloneNode(true);
    } else {
        return document.createElement(this.tagName);
    }        
};

UNIFACE.widget.input.prototype.setProperties = function() {
    // First call the base class' implementation.
    // TODO: is there a less ugly way of doing this?
    UNIFACE.widget.element.prototype.setProperties.call(this);
    
    if (typeof this.element == "object" &&  this.element.style != "undefined")
    {
        var l_props = this.callBack.getProperties();
        var a;
        for (a in l_props) if (l_props.hasOwnProperty(a))
        {
            if (a.toLowerCase() == "disabled")
            {
                this.element.disabled = l_props[a].toLowerCase() === "true" ? true : false;
                break;
            }
        }
    }
};

UNIFACE.widget.input.prototype.fillAttributes = function () {
    this.element.type = this.controlType;
};

UNIFACE.widget.input.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "ondblclick", this, "detail");
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.multilinebox
// The HTML multi-line text box.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.multilinebox = function(fieldID) {
    UNIFACE.widget.element.apply(this, arguments);
    this.tagName = "textarea";
};

UNIFACE.widget.multilinebox.prototype = new UNIFACE.widget.element();

UNIFACE.widget.multilinebox.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "ondblclick", this, "detail");
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.editbox
// The HTML single-line edit box.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.addWidgetCreator("UNIFACE.widget.editbox",
                         function(fieldID) {
                            return new UNIFACE.widget.input(fieldID);
                         });

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.password
// The HTML password control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.password = function(fieldID) {
    // Call base class
    UNIFACE.widget.input.apply(this, arguments);
    this.controlType = "password";
};

UNIFACE.widget.password.prototype = new UNIFACE.widget.input();

UNIFACE.widget.password.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "ondblclick", this, "detail");
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.checkbox
// The HTML checkbox control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.checkbox = function(fieldID) {
    // Call base class
    UNIFACE.widget.input.apply(this, arguments);
    this.controlType = "checkbox";
};

UNIFACE.widget.checkbox.prototype = new UNIFACE.widget.input();

UNIFACE.widget.checkbox.prototype.setElementValue = function(el, val) {
    el.checked = !!val;    
};
                
UNIFACE.widget.checkbox.prototype.getValue = function() {
    return this.element ? this.element.checked : this.callBack.getValue();    
};

UNIFACE.widget.checkbox.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.button
// The HTML button control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.button = function(fieldID) {
    UNIFACE.widget.input.apply(this, arguments);
    this.controlType = "button";
};
 
UNIFACE.widget.button.prototype = new UNIFACE.widget.input();

UNIFACE.widget.button.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onclick", this, "detail");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.img
// The HTML image control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.img = function(fieldID) {
    // Call base class
    UNIFACE.widget.element.apply(this, arguments);
    this.tagName = "img";
};

UNIFACE.widget.img.prototype = new UNIFACE.widget.element();

UNIFACE.widget.img.prototype.createElement = function(a_placeHolder) {
    var syntax = this.callBack.getSyntax();
    var isPromptable = syntax.YPR || !syntax.NPR;
    // If the field is 'promptable' then create an anchor with an image inside;
    // otherwise just create an image.
    this.tagName = isPromptable ? "input" : "img";
    var element;
    if (a_placeHolder.tagName.toLowerCase() == this.tagName) {
        element = a_placeHolder.cloneNode(true);
    } else {
        element = document.createElement(this.tagName);
    }
    if (this.tagName == "input") {
        element.type = "image";
    }
    return element;
};

UNIFACE.widget.img.prototype.setTriggerProp = function(triggerName, triggerType) {
    if (triggerName === "detail" && this.tagName !== "input") {
        // Change the cursor to 'pointer', if it is not already set.
        if (!this.element.style.cursor) {
            if (UNIFACE.luv.util.isValidRequestType(triggerType)) {
                this.element.style.cursor = "pointer";
            } else {
                this.element.style.cursor = "default";
            }
        }
    }
};

UNIFACE.widget.img.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onclick", this, "detail");
};

UNIFACE.widget.element.prototype.setElementValue = function(aVal) {
    var prevValue = this.getValue();
    if (prevValue !== undefined && aVal != prevValue) {
        // Clear the resource, in case it was previously set to something else (@CR27251).
        // Note: this assumes that, for images, setValue is called *before* setResource.
        this.setElementResource("");
    }
    if (this.element) {
        this.element.value = aVal;
    }
};

UNIFACE.widget.img.prototype.setElementResource = function(aRes) {
    if (this.element) {
        this.element.src = aRes;
    }
};

UNIFACE.widget.img.prototype.setResource = function(aRes) {
    this.resource = aRes;
    if (this.element) {
        this.setElementResource(aRes);
    }
    return this.callBack.getValue();
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.img
// The HTML plain text control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.plain = function(fieldID) {
    // Call base class
    UNIFACE.widget.element.apply(this, arguments);
};

UNIFACE.widget.plain.prototype = new UNIFACE.widget.element();

UNIFACE.widget.plain.prototype.setUnifaceProp = function(a_prop, a_value) {
    if (a_prop === "rawhtml") {
        a_value = (a_value.toLowerCase() == "true"); // pragma(allow-loose-compare)
        if (this.isRawHTML != a_value) {
            this.isRawHTML = a_value;
            // Setting this propery has effect on how the value is displayed.
            // Therefore redisplay the value, by calling setElementValue.
            this.setElementValue(this.element.value);
        }
    }
};

UNIFACE.widget.plain.prototype.createElement = function(a_placeHolder) {
    var syntax = this.callBack.getSyntax();
    var isPromptable = syntax.YPR || !syntax.NPR;
    // If the field is 'promptable' then create an anchor;
    // otherwise just create an span.
    this.tagName = isPromptable ? "a" : "span";
    var element;
    if (a_placeHolder.tagName.toLowerCase() == this.tagName) {
        element = a_placeHolder.cloneNode(true);
    } else {
        element = document.createElement(this.tagName);
    }
    if (this.tagName == "a") {
        element.href = "javascript://";
    }
    return element;
};

UNIFACE.widget.plain.prototype.setElementValue = function(aValue) {
    this.element.value = aValue;
    if (this.isRawHTML) {
        this.element.innerHTML = aValue;
    } else {
        this.element.innerHTML = "";
        var l_lines = aValue.split("\n");
        if (l_lines.length > 0) {
            this.element.appendChild(document.createTextNode(l_lines[0]));
        }
        for (var i = 1; i < l_lines.length; i++) {
            this.element.appendChild(document.createElement("br")); // preserve line breaks
            if (l_lines[i]) {
                this.element.appendChild(document.createTextNode(l_lines[i]));
            }
        }
    }
};

UNIFACE.widget.plain.prototype.setTriggerProp = function(triggerName, triggerType) {
    if (triggerName === "detail" && this.tagName !== "a") {
    	// Change the cursor to 'pointer', if it is not already set.
    	if (!this.element.style.cursor) {
            if (UNIFACE.luv.util.isValidRequestType(triggerType)) {
                this.element.style.cursor = "pointer";
            } else {
                this.element.style.cursor = "default";
            }
    	}
    }
};

UNIFACE.widget.plain.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onclick", this, "detail");
};

UNIFACE.widget.plain.prototype.postRender = function() {
    // Empty.
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.select
// The HTML select control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.select = function(fieldID) {
    // Call base class
    UNIFACE.widget.element.apply(this, arguments);
    this.tagName = "select";
};

UNIFACE.widget.select.prototype = new UNIFACE.widget.element();

UNIFACE.widget.select.prototype.setValrep = function(aValrep) {
    if (!this.element) {
        return;
    }
    while (this.element.firstChild) {
        this.element.removeChild(this.element.firstChild);
    }
    
    var a;
    if (typeof aValrep == 'object' && aValrep){
        for (a in aValrep) if (aValrep.hasOwnProperty(a)) {
            var vr = document.createElement("option");
            vr.value = a;
            //vr.text = aValrep[a]; // FF
            vr.appendChild(document.createTextNode(aValrep[a])); // IE
            this.element.appendChild(vr);
        }
    }
};

UNIFACE.widget.select.prototype.fillAttributes = function() {
    this.setValrep(this.callBack.getValrep());
};

UNIFACE.widget.select.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.dropdown
// The HTML dropdown control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.dropdownlist = function(fieldID) {
    // Call base class
    UNIFACE.widget.select.apply(this, arguments);
};

UNIFACE.widget.dropdownlist.prototype = new UNIFACE.widget.select();

UNIFACE.widget.dropdownlist.prototype.setValrep = function(valrep) {
    // Call the base class implementation.
    UNIFACE.widget.select.prototype.setValrep.call(this, valrep);
    this.element.size = 1;
};

UNIFACE.widget.dropdownlist.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

///////////////////////////////////////////////////////////////////////////////
// UNIFACE.widget.listbox
// The HTML listbox control.
///////////////////////////////////////////////////////////////////////////////

UNIFACE.widget.listbox = function(fieldID) {
    // Call base class
    UNIFACE.widget.select.apply(this, arguments);
};

UNIFACE.widget.listbox.prototype = new UNIFACE.widget.select();

UNIFACE.widget.listbox.prototype.postRender = function(a_placeHolder) {
    if (a_placeHolder.size != undefined) { // pragma(allow-loose-compare)
        this.element.size = a_placeHolder.size;
    }
};

UNIFACE.widget.listbox.prototype.mapEvents = function() {
    UNIFACE.eventMapper.map(this.element, "onchange", this, "onchange");
};

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

                         });

}());
