Add confirmation of destroying
Browse files- index.html +5 -0
- src/config.ts +1 -0
- src/driver.ts +20 -8
index.html
CHANGED
|
@@ -402,6 +402,11 @@ npm install driver.js</pre
|
|
| 402 |
const driverObj = driver({
|
| 403 |
animate: true,
|
| 404 |
opacity: 0.3,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
steps: [
|
| 406 |
{
|
| 407 |
element: ".page-header",
|
|
|
|
| 402 |
const driverObj = driver({
|
| 403 |
animate: true,
|
| 404 |
opacity: 0.3,
|
| 405 |
+
onDestroyStarted: (element, step) => {
|
| 406 |
+
if (confirm('Are you sure?')) {
|
| 407 |
+
driverObj.destroy();
|
| 408 |
+
}
|
| 409 |
+
},
|
| 410 |
steps: [
|
| 411 |
{
|
| 412 |
element: ".page-header",
|
src/config.ts
CHANGED
|
@@ -33,6 +33,7 @@ export type Config = {
|
|
| 33 |
onHighlightStarted?: (element: Element | undefined, step: DriveStep) => void;
|
| 34 |
onHighlighted?: (element: Element | undefined, step: DriveStep) => void;
|
| 35 |
onDeselected?: (element: Element | undefined, step: DriveStep) => void;
|
|
|
|
| 36 |
onDestroyed?: (element: Element | undefined, step: DriveStep) => void;
|
| 37 |
|
| 38 |
// Event based callbacks, called upon events
|
|
|
|
| 33 |
onHighlightStarted?: (element: Element | undefined, step: DriveStep) => void;
|
| 34 |
onHighlighted?: (element: Element | undefined, step: DriveStep) => void;
|
| 35 |
onDeselected?: (element: Element | undefined, step: DriveStep) => void;
|
| 36 |
+
onDestroyStarted?: (element: Element | undefined, step: DriveStep) => void;
|
| 37 |
onDestroyed?: (element: Element | undefined, step: DriveStep) => void;
|
| 38 |
|
| 39 |
// Event based callbacks, called upon events
|
src/driver.ts
CHANGED
|
@@ -25,9 +25,9 @@ export function driver(options: Config = {}) {
|
|
| 25 |
}
|
| 26 |
|
| 27 |
function moveNext() {
|
| 28 |
-
const activeIndex = getState(
|
| 29 |
-
const steps = getConfig(
|
| 30 |
-
if (typeof
|
| 31 |
return;
|
| 32 |
}
|
| 33 |
|
|
@@ -40,9 +40,9 @@ export function driver(options: Config = {}) {
|
|
| 40 |
}
|
| 41 |
|
| 42 |
function movePrevious() {
|
| 43 |
-
const activeIndex = getState(
|
| 44 |
-
const steps = getConfig(
|
| 45 |
-
if (typeof
|
| 46 |
return;
|
| 47 |
}
|
| 48 |
|
|
@@ -145,10 +145,20 @@ export function driver(options: Config = {}) {
|
|
| 145 |
});
|
| 146 |
}
|
| 147 |
|
| 148 |
-
function destroy() {
|
| 149 |
const activeElement = getState("activeElement");
|
| 150 |
const activeStep = getState("activeStep");
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
const onDeselected = activeStep?.popover?.onDeselected || getConfig("onDeselected");
|
| 153 |
const onDestroyed = getConfig("onDestroyed");
|
| 154 |
|
|
@@ -197,6 +207,8 @@ export function driver(options: Config = {}) {
|
|
| 197 |
: undefined,
|
| 198 |
});
|
| 199 |
},
|
| 200 |
-
destroy
|
|
|
|
|
|
|
| 201 |
};
|
| 202 |
}
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
function moveNext() {
|
| 28 |
+
const activeIndex = getState("activeIndex");
|
| 29 |
+
const steps = getConfig("steps") || [];
|
| 30 |
+
if (typeof activeIndex === "undefined") {
|
| 31 |
return;
|
| 32 |
}
|
| 33 |
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
function movePrevious() {
|
| 43 |
+
const activeIndex = getState("activeIndex");
|
| 44 |
+
const steps = getConfig("steps") || [];
|
| 45 |
+
if (typeof activeIndex === "undefined") {
|
| 46 |
return;
|
| 47 |
}
|
| 48 |
|
|
|
|
| 145 |
});
|
| 146 |
}
|
| 147 |
|
| 148 |
+
function destroy(withStartHook = true) {
|
| 149 |
const activeElement = getState("activeElement");
|
| 150 |
const activeStep = getState("activeStep");
|
| 151 |
|
| 152 |
+
const onDestroyStarted = getConfig("onDestroyStarted");
|
| 153 |
+
|
| 154 |
+
// `onDestroyStarted` is used to confirm the exit of tour. If we trigger
|
| 155 |
+
// the hook for when user calls `destroy`, driver will get into infinite loop
|
| 156 |
+
// not causing tour to be destroyed.
|
| 157 |
+
if (withStartHook && onDestroyStarted) {
|
| 158 |
+
onDestroyStarted(activeElement, activeStep!);
|
| 159 |
+
return;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
const onDeselected = activeStep?.popover?.onDeselected || getConfig("onDeselected");
|
| 163 |
const onDestroyed = getConfig("onDestroyed");
|
| 164 |
|
|
|
|
| 207 |
: undefined,
|
| 208 |
});
|
| 209 |
},
|
| 210 |
+
destroy: () => {
|
| 211 |
+
destroy(false);
|
| 212 |
+
},
|
| 213 |
};
|
| 214 |
}
|