Improve performance and fix positioning issue with popover
Browse files- demo/scripts/demo.js +3 -0
- src/common/utils.js +1 -2
- src/core/element.js +4 -10
- src/core/popover.js +9 -6
demo/scripts/demo.js
CHANGED
@@ -6,6 +6,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
6 |
{
|
7 |
element: document.getElementById('driver-demo-head'),
|
8 |
popover: {
|
|
|
9 |
title: 'Before we start',
|
10 |
description: 'This is just one use-case, make sure to check out the rest of the docs below.',
|
11 |
nextBtnText: 'Okay, Start!',
|
@@ -102,6 +103,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
102 |
opacity: 0.8,
|
103 |
padding: 5,
|
104 |
showButtons: true,
|
|
|
105 |
});
|
106 |
|
107 |
boringTourDriver.defineSteps(tourSteps);
|
@@ -331,6 +333,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
331 |
{
|
332 |
element: '#first-element-introduction',
|
333 |
popover: {
|
|
|
334 |
title: 'Title on Popover',
|
335 |
description: 'Body of the popover',
|
336 |
position: 'top'
|
|
|
6 |
{
|
7 |
element: document.getElementById('driver-demo-head'),
|
8 |
popover: {
|
9 |
+
className: 'scoped-driver-popover',
|
10 |
title: 'Before we start',
|
11 |
description: 'This is just one use-case, make sure to check out the rest of the docs below.',
|
12 |
nextBtnText: 'Okay, Start!',
|
|
|
103 |
opacity: 0.8,
|
104 |
padding: 5,
|
105 |
showButtons: true,
|
106 |
+
className: 'boring-scope',
|
107 |
});
|
108 |
|
109 |
boringTourDriver.defineSteps(tourSteps);
|
|
|
333 |
{
|
334 |
element: '#first-element-introduction',
|
335 |
popover: {
|
336 |
+
className: 'first-step-popover-class',
|
337 |
title: 'Title on Popover',
|
338 |
description: 'Body of the popover',
|
339 |
position: 'top'
|
src/common/utils.js
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
/**
|
2 |
* Turn a string into a node
|
3 |
* @param {String} htmlString to convert
|
4 |
-
* @return {Node} Converted node element
|
5 |
*/
|
6 |
-
// eslint-disable-next-line
|
7 |
export const createNodeFromString = (htmlString) => {
|
8 |
const div = document.createElement('div');
|
9 |
div.innerHTML = htmlString.trim();
|
|
|
1 |
/**
|
2 |
* Turn a string into a node
|
3 |
* @param {String} htmlString to convert
|
4 |
+
* @return {HTMLElement|Node} Converted node element
|
5 |
*/
|
|
|
6 |
export const createNodeFromString = (htmlString) => {
|
7 |
const div = document.createElement('div');
|
8 |
div.innerHTML = htmlString.trim();
|
src/core/element.js
CHANGED
@@ -38,14 +38,13 @@ export default class Element {
|
|
38 |
this.overlay = overlay;
|
39 |
this.popover = popover;
|
40 |
this.stage = stage;
|
41 |
-
|
42 |
this.animationTimeout = null;
|
43 |
}
|
44 |
|
45 |
/**
|
46 |
* Checks if the current element is visible in viewport
|
47 |
* @returns {boolean}
|
48 |
-
* @
|
49 |
*/
|
50 |
isInView() {
|
51 |
let top = this.node.offsetTop;
|
@@ -83,10 +82,11 @@ export default class Element {
|
|
83 |
|
84 |
/**
|
85 |
* Brings the element to middle of the view port if not in view
|
86 |
-
* @
|
87 |
*/
|
88 |
bringInView() {
|
89 |
-
|
|
|
90 |
return;
|
91 |
}
|
92 |
|
@@ -196,12 +196,6 @@ export default class Element {
|
|
196 |
this.addHighlightClasses();
|
197 |
|
198 |
const highlightedElement = this;
|
199 |
-
const popoverElement = this.popover;
|
200 |
-
|
201 |
-
if (popoverElement && !popoverElement.isInView()) {
|
202 |
-
popoverElement.bringInView();
|
203 |
-
}
|
204 |
-
|
205 |
if (!highlightedElement.isInView()) {
|
206 |
highlightedElement.bringInView();
|
207 |
}
|
|
|
38 |
this.overlay = overlay;
|
39 |
this.popover = popover;
|
40 |
this.stage = stage;
|
|
|
41 |
this.animationTimeout = null;
|
42 |
}
|
43 |
|
44 |
/**
|
45 |
* Checks if the current element is visible in viewport
|
46 |
* @returns {boolean}
|
47 |
+
* @public
|
48 |
*/
|
49 |
isInView() {
|
50 |
let top = this.node.offsetTop;
|
|
|
82 |
|
83 |
/**
|
84 |
* Brings the element to middle of the view port if not in view
|
85 |
+
* @public
|
86 |
*/
|
87 |
bringInView() {
|
88 |
+
// If the node is not there or already is in view
|
89 |
+
if (!this.node || this.isInView()) {
|
90 |
return;
|
91 |
}
|
92 |
|
|
|
196 |
this.addHighlightClasses();
|
197 |
|
198 |
const highlightedElement = this;
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
if (!highlightedElement.isInView()) {
|
200 |
highlightedElement.bringInView();
|
201 |
}
|
src/core/popover.js
CHANGED
@@ -42,9 +42,6 @@ export default class Popover extends Element {
|
|
42 |
|
43 |
this.window = window;
|
44 |
this.document = document;
|
45 |
-
|
46 |
-
this.attachNode();
|
47 |
-
this.hide();
|
48 |
}
|
49 |
|
50 |
/**
|
@@ -53,11 +50,13 @@ export default class Popover extends Element {
|
|
53 |
*/
|
54 |
attachNode() {
|
55 |
let popover = this.document.getElementById(ID_POPOVER);
|
56 |
-
if (
|
57 |
-
popover
|
58 |
-
document.body.appendChild(popover);
|
59 |
}
|
60 |
|
|
|
|
|
|
|
61 |
this.node = popover;
|
62 |
this.tipNode = popover.querySelector(`.${CLASS_POPOVER_TIP}`);
|
63 |
this.titleNode = popover.querySelector(`.${CLASS_POPOVER_TITLE}`);
|
@@ -117,6 +116,7 @@ export default class Popover extends Element {
|
|
117 |
* @public
|
118 |
*/
|
119 |
show(position) {
|
|
|
120 |
this.setInitialState();
|
121 |
|
122 |
// Set the title and descriptions
|
@@ -172,6 +172,9 @@ export default class Popover extends Element {
|
|
172 |
this.autoPosition(position);
|
173 |
break;
|
174 |
}
|
|
|
|
|
|
|
175 |
}
|
176 |
|
177 |
/**
|
|
|
42 |
|
43 |
this.window = window;
|
44 |
this.document = document;
|
|
|
|
|
|
|
45 |
}
|
46 |
|
47 |
/**
|
|
|
50 |
*/
|
51 |
attachNode() {
|
52 |
let popover = this.document.getElementById(ID_POPOVER);
|
53 |
+
if (popover) {
|
54 |
+
popover.parentElement.removeChild(popover);
|
|
|
55 |
}
|
56 |
|
57 |
+
popover = createNodeFromString(POPOVER_HTML(this.options.className));
|
58 |
+
document.body.appendChild(popover);
|
59 |
+
|
60 |
this.node = popover;
|
61 |
this.tipNode = popover.querySelector(`.${CLASS_POPOVER_TIP}`);
|
62 |
this.titleNode = popover.querySelector(`.${CLASS_POPOVER_TITLE}`);
|
|
|
116 |
* @public
|
117 |
*/
|
118 |
show(position) {
|
119 |
+
this.attachNode();
|
120 |
this.setInitialState();
|
121 |
|
122 |
// Set the title and descriptions
|
|
|
172 |
this.autoPosition(position);
|
173 |
break;
|
174 |
}
|
175 |
+
|
176 |
+
// Bring the popover in view port once it is displayed
|
177 |
+
this.bringInView();
|
178 |
}
|
179 |
|
180 |
/**
|