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.

notificationFx.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Based on work by
  3. *
  4. * notificationFx.js v1.0.0
  5. * https://tympanus.net/codrops/
  6. *
  7. * Licensed under the MIT license.
  8. * https://opensource.org/licenses/mit-license.php
  9. *
  10. * Copyright 2014, Codrops
  11. * https://tympanus.net/codrops/
  12. */
  13. (function (window) {
  14. /**
  15. * Extend one object with another one
  16. *
  17. * @param {object} a The object to extend
  18. * @param {object} b The object which extends the other, overwrites existing keys
  19. * @returns {object} The merged object
  20. */
  21. function extend(a, b) {
  22. for (let key in b) {
  23. if (b.hasOwnProperty(key)) {
  24. a[key] = b[key];
  25. }
  26. }
  27. return a;
  28. }
  29. /**
  30. * NotificationFx constructor
  31. *
  32. * @param {object} options The configuration options
  33. * @class
  34. */
  35. function NotificationFx(options) {
  36. this.options = extend({}, this.options);
  37. extend(this.options, options);
  38. this._init();
  39. }
  40. /**
  41. * NotificationFx options
  42. */
  43. NotificationFx.prototype.options = {
  44. // element to which the notification will be appended
  45. // defaults to the document.body
  46. wrapper: document.body,
  47. // the message
  48. message: "yo!",
  49. // layout type: growl|attached|bar|other
  50. layout: "growl",
  51. // effects for the specified layout:
  52. // for growl layout: scale|slide|genie|jelly
  53. // for attached layout: flip|bouncyflip
  54. // for other layout: boxspinner|cornerexpand|loadingcircle|thumbslider
  55. // ...
  56. effect: "slide",
  57. // notice, warning, error, success
  58. // will add class ns-type-warning, ns-type-error or ns-type-success
  59. type: "notice",
  60. // if the user doesn´t close the notification then we remove it
  61. // after the following time
  62. ttl: 6000,
  63. al_no: "ns-box",
  64. // callbacks
  65. onClose: function () {
  66. return false;
  67. },
  68. onOpen: function () {
  69. return false;
  70. }
  71. };
  72. /**
  73. * Initialize and cache some vars
  74. */
  75. NotificationFx.prototype._init = function () {
  76. // create HTML structure
  77. this.ntf = document.createElement("div");
  78. this.ntf.className = this.options.al_no + " ns-" + this.options.layout + " ns-effect-" + this.options.effect + " ns-type-" + this.options.type;
  79. let strinner = '<div class="ns-box-inner">';
  80. strinner += this.options.message;
  81. strinner += "</div>";
  82. this.ntf.innerHTML = strinner;
  83. // append to body or the element specified in options.wrapper
  84. this.options.wrapper.insertBefore(this.ntf, this.options.wrapper.nextSibling);
  85. // dismiss after [options.ttl]ms
  86. if (this.options.ttl) {
  87. this.dismissttl = setTimeout(() => {
  88. if (this.active) {
  89. this.dismiss();
  90. }
  91. }, this.options.ttl);
  92. }
  93. // init events
  94. this._initEvents();
  95. };
  96. /**
  97. * Init events
  98. */
  99. NotificationFx.prototype._initEvents = function () {
  100. // dismiss notification by tapping on it if someone has a touchscreen
  101. this.ntf.querySelector(".ns-box-inner").addEventListener("click", () => {
  102. this.dismiss();
  103. });
  104. };
  105. /**
  106. * Show the notification
  107. */
  108. NotificationFx.prototype.show = function () {
  109. this.active = true;
  110. this.ntf.classList.remove("ns-hide");
  111. this.ntf.classList.add("ns-show");
  112. this.options.onOpen();
  113. };
  114. /**
  115. * Dismiss the notification
  116. *
  117. * @param {boolean} [close] call the onClose callback at the end
  118. */
  119. NotificationFx.prototype.dismiss = function (close = true) {
  120. this.active = false;
  121. clearTimeout(this.dismissttl);
  122. this.ntf.classList.remove("ns-show");
  123. setTimeout(() => {
  124. this.ntf.classList.add("ns-hide");
  125. // callback
  126. if (close) this.options.onClose();
  127. }, 25);
  128. // after animation ends remove ntf from the DOM
  129. const onEndAnimationFn = (ev) => {
  130. if (ev.target !== this.ntf) {
  131. return false;
  132. }
  133. this.ntf.removeEventListener("animationend", onEndAnimationFn);
  134. if (ev.target.parentNode === this.options.wrapper) {
  135. this.options.wrapper.removeChild(this.ntf);
  136. }
  137. };
  138. this.ntf.addEventListener("animationend", onEndAnimationFn);
  139. };
  140. /**
  141. * Add to global namespace
  142. */
  143. window.NotificationFx = NotificationFx;
  144. })(window);