notif.js 2.1 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. * 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. const Cls = (app.views.Notif = class Notif extends app.View {
  11. static initClass() {
  12. this.className = '_notif';
  13. this.activeClass = '_in';
  14. this.attributes =
  15. {role: 'alert'};
  16. this.defautOptions =
  17. {autoHide: 15000};
  18. this.events =
  19. {click: 'onClick'};
  20. }
  21. constructor(type, options) {
  22. this.onClick = this.onClick.bind(this);
  23. this.type = type;
  24. if (options == null) { options = {}; }
  25. this.options = options;
  26. this.options = $.extend({}, this.constructor.defautOptions, this.options);
  27. super(...arguments);
  28. }
  29. init() {
  30. this.show();
  31. }
  32. show() {
  33. if (this.timeout) {
  34. clearTimeout(this.timeout);
  35. this.timeout = this.delay(this.hide, this.options.autoHide);
  36. } else {
  37. this.render();
  38. this.position();
  39. this.activate();
  40. this.appendTo(document.body);
  41. this.el.offsetWidth; // force reflow
  42. this.addClass(this.constructor.activeClass);
  43. if (this.options.autoHide) { this.timeout = this.delay(this.hide, this.options.autoHide); }
  44. }
  45. }
  46. hide() {
  47. clearTimeout(this.timeout);
  48. this.timeout = null;
  49. this.detach();
  50. }
  51. render() {
  52. this.html(this.tmpl(`notif${this.type}`));
  53. }
  54. position() {
  55. const notifications = $$(`.${app.views.Notif.className}`);
  56. if (notifications.length) {
  57. const lastNotif = notifications[notifications.length - 1];
  58. this.el.style.top = lastNotif.offsetTop + lastNotif.offsetHeight + 16 + 'px';
  59. }
  60. }
  61. onClick(event) {
  62. if (event.which !== 1) { return; }
  63. const target = $.eventTarget(event);
  64. if (target.hasAttribute('data-behavior')) { return; }
  65. if ((target.tagName !== 'A') || target.classList.contains('_notif-close')) {
  66. $.stopEvent(event);
  67. this.hide();
  68. }
  69. }
  70. });
  71. Cls.initClass();