You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.8 KiB
117 lines
4.8 KiB
/**
|
|
* Load Assets
|
|
* @author Robert Strutts <Robert@TryingToScale.com>
|
|
* @copyright Copyright (c) 2019, Robert Strutts.
|
|
* @license https://mit-license.org/
|
|
*/
|
|
|
|
tts.DoCacheAssets = true;
|
|
|
|
var assets_loaded_once = [];
|
|
|
|
tts.AddAssetCachedFile = function (full_file) {
|
|
assets_loaded_once.push(full_file);
|
|
};
|
|
/**
|
|
* Get an StyleSheet/Script
|
|
* @param {Object} source - CSS/JavaScript File with Options
|
|
* @param {Object} options - element to use, etc...
|
|
* @returns {Promise}
|
|
*/
|
|
tts.getAsset = (source, options) => {
|
|
if (typeof options === "undefined") {
|
|
options = {};
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
var defaults_caching = tts.getValue(tts.DoCacheAssets, true);
|
|
var file = source.name, kind = file.split('.').pop(), doCache = tts.getValue(source.useCache, defaults_caching);
|
|
var full_file = tts.getValue(source.path, "") + file;
|
|
var ts = tts.getValue(source.ts, "");
|
|
var has_loaded = (assets_loaded_once.indexOf(full_file) === -1) ? false : true;
|
|
if (has_loaded) {
|
|
resolve("Already Loaded..." + full_file);
|
|
} else {
|
|
assets_loaded_once.push(full_file);
|
|
switch (kind) {
|
|
case 'css':
|
|
var dataType = tts.getValue(source.dataType, "text/css"), style = document.createElement('style');
|
|
var elm = tts.getValue(options.element, document.getElementsByTagName('style')[0] || document.getElementsByTagName('head')[0]);
|
|
var loaded = tts.ajax(full_file, {dataType: "text/css", useCache: doCache, ts: ts});
|
|
loaded.then(function (code) {
|
|
style.type = dataType;
|
|
style.innerText = code;
|
|
elm.parentNode.insertBefore(style, elm.nextSibling);
|
|
setTimeout(function () {
|
|
resolve();
|
|
}, 12);
|
|
});
|
|
loaded.catch(function (e) {
|
|
reject(e);
|
|
});
|
|
break;
|
|
default:
|
|
var dataType = tts.getValue(source.dataType, "text/javascript");
|
|
var script = document.createElement('script');
|
|
var elm = tts.getValue(options.element, document.getElementsByTagName('script')[0] || document.getElementsByTagName('head')[0]);
|
|
var loaded = tts.ajax(full_file, {dataType: "text/javascript", useCache: doCache, ts: ts});
|
|
loaded.then(function (code) {
|
|
script.type = dataType;
|
|
script.text = code;
|
|
elm.parentNode.insertBefore(script, elm);
|
|
setTimeout(function () {
|
|
resolve();
|
|
}, 12);
|
|
});
|
|
loaded.catch(function (e) {
|
|
reject(e);
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
};
|
|
/** Useage: to combine options on each Asset for use with getMultiAssets.
|
|
* Updates the sources Array/Objects with new data from Options.....as it is passed by Reference!!!
|
|
* @param {Array of Objects} sources - CSS/JavaScript File with Options
|
|
* @param {Array of Objects} options - path, useCache, dataType...
|
|
*/
|
|
tts.mergeOptions = function (sources, options) {
|
|
let keys = Object.keys(options);
|
|
for (let key of keys) {
|
|
for (var x = 0; x < sources.length; x++) {
|
|
if (typeof sources[x][key] === 'undefined') {
|
|
sources[x][key] = options[key];
|
|
}
|
|
}
|
|
}
|
|
};
|
|
/**
|
|
*
|
|
* @param {Array of Objects} sources - CSS/JavaScript File with Options
|
|
* @param {Object} options - element to apply to, etc...
|
|
* @returns {Promise} - All done will all Files.
|
|
*/
|
|
tts.getMultiAssets = (sources, options) => {
|
|
if (typeof options === "undefined") {
|
|
options = {};
|
|
}
|
|
var prArr = [];
|
|
sources.forEach(function (source) {
|
|
(function (source) {
|
|
prArr.push(new Promise(function (resolve, reject) {
|
|
tts.getAsset(source, options).then(function () {
|
|
resolve();
|
|
}).catch(function (e) {
|
|
reject(e);
|
|
});
|
|
}));
|
|
})(source);
|
|
});
|
|
return Promise.all(prArr, function () {
|
|
return true;
|
|
});
|
|
};
|
|
|
|
/*
|
|
* End of Assets
|
|
*/
|
|
|