Make popover optional on elements
Browse files- assets/scripts/src/element.js +21 -5
- assets/scripts/src/overlay.js +18 -23
- assets/scripts/src/sholo.js +8 -5
- index.html +16 -5
assets/scripts/src/element.js
CHANGED
|
@@ -102,19 +102,35 @@ export default class Element {
|
|
| 102 |
return position;
|
| 103 |
}
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
onDeselected() {
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
this.popover.hide();
|
| 108 |
}
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
onHighlightStarted() {
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
this.showPopover();
|
| 114 |
}
|
| 115 |
|
| 116 |
onHighlighted() {
|
| 117 |
-
this.
|
|
|
|
|
|
|
| 118 |
|
| 119 |
const highlightedElement = this;
|
| 120 |
const lastHighlightedElement = this.overlay.getLastHighlightedElement();
|
|
@@ -130,7 +146,7 @@ export default class Element {
|
|
| 130 |
highlightedElement.bringInView();
|
| 131 |
}
|
| 132 |
|
| 133 |
-
if (!popoverElement.isInView()) {
|
| 134 |
popoverElement.bringInView();
|
| 135 |
}
|
| 136 |
}
|
|
|
|
| 102 |
return position;
|
| 103 |
}
|
| 104 |
|
| 105 |
+
/**
|
| 106 |
+
* Is called when element is about to be deselected
|
| 107 |
+
* i.e. when moving the focus to next element of closing
|
| 108 |
+
*/
|
| 109 |
onDeselected() {
|
| 110 |
+
if (!this.popover) {
|
| 111 |
+
return;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
this.popover.hide();
|
| 115 |
}
|
| 116 |
|
| 117 |
+
/**
|
| 118 |
+
* Is called when the element is about to be highlighted
|
| 119 |
+
* i.e. either if overlay has started moving the highlight towards
|
| 120 |
+
* this element of has just decided to highlight it
|
| 121 |
+
*/
|
| 122 |
onHighlightStarted() {
|
| 123 |
+
if (!this.popover) {
|
| 124 |
+
return;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
this.showPopover();
|
| 128 |
}
|
| 129 |
|
| 130 |
onHighlighted() {
|
| 131 |
+
if (this.popover) {
|
| 132 |
+
this.showPopover();
|
| 133 |
+
}
|
| 134 |
|
| 135 |
const highlightedElement = this;
|
| 136 |
const lastHighlightedElement = this.overlay.getLastHighlightedElement();
|
|
|
|
| 146 |
highlightedElement.bringInView();
|
| 147 |
}
|
| 148 |
|
| 149 |
+
if (popoverElement && !popoverElement.isInView()) {
|
| 150 |
popoverElement.bringInView();
|
| 151 |
}
|
| 152 |
}
|
assets/scripts/src/overlay.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import Position from './position';
|
| 2 |
-
import { ID_OVERLAY,
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Responsible for overlay creation and manipulation i.e.
|
|
@@ -7,20 +7,12 @@ import { ID_OVERLAY, OVERLAY_ANIMATE, OVERLAY_OPACITY, OVERLAY_PADDING, OVERLAY_
|
|
| 7 |
*/
|
| 8 |
export default class Overlay {
|
| 9 |
/**
|
| 10 |
-
* @param
|
| 11 |
-
* @param padding number
|
| 12 |
-
* @param animate bool
|
| 13 |
* @param window
|
| 14 |
* @param document
|
| 15 |
*/
|
| 16 |
-
constructor({
|
| 17 |
-
|
| 18 |
-
padding = OVERLAY_PADDING,
|
| 19 |
-
animate = OVERLAY_ANIMATE,
|
| 20 |
-
}, window, document) {
|
| 21 |
-
this.opacity = opacity; // Fixed opacity for the layover
|
| 22 |
-
this.padding = padding; // Padding around the highlighted item
|
| 23 |
-
this.animate = animate; // Should animate between the transitions
|
| 24 |
|
| 25 |
this.overlayAlpha = 0; // Is used to animate the layover
|
| 26 |
this.positionToHighlight = new Position({}); // position at which layover is to be patched at
|
|
@@ -93,7 +85,7 @@ export default class Overlay {
|
|
| 93 |
|
| 94 |
// If animation is not required then set the last path to be same
|
| 95 |
// as the current path so that there is no easing towards it
|
| 96 |
-
if (!this.animate || !animate) {
|
| 97 |
this.highlightedPosition = this.positionToHighlight;
|
| 98 |
}
|
| 99 |
|
|
@@ -121,7 +113,10 @@ export default class Overlay {
|
|
| 121 |
*/
|
| 122 |
clear() {
|
| 123 |
this.positionToHighlight = new Position();
|
| 124 |
-
this.highlightedElement
|
|
|
|
|
|
|
|
|
|
| 125 |
this.highlightedElement = null;
|
| 126 |
this.lastHighlightedElement = null;
|
| 127 |
|
|
@@ -160,18 +155,18 @@ export default class Overlay {
|
|
| 160 |
|
| 161 |
// Cut the chunk of overlay that is over the highlighted item
|
| 162 |
this.removeCloak({
|
| 163 |
-
posX: this.highlightedPosition.left - this.window.scrollX - this.padding,
|
| 164 |
-
posY: this.highlightedPosition.top - this.window.scrollY - this.padding,
|
| 165 |
-
width: (this.highlightedPosition.right - this.highlightedPosition.left) + (this.padding * 2),
|
| 166 |
-
height: (this.highlightedPosition.bottom - this.highlightedPosition.top) + (this.padding * 2),
|
| 167 |
});
|
| 168 |
|
| 169 |
// Fade the overlay in if we can highlight
|
| 170 |
if (canHighlight) {
|
| 171 |
-
if (!this.animate) {
|
| 172 |
-
this.overlayAlpha = this.opacity;
|
| 173 |
} else {
|
| 174 |
-
this.overlayAlpha += (this.opacity - this.overlayAlpha) * 0.08;
|
| 175 |
}
|
| 176 |
} else {
|
| 177 |
// otherwise fade out
|
|
@@ -193,7 +188,7 @@ export default class Overlay {
|
|
| 193 |
// or the alpha has not yet fully reached fully required opacity
|
| 194 |
if (!this.hasPositionHighlighted()) {
|
| 195 |
this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
|
| 196 |
-
} else if (!this.animate && isFadingIn) {
|
| 197 |
this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
|
| 198 |
} else {
|
| 199 |
// Element has been highlighted
|
|
@@ -207,7 +202,7 @@ export default class Overlay {
|
|
| 207 |
|
| 208 |
hasPositionHighlighted() {
|
| 209 |
return this.positionToHighlight.equals(this.highlightedPosition) &&
|
| 210 |
-
this.overlayAlpha > (this.opacity - 0.05);
|
| 211 |
}
|
| 212 |
|
| 213 |
/**
|
|
|
|
| 1 |
import Position from './position';
|
| 2 |
+
import { ID_OVERLAY, OVERLAY_ZINDEX } from './constants';
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Responsible for overlay creation and manipulation i.e.
|
|
|
|
| 7 |
*/
|
| 8 |
export default class Overlay {
|
| 9 |
/**
|
| 10 |
+
* @param options
|
|
|
|
|
|
|
| 11 |
* @param window
|
| 12 |
* @param document
|
| 13 |
*/
|
| 14 |
+
constructor(options, window, document) {
|
| 15 |
+
this.options = options;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
this.overlayAlpha = 0; // Is used to animate the layover
|
| 18 |
this.positionToHighlight = new Position({}); // position at which layover is to be patched at
|
|
|
|
| 85 |
|
| 86 |
// If animation is not required then set the last path to be same
|
| 87 |
// as the current path so that there is no easing towards it
|
| 88 |
+
if (!this.options.animate || !animate) {
|
| 89 |
this.highlightedPosition = this.positionToHighlight;
|
| 90 |
}
|
| 91 |
|
|
|
|
| 113 |
*/
|
| 114 |
clear() {
|
| 115 |
this.positionToHighlight = new Position();
|
| 116 |
+
if (this.highlightedElement) {
|
| 117 |
+
this.highlightedElement.onDeselected();
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
this.highlightedElement = null;
|
| 121 |
this.lastHighlightedElement = null;
|
| 122 |
|
|
|
|
| 155 |
|
| 156 |
// Cut the chunk of overlay that is over the highlighted item
|
| 157 |
this.removeCloak({
|
| 158 |
+
posX: this.highlightedPosition.left - this.window.scrollX - this.options.padding,
|
| 159 |
+
posY: this.highlightedPosition.top - this.window.scrollY - this.options.padding,
|
| 160 |
+
width: (this.highlightedPosition.right - this.highlightedPosition.left) + (this.options.padding * 2),
|
| 161 |
+
height: (this.highlightedPosition.bottom - this.highlightedPosition.top) + (this.options.padding * 2),
|
| 162 |
});
|
| 163 |
|
| 164 |
// Fade the overlay in if we can highlight
|
| 165 |
if (canHighlight) {
|
| 166 |
+
if (!this.options.animate) {
|
| 167 |
+
this.overlayAlpha = this.options.opacity;
|
| 168 |
} else {
|
| 169 |
+
this.overlayAlpha += (this.options.opacity - this.overlayAlpha) * 0.08;
|
| 170 |
}
|
| 171 |
} else {
|
| 172 |
// otherwise fade out
|
|
|
|
| 188 |
// or the alpha has not yet fully reached fully required opacity
|
| 189 |
if (!this.hasPositionHighlighted()) {
|
| 190 |
this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
|
| 191 |
+
} else if (!this.options.animate && isFadingIn) {
|
| 192 |
this.redrawAnimation = this.window.requestAnimationFrame(this.draw);
|
| 193 |
} else {
|
| 194 |
// Element has been highlighted
|
|
|
|
| 202 |
|
| 203 |
hasPositionHighlighted() {
|
| 204 |
return this.positionToHighlight.equals(this.highlightedPosition) &&
|
| 205 |
+
this.overlayAlpha > (this.options.opacity - 0.05);
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
assets/scripts/src/sholo.js
CHANGED
|
@@ -8,6 +8,9 @@ import {
|
|
| 8 |
CLASS_PREV_STEP_BTN,
|
| 9 |
ESC_KEY_CODE,
|
| 10 |
ID_POPOVER,
|
|
|
|
|
|
|
|
|
|
| 11 |
} from './constants';
|
| 12 |
|
| 13 |
/**
|
|
@@ -19,9 +22,9 @@ export default class Sholo {
|
|
| 19 |
*/
|
| 20 |
constructor(options = {}) {
|
| 21 |
this.options = Object.assign({
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
}, options);
|
| 26 |
|
| 27 |
this.document = document;
|
|
@@ -173,6 +176,7 @@ export default class Sholo {
|
|
| 173 |
}
|
| 174 |
|
| 175 |
const elementOptions = Object.assign({}, this.options, step);
|
|
|
|
| 176 |
|
| 177 |
const domElement = this.document.querySelector(step.element);
|
| 178 |
if (!domElement) {
|
|
@@ -180,8 +184,7 @@ export default class Sholo {
|
|
| 180 |
return;
|
| 181 |
}
|
| 182 |
|
| 183 |
-
|
| 184 |
-
const popover = new Popover(elementOptions, this.window, this.document);
|
| 185 |
const element = new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
|
| 186 |
|
| 187 |
this.steps.push(element);
|
|
|
|
| 8 |
CLASS_PREV_STEP_BTN,
|
| 9 |
ESC_KEY_CODE,
|
| 10 |
ID_POPOVER,
|
| 11 |
+
OVERLAY_ANIMATE,
|
| 12 |
+
OVERLAY_OPACITY,
|
| 13 |
+
OVERLAY_PADDING,
|
| 14 |
} from './constants';
|
| 15 |
|
| 16 |
/**
|
|
|
|
| 22 |
*/
|
| 23 |
constructor(options = {}) {
|
| 24 |
this.options = Object.assign({
|
| 25 |
+
animate: OVERLAY_ANIMATE, // Whether to animate or not
|
| 26 |
+
opacity: OVERLAY_OPACITY, // Overlay opacity
|
| 27 |
+
padding: OVERLAY_PADDING, // Spacing around the element from the overlay
|
| 28 |
}, options);
|
| 29 |
|
| 30 |
this.document = document;
|
|
|
|
| 176 |
}
|
| 177 |
|
| 178 |
const elementOptions = Object.assign({}, this.options, step);
|
| 179 |
+
const popoverOptions = Object.assign({}, this.options, elementOptions.popover || {});
|
| 180 |
|
| 181 |
const domElement = this.document.querySelector(step.element);
|
| 182 |
if (!domElement) {
|
|
|
|
| 184 |
return;
|
| 185 |
}
|
| 186 |
|
| 187 |
+
const popover = elementOptions.popover ? new Popover(popoverOptions, this.window, this.document) : null;
|
|
|
|
| 188 |
const element = new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
|
| 189 |
|
| 190 |
this.steps.push(element);
|
index.html
CHANGED
|
@@ -64,11 +64,22 @@
|
|
| 64 |
});
|
| 65 |
|
| 66 |
sholo.defineSteps([
|
| 67 |
-
{
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
{
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
]);
|
| 73 |
|
| 74 |
document.querySelector('.btn__example')
|
|
|
|
| 64 |
});
|
| 65 |
|
| 66 |
sholo.defineSteps([
|
| 67 |
+
{
|
| 68 |
+
element: '.section__header'
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
element: '.section__how',
|
| 72 |
+
popover: {}
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
element: '.section__purpose',
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
element: '.section__examples',
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
element: '.section__contributing',
|
| 82 |
+
},
|
| 83 |
]);
|
| 84 |
|
| 85 |
document.querySelector('.btn__example')
|