提交 5d08a612 编写于 作者: K Kamran Ahmed

Make popover optional on elements

上级 6a7ee1e2
...@@ -102,19 +102,35 @@ export default class Element { ...@@ -102,19 +102,35 @@ export default class Element {
return position; return position;
} }
/**
* Is called when element is about to be deselected
* i.e. when moving the focus to next element of closing
*/
onDeselected() { onDeselected() {
// Will be called when element is about to be deselected if (!this.popover) {
return;
}
this.popover.hide(); this.popover.hide();
} }
/**
* Is called when the element is about to be highlighted
* i.e. either if overlay has started moving the highlight towards
* this element of has just decided to highlight it
*/
onHighlightStarted() { onHighlightStarted() {
// Will be triggered when the element is about to be highlighted if (!this.popover) {
// i.e. overlay has started transitioning towards this element return;
}
this.showPopover(); this.showPopover();
} }
onHighlighted() { onHighlighted() {
if (this.popover) {
this.showPopover(); this.showPopover();
}
const highlightedElement = this; const highlightedElement = this;
const lastHighlightedElement = this.overlay.getLastHighlightedElement(); const lastHighlightedElement = this.overlay.getLastHighlightedElement();
...@@ -130,7 +146,7 @@ export default class Element { ...@@ -130,7 +146,7 @@ export default class Element {
highlightedElement.bringInView(); highlightedElement.bringInView();
} }
if (!popoverElement.isInView()) { if (popoverElement && !popoverElement.isInView()) {
popoverElement.bringInView(); popoverElement.bringInView();
} }
} }
......
import Position from './position'; import Position from './position';
import { ID_OVERLAY, OVERLAY_ANIMATE, OVERLAY_OPACITY, OVERLAY_PADDING, OVERLAY_ZINDEX } from './constants'; import { ID_OVERLAY, OVERLAY_ZINDEX } from './constants';
/** /**
* Responsible for overlay creation and manipulation i.e. * Responsible for overlay creation and manipulation i.e.
...@@ -7,20 +7,12 @@ import { ID_OVERLAY, OVERLAY_ANIMATE, OVERLAY_OPACITY, OVERLAY_PADDING, OVERLAY_ ...@@ -7,20 +7,12 @@ import { ID_OVERLAY, OVERLAY_ANIMATE, OVERLAY_OPACITY, OVERLAY_PADDING, OVERLAY_
*/ */
export default class Overlay { export default class Overlay {
/** /**
* @param opacity number * @param options
* @param padding number
* @param animate bool
* @param window * @param window
* @param document * @param document
*/ */
constructor({ constructor(options, window, document) {
opacity = OVERLAY_OPACITY, this.options = options;
padding = OVERLAY_PADDING,
animate = OVERLAY_ANIMATE,
}, window, document) {
this.opacity = opacity; // Fixed opacity for the layover
this.padding = padding; // Padding around the highlighted item
this.animate = animate; // Should animate between the transitions
this.overlayAlpha = 0; // Is used to animate the layover this.overlayAlpha = 0; // Is used to animate the layover
this.positionToHighlight = new Position({}); // position at which layover is to be patched at this.positionToHighlight = new Position({}); // position at which layover is to be patched at
...@@ -93,7 +85,7 @@ export default class Overlay { ...@@ -93,7 +85,7 @@ export default class Overlay {
// If animation is not required then set the last path to be same // If animation is not required then set the last path to be same
// as the current path so that there is no easing towards it // as the current path so that there is no easing towards it
if (!this.animate || !animate) { if (!this.options.animate || !animate) {
this.highlightedPosition = this.positionToHighlight; this.highlightedPosition = this.positionToHighlight;
} }
...@@ -121,7 +113,10 @@ export default class Overlay { ...@@ -121,7 +113,10 @@ export default class Overlay {
*/ */
clear() { clear() {
this.positionToHighlight = new Position(); this.positionToHighlight = new Position();
if (this.highlightedElement) {
this.highlightedElement.onDeselected(); this.highlightedElement.onDeselected();
}
this.highlightedElement = null; this.highlightedElement = null;
this.lastHighlightedElement = null; this.lastHighlightedElement = null;
...@@ -160,18 +155,18 @@ export default class Overlay { ...@@ -160,18 +155,18 @@ export default class Overlay {
// Cut the chunk of overlay that is over the highlighted item // Cut the chunk of overlay that is over the highlighted item
this.removeCloak({ this.removeCloak({
posX: this.highlightedPosition.left - this.window.scrollX - this.padding, posX: this.highlightedPosition.left - this.window.scrollX - this.options.padding,
posY: this.highlightedPosition.top - this.window.scrollY - this.padding, posY: this.highlightedPosition.top - this.window.scrollY - this.options.padding,
width: (this.highlightedPosition.right - this.highlightedPosition.left) + (this.padding * 2), width: (this.highlightedPosition.right - this.highlightedPosition.left) + (this.options.padding * 2),
height: (this.highlightedPosition.bottom - this.highlightedPosition.top) + (this.padding * 2), height: (this.highlightedPosition.bottom - this.highlightedPosition.top) + (this.options.padding * 2),
}); });
// Fade the overlay in if we can highlight // Fade the overlay in if we can highlight
if (canHighlight) { if (canHighlight) {
if (!this.animate) { if (!this.options.animate) {
this.overlayAlpha = this.opacity; this.overlayAlpha = this.options.opacity;
} else { } else {
this.overlayAlpha += (this.opacity - this.overlayAlpha) * 0.08; this.overlayAlpha += (this.options.opacity - this.overlayAlpha) * 0.08;
} }
} else { } else {
// otherwise fade out // otherwise fade out
...@@ -193,7 +188,7 @@ export default class Overlay { ...@@ -193,7 +188,7 @@ export default class Overlay {
// or the alpha has not yet fully reached fully required opacity // or the alpha has not yet fully reached fully required opacity
if (!this.hasPositionHighlighted()) { if (!this.hasPositionHighlighted()) {
this.redrawAnimation = this.window.requestAnimationFrame(this.draw); this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
} else if (!this.animate && isFadingIn) { } else if (!this.options.animate && isFadingIn) {
this.redrawAnimation = this.window.requestAnimationFrame(this.draw); this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
} else { } else {
// Element has been highlighted // Element has been highlighted
...@@ -207,7 +202,7 @@ export default class Overlay { ...@@ -207,7 +202,7 @@ export default class Overlay {
hasPositionHighlighted() { hasPositionHighlighted() {
return this.positionToHighlight.equals(this.highlightedPosition) && return this.positionToHighlight.equals(this.highlightedPosition) &&
this.overlayAlpha > (this.opacity - 0.05); this.overlayAlpha > (this.options.opacity - 0.05);
} }
/** /**
......
...@@ -8,6 +8,9 @@ import { ...@@ -8,6 +8,9 @@ import {
CLASS_PREV_STEP_BTN, CLASS_PREV_STEP_BTN,
ESC_KEY_CODE, ESC_KEY_CODE,
ID_POPOVER, ID_POPOVER,
OVERLAY_ANIMATE,
OVERLAY_OPACITY,
OVERLAY_PADDING,
} from './constants'; } from './constants';
/** /**
...@@ -19,9 +22,9 @@ export default class Sholo { ...@@ -19,9 +22,9 @@ export default class Sholo {
*/ */
constructor(options = {}) { constructor(options = {}) {
this.options = Object.assign({ this.options = Object.assign({
padding: 10, animate: OVERLAY_ANIMATE, // Whether to animate or not
animate: true, opacity: OVERLAY_OPACITY, // Overlay opacity
opacity: 0.75, padding: OVERLAY_PADDING, // Spacing around the element from the overlay
}, options); }, options);
this.document = document; this.document = document;
...@@ -173,6 +176,7 @@ export default class Sholo { ...@@ -173,6 +176,7 @@ export default class Sholo {
} }
const elementOptions = Object.assign({}, this.options, step); const elementOptions = Object.assign({}, this.options, step);
const popoverOptions = Object.assign({}, this.options, elementOptions.popover || {});
const domElement = this.document.querySelector(step.element); const domElement = this.document.querySelector(step.element);
if (!domElement) { if (!domElement) {
...@@ -180,8 +184,7 @@ export default class Sholo { ...@@ -180,8 +184,7 @@ export default class Sholo {
return; return;
} }
// @todo pass the options such as position, button text etc const popover = elementOptions.popover ? new Popover(popoverOptions, this.window, this.document) : null;
const popover = new Popover(elementOptions, this.window, this.document);
const element = new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document); const element = new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
this.steps.push(element); this.steps.push(element);
......
...@@ -64,11 +64,22 @@ ...@@ -64,11 +64,22 @@
}); });
sholo.defineSteps([ sholo.defineSteps([
{ element: '.section__header' }, {
{ element: '.section__how' }, element: '.section__header'
{ element: '.section__purpose' }, },
{ element: '.section__examples' }, {
{ element: '.section__contributing' }, element: '.section__how',
popover: {}
},
{
element: '.section__purpose',
},
{
element: '.section__examples',
},
{
element: '.section__contributing',
},
]); ]);
document.querySelector('.btn__example') document.querySelector('.btn__example')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册