JavaScript Toolkit

 

 

This page contains the source code to a range of JavaScript code elements that we have found useful when developing EIS pages for Brio Intelligence. The bulk of the code is made up of a range of utility functions but we have also included some code fragments to show the way these can be used. An appreciation of the standards we use for Brio development is recommended – document details available on request.

The code is grouped into a number of areas – the table of contents below shows the overall organisation

 

General utility functions.
showDeveloperView .
Logon functions
reconnect
showOCEDetails.
sample code for a Process button. 3
Controlling limits.
setLimitValue.
deselectVarLimits.
limitByWeek.
limitByDay.
copyLimit 
copyLimit2DD.
copyDD2Limit
Validation functions.
valDateNumMonth.
valDateTxtMonth
Formatting functions
formatToDP.
formatCurrency.
fmtDateNumMonth
autofitAllCols
Miscellaneous.
makeCode128.

 

General utility functions

showDeveloperView

 Show or hide the toolbars, section catalog etc. We also hide all sections that end users are not likely to use – usually the query sections but could also include results sets.  

This version also creates default file export names – the file name for export is defined as a global and we use different default export directories in production and test.

 

/*----------------------------------------------------------
-- Function: showDeveloperView
-- Parameters: abShow - true/false
--
-- Turn catalog, toolbars etc on/off according to parameter
-----------------------------------------------------------*/
function showDeveloperView(abShow){

var nSecNum; //Number of Sections
var nSecCount; //Section count

ShowSectionTitleBar = true;
ShowMenuBar = true;
ShowCatalog = abShow;
ShowStatusBar = abShow;

Toolbars["Sections"].Visible = abShow;
Toolbars["Formatting"].Visible = abShow;
Toolbars["Standard"].Visible = abShow;

//Loop turning section properties on/off
nSecNum = ActiveDocument.Sections.Count;
for (nSecCount = 1; nSecCount <= nSecNum; nSecCount++){

   if ( (ActiveDocument.Sections[nSecCount].Type != bqEIS) && (ActiveDocument.Sections[nSecCount].Type != bqReport) ) {
      ActiveDocument.Sections[nSecCount].Visible = abShow;
   }
   ActiveDocument.Sections[nSecCount].ShowOutliner = abShow;
} //end for

//Developer objects on Selector
Sections["Selector"].Shapes["cbGlobals"].Visible = abShow;
Sections["Selector"].Shapes["cbGlobals"].Enabled = abShow;
Sections["Selector"].Shapes["cbTest"].Visible = abShow;
Sections["Selector"].Shapes["cbTest"].Enabled = abShow;

//Set export file location
g_sFileName = abShow ? g_sDevExportDir : g_sPrdExportDir
g_sFileName += g_sExportName
Console.Writeln('Export file '+ g_sFileName)

}
g_showDeveloperView = showDeveloperView;  // define global version

Home

 

Logon functions

 

reconnect

Connects to database – will disconnect first. Assumes globals for User ID and Password have been defined in the EIS – these are supplied as parameters.

 

/*----------------------------------------------------------------------------------------------------------
-- Function: reconnect
-- Result: boolean true if connected, else false
-- Parameters: asQuery - Section Name
-- asId - Userid
-- asPw - Password
--
-- Disconnect and reconnect query to database
-----------------------------------------------------------------------------------------------------------*/
function reconnect( asQuery, asId, asPw) {

var sOCEName = ActiveDocument. Sections[ asQuery].DataModel.Connection.Filename


//Disconnect from database
ActiveDocument.Sections[ asQuery ].DataModel.Connection.Disconnect();

//Reload OCE in case details have changed
ActiveDocument. Sections[ asQuery ].DataModel.Connection.Open(sOCEName);

// Set Query connection parameters
ActiveDocument.Sections[ asQuery ].DataModel.Connection.Username = asId;
ActiveDocument.Sections[ asQuery ].DataModel.Connection.SetPassword( asPw );

// Log Query on to database
try{
ActiveDocument.Sections[ asQuery ].DataModel.Connection.Connect();
} catch(errConnect) {
Alert('Database connection error: '+errConnect.toString())
return false;
}

// Connected OK
return true;

}
g_reconnect = reconnect;  // define global version

 

