|
import Overlay from './overlay'; |
|
import Element from './element'; |
|
import './polyfill'; |
|
|
|
|
|
|
|
|
|
export default class Sholo { |
|
|
|
|
|
|
|
constructor(options = {}) { |
|
this.options = Object.assign({ |
|
padding: 10, |
|
animate: true, |
|
opacity: 0.75, |
|
}, options); |
|
|
|
this.overlay = new Overlay(options); |
|
|
|
this.document = document; |
|
this.window = window; |
|
|
|
this.onScroll = this.onScroll.bind(this); |
|
this.onResize = this.onResize.bind(this); |
|
this.onKeyUp = this.onKeyUp.bind(this); |
|
this.onMouseUp = this.onMouseUp.bind(this); |
|
|
|
|
|
this.bind(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
bind() { |
|
this.document.addEventListener('scroll', this.onScroll, false); |
|
this.document.addEventListener('DOMMouseScroll', this.onScroll, false); |
|
this.window.addEventListener('resize', this.onResize, false); |
|
this.window.addEventListener('keyup', this.onKeyUp, false); |
|
this.window.addEventListener('mouseup', this.onMouseUp, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onMouseUp(e) { |
|
const highlightedElement = this.overlay.getHighlightedElement(); |
|
const popover = document.getElementById('sholo-popover-item'); |
|
|
|
if (!highlightedElement || !highlightedElement.node) { |
|
return; |
|
} |
|
|
|
const clickedHighlightedElement = highlightedElement.node.contains(e.target); |
|
const clickedPopover = popover && popover.contains(e.target); |
|
|
|
|
|
if (!clickedHighlightedElement && !clickedPopover) { |
|
this.overlay.clear(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onScroll() { |
|
this.overlay.refresh(false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onResize() { |
|
|
|
this.overlay.refresh(true); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onKeyUp(event) { |
|
if (event.keyCode === 27) { |
|
this.overlay.clear(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
highlight(selector) { |
|
let domElement; |
|
|
|
if (typeof selector === 'string') { |
|
domElement = document.querySelector(selector); |
|
} else if (typeof selector === 'object') { |
|
domElement = selector; |
|
} else { |
|
throw new Error('Element can only be string or the dom element'); |
|
} |
|
|
|
if (domElement) { |
|
const element = new Element(domElement, this.options); |
|
this.overlay.highlight(element); |
|
} else { |
|
this.overlay.clear(); |
|
} |
|
} |
|
} |
|
|