1jQuery(function($) { 2 3 var spinnerCount = 0; 4 5 /** 6 * Show the spinner and increase counter. 7 */ 8 function showSpinner() { 9 spinnerCount++; 10 $("#wp-h5p-xapi-spinner").show(); 11 } 12 13 /** 14 * Decrease the spinner count. Hide if no more spinners. 15 */ 16 function hideSpinner() { 17 spinnerCount--; 18 19 if (!spinnerCount) 20 $("#wp-h5p-xapi-spinner").fadeOut(500); 21 } 22 23 /** 24 * Show error. 25 */ 26 function showError(message, code) { 27 console.error("Unable to save xAPI statement"); 28 29 alert("Unable to save result data.\n\nMessage: " + message + "\n" + "Code: " + code); 30 } 31 32 /** 33 * Post error. 34 */ 35 function onXapiPostError(xhr, message, error) { 36 hideSpinner(); 37 38 console.log("xapi post error"); 39 console.log(xhr.responseText); 40 41 showError(message, xhr.status); 42 } 43 44 /** 45 * Post success. 46 */ 47 function onXapiPostSuccess(res, textStatus, xhr) { 48 hideSpinner(); 49 50 if (!res.hasOwnProperty("ok")) { 51 console.log("xapi post error"); 52 console.log(xhr.responseText); 53 showError("Got bad response back...", 500); 54 } 55 56 if (!res.ok) { 57 console.log("xapi post error"); 58 console.log(xhr.responseText); 59 showError(res.message, res.code); 60 } 61 62 if (res.ok) { 63 $.event.trigger({ 64 type: "h5pXapiStatementSaved", 65 message: res.message 66 }); 67 } 68 } 69 70 /** 71 * xAPI statement event listener. 72 */ 73 function onXapi(event) { 74 if (!WP_H5P_XAPI_STATEMENT_URL) 75 return; 76 77 showSpinner(); 78 79 var data = {}; 80 /*console.log("on xapi, statement:"); 81 console.log(JSON.stringify(event.data.statement));*/ 82 83 if (typeof event.data.statement.context == 'undefined') { 84 //console.log("here, context"); 85 event.data.statement.context = {}; 86 } 87 if (typeof event.data.statement.context.contextActivities == 'undefined') { 88 //console.log("here, contextActivities"); 89 event.data.statement.context.contextActivities = {}; 90 } 91 if (typeof event.data.statement.context.contextActivities.grouping == 'undefined') { 92 //console.log("here, grouping"); 93 event.data.statement.context.contextActivities.grouping = []; 94 } 95 96 if (WP_H5P_XAPI_CONTEXTACTIVITY) 97 event.data.statement.context.contextActivities.grouping.push(WP_H5P_XAPI_CONTEXTACTIVITY); 98 99 data.statement = JSON.stringify(event.data.statement); 100 //data.statement = event.data.statement; 101 102 $.ajax({ 103 type: "POST", 104 url: WP_H5P_XAPI_STATEMENT_URL, 105 data: data, 106 dataType: "json", 107 success: onXapiPostSuccess, 108 error: onXapiPostError 109 }); 110 } 111 112 /** 113 * Main. 114 * Create save spinner and register event listener. 115 */ 116 var has_attachment_attempted = false; 117 $(document).ready(function() { 118 //console.log("h5p xapi ready"); 119 120 if (typeof H5P !== 'undefined' && H5P.externalDispatcher) 121 H5P.externalDispatcher.on('xAPI', onXapi); 122 123 $("body").append("<div id='wp-h5p-xapi-spinner'>Saving...</div>"); 124 $("#wp-h5p-xapi-spinner").hide(); 125 126 // Listen for messages from iframes 127 window.addEventListener('message', function receiveMessage(event) { 128 if (event.data.context !== 'h5p') { 129 return; // Only handle h5p requests. 130 } 131 if( ! has_attachment_attempted ) { 132 // Find out who sent the message 133 var iframe, iframes = document.getElementsByTagName('iframe'); 134 for (var i = 0; i < iframes.length; i++) { 135 if (iframes[i].contentWindow.H5P) { 136 iframes[i].contentWindow.H5P.externalDispatcher.on('xAPI', onXapi); 137 has_attachment_attempted = true; 138 } 139 } 140 } 141 142 }, false); 143 }); 144});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.