Home

 

showOCEDetails

We will often show the name of the OCE being used on an EIS page. We will also show the SID (for Oracle databases). This is helpful in a production environment where database locations can vary and a query using the wrong database can cause problems.

 

/*----------------------------------------------------------------------------------------------------------
-- Function: showOCEDetails
-- Result: 
-- Parameters: asQuery - Section Name
-- asLabel - Label on Logon page to update
--
-- updates OCE details on Logon page
-----------------------------------------------------------------------------------------------------------*/
function showOCEDetails(asQuery,asLabel){


ActiveDocument.Sections["Logon"].Shapes[asLabel].Text = ActiveDocument.Sections[asQuery].DataModel.Connection.Filename + ' 
- ' +ActiveDocument.Sections[asQuery].DataModel.Connection.HostName
Console.Writeln('OCE details updated')

}
g_showOCEDetails = showOCEDetails; // define global version

 

Home

 

sample code for a Process button

//*----------------------------------------------------------------------------------------------------------
-- Project: XYZ
-- Desc: Process queries if connected
--
--ChngLog Author Reason
-- 09Jul01 ANT Created
-----------------------------------------------------------------------------------------------------------*/

//If any query unconnected - go to logon section
if ( ActiveDocument.Sections["Received Query"].DataModel.Connection.Connected == false || 
ActiveDocument.Sections["Returned Query"].DataModel.Connection.Connected 
==false || 
ActiveDocument.Sections["Issued Query"].DataModel.Connection.Connected 
== false ) {
Alert("You are not connected to XYZ - Please logon to Process","Connect Error");
Sections["Logon"].Activate();
return;
} //end connect test


//Disconnect from Queries and then Reconnect - in case of timeout
if( g_reconnect( "Query", g_sXYZId, g_sXYZPassword ) ){
// add limit setting code here
//..
// add limit copying from earlier sections
//..
ActiveDocument.Sections["Query"].Process();
} else {
Sections["Logon"].Activate();
return;
}

 

Home

 

Controlling limits

This is probably the biggest category of utilities. We have found it extremely useful to create a wide range of utility functions to control limit dialogs. We will often rely on a variable limit dialog rather than coding the limit functionality onto the EIS page.

Many of these functions will be varied slightly to suit the query in which they are used – default date ranges being a good example.

 

setLimitValue

A very simple utility to put a selected value into a limit.

/*----------------------------------------------------------------------------------------------------------
-- Function: setLimitValue
-- Parameters: asQueryName, asLimitName, adValue,adOperator
--
-- Set custom limit on <asLimitName> in section <asQueryName>
-- to <adValue> using <adOperator>
-- set single value in limit
-----------------------------------------------------------------------------------------------------------*/
function setLimitValue(asQueryName, asLimitName, advalue,adOperator) {

var oLimit = ActiveDocument.Sections[asQueryName].Limits[asLimitName];

oLimit.Operator = adOperator;
oLimit.SelectedValues.RemoveAll();
oLimit.CustomValues.RemoveAll();

oLimit.CustomValues.Add(adValue);
oLimit.SelectedValues.Add(adValue);


}
g_setLimitValue = setLimitValue; // define global version

 

Home

 

deselectVarLimits

It’s quite helpful to remove previous entries from variable limits – it forces the user to enter something and also avoids the inadvertent use of pre-selected values.

 

