File size: 8,376 Bytes
6aa2828 e459aae 7fbff02 3223525 7fbff02 63b089b 836d49f 5e1648c aa3b624 a0a08e9 3223525 8480986 aa3b624 66a740c 63b089b ec183be 0c90a3d ec183be 0c90a3d ec183be aafd6df e459aae ec183be aafd6df e459aae aafd6df e459aae aafd6df e459aae ec183be aafd6df ec183be e5cb552 aafd6df ec183be aafd6df aa3b624 9bde4cc 4c98241 9bde4cc aa3b624 3833190 63b089b d0e0b03 aafd6df aa3b624 6aa2828 e459aae 6aa2828 ec183be aafd6df 6aa2828 e9a1e79 9bcba14 03f5109 9bcba14 6aa2828 a0a9d5a 4fb4c20 a0a9d5a 4fb4c20 6aa2828 a0a9d5a 6aa2828 03f5109 4fb4c20 6aa2828 4fb4c20 6aa2828 4fb4c20 6aa2828 bd90a81 215369d 0c90a3d bd90a81 a0a08e9 e5cb552 0c90a3d 9bcba14 c7a3398 9bde4cc aa3b624 b8874fd aa3b624 e459aae 63b089b 836d49f 215369d c7a3398 215369d c7a3398 e5cb552 c7a3398 215369d c7a3398 e5cb552 c7a3398 215369d aa3b624 ff5f16d bd90a81 6aa2828 a0a08e9 ec183be bd90a81 aa3b624 40c6a95 d656141 fe7906f 03f5109 d656141 40c6a95 aa3b624 0c90a3d aa3b624 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import { AllowedButtons, destroyPopover, Popover } from "./popover";
import { destroyOverlay } from "./overlay";
import { destroyEvents, initEvents, requireRefresh } from "./events";
import { Config, configure, DriverHook, getConfig } from "./config";
import { destroyHighlight, highlight } from "./highlight";
import { destroyEmitter, listen } from "./emitter";
import { getState, resetState, setState } from "./state";
import "./driver.css";
export type DriveStep = {
element?: string | Element;
onHighlightStarted?: DriverHook;
onHighlighted?: DriverHook;
onDeselected?: DriverHook;
popover?: Popover;
};
export function driver(options: Config = {}) {
configure(options);
function handleClose() {
if (!getConfig("allowClose")) {
return;
}
destroy();
}
function moveNext() {
const activeIndex = getState("activeIndex");
const steps = getConfig("steps") || [];
if (typeof activeIndex === "undefined") {
return;
}
const nextStepIndex = activeIndex + 1;
if (steps[nextStepIndex]) {
drive(nextStepIndex);
} else {
destroy();
}
}
function movePrevious() {
const activeIndex = getState("activeIndex");
const steps = getConfig("steps") || [];
if (typeof activeIndex === "undefined") {
return;
}
const previousStepIndex = activeIndex - 1;
if (steps[previousStepIndex]) {
drive(previousStepIndex);
} else {
destroy();
}
}
function handleArrowLeft() {
const isTransitioning = getState("__transitionCallback");
if (isTransitioning) {
return;
}
const activeIndex = getState("activeIndex");
const activeStep = getState("activeStep");
const activeElement = getState("activeElement");
if (typeof activeIndex === "undefined" || typeof activeStep === "undefined") {
return;
}
const currentStepIndex = getState("activeIndex");
if (typeof currentStepIndex === "undefined") {
return;
}
const onPrevClick = activeStep.popover?.onPrevClick || getConfig("onPrevClick");
if (onPrevClick) {
return onPrevClick(activeElement, activeStep, {
config: getConfig(),
state: getState(),
});
}
movePrevious();
}
function handleArrowRight() {
const isTransitioning = getState("__transitionCallback");
if (isTransitioning) {
return;
}
const activeIndex = getState("activeIndex");
const activeStep = getState("activeStep");
const activeElement = getState("activeElement");
if (typeof activeIndex === "undefined" || typeof activeStep === "undefined") {
return;
}
const onNextClick = activeStep.popover?.onNextClick || getConfig("onNextClick");
if (onNextClick) {
return onNextClick(activeElement, activeStep, {
config: getConfig(),
state: getState(),
});
}
moveNext();
}
function init() {
if (getState("isInitialized")) {
return;
}
setState("isInitialized", true);
document.body.classList.add("driver-active", getConfig("animate") ? "driver-fade" : "driver-simple");
initEvents();
listen("overlayClick", handleClose);
listen("escapePress", handleClose);
listen("arrowLeftPress", handleArrowLeft);
listen("arrowRightPress", handleArrowRight);
}
function drive(stepIndex: number = 0) {
const steps = getConfig("steps");
if (!steps) {
console.error("No steps to drive through");
destroy();
return;
}
if (!steps[stepIndex]) {
destroy();
return;
}
setState("activeIndex", stepIndex);
const currentStep = steps[stepIndex];
const hasNextStep = steps[stepIndex + 1];
const hasPreviousStep = steps[stepIndex - 1];
const doneBtnText = currentStep.popover?.doneBtnText || getConfig("doneBtnText") || "Done";
const allowsClosing = getConfig("allowClose");
const showProgress =
typeof currentStep.popover?.showProgress !== "undefined"
? currentStep.popover?.showProgress
: getConfig("showProgress");
const progressText = currentStep.popover?.progressText || getConfig("progressText") || "{{current}} of {{total}}";
const progressTextReplaced = progressText
.replace("{{current}}", `${stepIndex + 1}`)
.replace("{{total}}", `${steps.length}`);
const configuredButtons = currentStep.popover?.showButtons || getConfig("showButtons");
const calculatedButtons: AllowedButtons[] = [
"next",
"previous",
...(allowsClosing ? ["close" as AllowedButtons] : []),
].filter(b => {
return !configuredButtons?.length || configuredButtons.includes(b as AllowedButtons);
}) as AllowedButtons[];
const onNextClick = currentStep.popover?.onNextClick || getConfig("onNextClick");
const onPrevClick = currentStep.popover?.onPrevClick || getConfig("onPrevClick");
const onCloseClick = currentStep.popover?.onCloseClick || getConfig("onCloseClick");
highlight({
...currentStep,
popover: {
showButtons: calculatedButtons,
nextBtnText: !hasNextStep ? doneBtnText : undefined,
disableButtons: [...(!hasPreviousStep ? ["previous" as AllowedButtons] : [])],
showProgress: showProgress,
progressText: progressTextReplaced,
onNextClick: onNextClick ? onNextClick : () => {
if (!hasNextStep) {
destroy();
} else {
drive(stepIndex + 1);
}
},
onPrevClick: onPrevClick ? onPrevClick : () => {
drive(stepIndex - 1);
},
onCloseClick: onCloseClick ? onCloseClick : () => {
destroy();
},
...(currentStep?.popover || {}),
},
});
}
function destroy(withOnDestroyStartedHook = true) {
const activeElement = getState("activeElement");
const activeStep = getState("activeStep");
const onDestroyStarted = getConfig("onDestroyStarted");
// `onDestroyStarted` is used to confirm the exit of tour. If we trigger
// the hook for when user calls `destroy`, driver will get into infinite loop
// not causing tour to be destroyed.
if (withOnDestroyStartedHook && onDestroyStarted) {
const isActiveDummyElement = !activeElement || activeElement?.id === "driver-dummy-element";
onDestroyStarted(isActiveDummyElement ? undefined : activeElement, activeStep!, {
config: getConfig(),
state: getState(),
});
return;
}
const onDeselected = activeStep?.onDeselected || getConfig("onDeselected");
const onDestroyed = getConfig("onDestroyed");
document.body.classList.remove("driver-active", "driver-fade", "driver-simple");
destroyEvents();
destroyPopover();
destroyHighlight();
destroyOverlay();
destroyEmitter();
resetState();
if (activeElement && activeStep) {
const isActiveDummyElement = activeElement.id === "driver-dummy-element";
if (onDeselected) {
onDeselected(isActiveDummyElement ? undefined : activeElement, activeStep, {
config: getConfig(),
state: getState(),
});
}
if (onDestroyed) {
onDestroyed(isActiveDummyElement ? undefined : activeElement, activeStep, {
config: getConfig(),
state: getState(),
});
}
}
}
return {
isActive: () => getState("isInitialized") || false,
refresh: requireRefresh,
drive: (stepIndex: number = 0) => {
init();
drive(stepIndex);
},
setConfig: configure,
getConfig,
getState,
moveNext,
movePrevious,
hasNextStep: () => {
const steps = getConfig("steps") || [];
const activeIndex = getState("activeIndex");
return activeIndex !== undefined && steps[activeIndex + 1];
},
hasPreviousStep: () => {
const steps = getConfig("steps") || [];
const activeIndex = getState("activeIndex");
return activeIndex !== undefined && steps[activeIndex - 1];
},
highlight: (step: DriveStep) => {
init();
highlight({
...step,
popover: step.popover
? {
showButtons: [],
showProgress: false,
progressText: "",
...step.popover!,
}
: undefined,
});
},
destroy: () => {
destroy(false);
},
};
}
|