mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Improved module loader.
This commit is contained in:
parent
c1bd469086
commit
f28b0316f4
152
js/loader.js
152
js/loader.js
@ -14,13 +14,10 @@ var Loader = (function() {
|
|||||||
|
|
||||||
/* Create helper valiables */
|
/* Create helper valiables */
|
||||||
|
|
||||||
|
var loadedModuleFiles = [];
|
||||||
var loadedFiles = [];
|
var loadedFiles = [];
|
||||||
var moduleObjects = [];
|
var moduleObjects = [];
|
||||||
|
|
||||||
var totalFiles = 0;
|
|
||||||
var moduleLoadCount = 0;
|
|
||||||
var fileLoadCount = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/* Private Methods */
|
/* Private Methods */
|
||||||
|
|
||||||
@ -32,10 +29,19 @@ var Loader = (function() {
|
|||||||
|
|
||||||
var moduleData = getModuleData();
|
var moduleData = getModuleData();
|
||||||
|
|
||||||
for (var m in moduleData) {
|
var loadNextModule = function() {
|
||||||
var module = moduleData[m];
|
if (moduleData.length > 0) {
|
||||||
loadModule(module);
|
var nextModule = moduleData[0];
|
||||||
}
|
loadModule(nextModule, function() {
|
||||||
|
moduleData = moduleData.slice(1);
|
||||||
|
loadNextModule();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
startModules();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadNextModule();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* startModules()
|
/* startModules()
|
||||||
@ -94,33 +100,28 @@ var Loader = (function() {
|
|||||||
/* loadModule(module)
|
/* loadModule(module)
|
||||||
* Load modules via ajax request and create module objects.
|
* Load modules via ajax request and create module objects.
|
||||||
*
|
*
|
||||||
|
* argument callback function - Function called when done.
|
||||||
* argument module object - Information about the module we want to load.
|
* argument module object - Information about the module we want to load.
|
||||||
*/
|
*/
|
||||||
var loadModule = function(module) {
|
var loadModule = function(module, callback) {
|
||||||
Log.log('Loading module: <' + module.name + '> from: ' + module.path + '/' + module.file);
|
|
||||||
|
|
||||||
var url = module.path + '/' + module.file;
|
var url = module.path + '/' + module.file;
|
||||||
var moduleRequest = new XMLHttpRequest();
|
|
||||||
moduleRequest.open("GET", url, true);
|
|
||||||
moduleRequest.onreadystatechange = function() {
|
|
||||||
if(this.readyState === 4) {
|
|
||||||
if(this.status === 200) {
|
|
||||||
|
|
||||||
// FIXME:
|
var afterLoad = function() {
|
||||||
// Create the module by evaluating the response.
|
var moduleObject = Module.create(module.name);
|
||||||
// This might not be the best way.
|
bootstrapModule(module, moduleObject, function() {
|
||||||
var ModuleDefinition = eval(this.response);
|
callback();
|
||||||
var moduleObject = new ModuleDefinition();
|
});
|
||||||
|
|
||||||
bootstrapModule(module, moduleObject);
|
|
||||||
moduleProcessed();
|
|
||||||
} else {
|
|
||||||
Log.error("Could not load module: " + module.name);
|
|
||||||
moduleProcessed();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
moduleRequest.send();
|
|
||||||
|
if (loadedModuleFiles.indexOf(url) !== -1) {
|
||||||
|
afterLoad();
|
||||||
|
} else {
|
||||||
|
loadFile(url, function() {
|
||||||
|
loadedModuleFiles.push(url);
|
||||||
|
afterLoad();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* bootstrapModule(module, mObj)
|
/* bootstrapModule(module, mObj)
|
||||||
@ -128,14 +129,21 @@ var Loader = (function() {
|
|||||||
*
|
*
|
||||||
* argument module object - Information about the module we want to load.
|
* argument module object - Information about the module we want to load.
|
||||||
* argument mObj object - Modules instance.
|
* argument mObj object - Modules instance.
|
||||||
|
* argument callback function - Function called when done.
|
||||||
*/
|
*/
|
||||||
var bootstrapModule = function(module, mObj) {
|
var bootstrapModule = function(module, mObj, callback) {
|
||||||
Log.info('Bootstrapping module: ' + module.name);
|
Log.info('Bootstrapping module: ' + module.name);
|
||||||
|
|
||||||
mObj.setData(module);
|
mObj.setData(module);
|
||||||
|
|
||||||
mObj.loadScripts();
|
mObj.loadScripts(function() {
|
||||||
mObj.loadStyles();
|
Log.log('Scripts loaded for: ' + module.name);
|
||||||
|
mObj.loadStyles(function(){
|
||||||
|
Log.log('Styles loaded for: ' + module.name);
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
moduleObjects.push(mObj);
|
moduleObjects.push(mObj);
|
||||||
};
|
};
|
||||||
@ -144,9 +152,9 @@ var Loader = (function() {
|
|||||||
* Load a script or stylesheet by adding it to the dom.
|
* Load a script or stylesheet by adding it to the dom.
|
||||||
*
|
*
|
||||||
* argument fileName string - Path of the file we want to load.
|
* argument fileName string - Path of the file we want to load.
|
||||||
|
* argument callback function - Function called when done.
|
||||||
*/
|
*/
|
||||||
var loadFile = function(fileName) {
|
var loadFile = function(fileName, callback) {
|
||||||
totalFiles++;
|
|
||||||
|
|
||||||
var extension = fileName.slice((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
|
var extension = fileName.slice((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
|
||||||
|
|
||||||
@ -158,7 +166,7 @@ var Loader = (function() {
|
|||||||
script.type = "text/javascript";
|
script.type = "text/javascript";
|
||||||
script.src = fileName;
|
script.src = fileName;
|
||||||
script.onload = function() {
|
script.onload = function() {
|
||||||
fileProcessed();
|
if (typeof callback === 'function') {callback();}
|
||||||
};
|
};
|
||||||
|
|
||||||
document.getElementsByTagName("body")[0].appendChild(script);
|
document.getElementsByTagName("body")[0].appendChild(script);
|
||||||
@ -172,7 +180,7 @@ var Loader = (function() {
|
|||||||
stylesheet.type = "text/css";
|
stylesheet.type = "text/css";
|
||||||
stylesheet.href = fileName;
|
stylesheet.href = fileName;
|
||||||
stylesheet.onload = function() {
|
stylesheet.onload = function() {
|
||||||
fileProcessed();
|
if (typeof callback === 'function') {callback();}
|
||||||
};
|
};
|
||||||
|
|
||||||
document.getElementsByTagName("head")[0].appendChild(stylesheet);
|
document.getElementsByTagName("head")[0].appendChild(stylesheet);
|
||||||
@ -181,40 +189,6 @@ var Loader = (function() {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* fileProcessed()
|
|
||||||
* Increase the fileLoadCount and check if we are ready to start all modules.
|
|
||||||
*/
|
|
||||||
var fileProcessed = function() {
|
|
||||||
fileLoadCount++;
|
|
||||||
prepareForStart();
|
|
||||||
};
|
|
||||||
|
|
||||||
/* moduleProcessed()
|
|
||||||
* Increase the moduleLoadCount and check if we are ready to start all modules.
|
|
||||||
*/
|
|
||||||
var moduleProcessed = function() {
|
|
||||||
moduleLoadCount++;
|
|
||||||
prepareForStart();
|
|
||||||
};
|
|
||||||
|
|
||||||
/* prepareForStart()
|
|
||||||
* Check if all files and modules are loaded. If so, start all modules.
|
|
||||||
*/
|
|
||||||
var prepareForStart = function() {
|
|
||||||
if (moduleLoadCount !== getAllModules().length) {
|
|
||||||
Log.log("Waiting for all modules to be loaded.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileLoadCount !== totalFiles) {
|
|
||||||
Log.log("Waiting for all files to be loaded.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info('Ready to start!');
|
|
||||||
startModules();
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Public Methods */
|
/* Public Methods */
|
||||||
return {
|
return {
|
||||||
|
|
||||||
@ -228,21 +202,24 @@ var Loader = (function() {
|
|||||||
/* loadFile()
|
/* loadFile()
|
||||||
* Load a file (script or stylesheet).
|
* Load a file (script or stylesheet).
|
||||||
* Prevent double loading and search for files in the vendor folder.
|
* Prevent double loading and search for files in the vendor folder.
|
||||||
|
*
|
||||||
|
* argument fileName string - Path of the file we want to load.
|
||||||
|
* argument module Module Object - the module that calls the loadFile function.
|
||||||
|
* argument callback function - Function called when done.
|
||||||
*/
|
*/
|
||||||
loadFile: function(fileName, module) {
|
loadFile: function(fileName, module, callback) {
|
||||||
|
|
||||||
if (fileName.indexOf(config.paths.modules + '/') === 0) {
|
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
|
||||||
// This is a module specific files.
|
Log.log('File already loaded: ' + fileName);
|
||||||
// Load it and then return.
|
callback();
|
||||||
loadFile(fileName);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileName.indexOf('/') !== -1) {
|
if (fileName.indexOf('http://') === 0 || fileName.indexOf('https://') === 0 || fileName.indexOf('/') !== -1) {
|
||||||
// This is an external file.
|
// This is an absolute or relative path.
|
||||||
// External files will always be loaded.
|
|
||||||
// Load it and then return.
|
// Load it and then return.
|
||||||
loadFile(fileName);
|
loadedFiles.push(fileName.toLowerCase());
|
||||||
|
loadFile(fileName, callback);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,19 +227,14 @@ var Loader = (function() {
|
|||||||
// This file is available in the vendor folder.
|
// This file is available in the vendor folder.
|
||||||
// Load it from this vendor folder.
|
// Load it from this vendor folder.
|
||||||
loadedFiles.push(fileName.toLowerCase());
|
loadedFiles.push(fileName.toLowerCase());
|
||||||
loadFile(config.paths.vendor+'/'+vendor[fileName]);
|
loadFile(config.paths.vendor+'/'+vendor[fileName], callback);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadedFiles.indexOf(fileName.toLowerCase()) === -1) {
|
// File not loaded yet.
|
||||||
// File not loaded yet.
|
// Load it based on the module path.
|
||||||
// Load it based on the module path.
|
loadedFiles.push(fileName.toLowerCase());
|
||||||
loadedFiles.push(fileName.toLowerCase());
|
loadFile(module.file(fileName), callback);
|
||||||
loadFile(module.file(fileName));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.log('File already loaded: ' + fileName);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
80
js/module.js
80
js/module.js
@ -156,26 +156,50 @@ var Module = Class.extend({
|
|||||||
|
|
||||||
/* loadStyles()
|
/* loadStyles()
|
||||||
* Load all required stylesheets by requesting the MM object to load the files.
|
* Load all required stylesheets by requesting the MM object to load the files.
|
||||||
|
*
|
||||||
|
* argument callback function - Function called when done.
|
||||||
*/
|
*/
|
||||||
loadStyles: function() {
|
loadStyles: function(callback) {
|
||||||
|
var self = this;
|
||||||
var styles = this.getStyles();
|
var styles = this.getStyles();
|
||||||
for (var s in styles) {
|
|
||||||
var style = styles[s];
|
|
||||||
|
|
||||||
Loader.loadFile(style, this);
|
var loadNextStyle = function() {
|
||||||
}
|
if (styles.length > 0) {
|
||||||
|
var nextStyle = styles[0];
|
||||||
|
Loader.loadFile(nextStyle, self, function() {
|
||||||
|
styles = styles.slice(1);
|
||||||
|
loadNextStyle();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadNextStyle();
|
||||||
},
|
},
|
||||||
|
|
||||||
/* loadScripts()
|
/* loadScripts()
|
||||||
* Load all required scripts by requesting the MM object to load the files.
|
* Load all required scripts by requesting the MM object to load the files.
|
||||||
|
*
|
||||||
|
* argument callback function - Function called when done.
|
||||||
*/
|
*/
|
||||||
loadScripts: function() {
|
loadScripts: function(callback) {
|
||||||
|
var self = this;
|
||||||
var scripts = this.getScripts();
|
var scripts = this.getScripts();
|
||||||
for (var s in scripts) {
|
|
||||||
var script = scripts[s];
|
|
||||||
|
|
||||||
Loader.loadFile(script, this);
|
var loadNextScript = function() {
|
||||||
}
|
if (scripts.length > 0) {
|
||||||
|
var nextScript = scripts[0];
|
||||||
|
Loader.loadFile(nextScript, self, function() {
|
||||||
|
scripts = scripts.slice(1);
|
||||||
|
loadNextScript();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadNextScript();
|
||||||
},
|
},
|
||||||
|
|
||||||
/* updateDom(speed)
|
/* updateDom(speed)
|
||||||
@ -208,6 +232,38 @@ var Module = Class.extend({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Module.create = function(moduleDefinition) {
|
Module.definitions = {};
|
||||||
return Module.extend(moduleDefinition);
|
|
||||||
|
Module.create = function(name) {
|
||||||
|
|
||||||
|
//Define the clone method for later use.
|
||||||
|
function cloneObject(obj) {
|
||||||
|
if (obj === null || typeof obj !== 'object') {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
var temp = obj.constructor(); // give temp the original obj's constructor
|
||||||
|
for (var key in obj) {
|
||||||
|
temp[key] = cloneObject(obj[key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
var moduleDefinition = Module.definitions[name];
|
||||||
|
var clonedDefinition = cloneObject(moduleDefinition);
|
||||||
|
|
||||||
|
// Note that we clone the definition. Otherwise the objects are shared, which gives problems.
|
||||||
|
var ModuleClass = Module.extend(clonedDefinition);
|
||||||
|
|
||||||
|
return new ModuleClass();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Module.register = function(name, moduleDefinition) {
|
||||||
|
Log.log('Module registered: ' + name);
|
||||||
|
Module.definitions[name] = moduleDefinition;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('calendar',{
|
||||||
|
|
||||||
// Define module defaults
|
// Define module defaults
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('clock',{
|
||||||
|
|
||||||
// Module config defaults.
|
// Module config defaults.
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('compliments',{
|
||||||
|
|
||||||
// Module config defaults.
|
// Module config defaults.
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('currentweather',{
|
||||||
|
|
||||||
// Default module config.
|
// Default module config.
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('helloworld',{
|
||||||
|
|
||||||
// Default module config.
|
// Default module config.
|
||||||
defaults: {
|
defaults: {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('newsfeed',{
|
||||||
|
|
||||||
// Default module config.
|
// Default module config.
|
||||||
defaults: {
|
defaults: {
|
||||||
@ -35,6 +35,7 @@ Module.create({
|
|||||||
this.activeItem = 0;
|
this.activeItem = 0;
|
||||||
|
|
||||||
this.fetchNews();
|
this.fetchNews();
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Override socket notification handler.
|
// Override socket notification handler.
|
||||||
@ -53,7 +54,6 @@ Module.create({
|
|||||||
|
|
||||||
// Override dom generator.
|
// Override dom generator.
|
||||||
getDom: function() {
|
getDom: function() {
|
||||||
|
|
||||||
var wrapper = document.createElement("div");
|
var wrapper = document.createElement("div");
|
||||||
|
|
||||||
if (this.activeItem >= this.newsItems.length) {
|
if (this.activeItem >= this.newsItems.length) {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Module.create({
|
Module.register('weatherforecast',{
|
||||||
|
|
||||||
// Default module config.
|
// Default module config.
|
||||||
defaults: {
|
defaults: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user