Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

alert.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* global NotificationFx */
  2. /* Magic Mirror
  3. * Module: alert
  4. *
  5. * By Paul-Vincent Roll https://paulvincentroll.com/
  6. * MIT Licensed.
  7. */
  8. Module.register("alert", {
  9. defaults: {
  10. // scale|slide|genie|jelly|flip|bouncyflip|exploader
  11. effect: "slide",
  12. // scale|slide|genie|jelly|flip|bouncyflip|exploader
  13. alert_effect: "jelly",
  14. //time a notification is displayed in seconds
  15. display_time: 3500,
  16. //Position
  17. position: "center",
  18. //shown at startup
  19. welcome_message: false
  20. },
  21. getScripts: function () {
  22. return ["notificationFx.js"];
  23. },
  24. getStyles: function () {
  25. return ["notificationFx.css", "font-awesome.css"];
  26. },
  27. // Define required translations.
  28. getTranslations: function () {
  29. return {
  30. en: "translations/en.json",
  31. de: "translations/de.json",
  32. nl: "translations/nl.json"
  33. };
  34. },
  35. show_notification: function (message) {
  36. if (this.config.effect === "slide") {
  37. this.config.effect = this.config.effect + "-" + this.config.position;
  38. }
  39. let msg = "";
  40. if (message.title) {
  41. msg += "<span class='thin dimmed medium'>" + message.title + "</span>";
  42. }
  43. if (message.message) {
  44. if (msg !== "") {
  45. msg += "<br />";
  46. }
  47. msg += "<span class='light bright small'>" + message.message + "</span>";
  48. }
  49. new NotificationFx({
  50. message: msg,
  51. layout: "growl",
  52. effect: this.config.effect,
  53. ttl: message.timer !== undefined ? message.timer : this.config.display_time
  54. }).show();
  55. },
  56. show_alert: function (params, sender) {
  57. let image = "";
  58. //Set standard params if not provided by module
  59. if (typeof params.timer === "undefined") {
  60. params.timer = null;
  61. }
  62. if (typeof params.imageHeight === "undefined") {
  63. params.imageHeight = "80px";
  64. }
  65. if (typeof params.imageUrl === "undefined" && typeof params.imageFA === "undefined") {
  66. params.imageUrl = null;
  67. } else if (typeof params.imageFA === "undefined") {
  68. image = "<img src='" + params.imageUrl.toString() + "' height='" + params.imageHeight.toString() + "' style='margin-bottom: 10px;'/><br />";
  69. } else if (typeof params.imageUrl === "undefined") {
  70. image = "<span class='bright " + "fa fa-" + params.imageFA + "' style='margin-bottom: 10px;font-size:" + params.imageHeight.toString() + ";'/></span><br />";
  71. }
  72. //Create overlay
  73. const overlay = document.createElement("div");
  74. overlay.id = "overlay";
  75. overlay.innerHTML += '<div class="black_overlay"></div>';
  76. document.body.insertBefore(overlay, document.body.firstChild);
  77. //If module already has an open alert close it
  78. if (this.alerts[sender.name]) {
  79. this.hide_alert(sender, false);
  80. }
  81. //Display title and message only if they are provided in notification parameters
  82. let message = "";
  83. if (params.title) {
  84. message += "<span class='light dimmed medium'>" + params.title + "</span>";
  85. }
  86. if (params.message) {
  87. if (message !== "") {
  88. message += "<br />";
  89. }
  90. message += "<span class='thin bright small'>" + params.message + "</span>";
  91. }
  92. //Store alert in this.alerts
  93. this.alerts[sender.name] = new NotificationFx({
  94. message: image + message,
  95. effect: this.config.alert_effect,
  96. ttl: params.timer,
  97. onClose: () => this.hide_alert(sender),
  98. al_no: "ns-alert"
  99. });
  100. //Show alert
  101. this.alerts[sender.name].show();
  102. //Add timer to dismiss alert and overlay
  103. if (params.timer) {
  104. setTimeout(() => {
  105. this.hide_alert(sender);
  106. }, params.timer);
  107. }
  108. },
  109. hide_alert: function (sender, close = true) {
  110. //Dismiss alert and remove from this.alerts
  111. if (this.alerts[sender.name]) {
  112. this.alerts[sender.name].dismiss(close);
  113. this.alerts[sender.name] = null;
  114. //Remove overlay
  115. const overlay = document.getElementById("overlay");
  116. overlay.parentNode.removeChild(overlay);
  117. }
  118. },
  119. setPosition: function (pos) {
  120. //Add css to body depending on the set position for notifications
  121. const sheet = document.createElement("style");
  122. if (pos === "center") {
  123. sheet.innerHTML = ".ns-box {margin-left: auto; margin-right: auto;text-align: center;}";
  124. }
  125. if (pos === "right") {
  126. sheet.innerHTML = ".ns-box {margin-left: auto;text-align: right;}";
  127. }
  128. if (pos === "left") {
  129. sheet.innerHTML = ".ns-box {margin-right: auto;text-align: left;}";
  130. }
  131. document.body.appendChild(sheet);
  132. },
  133. notificationReceived: function (notification, payload, sender) {
  134. if (notification === "SHOW_ALERT") {
  135. if (typeof payload.type === "undefined") {
  136. payload.type = "alert";
  137. }
  138. if (payload.type === "alert") {
  139. this.show_alert(payload, sender);
  140. } else if (payload.type === "notification") {
  141. this.show_notification(payload);
  142. }
  143. } else if (notification === "HIDE_ALERT") {
  144. this.hide_alert(sender);
  145. }
  146. },
  147. start: function () {
  148. this.alerts = {};
  149. this.setPosition(this.config.position);
  150. if (this.config.welcome_message) {
  151. if (this.config.welcome_message === true) {
  152. this.show_notification({ title: this.translate("sysTitle"), message: this.translate("welcome") });
  153. } else {
  154. this.show_notification({ title: this.translate("sysTitle"), message: this.config.welcome_message });
  155. }
  156. }
  157. Log.info("Starting module: " + this.name);
  158. }
  159. });