Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Now you can use it without NPM.
Browse files Browse the repository at this point in the history
  • Loading branch information
arisjulio committed Oct 15, 2018
1 parent e6ad6c9 commit 4dc2137
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 165 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
test/index-compiled.js
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
# SCORM Adapter
# SCORM API Adapter

## Installation

Install with npm executing:

```bash
npm i arisjulio/scorm-api-adapter
```

## Usage
Import, create an instance and set the API REST URI:

```javascript
import SCORMAdapter from 'scorm-api-adapter';

window.API = new SCORMAdapter('http://your-api-to-comit.org');
window.API = new SCORMAdapter();
window.API.uri = 'https://your-api-to-commit.org/great-endpoit';
```

## Download

You can download the minified file from [here.](https://github.com/arisjulio/scorm-api-adapter/releases/latest)

If you download or clone the repository, you must execute `npm run compile` and the minified file will be generated in the `dist` folder.

The minified file must be included in your website:

```html
<script src="scorm-api-adapter.js"></script>
```

> You must return an `success` in your API for know if the commit attempt was successfully executed.
After, in your JS code, You can access to the `window.API` element and must set your API REST URI:

For example:

```javascript
window.API.uri = 'https://your-api-to-commit.org/great-endpoit';
```

Now you can load an SCORM Object in a frame or new window and it will work.

> **IMPORTANT:** You must return an `success` field in your API REST response to know if the commit attempt was successfully executed.
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "scorm-api-adapter",
"version": "0.1.0",
"version": "0.3.0",
"description": "Simple SCORM API Adapter Implementation.",
"main": "src/index.js",
"main": "src/scormAdapter.js",
"scripts": {
"compile-test": "webpack --mode production --config webpack.config.js --watch",
"test-on-browser": "serve test -p 8080"
"compile": "webpack --mode production --config webpack/dist.js",
"compile-test": "webpack --mode development --config webpack/test.js --watch",
"test-on-browser": "serve -p 8080"
},
"repository": {
"type": "git",
Expand Down
114 changes: 2 additions & 112 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,113 +1,3 @@
import $ from 'jquery';
import { Base64 } from 'js-base64';
import Storage from './storage';
import errorDefinition from './errors';
import SCORMAdapter from './scormAdapter';

export default class SCORMAdapter {
constructor(uri = 'http://localhost') {
this.uri = uri;
}

LMSInitialize() {
Storage.setItem('Initialized', true);
return true;
}

LMSFinish() {
if (!this._isInitialized()) {
this._setError(301);
return false;
}
let _return = this.LMSCommit();
Storage.setItem('Initialized', false);
Storage.clearAll();
return _return;
}

LMSGetValue(element) {
if (!this._isInitialized()) {
this._setError(301);
return false;
}
let value = Storage.getItem(element);
if (!value) {
this._setError(201);
return "";
}
return value;
}

LMSSetValue(element, value) {
if (!this._isInitialized()) {
this._setError(301);
return false;
}
Storage.setItem(element, value);
return Storage.getItem(element);
}

LMSCommit() {
let data = Storage.getAll();
delete data['errors'];
delete data['Initialized'];
data = JSON.stringify(data);
data = Base64.encode(data);
let response;
let _return = true;
$.post({
url: this.uri,
async: false,
data: { data, success: true },
success: res => {
response = res;
},
error: () => {
this._setError(101);
_return = false;
}
});
if (!response.success) {
this._setError(101);
return false;
}
return _return;
}

LMSGetLastError() {
let errors = Storage.getItem('errors');
errors = JSON.parse(errors);
if (errors && errors.length > 0) {
return errors.pop();
}
return "";
}

LMSGetErrorString(errorCode) {
errorCode = errorCode.toString();
let error = errorDefinition[errorCode];
if (!error) return "";
return error["errorString"];
}

LMSGetDiagnostic(errorCode) {
errorCode = errorCode.toString();
let error = errorDefinition[errorCode];
if (!error) return "";
return error["diagnostic"];
}

_isInitialized() {
let initialized = Storage.getItem('Initialized');
return initialized;
}

_setError(errorCode) {
errorCode = errorCode.toString();
let errors = Storage.getItem('errors');
if (!errors) errors = '[]';
errors = JSON.parse(errors);
errors.push(errorCode);
errors = JSON.stringify(errors);
Storage.setItem('errors', errors);
}
}
window.API = new SCORMAdapter();
Loading

0 comments on commit 4dc2137

Please sign in to comment.