2016-03-24 17:19:32 +01:00
|
|
|
/* global Log, Loader, Module, config, defaults */
|
|
|
|
/* jshint -W020 */
|
2014-02-19 16:45:36 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* Magic Mirror
|
|
|
|
* Main System
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2014-02-19 16:45:36 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
var MM = (function() {
|
2014-02-19 16:45:36 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
var modules = [];
|
2014-02-24 16:35:48 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* Private Methods */
|
2014-02-19 16:45:36 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* createDomObjects()
|
|
|
|
* Create dom objects for all modules that
|
|
|
|
* are configured for a specific position.
|
|
|
|
*/
|
|
|
|
var createDomObjects = function() {
|
|
|
|
for (var m in modules) {
|
|
|
|
var module = modules[m];
|
|
|
|
if (module.data.position) {
|
2014-02-26 14:14:29 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
var wrapper = selectWrapper(module.data.position);
|
2016-03-29 13:28:15 +02:00
|
|
|
|
|
|
|
if (typeof module.data.header !== 'undefined' && module.data.header !== '') {
|
|
|
|
var header = document.createElement("header");
|
|
|
|
header.innerHTML = module.data.header;
|
|
|
|
wrapper.appendChild(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dom = document.createElement("div");
|
|
|
|
dom.id = module.identifier;
|
|
|
|
dom.className = module.name;
|
2016-03-24 17:19:32 +01:00
|
|
|
wrapper.appendChild(dom);
|
2014-02-24 16:35:48 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
dom.appendChild(module.getDom());
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 16:35:48 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
sendNotification('DOM_OBJECTS_CREATED');
|
|
|
|
};
|
2014-04-21 16:51:21 +02:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* selectWrapper(position)
|
|
|
|
* Select the wrapper dom object for a specific position.
|
|
|
|
*
|
|
|
|
* argument position string - The name of the position.
|
|
|
|
*/
|
|
|
|
var selectWrapper = function(position) {
|
|
|
|
var classes = position.replace('_',' ');
|
|
|
|
var parentWrapper = document.getElementsByClassName(classes);
|
|
|
|
if (parentWrapper.length > 0) {
|
|
|
|
var wrapper = parentWrapper[0].getElementsByClassName('container');
|
|
|
|
if (wrapper.length > 0) {
|
|
|
|
return wrapper[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-02-19 17:02:17 +01:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* sendNotification(notification, payload, sender)
|
|
|
|
* Send a notification to all modules.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the noitication.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
* argument sender Module - The module that sent the notification.
|
|
|
|
*/
|
|
|
|
var sendNotification = function(notification, payload, sender) {
|
|
|
|
for (var m in modules) {
|
|
|
|
var module = modules[m];
|
|
|
|
if (module !== sender) {
|
|
|
|
module.notificationReceived(notification, payload, sender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* updateDom(module, speed)
|
|
|
|
* Update the dom for a specific module.
|
|
|
|
*
|
|
|
|
* argument module Module - The module that needs an update.
|
|
|
|
* argument speed Number - The number of microseconds for the animation. (optional)
|
|
|
|
*/
|
|
|
|
var updateDom = function(module, speed) {
|
|
|
|
var wrapper = document.getElementById(module.identifier);
|
2016-03-29 13:28:15 +02:00
|
|
|
var newContent = module.getDom();
|
|
|
|
|
|
|
|
var tempWrapper = document.createElement('div');
|
|
|
|
tempWrapper.appendChild(newContent);
|
|
|
|
|
|
|
|
if (tempWrapper.innerHTML === wrapper.innerHTML) {
|
|
|
|
// Content did not change. Abort update.
|
|
|
|
return;
|
|
|
|
}
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
if (!speed) {
|
|
|
|
wrapper.innerHTML = null;
|
2016-03-29 13:28:15 +02:00
|
|
|
wrapper.appendChild(newContent);
|
2016-03-24 17:19:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper.style.opacity = 1;
|
|
|
|
wrapper.style.transition = "opacity " + speed / 2 / 1000 + "s";
|
|
|
|
wrapper.style.opacity = 0;
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
wrapper.innerHTML = null;
|
2016-03-29 13:28:15 +02:00
|
|
|
wrapper.appendChild(newContent);
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
wrapper.style.opacity = 1;
|
|
|
|
}, speed / 2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/* loadConfig()
|
|
|
|
* Loads the core config and combines it with de system defaults.
|
|
|
|
*/
|
|
|
|
var loadConfig = function() {
|
|
|
|
if (typeof config === 'undefined') {
|
|
|
|
config = defaults;
|
|
|
|
Log.error('Config file is missing! Please create a config file.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
config = Object.assign(defaults, config);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
/* Public Methods */
|
|
|
|
|
|
|
|
/* init()
|
|
|
|
* Main init method.
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
Log.info('Initializing MagicMirror.');
|
|
|
|
loadConfig();
|
|
|
|
Loader.loadModules();
|
|
|
|
},
|
|
|
|
|
|
|
|
/* modulesStarted(moduleObjects)
|
|
|
|
* Gets called when all modules are started.
|
|
|
|
*
|
|
|
|
* argument moduleObjects array<Module> - All module instances.
|
|
|
|
*/
|
|
|
|
modulesStarted: function(moduleObjects) {
|
|
|
|
modules = [];
|
|
|
|
for (var m in moduleObjects) {
|
|
|
|
var module = moduleObjects[m];
|
|
|
|
modules[module.data.index] = module;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.info('All modules started!');
|
|
|
|
sendNotification('ALL_MODULES_STARTED');
|
|
|
|
|
|
|
|
createDomObjects();
|
|
|
|
},
|
|
|
|
|
|
|
|
/* sendNotification(notification, payload, sender)
|
|
|
|
* Send a notification to all modules.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the noitication.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
* argument sender Module - The module that sent the notification.
|
|
|
|
*/
|
|
|
|
sendNotification: function(notification, payload, sender) {
|
|
|
|
if (arguments.length < 3) {
|
|
|
|
Log.error('sendNotification: Missing arguments.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof notification !== 'string') {
|
|
|
|
Log.error('sendNotification: Notification should be a string.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sender instanceof Module)) {
|
|
|
|
Log.error('sendNotification: Sender should be a module.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Further implementation is done in the private method.
|
|
|
|
sendNotification(notification, payload, sender);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* updateDom(module, speed)
|
|
|
|
* Update the dom for a specific module.
|
|
|
|
*
|
|
|
|
* argument module Module - The module that needs an update.
|
|
|
|
* argument speed Number - The number of microseconds for the animation. (optional)
|
|
|
|
*/
|
|
|
|
updateDom: function(module, speed) {
|
|
|
|
if (!(module instanceof Module)) {
|
|
|
|
Log.error('updateDom: Sender should be a module.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Further implementation is done in the private method.
|
|
|
|
updateDom(module, speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
MM.init();
|
2014-02-24 11:19:51 +01:00
|
|
|
|
2014-02-26 14:14:29 +01:00
|
|
|
|
2014-02-19 16:45:36 +01:00
|
|
|
|
|
|
|
|
2014-05-22 19:48:46 +02:00
|
|
|
|