$(function () {
// -- Application Variables --------------------------------------------------------------------------------
var crossPostData = {}; // Required when storing and retrieving data
var ServerAddr = ''; // Required if using Session Data
var curvKey; // global variable for the assigned vKey
// The FieldIDs and default data where you will store your data for the option list
var AppKey = {
id: 3000,
data:''
}; // The id is always 3000. Variable is required only if using the Application Configuration field
// You can store data in any number of fields between 10000 and 10999
var StoreField1 = {
id:10000,
data: 'Default Data for StoreField1'
}; // id must be in the range of 10000-10999, data is your default data
var StoreField2 = {
id:10001,
data: 'Default Data for StoreField2'
}; // id must be in the range of 10000-10999, data is your default data
var User = {
userID:'',
CID:0,
FName:'',
MI:'',
LName:'',
Status:'',
Groups: ''
}; // Sets Default Values in case the user does not have some values - Required if using Session Data
var LaunchType = ''; // this will either be InitLaunch or Continue as determined by whether or not this
// is a first or subsequent launch
/*------------- Testing Code without installing the app ------------------------------------------------------
1. Enter vales for all test data below
2. Uncomment the entire 'Test data' block below
3. Comment out the lines in your portion of the code (ex AppReady() function) that use the SetData function
*/
/*
// Test data section - Comment out this entire section when installing for FirstClass
AppKey.data='App Config Field'
LaunchType = 'InitLaunch';
User = {
userID:'jschmo',
CID:1234,
FName:'Jo',
MI:'',
LName:'Schmo',
Status:'I am testing this out',
Groups:{0:'First Group',1:'Second Group'}
};
AppReady();
// End Test data section
*/
// Set up event listener for the window
window.addEventListener("message",receiveCrossPostMsg,false);
// Appliction Code here when run either offline or installed -------------------------------------------------
function AppReady(){
/* Note. This is just some sample code to demonstrate how you can use the stored and retrieved data
and how you can update the data.
When you are building your own applications, this function can be left empty if you like if you do
not want to have any action take place that uses the stored and session data that has been retrieved
The StartApp() Function below retrieves all of the data and once done, this function is run so
the data is not available to you until this function runs.
*/
if (LaunchType=='InitLaunch') {
// This is the first time that someone has launched the application and the default data has been
var p = '
First Launch of Application
'
} else {
// This is a subsequent launch after data has been stored
var p = 'Subsequent Launch of Application
'
}
// The following code runs always
p += 'AppKey.data: ' + AppKey.data + '
';
p += 'StoreField1.data: ' + StoreField1.data + '
';
p += 'StoreField2.data: ' + StoreField2.data + '
';
p += 'User.userID: ' + User.userID + '
';
p += 'User.CID: ' + User.CID + '
';
p += 'User.FName: ' + User.FName + '
';
p += 'User.MI: ' + User.MI + '
';
p += 'User.LName: ' + User.LName + '
';
p += 'User.Status: ' + User.Status + '
';
// need to parse the Groups array to display it.
var exposedGroups = '';
if (typeof User.Groups === 'string') {
exposedGroups = User.Groups
} else if (typeof User.Groups === 'object') {
var keys = Object.keys(User.Groups);
for (var i = 0; i < keys.length; i++) {
exposedGroups += User.Groups[keys[i]];
if (i < keys.length - 1) {
exposedGroups += ", ";
}
}
}
p += 'User.Groups: ' + '[' + exposedGroups + ']
';
$('#StoredData').html(p);
// Ask for updates to StoreFields
var newData= prompt('Enter new value for StoreField1.data:' , 'Updated information' );
if (newData != null) {
// Update the StoreField2.data variable and store it for the future
StoreField1.data = newData;
setData(StoreField1.id,StoreField1.data);
}
newData= prompt('Enter new value for StoreField2.data:' , 'Updated information' );
if (newData != null) {
// Update the StoreField2.data variable and store it for the future
StoreField2.data = newData;
setData(StoreField2.id,StoreField2.data);
}
p = "Updated Data
"
p += 'StoreField1.data: ' + StoreField1.data + '
';
p += 'StoreField2.data: ' + StoreField2.data + '
';
$('#UpdatedData').html(p);
}
// Code that is only run when the application is installed in FirstClass ----------------------------------------
function StartApp() {
var getURL = getParentUrl(); // Get the URL of the FCWS Server, not the iFrame
getURL.done(function(){
var getSession = getSessionInfo(); // Get the session Information
$.when(getSession).done(function(){
AppReady();
});
});
}
// Data Storage and Update Functions ----------------------------------------------------------------------------
/* processData and receiveCrossPostMsg are the only functions below that need to be modified for your application
to run when installed. In each function you handle the different cases for each of the StoreFields.
processData
Just remove or add additional cases in processData's two switch blocks but always make sure you have the
LaunchType variable and StartApp() function in the last case.
receiveCrossPostMsg
Make sure there is a 'getData' function for each of the storage fields used
*/
function processData(data,cmd){
var resultText = '';
var dtaObj = $.parseJSON(data);
var successObj = dtaObj.SUCCESS;
var errorObj = dtaObj.ERROR;
switch(cmd){
case 'get':
if(successObj){
switch (successObj.fldid) {
case AppKey.id:
AppKey.data = successObj.data;
break;
case StoreField1.id:
StoreField1.data = successObj.data;
break;
case StoreField2.id:
StoreField2.data=successObj.data;
// Start the app knowing this is a subsequent launch
LaunchType = 'Continue'
StartApp();
break;
}
}
if (errorObj){
// Need to set the fields with the default values
switch (errorObj.fldid) {
case AppKey.id:
// Missing Key field so deal with it
alert("Error: Missing Key Field Data. App not installed properly");
break;
case StoreField1.id:
// Set to defaults
setData(StoreField1.id,StoreField1.data);
// Store it for the future
break;
case StoreField2.id:
// Set to defaults
setData(StoreField2.id,StoreField2.data);
// Start the app knowing you now have default values and 1st time launch
LaunchType = 'InitLaunch'
StartApp();
break;
}
}
break;
case 'set':
if (successObj) {
console.log('Setting Data');
// it was successful so the variable h ad to be set upon submission;
}
if (errorObj) {
curValue = 'Error';
alert('Error setting field');
console.log(errorObj);
// Handle error routine here
}
break;
}
}
function receiveCrossPostMsg(event) {
// the event command is either init, get or send
var msg = event.data;
var data = msg.data;
if (msg.cmd=='init') {
// Performed when app launches
crossPostData.appID = data.appID;
crossPostData.ticket = data.ticket;
crossPostData.source = event.source;
crossPostData.origin = event.origin;
// Assign the returned vKey
curvKey = data.vKey;
// Get the Configuration Key first - Stored in AppKey (configuration) Field on the Install Form
getData(AppKey.id);
// Get the data stored in the StoreField1.id
getData(StoreField1.id);
// Get the data stored in the StoreField2.id
getData(StoreField2.id);
} else {
processData(data,msg.cmd);
}
}
// -- None of the following functions should be modified ---------------------------------------------------
function getData(fieldid,idx) {
if (typeof idx == 'undefined') idx = 0;
var a = {};
a.fldid = fieldid;
a.index = idx;
var b= {};
b.cmd = 'get';
b.appID = crossPostData.appID;
b.ticket = crossPostData.ticket;
b.data = a
crossPostData.source.postMessage(b,crossPostData.origin);
}
function setData(fieldid,fielddata,typ,idx) {
var a = {};
if (typeof typ == 'undefined') typ = 0;
if (typeof idx == 'undefined') idx = 0;
a.fldid = fieldid;
a.index = idx;
a.type = typ;
a.data = fielddata;
var b = {};
b.cmd = 'set';
b.appID = crossPostData.appID;
b.ticket = crossPostData.ticket;
b.data = a;
crossPostData.source.postMessage(b,crossPostData.origin);
}
// Get the URL of the parent window -------------------------------------------------------------------------
function getParentUrl() {
var deferred = $.Deferred();
var isInIframe = (parent !== window)
var myURL = null;
if (isInIframe) {
myURL = document.referrer;
}
var protocol = myURL.substr(0,myURL.indexOf('://')+3);
var remaining_url = myURL.substr(protocol.length, myURL.length)
var domain_w_port = remaining_url.substr(0,remaining_url.indexOf('/'));
var domain = (domain_w_port.indexOf(':') == -1 ? domain_w_port : domain_w_port.substr(0,domain_w_port.indexOf(':')));
var port = (domain_w_port.indexOf(':') == - 1) ? '' : domain_w_port.substr(domain_w_port.indexOf(':'))
ServerAddr = protocol + domain + port;
deferred.resolve();
return deferred.promise();
}
// Session Information --------------------------------------------------------------------------------------
function getSessionInfo() {
GetURL=ServerAddr + "/FCP/?KnownObject=-3&ReplyAsJSON"
return $.ajax({
url:GetURL,
type:"GET",
crossDomain: true,
dataType: 'jsonp',
success: function(data, textStatus, jqXHR) {
User.userID = data.FORMDATA["7012"];
User.CID = data.FORMDATA["7006"];
if (data.FORMDATA["7001"]) {
User.FName = data.FORMDATA["7001"]
};
if (data.FORMDATA["7002"]) {
User.MI = data.FORMDATA["7002"]
};
User.LName = data.FORMDATA["7003"];
if (data.FORMDATA["7020"]) {
User.Status=data.FORMDATA["7020"]
};
if (data.FORMDATA["7040"]) {
User.Groups = data.FORMDATA["7040"]
};
},
error: function(data, textStatus, jqXHR) {
alert("textStatus:" + textStatus);
alert("jqXHR:" + jqXHR);
}
});
}
});