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. *
|
|
|
|
*********************************************************/
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
// Set the minimum MagicMirror module version for this module.
|
|
|
|
requiresVersion: "2.0.0",
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
// Module config defaults.
|
|
|
|
defaults: {},
|
|
|
|
|
2016-04-08 17:27:02 +02:00
|
|
|
// Timer reference used for showHide animation callbacks.
|
|
|
|
showHideTimer: null,
|
|
|
|
|
2016-10-13 15:00:59 +02:00
|
|
|
// Array to store lockStrings. These stings are used to lock
|
|
|
|
// visibility when hiding and showing module.
|
|
|
|
lockStrings: [],
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* init()
|
|
|
|
* Is called when the module is instantiated.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
init: function () {
|
2016-03-24 17:19:32 +01:00
|
|
|
//Log.log(this.defaults);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* start()
|
|
|
|
* Is called when the module is started.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
getScripts: function () {
|
2016-03-24 17:19:32 +01:00
|
|
|
return [];
|
|
|
|
},
|
|
|
|
|
|
|
|
/* getStyles()
|
|
|
|
* Returns a list of stylesheets the module requires to be loaded.
|
|
|
|
*
|
|
|
|
* return Array<String> - An array with filenames.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
getStyles: function () {
|
2016-03-24 17:19:32 +01:00
|
|
|
return [];
|
|
|
|
},
|
|
|
|
|
2016-04-21 01:03:26 +02:00
|
|
|
/* getTranslations()
|
|
|
|
* Returns a map of translation files the module requires to be loaded.
|
|
|
|
*
|
|
|
|
* return Map<String, String> - A map with langKeys and filenames.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
getTranslations: function () {
|
2016-05-11 12:38:41 +02:00
|
|
|
return false;
|
2016-04-21 01:03:26 +02:00
|
|
|
},
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
getDom: function () {
|
2016-03-24 17:19:32 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2016-09-20 17:22:24 +02:00
|
|
|
/* getHeader()
|
|
|
|
* This method generates the header string which needs to be displayed if a user has a header configured for this module.
|
|
|
|
* This method is called by the Magic Mirror core, but only if the user has configured a default header for the module.
|
|
|
|
* This method needs to be subclassed if the module wants to display modified headers on the mirror.
|
|
|
|
*
|
|
|
|
* return string - The header to display above the header.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
getHeader: function () {
|
2016-09-20 17:22:24 +02:00
|
|
|
return this.data.header;
|
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
notificationReceived: function (notification, payload, sender) {
|
2016-03-24 17:19:32 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
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
|
|
|
|
2016-05-11 13:49:40 +02:00
|
|
|
/* suspend()
|
|
|
|
* This method is called when a module is hidden.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
suspend: function () {
|
2016-07-20 13:31:46 -06:00
|
|
|
Log.log(this.name + " is suspended.");
|
2016-05-11 13:49:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* resume()
|
|
|
|
* This method is called when a module is shown.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
resume: function () {
|
2016-05-11 13:49:40 +02:00
|
|
|
Log.log(this.name + " is resumed.");
|
|
|
|
},
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/*********************************************
|
2016-05-03 19:09:38 -04:00
|
|
|
* The methods below don"t need subclassing. *
|
2016-03-24 17:19:32 +01:00
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
/* setData(data)
|
|
|
|
* Set the module data.
|
|
|
|
*
|
|
|
|
* argument data obejct - Module data.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
setData: function (data) {
|
2016-03-24 17:19:32 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
setConfig: function (config) {
|
2016-10-31 20:10:52 +01:00
|
|
|
this.config = Object.assign({}, this.defaults, config);
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
2016-03-30 12:20:46 +02:00
|
|
|
/* socket()
|
2016-06-04 20:32:55 -06:00
|
|
|
* Returns a socket object. If it doesn"t exist, it"s created.
|
2016-03-30 12:20:46 +02:00
|
|
|
* It also registers the notification callback.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
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;
|
2016-10-13 16:42:15 +02:00
|
|
|
this._socket.setNotificationCallback(function (notification, payload) {
|
2016-03-30 12:20:46 +02:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
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-10-13 16:42:15 +02:00
|
|
|
loadStyles: function (callback) {
|
2016-03-31 13:05:23 +02:00
|
|
|
var self = this;
|
2016-03-24 17:19:32 +01:00
|
|
|
var styles = this.getStyles();
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
var loadNextStyle = function () {
|
2016-03-31 13:05:23 +02:00
|
|
|
if (styles.length > 0) {
|
|
|
|
var nextStyle = styles[0];
|
2016-10-13 16:42:15 +02:00
|
|
|
Loader.loadFile(nextStyle, self, function () {
|
2016-03-31 13:05:23 +02:00
|
|
|
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-10-13 16:42:15 +02:00
|
|
|
loadScripts: function (callback) {
|
2016-03-31 13:05:23 +02:00
|
|
|
var self = this;
|
2016-03-24 17:19:32 +01:00
|
|
|
var scripts = this.getScripts();
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
var loadNextScript = function () {
|
2016-03-31 13:05:23 +02:00
|
|
|
if (scripts.length > 0) {
|
|
|
|
var nextScript = scripts[0];
|
2016-10-13 16:42:15 +02:00
|
|
|
Loader.loadFile(nextScript, self, function () {
|
2016-03-31 13:05:23 +02:00
|
|
|
scripts = scripts.slice(1);
|
|
|
|
loadNextScript();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loadNextScript();
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
2016-04-21 01:03:26 +02:00
|
|
|
/* loadScripts()
|
|
|
|
* Load all required scripts by requesting the MM object to load the files.
|
|
|
|
*
|
|
|
|
* argument callback function - Function called when done.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
loadTranslations: function (callback) {
|
2016-04-21 01:03:26 +02:00
|
|
|
var self = this;
|
|
|
|
var translations = this.getTranslations();
|
2016-05-11 12:38:41 +02:00
|
|
|
var lang = config.language.toLowerCase();
|
|
|
|
|
|
|
|
// The variable `first` will contain the first
|
|
|
|
// defined translation after the following line.
|
2016-10-13 16:42:15 +02:00
|
|
|
for (var first in translations) { break; }
|
2016-05-11 12:38:41 +02:00
|
|
|
|
|
|
|
if (translations) {
|
|
|
|
var translationFile = translations[lang] || undefined;
|
|
|
|
var translationsFallbackFile = translations[first];
|
|
|
|
|
|
|
|
// If a translation file is set, load it and then also load the fallback translation file.
|
|
|
|
// Otherwise only load the fallback translation file.
|
2016-08-02 20:14:24 +00:00
|
|
|
if (translationFile !== undefined && translationFile !== translationsFallbackFile) {
|
2016-10-13 16:42:15 +02:00
|
|
|
Translator.load(self, translationFile, false, function () {
|
2016-05-11 12:38:41 +02:00
|
|
|
Translator.load(self, translationsFallbackFile, true, callback);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Translator.load(self, translationsFallbackFile, true, callback);
|
|
|
|
}
|
2016-04-21 01:03:26 +02:00
|
|
|
} else {
|
2016-05-03 19:09:38 -04:00
|
|
|
callback();
|
2016-04-21 01:03:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
/* translate(key, defaultValue)
|
|
|
|
* Request the translation for a given key.
|
|
|
|
*
|
|
|
|
* argument key string - The key of the string to translage
|
|
|
|
* argument defaultValue string - The default value if no translation was found. (Optional)
|
|
|
|
*/
|
|
|
|
translate: function (key, defaultValue) {
|
2016-05-03 19:09:38 -04:00
|
|
|
return Translator.translate(this, key) || defaultValue || "";
|
2016-04-21 01:03:26 +02:00
|
|
|
},
|
|
|
|
|
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)
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
updateDom: function (speed) {
|
2016-03-24 17:19:32 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
sendNotification: function (notification, payload) {
|
2016-03-24 17:19:32 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
sendSocketNotification: function (notification, payload) {
|
2016-03-30 12:20:46 +02:00
|
|
|
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.
|
2016-10-13 15:00:59 +02:00
|
|
|
* argument options object - Optional settings for the hide method.
|
2016-03-31 19:15:58 +02:00
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
hide: function (speed, callback, options) {
|
2016-10-13 15:00:59 +02:00
|
|
|
if (typeof callback === "object") {
|
|
|
|
options = callback;
|
2016-10-13 16:42:15 +02:00
|
|
|
callback = function () { };
|
2016-10-13 15:00:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
callback = callback || function () { };
|
2016-10-13 15:00:59 +02:00
|
|
|
options = options || {};
|
2016-05-11 13:49:40 +02:00
|
|
|
|
|
|
|
var self = this;
|
2016-10-13 16:42:15 +02:00
|
|
|
MM.hideModule(self, speed, function () {
|
2016-05-11 13:49:40 +02:00
|
|
|
self.suspend();
|
|
|
|
callback();
|
2016-10-13 15:00:59 +02:00
|
|
|
}, options);
|
2016-03-31 19:15:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* 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.
|
2016-10-13 15:00:59 +02:00
|
|
|
* argument options object - Optional settings for the hide method.
|
2016-03-31 19:15:58 +02:00
|
|
|
*/
|
2016-10-13 16:42:15 +02:00
|
|
|
show: function (speed, callback, options) {
|
2016-10-13 15:00:59 +02:00
|
|
|
if (typeof callback === "object") {
|
|
|
|
options = callback;
|
2016-10-13 16:42:15 +02:00
|
|
|
callback = function () { };
|
2016-10-13 15:00:59 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
callback = callback || function () { };
|
2016-10-13 15:00:59 +02:00
|
|
|
options = options || {};
|
|
|
|
|
2016-05-11 13:49:40 +02:00
|
|
|
this.resume();
|
2016-10-13 15:00:59 +02:00
|
|
|
MM.showModule(this, speed, callback, options);
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-31 13:05:23 +02:00
|
|
|
Module.definitions = {};
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
Module.create = function (name) {
|
|
|
|
|
|
|
|
// Make sure module definition is available.
|
|
|
|
if (!Module.definitions[name]) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
/* cmpVersions(a,b)
|
|
|
|
* Compare two symantic version numbers and return the difference.
|
|
|
|
*
|
|
|
|
* argument a string - Version number a.
|
|
|
|
* argument a string - Version number b.
|
|
|
|
*/
|
|
|
|
function cmpVersions(a, b) {
|
|
|
|
var i, diff;
|
|
|
|
var regExStrip0 = /(\.0+)+$/;
|
|
|
|
var segmentsA = a.replace(regExStrip0, "").split(".");
|
|
|
|
var segmentsB = b.replace(regExStrip0, "").split(".");
|
|
|
|
var l = Math.min(segmentsA.length, segmentsB.length);
|
|
|
|
|
|
|
|
for (i = 0; i < l; i++) {
|
|
|
|
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
|
|
|
|
if (diff) {
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return segmentsA.length - segmentsB.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
Module.register = function (name, moduleDefinition) {
|
|
|
|
|
|
|
|
if (moduleDefinition.requiresVersion) {
|
|
|
|
Log.log("Check MagicMirror version for module '" + name + "' - Minimum version: " + moduleDefinition.requiresVersion + " - Current version: " + version);
|
|
|
|
if (cmpVersions(version, moduleDefinition.requiresVersion) >= 0) {
|
|
|
|
Log.log("Version is ok!");
|
|
|
|
} else {
|
|
|
|
Log.log("Version is incorrect. Skip module: '" + name + "'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
};
|