import { SlidehubPlugin } from '../core/SlidehubPlugin';
import { config } from '../config';
import { listener } from '../util/passive-event-listener';

export { Highlighter };

/**
 * Highlighter.
 *
 * Highlights documents/items on hover
 */
class Highlighter extends SlidehubPlugin {
  constructor(slidehub) {
    const description = 'Highlights documents/items on hover';
    super(slidehub, 'Highlighter', description);

    this.boundHandleHighlight = this.handleHighlight.bind(this);
  }

  enable() {
    document.addEventListener('mousemove', this.boundHandleHighlight, listener.passive);
    super.enable();
  }

  disable() {
    this.slidehub.unhighlightDocument();
    document.removeEventListener('mousemove', this.boundHandleHighlight);
    super.disable();
  }

  /**
   * @param {MouseEvent} event
   */
  handleHighlight(event) {
    if (event.target instanceof Element) {
      const docNode = event.target.closest(config.selector.doc);
      const doc = this.slidehub.documents.get(docNode.id);
      if (doc.loaded) {
        this.slidehub.highlightDocument(doc);

        if (config.keepSelectedPageInFirstColumn) {
          return;
        }

        const itemNode = event.target.closest(config.selector.item);
        if (itemNode) {
          doc.highlightItem(itemNode);
        }
      }
    }
  }
};

import { SlidehubPlugin } from '../core/SlidehubPlugin';
import { config } from '../config';
import { listener } from '../util/passive-event-listener';

export { Highlighter };

/**
 * Highlighter.
 *
 * Highlights documents/items on hover
 */
class Highlighter extends SlidehubPlugin {
  constructor(slidehub) {
    const description = 'Highlights documents/items on hover';
    super(slidehub, 'Highlighter', description);

    this.boundHandleHighlight = this.handleHighlight.bind(this);
  }

  enable() {
    document.addEventListener('mousemove', this.boundHandleHighlight, listener.passive);
    super.enable();
  }

  disable() {
    this.slidehub.unhighlightDocument();
    document.removeEventListener('mousemove', this.boundHandleHighlight);
    super.disable();
  }

  /**
   * @param {MouseEvent} event
   */
  handleHighlight(event) {
    if (event.target instanceof Element) {
      const docNode = event.target.closest(config.selector.doc);

      if (!docNode) {
        return;
      }

      const doc = this.slidehub.documents.get(docNode.id);

      if (!doc.loaded) {
        return;
      }

      this.slidehub.highlightDocument(doc);

      if (config.keepSelectedPageInFirstColumn) {
        return;
      }

      const itemNode = event.target.closest(config.selector.item);
      if (itemNode) {
        doc.highlightItem(itemNode);
      }
    }
  }
};

Fix another highlight attempt that is not possible
Fix another highlight attempt that is not possible