/*----------------------------------------------------------------------------------------------------------
-- Function: deselectVarLimits
--
-- Remove SelectedValues for any variable Limits
--
-- 05Dec02 ANT Created
-----------------------------------------------------------------------------------------------------------*/
function deselectVarLimits(){

var nSecNum; //Number of Sections
var nSecCount; //Section count
var oSection; //Section
var nLmt; //Limit Count

//Loop for all sections
nSecNum = ActiveDocument.Sections.Count;
for (nSecCount = 1; nSecCount <= nSecNum; nSecCount++){
oSection = ActiveDocument.Sections[nSecCount];

   if( oSection.Type == bqQuery ) {

//Loop for Limits in Section
      for (nLmt = 1; nLmt <= oSection.Limits.Count; nLmt++){
         if(oSection.Limits[nLmt].VariableLimit){
            oSection.Limits[nLmt].SelectedValues.RemoveAll();
         }
      } //end Limit loop
   } // end if right section type

} //end Section loop
}
g_deselectVarLimits = deselectVarLimits;  // define global version

 

Home

 

limitByWeek

One of a number of default date ranges.

 

/*----------------------------------------------------------------------------------------------------------
-- Function: limitByWeek
-- Parameters: asQueryName, asLimitName, adDate
--
-- Set custom limit on <asLimitName> in section <asQueryName>
-- to <adDate> between 00:00:00 one week ago and 23:59:59 yesterday
-----------------------------------------------------------------------------------------------------------*/
function limitByWeek(asQueryName, asLimitName, adDate) {

// dates are objects &v passed by ref - we'll make two new objects

var dStart = new Date(Date.parse(adDate.toGMTString())) - 7;
var dEnd = new Date(Date.parse(adDate.toGMTString()));
//Start datetime is beginning of supplied date

dStart.setHours(0);
dStart.setMinutes(0);
dStart.setSeconds(0);

//End datetime is 1 second to midnight

dEnd.setHours(23);
dEnd.setMinutes(59);
dEnd.setSeconds(59);

var oLimit = ActiveDocument.Sections[asQueryName].Limits[asLimitName];
oLimit.SelectedValues.RemoveAll();
oLimit.CustomValues.RemoveAll();

oLimit.CustomValues.Add(dStart);
oLimit.CustomValues.Add(dEnd);
oLimit.SelectedValues.Add(dStart);
oLimit.SelectedValues.Add(dEnd);

}
g_limitByWeek = limitByWeek;  // global version

 

Home

 

limitByDay

Set the limit to a specified 24 hour range – usually yesterday.

 

 

/*----------------------------------------------------------------------------------------------------------

--   Function:    limitByDay

-- Parameters:    asQueryName, asLimitName, adDate

--

-- In section <asQueryName>, set custom limit on <asLimitName> to be range

--    between 00:00:00 and 23:59:59 on <adDate>

-----------------------------------------------------------------------------------------------------------*/

function limitByDay(asQueryName, asLimitName, adDate) {

// dates are objects & passed by ref - we'll make two new objects

var dStart     = new Date(Date.parse(adDate.toGMTString()));

var dEnd       = new Date(Date.parse(adDate.toGMTString()));

 

//Start datetime is beginning of supplied date

dStart.setHours(0);

dStart.setMinutes(0);

dStart.setSeconds(0);

 

//End datetime is 1 second to midnight

dEnd.setHours(23);

dEnd.setMinutes(59);

dEnd.setSeconds(59);

 

var oLimit = ActiveDocument.Sections[asQueryName].Limits[asLimitName];

oLimit.SelectedValues.RemoveAll();

oLimit.CustomValues.RemoveAll();

oLimit.CustomValues.Add(dStart);

oLimit.CustomValues.Add(dEnd);

oLimit.SelectedValues.Add(dStart);

oLimit.SelectedValues.Add(dEnd);

 

}     // end of limitByDay definition

 

g_limitByDay = limitByDay;          //Define global version of function

 

Home

 

copyLimit

We often find that we are applying the same limit values to multiple sections – either because we are accessing more than one database or because we are running a particularly complex query. This function will let us use variable limits on one query section and then copy these limit values to other sections before they are processed.

 

