| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /**
- * @abstract
- * @class BaseHoverManager
- * An abstract class that provides hover and click state management for a group of DOM elements.
- * Extend this class to build custom managers with specific behaviors.
- */
- export class BaseHoverManager {
- /**
- *
- * @param {string} selector - CSS selector for targeting DOM elements.
- * @param {string} [hoverClass="hovered"] - CSS class applied on hover.
- * @param {string} [clickedClass="clicked"] - CSS class applied on click.
- */
- constructor(selector, hoverClass = "hovered", clickedClass = "clicked") {
- this.selector = selector;
- this.hoverClass = hoverClass;
- this.clickedClass = clickedClass;
- this.hoverActive = false;
- this.addHoverClass = this.addHoverClass.bind(this);
- this.removeHoverClass = this.removeHoverClass.bind(this);
- this.toggleHover = this.toggleHover.bind(this);
- }
- /**
- * Use the data-newmanager with value attribute to exclude from the selected elements
- */
- getElements() {
- return Array.from(document.querySelectorAll(this.selector)).filter(
- (el) => el.getAttribute("data-newmanager") !== "false"
- );
- }
- disableHover() {
- if (this.hoverActive) {
- this.hoverActive = false;
- this.onHoverDisabled();
- }
- }
- toggleHover() {
- this.hoverActive = !this.hoverActive;
- this.getElements().forEach((el) => {
- if (this.hoverActive) {
- //On supprime chaque instance de TinyMce sinon il s'active lors des clics
- el.addEventListener("mouseenter", this.addHoverClass);
- el.addEventListener("mouseleave", this.removeHoverClass);
- } else {
- el.removeEventListener("mouseenter", this.addHoverClass);
- el.removeEventListener("mouseleave", this.removeHoverClass);
- }
- this.clearElementState(el);
- });
- if (!this.hoverActive) {
- // TinyMCE function will be provided by the main module
- if (typeof TinyMCE === 'function') {
- TinyMCE();
- }
- this.onHoverDisabled?.();
- } else {
- if (typeof tinymce !== 'undefined') {
- tinymce.remove();
- }
- }
- }
- addHoverClass(e) {
- e.currentTarget.classList.add(this.hoverClass);
- }
- removeHoverClass(e) {
- e.currentTarget.classList.remove(this.hoverClass);
- }
- clearElementState(el) {
- }
- onHoverDisabled() {}
- handleClick(e) {}
- init() {
- this.getElements().forEach((el) => {
- el.addEventListener("click", this.handleClick);
- });
- }
- destroy() {
- this.getElements().forEach((e) => e.classList.remove(this.hoverClass, this.clickedClass))
- this.smartButton.hide();
- this.getElements().forEach((el) => {
- el.removeEventListener("click", this.handleClick);
- });
- if(this.hoverActive) {
- this.toggleHover();
- }
- }
- }
|