From 30ba6233a07de44c724cb9f122f0ef594df203ce Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 26 Jun 2016 16:24:59 +0200 Subject: [PATCH] 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?! --- Asset/AssetManager.js | 24 +++++++++++------------- Autoloader.js | 13 +++++++------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Asset/AssetManager.js b/Asset/AssetManager.js index bdf414c..5317ac1 100644 --- a/Asset/AssetManager.js +++ b/Asset/AssetManager.js @@ -11,8 +11,7 @@ { "use strict"; - /** @namespace jsOMS.Asset */ - jsOMS.Autoloader.defineNamespace('jsOMS.Asset'); + jsOMS.Asset = {}; /** * @constructor @@ -29,7 +28,6 @@ * Load asset. * * @param {string} path Asset path - * @param {string} filename Name of the asset * @param {string} filetype Filetype of the asset * @param {requestCallback} [callback] Callback after load * @@ -40,37 +38,37 @@ * @since 1.0.0 * @author Dennis Eichhorn */ - 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))]) { - var fileref = null; + if (!this.assets[(hash = jsOMS.hash(path))]) { + let fileref = null; if (filetype === 'js') { fileref = document.createElement('script'); fileref.setAttribute('type', 'text/javascript'); - fileref.setAttribute('src', path + '/' + filename); + fileref.setAttribute('src', path); if (typeof fileref !== 'undefined') { document.getElementsByTagName('head')[0].appendChild(fileref); } - this.assets[hash] = path + '/' + filename; + this.assets[hash] = path; } else if (filetype === 'css') { fileref = document.createElement('link'); fileref.setAttribute('rel', 'stylesheet'); fileref.setAttribute('type', 'text/css'); - fileref.setAttribute('href', path + '/' + filename); + fileref.setAttribute('href', path); if (typeof fileref !== 'undefined') { document.getElementsByTagName('head')[0].appendChild(fileref); } - this.assets[hash] = path + '/' + filename; + this.assets[hash] = path; } else if (filetype === 'img') { this.assets[hash] = new Image(); - this.assets[hash].src = path + '/' + filename; + this.assets[hash].src = path; } else if (filetype === 'audio') { // TODO: implement audio asset } else if (filetype === 'video') { @@ -80,7 +78,7 @@ if (callback) { fileref.onreadystatechange = function () { - if (this.readyState == 'complete') { + if (this.readyState === 'complete') { callback(); } }; diff --git a/Autoloader.js b/Autoloader.js index 1658797..9ae8960 100644 --- a/Autoloader.js +++ b/Autoloader.js @@ -10,10 +10,11 @@ (function (jsOMS) { "use strict"; - - jsOMS.Autoloader = {}; - jsOMS.Autoloader.loaded = []; - jsOMS.Autoloader.namespaced = []; + + jsOMS.Autoloader = {}; + jsOMS.Autoloader.loaded = []; + jsOMS.Autoloader.namespaced = []; + jsOMS.Autoloader.assetLoader = new jsOMS.Asset.AssetManager(); /** * Define namespace @@ -85,7 +86,7 @@ * Include script * * @param {string} file Script URI - * @param {callback} callback Callback after script loading + * @param {function} callback Callback after script loading * * @since 1.0.0 * @author Dennis Eichhorn @@ -96,7 +97,7 @@ for (let i = 0; i < length; i++) { if (jsOMS.Autoloader.loaded.indexOf(file) === -1) { - // todo: implement asset loading and pass callback + this.assetLoader.load(file, 'js'); jsOMS.Autoloader.loaded.push(file); }