notif.js 2.2 KB

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