define(function () {
"use strict";
var oPage;
function promptValueSaved() {
};
promptValueSaved.prototype.draw = function (oControlHost) {
oPage = oControlHost.page;
var elDate = oControlHost.container;
elDate.innerHTML =
'' +
'' +
'';
elDate.querySelector(".btnSavePromptValues").onclick = buttonActionSavePromptSelections;
elDate.querySelector(".btnFetchPromptSelections").onclick = buttonActionFetchPromptSelections;
};
/*
*
* This function coordinates the basic operations of this program as so:
* 1. Loop through the prompts on the page
* 2. For each prompt look to see if there is a cookie with the naming convention cog containing prompt values
* 3. Apply the saved values to each prompt
*
* Note:
* The prompt names must not contain spaces or special characters
* The prompt names must match the prompt names in subsequent reports
* Cookie values are JSON encoded and ready for use
*/
function buttonActionFetchPromptSelections() {
// Get the current prompt value
// Create the report object
// Create an array of prompt controls
var aPromptControls = oPage.getAllPromptControls();
// Determine
var loadedValues = false;
// Loop through the prompt controls on this page
for (var i = 0; i < aPromptControls.length; i++) {
// For each prompt control ...
var currentPrompt = aPromptControls[i];
// Create the cookie name
var cookieName = "cog" + currentPrompt.name;
// Fetch the cookie values (if they exist) as a JavaScript object
var JSONEncodedPromptSelections = fetchCookie(cookieName);
if (JSONEncodedPromptSelections) {
if (JSONEncodedPromptSelections.length > 0) {
// Apply cookie values to prompt
currentPrompt.setValues(JSONEncodedPromptSelections);
loadedValues = true;
};
};
}; // end for
if (loadedValues) {
alert("Your personal default prompt selections have been loaded. You may now edit them as required and press the Save Prompt Selections button.");
}
// Return false, also in an effort to stop the onClick event from propagating to other controls
return false;
};
/*
*
* This function parses the browser cookie string looking for the cookie
* and passing it to the calling function
* @cookieName is the name of the cookie we wish to find
*
*/
function fetchCookie(passedCookieName) {
// Create an array of strings from the document cookie object using the split method to separate the cookies
var aCookies = document.cookie.split(";");
//create a variable to hold the cookie value
var cookie = "";
// Loop through the cookie array to find cognosPrompts
for (var i = 0; i < aCookies.length; i++) {
var currentCookie = aCookies[i];
// The = sign separates the cookie name from the cookie value
var signLocation = currentCookie.indexOf("=");
var currentCookieName = currentCookie.substr(0, signLocation);
var regexPattern = passedCookieName.valueOf();
if (currentCookieName.match(regexPattern)) {
cookie = currentCookie.substr(signLocation + 1, currentCookie.length);
}
}
// Convert the JavaScript object to useable form
cookie = eval(cookie);
return cookie;
};
// Set
function buttonActionSavePromptSelections() {
// Get the current prompt value
// Create an array of prompt controls
var aPromptControls = oPage.getAllPromptControls();
// Loop through the prompt controls on this page
for (var i = 0; i < aPromptControls.length; i++) {
// For each prompt control, call a function to JSON encode the user selections (prompt values)
var currentPrompt = aPromptControls[i];
// Create the cookie name
var cookieName = "cog" + currentPrompt.name;
// Now save the JSON into a cookie
storeCookie(cookieName, JSON.stringify(currentPrompt.getValues()));
};
alert("Your personal default prompt selections have been saved. You may now close this report.");
// Return false, also in an effort to stop the onClick event from propagating to other controls
return false;
};
/*
*
* This function creates a cookie
* @cookieName is the name of the cookie to be created
* @cookieValue is the value to be used to create the cookie
* The date is always set to expire 1 January 2100 to ensure the cookie is effectively permanent
*
*/
function storeCookie(cookieName, cookieValue) {
// Set the cookie expiry date to 1 January 2100 to ensure it does not expire
var expDate = new Date();
expDate.setFullYear(2100, 0, 1);
document.cookie = cookieName + "=" + cookieValue + "; expires=" + expDate.toUTCString();
};
return promptValueSaved;
}
);