Refactor and single element highlight
Browse files- assets/scripts/src/popover.js +1 -1
- assets/scripts/src/sholo.js +63 -33
- index.html +17 -0
assets/scripts/src/popover.js
CHANGED
|
@@ -133,7 +133,7 @@ export default class Popover extends Element {
|
|
| 133 |
this.closeBtnNode.innerHTML = this.options.closeBtnText;
|
| 134 |
|
| 135 |
// If there was only one item, hide the buttons
|
| 136 |
-
if (this.options.totalCount === 1) {
|
| 137 |
this.footerNode.style.display = 'none';
|
| 138 |
return;
|
| 139 |
}
|
|
|
|
| 133 |
this.closeBtnNode.innerHTML = this.options.closeBtnText;
|
| 134 |
|
| 135 |
// If there was only one item, hide the buttons
|
| 136 |
+
if (!this.options.totalCount || this.options.totalCount === 1) {
|
| 137 |
this.footerNode.style.display = 'none';
|
| 138 |
return;
|
| 139 |
}
|
assets/scripts/src/sholo.js
CHANGED
|
@@ -126,6 +126,7 @@ export default class Sholo {
|
|
| 126 |
*/
|
| 127 |
reset() {
|
| 128 |
this.currentStep = 0;
|
|
|
|
| 129 |
this.overlay.clear();
|
| 130 |
}
|
| 131 |
|
|
@@ -167,6 +168,10 @@ export default class Sholo {
|
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
defineSteps(steps) {
|
| 171 |
this.steps = [];
|
| 172 |
|
|
@@ -175,58 +180,83 @@ export default class Sholo {
|
|
| 175 |
throw new Error(`Element (query selector string) missing in step ${index}`);
|
| 176 |
}
|
| 177 |
|
| 178 |
-
const
|
| 179 |
-
|
| 180 |
-
if (!domElement) {
|
| 181 |
-
console.warn(`Element to highlight ${step.element} not found`);
|
| 182 |
return;
|
| 183 |
}
|
| 184 |
|
| 185 |
-
let popover = null;
|
| 186 |
-
if (elementOptions.popover && elementOptions.popover.description) {
|
| 187 |
-
const popoverOptions = Object.assign(
|
| 188 |
-
{},
|
| 189 |
-
this.options,
|
| 190 |
-
elementOptions.popover, {
|
| 191 |
-
totalCount: steps.length,
|
| 192 |
-
currentIndex: index,
|
| 193 |
-
isFirst: index === 0,
|
| 194 |
-
isLast: index === steps.length - 1,
|
| 195 |
-
},
|
| 196 |
-
);
|
| 197 |
-
popover = new Popover(popoverOptions, this.window, this.document);
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
const element = new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
|
| 201 |
-
|
| 202 |
this.steps.push(element);
|
| 203 |
});
|
| 204 |
}
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
if (!this.steps || this.steps.length === 0) {
|
| 208 |
throw new Error('There are no steps defined to iterate');
|
| 209 |
}
|
| 210 |
|
| 211 |
-
this.currentStep =
|
| 212 |
-
this.overlay.highlight(this.steps[
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
| 216 |
-
* Highlights the given
|
| 217 |
-
* @param
|
| 218 |
-
* @todo make it accept json or query selector
|
| 219 |
*/
|
| 220 |
highlight(selector) {
|
| 221 |
-
const
|
| 222 |
-
if (!
|
| 223 |
-
console.warn(`Element to highlight ${selector} not found`);
|
| 224 |
return;
|
| 225 |
}
|
| 226 |
|
| 227 |
-
// @todo add options such as position, button texts, additional classes etc
|
| 228 |
-
const popover = new Popover(this.options, this.window, this.document);
|
| 229 |
-
const element = new Element(domElement, this.options, popover, this.overlay, this.window, this.document);
|
| 230 |
this.overlay.highlight(element);
|
| 231 |
}
|
| 232 |
}
|
|
|
|
| 126 |
*/
|
| 127 |
reset() {
|
| 128 |
this.currentStep = 0;
|
| 129 |
+
this.steps = [];
|
| 130 |
this.overlay.clear();
|
| 131 |
}
|
| 132 |
|
|
|
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
+
/**
|
| 172 |
+
* Defines steps to be highlighted
|
| 173 |
+
* @param {array} steps
|
| 174 |
+
*/
|
| 175 |
defineSteps(steps) {
|
| 176 |
this.steps = [];
|
| 177 |
|
|
|
|
| 180 |
throw new Error(`Element (query selector string) missing in step ${index}`);
|
| 181 |
}
|
| 182 |
|
| 183 |
+
const element = this.prepareElementFromStep(step, steps, index);
|
| 184 |
+
if (!element) {
|
|
|
|
|
|
|
| 185 |
return;
|
| 186 |
}
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
this.steps.push(element);
|
| 189 |
});
|
| 190 |
}
|
| 191 |
|
| 192 |
+
/**
|
| 193 |
+
* Prepares the step received from the user and returns an instance
|
| 194 |
+
* of Element
|
| 195 |
+
*
|
| 196 |
+
* @param currentStep Step that is being prepared
|
| 197 |
+
* @param allSteps List of all the steps
|
| 198 |
+
* @param index Index of the current step
|
| 199 |
+
* @returns {null|Element}
|
| 200 |
+
*/
|
| 201 |
+
prepareElementFromStep(currentStep, allSteps = [], index = 0) {
|
| 202 |
+
let querySelector = '';
|
| 203 |
+
let elementOptions = {};
|
| 204 |
+
|
| 205 |
+
if (typeof currentStep === 'string') {
|
| 206 |
+
querySelector = currentStep;
|
| 207 |
+
} else {
|
| 208 |
+
querySelector = currentStep.element;
|
| 209 |
+
elementOptions = Object.assign({}, this.options, currentStep);
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
const domElement = this.document.querySelector(querySelector);
|
| 213 |
+
if (!domElement) {
|
| 214 |
+
console.warn(`Element to highlight ${querySelector} not found`);
|
| 215 |
+
return null;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
let popover = null;
|
| 219 |
+
if (elementOptions.popover && elementOptions.popover.description) {
|
| 220 |
+
const popoverOptions = Object.assign(
|
| 221 |
+
{},
|
| 222 |
+
this.options,
|
| 223 |
+
elementOptions.popover, {
|
| 224 |
+
totalCount: allSteps.length,
|
| 225 |
+
currentIndex: index,
|
| 226 |
+
isFirst: index === 0,
|
| 227 |
+
isLast: index === allSteps.length - 1,
|
| 228 |
+
},
|
| 229 |
+
);
|
| 230 |
+
|
| 231 |
+
popover = new Popover(popoverOptions, this.window, this.document);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
return new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
/**
|
| 238 |
+
* Initiates highlighting steps from first step
|
| 239 |
+
* @param {number} index at which highlight is to be started
|
| 240 |
+
*/
|
| 241 |
+
start(index = 0) {
|
| 242 |
if (!this.steps || this.steps.length === 0) {
|
| 243 |
throw new Error('There are no steps defined to iterate');
|
| 244 |
}
|
| 245 |
|
| 246 |
+
this.currentStep = index;
|
| 247 |
+
this.overlay.highlight(this.steps[index]);
|
| 248 |
}
|
| 249 |
|
| 250 |
/**
|
| 251 |
+
* Highlights the given element
|
| 252 |
+
* @param {string|{element: string, popover: {}}} selector Query selector or a step definition
|
|
|
|
| 253 |
*/
|
| 254 |
highlight(selector) {
|
| 255 |
+
const element = this.prepareElementFromStep(selector);
|
| 256 |
+
if (!element) {
|
|
|
|
| 257 |
return;
|
| 258 |
}
|
| 259 |
|
|
|
|
|
|
|
|
|
|
| 260 |
this.overlay.highlight(element);
|
| 261 |
}
|
| 262 |
}
|
index.html
CHANGED
|
@@ -66,12 +66,28 @@
|
|
| 66 |
sholo.defineSteps([
|
| 67 |
{
|
| 68 |
element: '.section__header',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
popover: {
|
| 70 |
title: 'Adding Introductions',
|
| 71 |
description: 'You can use it to add popovers on top of the website',
|
| 72 |
position: 'left',
|
| 73 |
},
|
| 74 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
{
|
| 76 |
element: '.section__how',
|
| 77 |
popover: {
|
|
@@ -103,6 +119,7 @@
|
|
| 103 |
.addEventListener('click', function () {
|
| 104 |
sholo.start();
|
| 105 |
});
|
|
|
|
| 106 |
</script>
|
| 107 |
</body>
|
| 108 |
</html>
|
|
|
|
| 66 |
sholo.defineSteps([
|
| 67 |
{
|
| 68 |
element: '.section__header',
|
| 69 |
+
popover: {
|
| 70 |
+
title: 'Adding Introductions',
|
| 71 |
+
description: 'You can use it to add popovers on top of the website',
|
| 72 |
+
position: 'bottom',
|
| 73 |
+
},
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
element: '.btn__example',
|
| 77 |
popover: {
|
| 78 |
title: 'Adding Introductions',
|
| 79 |
description: 'You can use it to add popovers on top of the website',
|
| 80 |
position: 'left',
|
| 81 |
},
|
| 82 |
},
|
| 83 |
+
{
|
| 84 |
+
element: '.btn__light',
|
| 85 |
+
popover: {
|
| 86 |
+
title: 'Adding Introductions',
|
| 87 |
+
description: 'You can use it to add popovers on top of the website',
|
| 88 |
+
position: 'right',
|
| 89 |
+
},
|
| 90 |
+
},
|
| 91 |
{
|
| 92 |
element: '.section__how',
|
| 93 |
popover: {
|
|
|
|
| 119 |
.addEventListener('click', function () {
|
| 120 |
sholo.start();
|
| 121 |
});
|
| 122 |
+
|
| 123 |
</script>
|
| 124 |
</body>
|
| 125 |
</html>
|