@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
167 lines • 6.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("../helpers/debug");
const typecheckers_1 = require("../helpers/typecheckers");
// eslint-disable-next-line no-shadow
var SodState;
(function (SodState) {
SodState["pending"] = "pending";
SodState["done"] = "done";
})(SodState || (SodState = {}));
class Sod {
constructor(url, sodName) {
this.url = url;
this.sodName = sodName;
this.state = SodState.pending;
this.notified = false;
this.callbacks = [];
this.script = null;
}
error() {
if ((0, debug_1.isDebug)())
console.log('unhandled error in sod');
}
loadScript(scriptUrl, sync = false) {
let self = this;
var successCallback = () => {
self.load();
};
var errorCallback = () => {
self.error();
};
self.script = Sod.loadScript(scriptUrl, successCallback, errorCallback, sync);
self.state = SodState.pending;
}
load() {
let self = this;
self.state = SodState.done;
if (!self.notified) {
self.notify();
}
}
notify() {
let self = this;
var callbackLength = self.callbacks.length;
for (var i = 0; i < callbackLength; i++) {
var sodCallback = self.callbacks[i];
if (!sodCallback.called && typeof (sodCallback.callback) === "function") {
try {
sodCallback.callback();
sodCallback.called = true;
}
catch (ex) {
if ((0, debug_1.isDebug)())
console.log('unhandled error in sod');
}
}
}
self.notified = true;
}
reset() {
let self = this;
var callbackLength = self.callbacks.length;
for (var i = 0; i < callbackLength; i++) {
this.callbacks[i].called = false;
}
self.notified = false;
}
static loadScript(url, successCallback, errCallback, sync = false) {
let scriptElm = document.createElement("script");
if (sync === true) {
let req = new XMLHttpRequest();
req.open("GET", url, false);
req.send();
scriptElm.appendChild(document.createTextNode(req.responseText));
successCallback();
}
else {
let agt = navigator.userAgent.toLowerCase();
let ie8down = agt.indexOf("msie") !== -1 && parseInt(agt.substring(agt.indexOf("msie ") + 5), 10) <= 8;
let getCallback = (cb) => {
return () => {
var loaded = false;
if (ie8down && typeof scriptElm.readyState !== "undefined") {
loaded = scriptElm.readyState === "complete" || scriptElm.readyState === "loaded";
}
else {
loaded = true;
}
if (loaded) {
scriptElm.onreadystatechange = null;
scriptElm.onload = null;
scriptElm.onerror = null;
cb();
}
};
};
scriptElm.type = "text/javascript";
scriptElm.src = url;
if (ie8down) {
scriptElm.onreadystatechange = getCallback(successCallback);
}
else {
scriptElm.onload = getCallback(successCallback);
scriptElm.onerror = getCallback(errCallback);
}
}
(document.head || document.getElementsByTagName("HEAD")[0]).appendChild(scriptElm);
return scriptElm;
}
static getGlobal(global) {
if ((0, typecheckers_1.isString)(global))
return (0, typecheckers_1.getFromFullName)(global);
else
return global.getter();
}
static ensureScriptNoPromise(scriptUrl, global, callback, sodName, sync = false) {
if (!(0, typecheckers_1.isNullOrEmptyString)(global) && (0, typecheckers_1.typeofFullName)(Sod.getGlobal(global)) !== "undefined") {
//this global object already exists, no need to reload this script.
if ((0, typecheckers_1.isFunction)(callback)) {
callback();
}
}
else {
sodName = ((0, typecheckers_1.isNullOrEmptyString)(sodName) === false && sodName || scriptUrl).toLowerCase();
var sod = Sod._getGlobalSod(sodName);
if (!sod) {
sod = Sod._addGlobalSod(sodName, scriptUrl);
}
if (!(0, typecheckers_1.isNullOrUndefined)(callback)) {
sod.callbacks.push({ "called": false, "callback": callback });
}
if (!sod.script) {
sod.loadScript(scriptUrl, sync);
}
else if (sod.state === SodState.done || sod.notified) {
sod.notify();
}
}
}
static async ensureScript(scriptUrl, global, callback, sodName, sync = false) {
return new Promise((resolve, reject) => {
let resolveCallback = () => {
if (!(0, typecheckers_1.isNullOrUndefined)(callback))
callback();
resolve();
};
Sod.ensureScriptNoPromise(scriptUrl, global, resolveCallback, sodName, sync);
});
}
static _initGlobalSods() {
//static must be globally shared between all instances...
if ((0, typecheckers_1.isTypeofFullNameNullOrUndefined)("g_kwizcom_sods")) {
window.g_kwizcom_sods = {};
}
}
static _getGlobalSod(name) {
Sod._initGlobalSods();
return window.g_kwizcom_sods[name];
}
static _addGlobalSod(name, scriptUrl) {
Sod._initGlobalSods();
window.g_kwizcom_sods[name] = new Sod(scriptUrl, name);
return window.g_kwizcom_sods[name];
}
}
exports.default = Sod;
//# sourceMappingURL=sod.js.map