Additional methods and fixes
Browse files- src/core/element.js +34 -11
- src/core/popover.js +2 -1
- src/index.js +25 -4
src/core/element.js
CHANGED
|
@@ -21,6 +21,8 @@ export default class Element {
|
|
| 21 |
this.options = options;
|
| 22 |
this.overlay = overlay;
|
| 23 |
this.popover = popover;
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
|
@@ -114,11 +116,12 @@ export default class Element {
|
|
| 114 |
* i.e. when moving the focus to next element of closing
|
| 115 |
*/
|
| 116 |
onDeselected() {
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
}
|
| 120 |
|
| 121 |
-
this.
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
|
@@ -127,20 +130,24 @@ export default class Element {
|
|
| 127 |
* this element of has just decided to highlight it
|
| 128 |
*/
|
| 129 |
onHighlightStarted() {
|
| 130 |
-
if (!this.popover) {
|
| 131 |
-
return;
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
this.showPopover();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Is called when the element has been successfully highlighted
|
| 139 |
*/
|
| 140 |
onHighlighted() {
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
const highlightedElement = this;
|
| 146 |
const lastHighlightedElement = this.overlay.getLastHighlightedElement();
|
|
@@ -160,12 +167,28 @@ export default class Element {
|
|
| 160 |
highlightedElement.bringInView();
|
| 161 |
}
|
| 162 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Shows the popover on the current element
|
| 167 |
*/
|
| 168 |
showPopover() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
const position = this.getCalculatedPosition();
|
| 170 |
|
| 171 |
this.popover.show(position);
|
|
|
|
| 21 |
this.options = options;
|
| 22 |
this.overlay = overlay;
|
| 23 |
this.popover = popover;
|
| 24 |
+
|
| 25 |
+
this.highlightFinished = false; // To track when the element has fully highlighted
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
|
|
|
| 116 |
* i.e. when moving the focus to next element of closing
|
| 117 |
*/
|
| 118 |
onDeselected() {
|
| 119 |
+
this.hidePopover();
|
| 120 |
+
this.highlightFinished = false;
|
|
|
|
| 121 |
|
| 122 |
+
if (this.options.onDeselected) {
|
| 123 |
+
this.options.onDeselected(this);
|
| 124 |
+
}
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
|
|
|
| 130 |
* this element of has just decided to highlight it
|
| 131 |
*/
|
| 132 |
onHighlightStarted() {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
this.showPopover();
|
| 134 |
+
|
| 135 |
+
// Because element has just started highlighting
|
| 136 |
+
// and hasn't completely highlighted
|
| 137 |
+
this.highlightFinished = false;
|
| 138 |
+
|
| 139 |
+
if (this.options.onHighlightStarted) {
|
| 140 |
+
this.options.onHighlightStarted(this);
|
| 141 |
+
}
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Is called when the element has been successfully highlighted
|
| 146 |
*/
|
| 147 |
onHighlighted() {
|
| 148 |
+
this.showPopover();
|
| 149 |
+
|
| 150 |
+
this.highlightFinished = true;
|
| 151 |
|
| 152 |
const highlightedElement = this;
|
| 153 |
const lastHighlightedElement = this.overlay.getLastHighlightedElement();
|
|
|
|
| 167 |
highlightedElement.bringInView();
|
| 168 |
}
|
| 169 |
}
|
| 170 |
+
|
| 171 |
+
if (this.options.onHighlighted) {
|
| 172 |
+
this.options.onHighlighted(this);
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
hidePopover() {
|
| 177 |
+
if (!this.popover) {
|
| 178 |
+
return;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
this.popover.hide();
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Shows the popover on the current element
|
| 186 |
*/
|
| 187 |
showPopover() {
|
| 188 |
+
if (!this.popover) {
|
| 189 |
+
return;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
const position = this.getCalculatedPosition();
|
| 193 |
|
| 194 |
this.popover.show(position);
|
src/core/popover.js
CHANGED
|
@@ -27,6 +27,7 @@ export default class Popover extends Element {
|
|
| 27 |
isLast: true,
|
| 28 |
totalCount: 1,
|
| 29 |
currentIndex: 0,
|
|
|
|
| 30 |
doneBtnText: 'Done',
|
| 31 |
closeBtnText: 'Close',
|
| 32 |
nextBtnText: 'Next →',
|
|
@@ -145,7 +146,7 @@ export default class Popover extends Element {
|
|
| 145 |
this.closeBtnNode.innerHTML = this.options.closeBtnText;
|
| 146 |
|
| 147 |
// If there was only one item, hide the buttons
|
| 148 |
-
if (!this.options.totalCount || this.options.totalCount === 1) {
|
| 149 |
this.footerNode.style.display = 'none';
|
| 150 |
return;
|
| 151 |
}
|
|
|
|
| 27 |
isLast: true,
|
| 28 |
totalCount: 1,
|
| 29 |
currentIndex: 0,
|
| 30 |
+
showButtons: true,
|
| 31 |
doneBtnText: 'Done',
|
| 32 |
closeBtnText: 'Close',
|
| 33 |
nextBtnText: 'Next →',
|
|
|
|
| 146 |
this.closeBtnNode.innerHTML = this.options.closeBtnText;
|
| 147 |
|
| 148 |
// If there was only one item, hide the buttons
|
| 149 |
+
if (!this.options.showButtons || !this.options.totalCount || this.options.totalCount === 1) {
|
| 150 |
this.footerNode.style.display = 'none';
|
| 151 |
return;
|
| 152 |
}
|
src/index.js
CHANGED
|
@@ -27,6 +27,12 @@ export default class Sholo {
|
|
| 27 |
animate: OVERLAY_ANIMATE, // Whether to animate or not
|
| 28 |
opacity: OVERLAY_OPACITY, // Overlay opacity
|
| 29 |
padding: OVERLAY_PADDING, // Spacing around the element from the overlay
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}, options);
|
| 31 |
|
| 32 |
this.document = document;
|
|
@@ -144,7 +150,6 @@ export default class Sholo {
|
|
| 144 |
reset() {
|
| 145 |
this.currentStep = 0;
|
| 146 |
this.isActivated = false;
|
| 147 |
-
this.steps = [];
|
| 148 |
this.overlay.clear();
|
| 149 |
}
|
| 150 |
|
|
@@ -154,7 +159,23 @@ export default class Sholo {
|
|
| 154 |
*/
|
| 155 |
hasHighlightedElement() {
|
| 156 |
const highlightedElement = this.overlay.getHighlightedElement();
|
| 157 |
-
return highlightedElement && highlightedElement.node;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
|
@@ -195,9 +216,9 @@ export default class Sholo {
|
|
| 195 |
|
| 196 |
if (event.keyCode === ESC_KEY_CODE) {
|
| 197 |
this.reset();
|
| 198 |
-
} else if (event.keyCode === RIGHT_KEY_CODE
|
| 199 |
this.moveNext();
|
| 200 |
-
} else if (event.keyCode === LEFT_KEY_CODE
|
| 201 |
this.movePrevious();
|
| 202 |
}
|
| 203 |
}
|
|
|
|
| 27 |
animate: OVERLAY_ANIMATE, // Whether to animate or not
|
| 28 |
opacity: OVERLAY_OPACITY, // Overlay opacity
|
| 29 |
padding: OVERLAY_PADDING, // Spacing around the element from the overlay
|
| 30 |
+
onHighlightStarted: () => { // When element is about to be highlighted
|
| 31 |
+
},
|
| 32 |
+
onHighlighted: () => { // When element has been highlighted
|
| 33 |
+
},
|
| 34 |
+
onDeselected: () => { // When the element has been deselected
|
| 35 |
+
},
|
| 36 |
}, options);
|
| 37 |
|
| 38 |
this.document = document;
|
|
|
|
| 150 |
reset() {
|
| 151 |
this.currentStep = 0;
|
| 152 |
this.isActivated = false;
|
|
|
|
| 153 |
this.overlay.clear();
|
| 154 |
}
|
| 155 |
|
|
|
|
| 159 |
*/
|
| 160 |
hasHighlightedElement() {
|
| 161 |
const highlightedElement = this.overlay.getHighlightedElement();
|
| 162 |
+
return highlightedElement && highlightedElement.node && highlightedElement.highlightFinished;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
/**
|
| 166 |
+
* Gets the currently highlighted element in overlay
|
| 167 |
+
* @returns {Element}
|
| 168 |
+
*/
|
| 169 |
+
getHighlightedElement() {
|
| 170 |
+
return this.overlay.getHighlightedElement();
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
/**
|
| 174 |
+
* Gets the element that was highlighted before currently highlighted element
|
| 175 |
+
* @returns {Element}
|
| 176 |
+
*/
|
| 177 |
+
getLastHighlightedElement() {
|
| 178 |
+
return this.overlay.getLastHighlightedElement();
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
|
|
|
| 216 |
|
| 217 |
if (event.keyCode === ESC_KEY_CODE) {
|
| 218 |
this.reset();
|
| 219 |
+
} else if (event.keyCode === RIGHT_KEY_CODE) {
|
| 220 |
this.moveNext();
|
| 221 |
+
} else if (event.keyCode === LEFT_KEY_CODE) {
|
| 222 |
this.movePrevious();
|
| 223 |
}
|
| 224 |
}
|