Created
January 13, 2012 20:20
-
-
Save pipwerks/1608506 to your computer and use it in GitHub Desktop.
Captivate SCORM 2004 Output, removed non-ExternalInterface communication code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var g_objAPI = null, | |
g_nAPI = 0, // type of API to start searching for; allowable values: 0 - SCORM 2004; 1 - SCORM 1.2 (or 1.1) | |
g_aAPI = ["1.0", "0.2"], // Array that stores the API versions | |
g_zAPIVersion = -1, | |
g_bFinishDone = false, | |
intIntervalSecs = 1, // Number of seconds to wait for SCORM API to load | |
g_intAPIOrder = 0, // Way to search for API object (0 - starts with bottom up; 1 - starts top down) | |
strURLParams = ""; | |
timeCurrent_int = new Date().getTime(), | |
timeLimit_int = new Date().getTime() + (intIntervalSecs * 1000); | |
function findAPI(win) { | |
// Search the window hierarchy for an object named "API_1484_11" for SCORM 2004 or "API" for SCORM 1.2 or below | |
// Look in the current window (win) and recursively look in any child frames | |
if(g_nAPI == 0){ | |
if(win.API_1484_11 != null){ | |
return win.API_1484_11; | |
} | |
} else if(g_nAPI == 1 || g_nAPI == "") { | |
if (win.API != null){ | |
g_zAPIVersion = g_aAPI[g_nAPI]; | |
return win.API; | |
} | |
} | |
if (win.length > 0){ // check frames | |
for (var i=0;i<win.length;i++){ | |
var objAPI = findAPI(win.frames[i]); | |
if (objAPI != null){ | |
return objAPI; | |
} | |
} | |
} | |
return null; | |
} | |
function getAPI(intAPISearchOrder) { | |
// intAPISearchOrder is 0 - start at current window and work way up; 1 - start at top window and work way down. | |
var objAPI = null; | |
intAPISearchOrder = (typeof intAPISearchOrder === "undefined") ? 0 : intAPISearchOrder; | |
if(intAPISearchOrder == 0) { | |
// start and the current window and recurse up through parent windows/frames | |
var objCurrentWindow = window, | |
xCount = 0; | |
objAPI = findAPI(objCurrentWindow); | |
while(objCurrentWindow && !objAPI && xCount < 100){ | |
xCount++; | |
if(objCurrentWindow.opener != null && typeof objCurrentWindow.opener !== "undefined"){ | |
objCurrentWindow = objCurrentWindow.opener; | |
} else { | |
objCurrentWindow = objCurrentWindow.parent; | |
} | |
objAPI = findAPI(objCurrentWindow); | |
} | |
if(objAPI == null && g_nAPI < (g_aAPI.length-1)){ | |
g_nAPI++; | |
objAPI = getAPI(intAPISearchOrder); | |
} | |
} else { | |
// start at the top window and recurse down through child frames | |
objAPI = findAPI(this.top); | |
if (objAPI == null){ | |
// the API wasn't found in the current window's hierarchy. If the | |
// current window has an opener (was launched by another window), | |
// check the opener's window hierarchy. | |
objTopWindow=window.top; | |
objTopWindow = objTopWindow.opener; | |
while (objTopWindow && !objAPI){ | |
//checking window opener | |
objAPI = findAPI(objTopWindow.top); | |
if (objAPI==null){ | |
objTopWindow = objTopWindow.opener; | |
} | |
} | |
if(objAPI == null && g_nAPI < (g_aAPI.length-1)){ | |
g_nAPI++; | |
objAPI = getAPI(intAPISearchOrder); | |
} | |
} | |
} | |
if(objAPI == null) { | |
//can't find API | |
} else if(objAPI != null && g_zAPIVersion == -1) { | |
g_zAPIVersion = objAPI.version; | |
} | |
return objAPI; | |
} | |
function setAPI() { | |
while(g_objAPI == undefined){ | |
g_objAPI = getAPI(0); | |
} | |
} | |
function isAPI() { | |
return typeof g_objAPI !== "undefined" && g_objAPI != null; | |
} | |
// called in the outer HTML file | |
// g_objAPI = getAPI(); | |
function Finish(){ | |
if(isAPI() && g_bFinishDone == false){ | |
g_objAPI.Commit(""); | |
g_bFinishDone = (g_objAPI.Terminate("") === "true"); | |
} | |
return g_bFinishDone + ""; // Force type to string | |
} | |
function Captivate_DoExternalInterface(command, parameter, value, variable){ | |
var CaptivateObj = document.getElementById("Captivate"), | |
strFSCmd = new String(command), | |
strErr = "true", | |
strFSArg1 = parameter, | |
strFSArg2 = value, | |
strFSArg3 = variable, | |
courseStatus; | |
// do nothing, if SCORM API is not available | |
if(!isAPI()){ return; } | |
if(strFSCmd == "Initialize"){ | |
CaptivateObj.SetScormVariable(strFSArg3, g_objAPI.Initialize("")); | |
courseStatus = g_objAPI.GetValue("cmi.completion_status"); | |
if(courseStatus == "not attempted"){ | |
g_objAPI.SetValue("cmi.completion_status", "incomplete"); | |
} | |
} else if(strFSCmd == "SetValue"){ | |
strErr = g_objAPI.SetValue(strFSArg1, strFSArg2); | |
CaptivateObj.SetScormVariable(strFSArg3, strErr); | |
} else if(strFSCmd == "Terminate"){ | |
g_bFinishDone = g_objAPI.Terminate(""); | |
strErr = g_bFinishDone; | |
CaptivateObj.SetScormVariable(strFSArg3, g_bFinishDone); | |
} else if(strFSCmd == "Commit"){ | |
strErr = g_objAPI.Commit(""); | |
CaptivateObj.SetScormVariable(strFSArg3, strErr); | |
} else if((strFSArg2) && (strFSArg2.length > 0)){ | |
if(strFSCmd == "GetLastError"){ | |
strErr = g_objAPI.GetLastError(); | |
CaptivateObj.SetScormVariable(strFSArg3, strErr); | |
} else { | |
strErr = g_objAPI[strFSCmd](strFSArg1); | |
CaptivateObj.SetScormVariable(strFSArg3, strErr); | |
} | |
} | |
return strErr; | |
} | |
// This simply loops for a set period of time, waiting for the API to | |
// load and/or be found. A better solution would be to use setInterval | |
// and on the timeout, redirect or load the resulting Captivate file. | |
while(g_objAPI == null && timeCurrent_int < timeLimit_int){ | |
g_objAPI = getAPI(g_intAPIOrder); | |
timeCurrent_int = new Date().getTime(); | |
} | |
//If SCORM connection is active, append the following querystring to the URL | |
if(g_objAPI != null){ | |
strURLParams = "?SCORM_API=" + g_zAPIVersion + "&SCORM_TYPE=0"; | |
} | |
window.onunload = Finish; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment