/*
*+------------------------------------------------------------------------+
*| Licensed Materials - Property of IBM
*| BI and PM: prmt
*| (C) Copyright IBM Corp. 2002, 2011, 2020
*|
*| US Government Users Restricted Rights - Use, duplication or
*| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*|
*+------------------------------------------------------------------------+
*/
/**
Should be accessed through cognos.Report.prompt.
@fileOverview Managing Prompts.
@namespace cognos.Prompt
@class Manager class for prompts.
*/
cognos.Prompt = function()
{
this.m_aControls = [];
this.m_aParameters = [];
};
/**
Adds a prompt control to the manager.
@private
@param {object} v_oProps properties of the object
@param {object} v_oExtra Extra properties to add to the object, like CVId.
@return {cognos.Prompt.Control} Newly created object.
*/
cognos.Prompt.prototype.F_Add = function( v_oProps, v_oExtra )
{
Object.f_extend( v_oProps, v_oExtra );
var v_o = this.f_create( v_oProps );
if ( v_o )
{
v_o.f_setPromptManager(this);
this.m_aControls.push( v_o );
window[ v_o.f_getId("p_") ] = v_o;
var cv = v_o.f_getCV();
if (cv)
{
if (typeof cv.setHasPrompts == "function")
{
cv.setHasPrompts(true);
}
var v_aPP = cv.preProcessControlArray;
if ( v_aPP )
{
v_aPP.push( v_o );
}
}
}
return ( v_o );
};
/**
Adds a prompt control to the manager.
@private
@param {object} v_oProps properties of the object
@param {object} v_oExtra Extra properties to add to the object, like CVId.
@return {cognos.Prompt.Control} Newly created object.
*/
cognos.Prompt.prototype.f_create = function( v_oProps )
{
var v_o = null;
if ( v_oProps )
{
var isHTML5 = this.isHTML5(v_oProps);
if ( v_oProps["@range"] )
{
v_o = new cognos.Prompt.Control.Range( v_oProps, this );
} else if ( v_oProps["@parameterizedTree"] && ( v_oProps["@parameterizedTree"] == true )) {
v_o = new cognos.Prompt.Control.PickTree( v_oProps );
} else {
switch( v_oProps.n )
{
case "textBox": v_o = new cognos.Prompt.Control.Text( v_oProps ); break;
case "selectFile": v_o = new cognos.Prompt.Control.SelectFile( v_oProps ); break;
case "selectValue": v_o = new cognos.Prompt.Control.SelectValue( v_oProps ); break;
case "selectInterval": v_o = new cognos.Prompt.Control.Interval( v_oProps, this ); break;
case "selectDate":
v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5Date( v_oProps ) : new cognos.Prompt.Control.Date( v_oProps ));
break;
case "selectTime":
v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5Time( v_oProps ) : new cognos.Prompt.Control.Time( v_oProps ));
break;
case "selectDateTime":
v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5DateTime( v_oProps ) : new cognos.Prompt.Control.DateTime( v_oProps ));
break;
case "selectWithTree": v_o = new cognos.Prompt.Control.Tree( v_oProps ); break;
case "selectWithSearch": v_o = new cognos.Prompt.Control.Search( v_oProps ); break;
}
}
}
return ( v_o );
};
cognos.Prompt.prototype.browserSupportsHTML5Input = function(sInputType)
{
var bIsSupported = true;
var inputEle = document.createElement("input");
switch(sInputType)
{
case "selectDate":
inputEle.setAttribute("type", "date");
bIsSupported = inputEle.type !== "text";
break;
case "selectTime":
inputEle.setAttribute("type", "time");
bIsSupported = inputEle.type !== "text";
break;
case "selectDateTime":
/*PRMT doesn't use the HTML5 datetime or datetime-local input type, instead we generate a composite control composed of HTML5 date and time controls, so we can support a datetime control as long as both the individual HTML5 date and time input types are supported.*/
inputEle.setAttribute("type", "date");
bIsSupported = inputEle.type !== "text";
inputEle.setAttribute("type", "time");
bIsSupported = (inputEle.type !== "text") && bIsSupported;
break;
}
return bIsSupported;
};
cognos.Prompt.prototype.isHTML5 = function( v_oProps )
{
var result = false;
if ((typeof G_PRMT_HTML5PromptControls) != K_PRMT_sUNDEFINED && G_PRMT_HTML5PromptControls) {
result = true;
}
else {
var reHTML5 = new RegExp("^_html5", "im");
if ( v_oProps["@name"] && ( v_oProps["@name"].match(reHTML5) ) ) {
result = true;
}
}
return result;
}
/**
Returns the control object associated to a name.
@param {String} sName Prompt Control ID. It could be the name property set for the control in Report Studio.
@return {cognos.Prompt.Control} Object associated to sName. Return null if it doesn't exists.
*/
cognos.Prompt.prototype.getControlByName = function(sName)
{
return this.getPromptControl(sName);
};
/**
Returns an array of all controls associated to this report.
@return {cognos.Prompt.Control[]}
*/
cognos.Prompt.prototype.getControls = function() { return this.m_aControls; };
/**
Returns the parameter object associated to a name.
@param {String} sName Parameter Name.
@return {cognos.Prompt.Parameter} Parameter associated to sName. Creates a new Parameter is it doesn't exists.
*/
cognos.Prompt.prototype.getParameterByName = function(sName)
{
if ( sName instanceof cognos.Prompt.Parameter )
{
return sName;
}
for (var v_idx = 0; v_idx < this.m_aParameters.length; v_idx++)
{
if ( this.m_aParameters[v_idx].getName() == sName )
{
return (this.m_aParameters[v_idx]);
}
}
var v_oParameter = new cognos.Prompt.Parameter(sName);
this.m_aParameters.push(v_oParameter);
return v_oParameter;
};
/**
Returns an array of all parameters associated to this report.
@return {cognos.Prompt.Parameter[]}
*/
cognos.Prompt.prototype.getParameters = function() { return this.m_aParameters; };
/**
Returns the object associated to an ID.
@private
@param {string} sID Prompt Control ID. It could be the name property set for the control in Report Studio.
@return {cognos.Prompt.Control} Object associated to sID. Return null if it doesn't exists.
*/
cognos.Prompt.prototype.getPromptControl = function( sID )
{
var v_aPrompts = this.m_aControls;
var v_oPrompt = null;
for (var v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
{
var v_oTemp = v_aPrompts[ v_idx ];
var v_sID = v_oTemp.f_getId();
if ( v_sID == sID ) {
v_oPrompt = v_oTemp;
break;
}
}
// didn't find using _id_, try with @id
if ( v_oPrompt === null )
{
for (v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
{
var v_oTemp = v_aPrompts[ v_idx ];
var v_sID = v_oTemp["@id"];
if ( v_sID == sID ) {
v_oPrompt = v_oTemp;
break;
}
}
}
// try with @name
if ( v_oPrompt === null )
{
for (v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
{
var v_oTemp = v_aPrompts[ v_idx ];
var v_sID = v_oTemp["@name"];
if ( v_sID == sID ) {
v_oPrompt = v_oTemp;
break;
}
}
}
return v_oPrompt;
};
/**
Return true if parameter sParameterName is satisfied.
@private
@param {String} [sParameterName] Parameter to validate. If omitted, returns true if ALL parameters are valid.
@return {boolean}
*/
cognos.Prompt.prototype.isValid = function( sParameterName )
{
var v_bValid = true;
for ( var v_idx = 0; v_bValid && v_idx < this.m_aParameters.length; v_idx++ ) {
v_bValid &= this.m_aParameters[ v_idx ].isValid();
}
return v_bValid;
};
/**
Associate a custom control to a parameter.
** For future use - It's only here to show our intention to support this kind of action - Still need to be worked out.
@param {String|cognos.Prompt.Parameter} parameter Parameter or Parameter Name to associate with the control.
@param {cognos.Prompt.Control} control The control.
@return {void}
@example var ctrl = new CustomMapControl(); // based on a cognos.Prompt.Control
var prmtr = cognos.Prompt.getParameterByName('Country');
cognos.Prompt.registerControl( prmtr, ctrl );
*/
cognos.Prompt.prototype.registerControl = function(parameter, control)
{
var v_oParameter = this.getParameterByName( parameter );
if ( v_oParameter )
{
v_oParameter.f_addControl( control );
}
this.m_aControls.push( control );
};
/**
Returns XML representation for parameter names and values.
@private
@return {string} XML string.
*/
cognos.Prompt.prototype.toXML = function( )
{
var v_aXML = [];
for ( var v_idx = 0; v_idx < this.m_aParameters.length; v_idx++ )
{
var sName = this.m_aParameters[v_idx].getName();
v_aXML.push('');
v_aXML.push( this.m_aParameters[v_idx].getXML() );
v_aXML.push('');
}
return ( v_aXML.join(K_PRMT_sEMPTY) );
};
var C_PromptManager = cognos.Prompt; // Keep old reference for backward compatibility with custom scripts.