public
Created

Captivate SCORM 2004 Output, removed getAPI timer, only embed SWF if API found

  • Download Gist
captivate.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// set document.domain property here, if it works for your environment/SCORM implementation
// document.domain="";
var strURLFile = "sample.swf", // Name of the flash file
flashvars = {},
params = { bgcolor: "#f5f4f1", menu: "false" },
attributes = { id: "Captivate", name: "Captivate" },
CaptivateSWF; //Cache the reference to the SWF to avoid future lookups
 
 
function callbackFn(e){
//e.ref is the <object> aka SWF file. No need for getElementById
if(e.success && e.ref){
CaptivateSWF = e.ref;
CaptivateSWF.tabIndex = -1; //Set tabIndex to enable focus on non-form elements
CaptivateSWF.focus();
}
}
 
//Initialize SCORM API
SCORM_API = getAPI();
 
//Only embed SWF if SCORM API is found
if(SCORM_API){
 
//swfobject.embedSWF has a built-in domready call,
//so it doesn't need to be wrapped in a window.onload event.
swfobject.embedSWF(strURLFile + "?SCORM_API=1.0&SCORM_TYPE=0", "CaptivateContent", "641", "512", "10", false, flashvars, params, attributes, callbackFn);
 
}
sample.htm
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<!-- Copyright [2008] Adobe Systems Incorporated. All rights reserved -->
<!-- saved from url=(0013)about:internet -->
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script src="standard.js"></script>
<script src="SCORM_support/scorm_support.js"></script>
<script src="captivate.js"></script>
<style>
body { background: #f5f4f1; text-align: center; }
</style>
</head>
<body>
<div id="CaptivateContent">&nbsp;</div>
<noscript>
This course requires JavaScript to be enabled in your browser. Please enable JavaScript, then relaunch the course.
</noscript>
</body>
</html>
scorm_support.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
var SCORM_API = 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_bFinishDone = false,
g_intAPIOrder = 0; // Way to search for API object (0 - starts with bottom up; 1 - starts top down)
 
 
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){
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
 
}
 
return objAPI;
 
}
 
function setAPI() {
 
while(SCORM_API == undefined){
SCORM_API = getAPI(0);
}
 
}
 
function isAPI() {
return typeof SCORM_API !== "undefined" && SCORM_API != null;
}
 
function Finish(){
 
if(isAPI() && g_bFinishDone == false){
SCORM_API.Commit("");
g_bFinishDone = (SCORM_API.Terminate("") === "true");
}
return g_bFinishDone + ""; // Force type to string
 
}
 
function Captivate_DoExternalInterface(command, parameter, value, variable){
 
var strErr = "true";
 
// do nothing, if SCORM API is not available
if(!isAPI()){ return; }
 
if(command === "Initialize"){
 
CaptivateSWF.SetScormVariable(variable, SCORM_API.Initialize(""));
 
if(SCORM_API.GetValue("cmi.completion_status") == "not attempted"){
SCORM_API.SetValue("cmi.completion_status", "incomplete");
}
 
} else if(command === "SetValue"){
 
strErr = SCORM_API.SetValue(parameter, value);
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else if(command === "Terminate"){
 
g_bFinishDone = SCORM_API.Terminate("");
strErr = g_bFinishDone;
CaptivateSWF.SetScormVariable(variable, g_bFinishDone);
 
} else if(command === "Commit"){
 
strErr = SCORM_API.Commit("");
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else if((value) && (value.length > 0)){
 
if(command === "GetLastError"){
 
strErr = SCORM_API.GetLastError();
CaptivateSWF.SetScormVariable(variable, strErr);
 
} else {
 
strErr = SCORM_API[command](parameter);
CaptivateSWF.SetScormVariable(variable, strErr);
 
}
 
}
 
return strErr;
 
}
 
window.onunload = Finish;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.