summaryrefslogtreecommitdiff
path: root/resources/js/scorm_player.js
blob: 3ea82fc9f4672e3341c5437dd9400af92e15dc8c (plain)
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
import Cookies from 'js-cookie';
import 'fetch-ie8';
import 'scorm-again';

const Scorm2004API = window.Scorm2004API;

function setupApi(data) {
  const api = new Scorm2004API({
    lmsCommitUrl: data.commit_url,
    xhrHeaders: {
      'X-XSRF-TOKEN': Cookies.get('XSRF-TOKEN'),
    }
  });

  api.loadFromJSON(data.tracking.cmi);

  window.API_1484_11 = api;
}

function loadIframe(entry) {
  const players = document.getElementsByClassName('scorm-player');
  for(const player of players) {
    player.src = entry;
  }
}

function handleScoData(data) {
  setupApi(data);
  loadIframe(data.entry_url);
}

function loadScoData() {
  fetch(window.scorm_api_data.routes.load, {
    method: 'GET',
    headers: {
      Accept: 'application/json',
    }
  }).then((response) => {
    if(response.status === 200)
      return response.json();
    return {};
  }).then(handleScoData);
}

document.addEventListener('DOMContentLoaded', loadScoData);