2
0

BaseHoverManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @abstract
  3. * @class BaseHoverManager
  4. * An abstract class that provides hover and click state management for a group of DOM elements.
  5. * Extend this class to build custom managers with specific behaviors.
  6. */
  7. export class BaseHoverManager {
  8. /**
  9. *
  10. * @param {string} selector - CSS selector for targeting DOM elements.
  11. * @param {string} [hoverClass="hovered"] - CSS class applied on hover.
  12. * @param {string} [clickedClass="clicked"] - CSS class applied on click.
  13. */
  14. constructor(selector, hoverClass = "hovered", clickedClass = "clicked") {
  15. this.selector = selector;
  16. this.hoverClass = hoverClass;
  17. this.clickedClass = clickedClass;
  18. this.hoverActive = false;
  19. this.addHoverClass = this.addHoverClass.bind(this);
  20. this.removeHoverClass = this.removeHoverClass.bind(this);
  21. this.toggleHover = this.toggleHover.bind(this);
  22. }
  23. /**
  24. * Use the data-newmanager with value attribute to exclude from the selected elements
  25. */
  26. getElements() {
  27. return Array.from(document.querySelectorAll(this.selector)).filter(
  28. (el) => el.getAttribute("data-newmanager") !== "false"
  29. );
  30. }
  31. disableHover() {
  32. if (this.hoverActive) {
  33. this.hoverActive = false;
  34. this.onHoverDisabled();
  35. }
  36. }
  37. toggleHover() {
  38. this.hoverActive = !this.hoverActive;
  39. this.getElements().forEach((el) => {
  40. if (this.hoverActive) {
  41. //On supprime chaque instance de TinyMce sinon il s'active lors des clics
  42. el.addEventListener("mouseenter", this.addHoverClass);
  43. el.addEventListener("mouseleave", this.removeHoverClass);
  44. } else {
  45. el.removeEventListener("mouseenter", this.addHoverClass);
  46. el.removeEventListener("mouseleave", this.removeHoverClass);
  47. }
  48. this.clearElementState(el);
  49. });
  50. if (!this.hoverActive) {
  51. // TinyMCE function will be provided by the main module
  52. if (typeof TinyMCE === 'function') {
  53. TinyMCE();
  54. }
  55. this.onHoverDisabled?.();
  56. } else {
  57. if (typeof tinymce !== 'undefined') {
  58. tinymce.remove();
  59. }
  60. }
  61. }
  62. addHoverClass(e) {
  63. e.currentTarget.classList.add(this.hoverClass);
  64. }
  65. removeHoverClass(e) {
  66. e.currentTarget.classList.remove(this.hoverClass);
  67. }
  68. clearElementState(el) {
  69. }
  70. onHoverDisabled() {}
  71. handleClick(e) {}
  72. init() {
  73. this.getElements().forEach((el) => {
  74. el.addEventListener("click", this.handleClick);
  75. });
  76. }
  77. destroy() {
  78. this.getElements().forEach((e) => e.classList.remove(this.hoverClass, this.clickedClass))
  79. this.smartButton.hide();
  80. this.getElements().forEach((el) => {
  81. el.removeEventListener("click", this.handleClick);
  82. });
  83. if(this.hoverActive) {
  84. this.toggleHover();
  85. }
  86. }
  87. }