notif.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS206: Consider reworking classes to avoid initClass
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  8. */
  9. app.views.Notif = class Notif extends app.View {
  10. static initClass() {
  11. this.className = "_notif";
  12. this.activeClass = "_in";
  13. this.attributes = { role: "alert" };
  14. this.defaultOptions = { autoHide: 15000 };
  15. this.events = { click: "onClick" };
  16. }
  17. constructor(type, options) {
  18. super();
  19. this.type = type;
  20. this.options = $.extend({}, this.constructor.defaultOptions, options || {});
  21. }
  22. init() {
  23. this.show();
  24. }
  25. show() {
  26. if (this.timeout) {
  27. clearTimeout(this.timeout);
  28. this.timeout = this.delay(this.hide, this.options.autoHide);
  29. } else {
  30. this.render();
  31. this.position();
  32. this.activate();
  33. this.appendTo(document.body);
  34. this.el.offsetWidth; // force reflow
  35. this.addClass(this.constructor.activeClass);
  36. if (this.options.autoHide) {
  37. this.timeout = this.delay(this.hide, this.options.autoHide);
  38. }
  39. }
  40. }
  41. hide() {
  42. clearTimeout(this.timeout);
  43. this.timeout = null;
  44. this.detach();
  45. }
  46. render() {
  47. this.html(this.tmpl(`notif${this.type}`));
  48. }
  49. position() {
  50. const notifications = $$(`.${app.views.Notif.className}`);
  51. if (notifications.length) {
  52. const lastNotif = notifications[notifications.length - 1];
  53. this.el.style.top =
  54. lastNotif.offsetTop + lastNotif.offsetHeight + 16 + "px";
  55. }
  56. }
  57. onClick(event) {
  58. if (event.which !== 1) {
  59. return;
  60. }
  61. const target = $.eventTarget(event);
  62. if (target.hasAttribute("data-behavior")) {
  63. return;
  64. }
  65. if (target.tagName !== "A" || target.classList.contains("_notif-close")) {
  66. $.stopEvent(event);
  67. this.hide();
  68. }
  69. }
  70. };
  71. app.views.Notif.initClass();