Support asset loader include

Sync loading still not implemented. Now only one file can be included
and the callback still needs to be implemented. Maybe allow arrays for
includes and THEN do the callback?!
This commit is contained in:
Dennis Eichhorn 2016-06-26 16:24:59 +02:00
parent 28e1088a24
commit 30ba6233a0
2 changed files with 18 additions and 19 deletions

View File

@ -11,8 +11,7 @@
{ {
"use strict"; "use strict";
/** @namespace jsOMS.Asset */ jsOMS.Asset = {};
jsOMS.Autoloader.defineNamespace('jsOMS.Asset');
/** /**
* @constructor * @constructor
@ -29,7 +28,6 @@
* Load asset. * Load asset.
* *
* @param {string} path Asset path * @param {string} path Asset path
* @param {string} filename Name of the asset
* @param {string} filetype Filetype of the asset * @param {string} filetype Filetype of the asset
* @param {requestCallback} [callback] Callback after load * @param {requestCallback} [callback] Callback after load
* *
@ -40,37 +38,37 @@
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
jsOMS.Asset.AssetManager.prototype.load = function (path, filename, filetype, callback) jsOMS.Asset.AssetManager.prototype.load = function (path, filetype, callback)
{ {
var hash; let hash;
if (!this.assets[(hash = jsOMS.hash(path + '/' + filename))]) { if (!this.assets[(hash = jsOMS.hash(path))]) {
var fileref = null; let fileref = null;
if (filetype === 'js') { if (filetype === 'js') {
fileref = document.createElement('script'); fileref = document.createElement('script');
fileref.setAttribute('type', 'text/javascript'); fileref.setAttribute('type', 'text/javascript');
fileref.setAttribute('src', path + '/' + filename); fileref.setAttribute('src', path);
if (typeof fileref !== 'undefined') { if (typeof fileref !== 'undefined') {
document.getElementsByTagName('head')[0].appendChild(fileref); document.getElementsByTagName('head')[0].appendChild(fileref);
} }
this.assets[hash] = path + '/' + filename; this.assets[hash] = path;
} else if (filetype === 'css') { } else if (filetype === 'css') {
fileref = document.createElement('link'); fileref = document.createElement('link');
fileref.setAttribute('rel', 'stylesheet'); fileref.setAttribute('rel', 'stylesheet');
fileref.setAttribute('type', 'text/css'); fileref.setAttribute('type', 'text/css');
fileref.setAttribute('href', path + '/' + filename); fileref.setAttribute('href', path);
if (typeof fileref !== 'undefined') { if (typeof fileref !== 'undefined') {
document.getElementsByTagName('head')[0].appendChild(fileref); document.getElementsByTagName('head')[0].appendChild(fileref);
} }
this.assets[hash] = path + '/' + filename; this.assets[hash] = path;
} else if (filetype === 'img') { } else if (filetype === 'img') {
this.assets[hash] = new Image(); this.assets[hash] = new Image();
this.assets[hash].src = path + '/' + filename; this.assets[hash].src = path;
} else if (filetype === 'audio') { } else if (filetype === 'audio') {
// TODO: implement audio asset // TODO: implement audio asset
} else if (filetype === 'video') { } else if (filetype === 'video') {
@ -80,7 +78,7 @@
if (callback) { if (callback) {
fileref.onreadystatechange = function () fileref.onreadystatechange = function ()
{ {
if (this.readyState == 'complete') { if (this.readyState === 'complete') {
callback(); callback();
} }
}; };

View File

@ -11,9 +11,10 @@
{ {
"use strict"; "use strict";
jsOMS.Autoloader = {}; jsOMS.Autoloader = {};
jsOMS.Autoloader.loaded = []; jsOMS.Autoloader.loaded = [];
jsOMS.Autoloader.namespaced = []; jsOMS.Autoloader.namespaced = [];
jsOMS.Autoloader.assetLoader = new jsOMS.Asset.AssetManager();
/** /**
* Define namespace * Define namespace
@ -85,7 +86,7 @@
* Include script * Include script
* *
* @param {string} file Script URI * @param {string} file Script URI
* @param {callback} callback Callback after script loading * @param {function} callback Callback after script loading
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
@ -96,7 +97,7 @@
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
if (jsOMS.Autoloader.loaded.indexOf(file) === -1) { if (jsOMS.Autoloader.loaded.indexOf(file) === -1) {
// todo: implement asset loading and pass callback this.assetLoader.load(file, 'js');
jsOMS.Autoloader.loaded.push(file); jsOMS.Autoloader.loaded.push(file);
} }