/*----------------------------------------------------------------------------------------------------------

--   Function:    copyLimit

-- Parameters:    asSourceSection, asSourceLimitName,

--                            asDestSection,   asDestLimitName

--

-- Copies the selected CUSTOM values from one limit to another, duplicating

--  other settings (Ignore, Operator, Nulls etc)

-- NOTE: Internal Error on attempt to set limit values on a computed column

-----------------------------------------------------------------------------------------------------------*/

function copyLimit(asSourceSection, asSourceLimitName, asDestSection, asDestLimitName) {

var oSourceObj = ActiveDocument.Sections[asSourceSection].Limits[asSourceLimitName]

var oDestObj         = ActiveDocument.Sections[asDestSection].Limits[asDestLimitName]

 

// read source limit settings

var bIgnore =  oSourceObj.Ignore

var bIncludeNulls    = oSourceObj.IncludeNulls

var nNegate = oSourceObj.Negate

var nOperator  = oSourceObj.Operator

var nSelectedValuesCount = oSourceObj.SelectedValues.Count

var nSelVal

 

// set the limits on the second query

if (bIgnore) {

            oDestObj.Ignore= true

}

else {

oDestObj.Ignore = false

   oDestObj.IncludeNulls = bIncludeNulls

   oDestObj.Negate = nNegate

   oDestObj.Operator = nOperator

   oDestObj.SelectedValues.RemoveAll()

   oDestObj.CustomValues.RemoveAll()

 

            for (var i=1; i<=nSelectedValuesCount; i++) {

                  nSelVal=(oSourceObj.SelectedValues[i])

                  oDestObj.CustomValues.Add(nSelVal)

                  oDestObj.SelectedValues.Add(nSelVal)

            } // end for all limit values

 

} // end else

 

}     // end of copyLimit definition

 

g_copyLimit = copyLimit;            //Define global version of function

 

Home

 

copyLimit2DD

Copies limit values to a dropdown. We also allow the top value (i.e. the one seen by the user first) to be preset.

 

/*----------------------------------------------------------------------------------------------------------

--   Function:    copyLimit2DD

-- Parameters:    asSrcSct    - source section name (table or results) on which limit is set

--                            asSrcLmt    - source limit name

--                            asTgtSct    - target section

--                            asTgtDD     - target dropdown name

--                            asTopLbl    - label to add to top of list

--

-- Populate drop down (or list box) with available limit values

-- & add supplied value to top

--

-- v1.0  01Jan02  Maddox Ford Created

------------------------------------------------------------------------------------------------------------*/

function copyLimit2DD(asSrcSct, asSrcLmt, asTgtSct, asTgtDD, asTopLbl){

//use pointers for clarity

var oLimit = ActiveDocument.Sections[asSrcSct].Limits[asSrcLmt];

var oDD =  ActiveDocument.Sections[asTgtSct].Shapes[asTgtDD];

 

//empty the dropdown

oDD.RemoveAll();

 

//Reset the limit values

oLimit.RefreshAvailableValues();

var nAvailValues = oLimit.AvailableValues.Count;

 

//update the dropdown

oDD.Add( asTopLbl );

for (var iVal=1; iVal <= nAvailValues ; iVal++){

oDD.Add(oLimit.AvailableValues[iVal]);

}

}     // end of copyLimit2DD definition

 

g_copyLimit2DD = copyLimit2DD;            //Define global version of function

 

Home

 

copyDD2Limit

Copies a dropdown selection to a limit. Should work on a multi-select list box too.

 

/*----------------------------------------------------------------------------------------------------------

--   Function:    copyDD2Limit

-- Parameters:    asSrcSct    - source EIS section name, containing dropdown

--                            asSrcDD     - source dropdown name

--                            asTgtSct    - target section name (table or results)

--                            asTgtLmt    - target limit name

--

-- Populate limit with values selected from DD

--

-- v1.0  18Feb02  Maddox Ford Created

------------------------------------------------------------------------------------------------------------*/

