diff --git a/.eslintrc.json b/.eslintrc.json index ad6242725986dd815480efe039e2b8147489e4a0..4a2a05180e722889dfaccb1e7368fbcbd7ea9a81 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -10,6 +10,7 @@ "no-cond-assign": "off", "func-names": "off", "no-bitwise": "off", + "class-methods-use-this": "off", "no-param-reassign": [ "off" ], diff --git a/assets/scripts/src/element.js b/assets/scripts/src/element.js index dbffd6bad665263d2985153e5c25ea449a0a606a..72db2990bf6d9467d4e981aeca3f1bee6d70a88c 100644 --- a/assets/scripts/src/element.js +++ b/assets/scripts/src/element.js @@ -11,6 +11,7 @@ export default class Element { this.document = document; this.window = window; this.options = options; + this.popover = this.getPopover(); } /** @@ -58,8 +59,38 @@ export default class Element { return position; } + onDeselected() { + // Will be called when element is about to be deselected + this.hidePopover(); + } + + onHighlightStarted() { + // Will be triggered when the element is about to be highlighted + // i.e. overlay has started transitioning towards this element + this.showPopover(); + } + onHighlighted() { - console.log('on highlighted'); - console.log(this.getScreenCoordinates()); + this.showPopover(); + } + + showPopover() { + const position = this.getCalculatedPosition(); + + this.popover.style.left = `${position.left}px`; + this.popover.style.top = `${position.bottom + 10}px`; + this.popover.style.display = 'block'; + } + + getPopover() { + // @todo: Create if not there + const popover = this.document.getElementById('sholo-popover-item'); + popover.style.position = 'absolute'; + + return popover; + } + + hidePopover() { + this.popover.style.display = 'none'; } } diff --git a/assets/scripts/src/overlay.js b/assets/scripts/src/overlay.js index e23fe71587523ad546836d78f70c8b076922fe40..3a0a7301de1bfc3f05c1285e14ca0a5496306a03 100644 --- a/assets/scripts/src/overlay.js +++ b/assets/scripts/src/overlay.js @@ -61,6 +61,14 @@ export default class Overlay { return; } + // Trigger the hook for highlight started + element.onHighlightStarted(); + + // Old element has been deselected + if (this.highlightedElement) { + this.highlightedElement.onDeselected(); + } + // get the position of element around which we need to draw const position = element.getCalculatedPosition(); if (!position.canHighlight()) { @@ -92,6 +100,7 @@ export default class Overlay { */ clear() { this.positionToHighlight = new Position(); + this.highlightedElement.onDeselected(); this.highlightedElement = null; this.draw(); @@ -245,6 +254,7 @@ export default class Overlay { if (this.highlightedElement) { this.window.cancelAnimationFrame(this.redrawAnimation); this.highlight(this.highlightedElement, animate); + this.highlightedElement.onHighlighted(); } } } diff --git a/assets/scripts/src/sholo.js b/assets/scripts/src/sholo.js index 5b1a074fef26a769f077226d68657b8e8921af6f..db57c896425dd20523e52d8df8d77eeb48d8bbc3 100644 --- a/assets/scripts/src/sholo.js +++ b/assets/scripts/src/sholo.js @@ -42,6 +42,11 @@ export default class Sholo { this.window.addEventListener('mouseup', this.onMouseUp, false); } + /** + * Removes the popover if clicked outside the highlighted element + * or outside the + * @param e + */ onMouseUp(e) { const highlightedElement = this.overlay.getHighlightedElement(); const popover = document.getElementById('sholo-popover-item'); @@ -50,8 +55,11 @@ export default class Sholo { return; } + const clickedHighlightedElement = highlightedElement.node.contains(e.target); + const clickedPopover = popover && popover.contains(e.target); + // Remove the overlay If clicked outside the highlighted element - if (!highlightedElement.node.contains(e.target) && (!popover || !popover.contains(e.target))) { + if (!clickedHighlightedElement && !clickedPopover) { this.overlay.clear(); } } diff --git a/assets/styles/scss/demo.scss b/assets/styles/scss/demo.scss index 43e4eb9fd1de43047795b7137f9895da97ad1ddc..b8f06a0d212b1ec8b20963e3661eee6a1a9ab4ae 100644 --- a/assets/styles/scss/demo.scss +++ b/assets/styles/scss/demo.scss @@ -48,13 +48,7 @@ section { ///////////////////////////////////////// div#sholo-popover-item { - //////// Temp style - position: absolute; - top: 285px; - left: 710px; - ////////////////// - - display: block; + display: none; background: white; margin: 0; padding: 15px; diff --git a/index.html b/index.html index 2fb583df5065440b75fc45988d6854540533ee3e..1c3f12c0a4e9f3870511c6998e4ed7741cae70d3 100644 --- a/index.html +++ b/index.html @@ -19,8 +19,8 @@

Sholo

A light-weight, no-dependency, vanilla JavaScript library to bring certain parts of page in spotlight

- Show an Example - Learn More + Show an Example + Learn More

Whats does it do?

@@ -62,12 +62,16 @@ \ No newline at end of file