MagicMirror/modules/default/alert/notificationFx.js

157 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-04-01 22:05:09 +02:00
/**
* Based on work by
2020-04-20 10:44:56 +02:00
*
2016-04-01 22:05:09 +02:00
* notificationFx.js v1.0.0
2020-04-28 23:05:28 +02:00
* https://tympanus.net/codrops/
2016-04-01 22:05:09 +02:00
*
* Licensed under the MIT license.
2020-04-28 23:05:28 +02:00
* https://opensource.org/licenses/mit-license.php
2016-04-05 14:35:11 -04:00
*
2016-04-01 22:05:09 +02:00
* Copyright 2014, Codrops
2020-04-28 23:05:28 +02:00
* https://tympanus.net/codrops/
2022-01-10 22:42:07 +01:00
* @param {object} window The window object
2016-04-01 22:05:09 +02:00
*/
(function (window) {
/**
2020-07-30 09:33:19 +02:00
* Extend one object with another one
* @param {object} a The object to extend
* @param {object} b The object which extends the other, overwrites existing keys
* @returns {object} The merged object
*/
function extend(a, b) {
for (let key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* NotificationFx constructor
* @param {object} options The configuration options
* @class
2016-04-01 22:05:09 +02:00
*/
2016-04-05 14:35:11 -04:00
function NotificationFx(options) {
this.options = extend({}, this.options);
extend(this.options, options);
2016-04-01 22:05:09 +02:00
this._init();
}
/**
* NotificationFx options
*/
NotificationFx.prototype.options = {
// element to which the notification will be appended
// defaults to the document.body
2016-04-05 14:35:11 -04:00
wrapper: document.body,
2016-04-01 22:05:09 +02:00
// the message
2016-04-05 14:35:11 -04:00
message: "yo!",
2016-04-01 22:05:09 +02:00
// layout type: growl|attached|bar|other
2016-04-05 14:35:11 -04:00
layout: "growl",
2016-04-01 22:05:09 +02:00
// effects for the specified layout:
// for growl layout: scale|slide|genie|jelly
// for attached layout: flip|bouncyflip
// for other layout: boxspinner|cornerexpand|loadingcircle|thumbslider
// ...
2016-04-05 14:35:11 -04:00
effect: "slide",
2016-04-01 22:05:09 +02:00
// notice, warning, error, success
// will add class ns-type-warning, ns-type-error or ns-type-success
2016-04-05 14:35:11 -04:00
type: "notice",
// if the user doesn´t close the notification then we remove it
2016-04-01 22:05:09 +02:00
// after the following time
2016-04-05 14:35:11 -04:00
ttl: 6000,
2016-04-03 03:04:38 +02:00
al_no: "ns-box",
2016-04-01 22:05:09 +02:00
// callbacks
onClose: function () {
return false;
},
onOpen: function () {
return false;
}
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* Initialize and cache some vars
2016-04-01 22:05:09 +02:00
*/
NotificationFx.prototype._init = function () {
2016-04-01 22:05:09 +02:00
// create HTML structure
2016-04-05 14:35:11 -04:00
this.ntf = document.createElement("div");
Release 2.23.0 (#3078) ## [2.23.0] - 2023-04-04 Thanks to: @angeldeejay, @buxxi, @CarJem, @dariom, @DaveChild, @dWoolridge, @grenagit, @Hirschberger, @KristjanESPERANTO, @MagMar94, @naveensrinivasan, @nfogal, @psieg, @rajniszp, @retroflex, @SkySails and @tomzt. Special thanks to @khassel, @rejas and @sdetweil for taking over most (if not all) of the work on this release as project collaborators. This version would not be there without their effort. Thank you guys! You are awesome! ### Added - Added increments for hourly forecasts in weather module (#2996) - Added tests for hourly weather forecast - Added possibility to ignore MagicMirror repo in updatenotification module - Added Pirate Weather as new weather provider (#3005) - Added possibility to use your own templates in Alert module - Added error message if `<modulename>.js` file is missing in module folder to get a hint in the logs (#2403) - Added possibility to use environment variables in `config.js` (#1756) - Added option `pastDaysCount` to default calendar module to control of how many days past events should be displayed - Added thai language to alert module - Added option `sendNotifications` in clock module (#3056) ### Removed - Removed darksky weather provider - Removed unneeded (and unwanted) '.' after the year in calendar repeatingCountTitle (#2896) ### Updated - Use develop as target branch for dependabot - Update issue template, contributing doc and sample config - The weather modules clearly separates precipitation amount and probability (risk of rain/snow) - This requires all providers that only supports probability to change the config from `showPrecipitationAmount` to `showPrecipitationProbability`. - Update tests for weather and calendar module - Changed updatenotification module for MagicMirror repo only: Send only notifications for `master` if there is a tag on a newer commit - Update dates in Calendar widgets every minute - Cleanup jest coverage for patches - Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues, update `main.css` matching new rules - Update Eslint config, add new rule and handle issue - Convert lots of callbacks to async/await - Revise require imports (#3071 and #3072) ### Fixed - Fix wrong day labels in envcanada forecast (#2987) - Fix for missing default class name prefix for customEvents in calendar - Fix electron flashing white screen on startup (#1919) - Fix weathergov provider hourly forecast (#3008) - Fix message display with HTML code into alert module (#2828) - Fix typo in french translation - Yr wind direction is no longer inverted - Fix async node_helper stopping electron start (#2487) - The wind direction arrow now points in the direction the wind is flowing, not into the wind (#3019) - Fix precipitation css styles and rounding value - Fix wrong vertical alignment of calendar title column when wrapEvents is true (#3053) - Fix empty news feed stopping the reload forever - Fix e2e tests (failed after async changes) by running calendar and newsfeed tests last - Lint: Use template literals instead of string concatenation - Fix default alert module to render HTML for title and message - Fix Open-Meteo wind speed units
2023-04-04 20:44:32 +02:00
this.ntf.className = `${this.options.al_no} ns-${this.options.layout} ns-effect-${this.options.effect} ns-type-${this.options.type}`;
let strinner = '<div class="ns-box-inner">';
2016-04-01 22:05:09 +02:00
strinner += this.options.message;
2016-04-05 14:35:11 -04:00
strinner += "</div>";
2016-04-01 22:05:09 +02:00
this.ntf.innerHTML = strinner;
// append to body or the element specified in options.wrapper
2016-04-05 14:35:11 -04:00
this.options.wrapper.insertBefore(this.ntf, this.options.wrapper.nextSibling);
2016-04-01 22:05:09 +02:00
// dismiss after [options.ttl]ms
2016-04-05 14:35:11 -04:00
if (this.options.ttl) {
2020-04-20 10:44:56 +02:00
this.dismissttl = setTimeout(() => {
if (this.active) {
this.dismiss();
}
}, this.options.ttl);
2016-04-03 03:04:38 +02:00
}
2016-04-01 22:05:09 +02:00
// init events
this._initEvents();
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* Init events
2016-04-01 22:05:09 +02:00
*/
NotificationFx.prototype._initEvents = function () {
2016-04-01 22:05:09 +02:00
// dismiss notification by tapping on it if someone has a touchscreen
this.ntf.querySelector(".ns-box-inner").addEventListener("click", () => {
this.dismiss();
});
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* Show the notification
2016-04-01 22:05:09 +02:00
*/
NotificationFx.prototype.show = function () {
2016-04-01 22:05:09 +02:00
this.active = true;
2020-04-20 10:44:56 +02:00
this.ntf.classList.remove("ns-hide");
this.ntf.classList.add("ns-show");
2016-04-01 22:05:09 +02:00
this.options.onOpen();
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* Dismiss the notification
2021-05-15 14:49:04 +02:00
* @param {boolean} [close] call the onClose callback at the end
2016-04-01 22:05:09 +02:00
*/
2021-04-10 20:38:33 -03:00
NotificationFx.prototype.dismiss = function (close = true) {
2016-04-01 22:05:09 +02:00
this.active = false;
2016-04-05 14:35:11 -04:00
clearTimeout(this.dismissttl);
2020-04-20 10:44:56 +02:00
this.ntf.classList.remove("ns-show");
setTimeout(() => {
this.ntf.classList.add("ns-hide");
2016-04-05 14:35:11 -04:00
2016-04-01 22:05:09 +02:00
// callback
2021-04-10 20:38:33 -03:00
if (close) this.options.onClose();
2016-04-05 14:35:11 -04:00
}, 25);
2016-04-01 22:05:09 +02:00
// after animation ends remove ntf from the DOM
2020-04-20 10:44:56 +02:00
const onEndAnimationFn = (ev) => {
if (ev.target !== this.ntf) {
return false;
}
2020-04-20 10:44:56 +02:00
this.ntf.removeEventListener("animationend", onEndAnimationFn);
2016-05-03 16:59:18 +02:00
2020-04-20 10:44:56 +02:00
if (ev.target.parentNode === this.options.wrapper) {
this.options.wrapper.removeChild(this.ntf);
2016-05-03 16:59:18 +02:00
}
2016-04-01 22:05:09 +02:00
};
2020-04-20 10:44:56 +02:00
this.ntf.addEventListener("animationend", onEndAnimationFn);
2016-04-05 14:35:11 -04:00
};
2016-04-01 22:05:09 +02:00
/**
2020-07-30 09:33:19 +02:00
* Add to global namespace
2016-04-01 22:05:09 +02:00
*/
window.NotificationFx = NotificationFx;
2016-04-05 14:35:11 -04:00
})(window);