function copyDD2Limit(asSrcSct, asSrcDD, asTgtSct, asTgtLmt){

//use pointers for clarity

var      oDD =  ActiveDocument.Sections[asSrcSct].Shapes[asSrcDD];

var oLimit = ActiveDocument.Sections[asTgtSct].Limits[asTgtLmt];

 

//Reset the limit

oLimit.SelectedValues.RemoveAll();

oLimit.CustomValues.RemoveAll();

 

//update the limit

for (var iRow=1; iRow <= oDD.SelectedValues.Count ; iRow++){

oLimit.CustomValues.Add(oDD[iRow]);

oLimit.SelectedValues.Add(oDD[iRow]);

}

}     // end of copyDD2Limit definition

 

g_copyDD2Limit = copyDD2Limit;            //Define global version of function

 

Home

 

Validation functions

If the user is entering dates onto an EIS page, you’ll need to validate them before using them. Here are a couple of functions to use.

 

valDateNumMonth

Checks whether a string contains a valid date.

This version is for use with a numeric month, if you use text months, see valDateTxtMonth, below.

 

/*---------------------------------------------------------------------------------------------

--   Function:    valDateNumMonth

-- Parameters:    asIn  -  String to be validated

--     Result:    Returns date or error message as appropriate

--

-- Is string a date of format d-mm-yy or dmmyy, with 2d or 4d year?

---------------------------------------------------------------------------------------------*/

function valDateNumMonth(asIn){

if( asIn.length < 5 || asIn.length > 10 ){return 'Invalid Length'};

 

//Declare local variables and constants

var nDay, nMonth, nYear, dIn;

var DAYSINMONTH = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var DELIMITERS = "-/ ._";

var DIGITS = "0123456789"

 

// 1st character must be a number

if( DIGITS.search(asIn[0]) == -1 ){return "Invalid Day"};

 

// Split entry into nDay, sMonth, nYear

if( DELIMITERS.search(asIn[1]) > -1 ){

 

// Must be d-mm-yy

nDay = Number( asIn[0] );

nMonth = Number(asIn.substr(2, 2));

nYear = Number( asIn.substr(5));

 

} else {

if( DELIMITERS.search(asIn[2]) > -1 ){

// Must be dd-mm-yy

nDay = Number( asIn.substr(0, 2) );

nMonth = Number( asIn.substr(3, 2) );

nYear = Number( asIn.substr(6) );

 

} else {

if( asIn.length == 5 || asIn.length == 7 ){

// Must be dmmyy or dmmyyyy

nDay = Number( asIn[0] );

nMonth = Number( asIn.substr(1, 2) );

nYear = Number( asIn.substr(3) );

 

} else {

// Must be ddmmyy or ddmmyyyy

nDay = Number( asIn.substr(0, 2) );

nMonth = Number( asIn.substr(2, 2) );

nYear = Number( asIn.substr(4) );

}

}

}

 

// Valid day number?

if(isNaN(nDay)){return "Invalid Day"};

 

// Valid month number?

if (isNaN(nMonth)){return "Invalid Month"};

 

// Valid year number?

if(isNaN(nYear)){return "Invalid Year"};

 

// Change 2-digit to full year, catering for Y2K

if( nYear <= 50 ){

nYear += 2000;

} else {

if( nYear < 1000 ){nYear += 1900};

}

 

// Is this a leap year?

if( nYear%100 == 0 ){

// Year divisible by 100, so must be divisible by 400 to be a leap year

if( nYear%400 == 0 ){DAYSINMONTH[1] = 29};

} else {

// Year not divisible by 100, so must be divisible by 4 to be a leap year

if( nYear%4 == 0 ){DAYSINMONTH[1] = 29};

}

<