2016-03-24 17:19:32 +01:00
|
|
|
/* global Log, Class, Loader, Class , MM */
|
|
|
|
/* exported Module */
|
|
|
|
|
|
|
|
/* Magic Mirror
|
|
|
|
* Module Blueprint.
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Module = Class.extend({
|
|
|
|
|
|
|
|
/*********************************************************
|
|
|
|
* All methods (and properties) below can be subclassed. *
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
// Module config defaults.
|
|
|
|
defaults: {},
|
|
|
|
|
2016-04-08 17:27:02 +02:00
|
|
|
// Timer reference used for showHide animation callbacks.
|
|
|
|
showHideTimer: null,
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* init()
|
|
|
|
* Is called when the module is instantiated.
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
//Log.log(this.defaults);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* start()
|
|
|
|
* Is called when the module is started.
|
|
|
|
*/
|
|
|
|
start: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.info("Starting module: " + this.name);
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/* getScripts()
|
|
|
|
* Returns a list of scripts the module requires to be loaded.
|
|
|
|
*
|
|
|
|
* return Array<String> - An array with filenames.
|
|
|
|
*/
|
|
|
|
getScripts: function() {
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
|
|
|
|
/* getStyles()
|
|
|
|
* Returns a list of stylesheets the module requires to be loaded.
|
|
|
|
*
|
|
|
|
* return Array<String> - An array with filenames.
|
|
|
|
*/
|
|
|
|
getStyles: function() {
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
|
|
|
|
/* getDom()
|
|
|
|
* This method generates the dom which needs to be displayed. This method is called by the Magic Mirror core.
|
|
|
|
* This method needs to be subclassed if the module wants to display info on the mirror.
|
|
|
|
*
|
|
|
|
* return domobject - The dom to display.
|
|
|
|
*/
|
|
|
|
getDom: function() {
|
|
|
|
var nameWrapper = document.createElement("div");
|
2016-04-03 19:52:13 +02:00
|
|
|
var name = document.createTextNode(this.name);
|
2016-03-24 17:19:32 +01:00
|
|
|
nameWrapper.appendChild(name);
|
|
|
|
|
|
|
|
var identifierWrapper = document.createElement("div");
|
2016-04-03 19:52:13 +02:00
|
|
|
var identifier = document.createTextNode(this.identifier);
|
2016-03-24 17:19:32 +01:00
|
|
|
identifierWrapper.appendChild(identifier);
|
|
|
|
identifierWrapper.className = "small dimmed";
|
|
|
|
|
|
|
|
var div = document.createElement("div");
|
|
|
|
div.appendChild(nameWrapper);
|
|
|
|
div.appendChild(identifierWrapper);
|
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
return div;
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/* notificationReceived(notification, payload, sender)
|
|
|
|
* This method is called when a notification arrives.
|
|
|
|
* This method is called by the Magic Mirror core.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
notificationReceived: function(notification, payload, sender) {
|
|
|
|
if (sender) {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
|
2016-03-24 17:19:32 +01:00
|
|
|
} else {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.log(this.name + " received a system notification: " + notification);
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-30 12:20:46 +02:00
|
|
|
/* socketNotificationReceived(notification, payload)
|
|
|
|
* This method is called when a socket notification arrives.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the noitication.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
*/
|
|
|
|
socketNotificationReceived: function(notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
|
2016-03-30 12:20:46 +02:00
|
|
|
},
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
/*********************************************
|
|
|
|
* The methods below don't need subclassing. *
|
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
/* setData(data)
|
|
|
|
* Set the module data.
|
|
|
|
*
|
|
|
|
* argument data obejct - Module data.
|
|
|
|
*/
|
|
|
|
setData: function(data) {
|
|
|
|
this.data = data;
|
|
|
|
this.name = data.name;
|
|
|
|
this.identifier = data.identifier;
|
2016-03-31 19:15:58 +02:00
|
|
|
this.hidden = false;
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
this.setConfig(data.config);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* setConfig(config)
|
|
|
|
* Set the module config and combine it with the module defaults.
|
|
|
|
*
|
|
|
|
* argument config obejct - Module config.
|
|
|
|
*/
|
|
|
|
setConfig: function(config) {
|
|
|
|
this.config = Object.assign(this.defaults, config);
|
|
|
|
},
|
|
|
|
|
2016-03-30 12:20:46 +02:00
|
|
|
/* socket()
|
|
|
|
* Returns a socket object. If it doesn't exsist, it's created.
|
|
|
|
* It also registers the notification callback.
|
|
|
|
*/
|
|
|
|
socket: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (typeof this._socket === "undefined") {
|
2016-03-30 12:20:46 +02:00
|
|
|
this._socket = this._socket = new MMSocket(this.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this._socket.setNotificationCallback(function(notification, payload) {
|
|
|
|
self.socketNotificationReceived(notification, payload);
|
|
|
|
});
|
|
|
|
|
|
|
|
return this._socket;
|
|
|
|
},
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* file(file)
|
2016-04-20 23:13:31 +02:00
|
|
|
* Retrieve the path to a module file.
|
2016-03-24 17:19:32 +01:00
|
|
|
*
|
|
|
|
* argument file string - Filename.
|
|
|
|
*
|
|
|
|
* return string - File path.
|
|
|
|
*/
|
|
|
|
file: function(file) {
|
2016-04-05 14:35:11 -04:00
|
|
|
return this.data.path + "/" + file;
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/* loadStyles()
|
|
|
|
* Load all required stylesheets by requesting the MM object to load the files.
|
2016-03-31 13:05:23 +02:00
|
|
|
*
|
|
|
|
* argument callback function - Function called when done.
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2016-03-31 13:05:23 +02:00
|
|
|
loadStyles: function(callback) {
|
|
|
|
var self = this;
|
2016-03-24 17:19:32 +01:00
|
|
|
var styles = this.getStyles();
|
|
|
|
|
2016-03-31 13:05:23 +02:00
|
|
|
var loadNextStyle = function() {
|
|
|
|
if (styles.length > 0) {
|
|
|
|
var nextStyle = styles[0];
|
|
|
|
Loader.loadFile(nextStyle, self, function() {
|
|
|
|
styles = styles.slice(1);
|
|
|
|
loadNextStyle();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loadNextStyle();
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/* loadScripts()
|
|
|
|
* Load all required scripts by requesting the MM object to load the files.
|
2016-03-31 13:05:23 +02:00
|
|
|
*
|
|
|
|
* argument callback function - Function called when done.
|
2016-03-24 17:19:32 +01:00
|
|
|
*/
|
2016-03-31 13:05:23 +02:00
|
|
|
loadScripts: function(callback) {
|
|
|
|
var self = this;
|
2016-03-24 17:19:32 +01:00
|
|
|
var scripts = this.getScripts();
|
|
|
|
|
2016-03-31 13:05:23 +02:00
|
|
|
var loadNextScript = function() {
|
|
|
|
if (scripts.length > 0) {
|
|
|
|
var nextScript = scripts[0];
|
|
|
|
Loader.loadFile(nextScript, self, function() {
|
|
|
|
scripts = scripts.slice(1);
|
|
|
|
loadNextScript();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loadNextScript();
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/* updateDom(speed)
|
|
|
|
* Request an (animated) update of the module.
|
|
|
|
*
|
|
|
|
* argument speed Number - The speed of the animation. (Optional)
|
|
|
|
*/
|
|
|
|
updateDom: function(speed) {
|
|
|
|
MM.updateDom(this, speed);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* sendNotification(notification, payload)
|
|
|
|
* Send a notification to all modules.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the noitication.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
*/
|
|
|
|
sendNotification: function(notification, payload) {
|
|
|
|
MM.sendNotification(notification, payload, this);
|
2016-03-30 12:20:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* sendSocketNotification(notification, payload)
|
|
|
|
* Send a socket notification to the node helper.
|
|
|
|
*
|
|
|
|
* argument notification string - The identifier of the noitication.
|
|
|
|
* argument payload mixed - The payload of the notification.
|
|
|
|
*/
|
|
|
|
sendSocketNotification: function(notification, payload) {
|
|
|
|
this.socket().sendNotification(notification, payload);
|
2016-03-31 19:15:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* hideModule(module, speed, callback)
|
|
|
|
* Hide this module.
|
|
|
|
*
|
|
|
|
* argument speed Number - The speed of the hide animation.
|
|
|
|
* argument callback function - Called when the animation is done.
|
|
|
|
*/
|
|
|
|
hide: function(speed, callback) {
|
|
|
|
MM.hideModule(this, speed, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* showModule(module, speed, callback)
|
|
|
|
* Show this module.
|
|
|
|
*
|
|
|
|
* argument speed Number - The speed of the show animation.
|
|
|
|
* argument callback function - Called when the animation is done.
|
|
|
|
*/
|
|
|
|
show: function(speed, callback) {
|
|
|
|
MM.showModule(this, speed, callback);
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-31 13:05:23 +02:00
|
|
|
Module.definitions = {};
|
|
|
|
|
|
|
|
Module.create = function(name) {
|
|
|
|
|
|
|
|
//Define the clone method for later use.
|
|
|
|
function cloneObject(obj) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (obj === null || typeof obj !== "object") {
|
|
|
|
return obj;
|
|
|
|
}
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
var temp = obj.constructor(); // give temp the original obj's constructor
|
|
|
|
for (var key in obj) {
|
|
|
|
temp[key] = cloneObject(obj[key]);
|
|
|
|
}
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
return temp;
|
2016-03-31 13:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-31 13:05:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Module.register = function(name, moduleDefinition) {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.log("Module registered: " + name);
|
2016-03-31 13:05:23 +02:00
|
|
|
Module.definitions[name] = moduleDefinition;
|
2016-03-24 17:19:32 +01:00
|
|
|
};
|