Skip to content

Commit

Permalink
Package basics
Browse files Browse the repository at this point in the history
  • Loading branch information
binrysearch committed Jun 14, 2024
1 parent b51910f commit cf4a5af
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 105 deletions.
15 changes: 7 additions & 8 deletions src/core/DOMEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IntroJs } from "../intro";
import stamp from "../util/stamp";

/**
Expand All @@ -16,21 +15,21 @@ class DOMEvent {
/**
* Gets a unique ID for an event listener
*/
private _id(type: string, listener: Function, context: IntroJs) {
private _id<T>(type: string, listener: Function, context: T) {
return type + stamp(listener) + (context ? `_${stamp(context)}` : "");
}

/**
* Adds event listener
*/
public on(
public on<T>(
obj: EventTarget,
type: string,
listener: (
context: IntroJs | EventTarget,
context: T | EventTarget,
e: Event
) => void | undefined | string | Promise<string | void>,
context: IntroJs,
context: T,
useCapture: boolean
) {
const id = this._id(type, listener, context);
Expand All @@ -52,14 +51,14 @@ class DOMEvent {
/**
* Removes event listener
*/
public off(
public off<T>(
obj: EventTarget,
type: string,
listener: (
context: IntroJs | EventTarget,
context: T | EventTarget,
e: Event
) => void | undefined | string | Promise<string | void>,
context: IntroJs,
context: T,
useCapture: boolean
) {
const id = this._id(type, listener, context);
Expand Down
2 changes: 1 addition & 1 deletion src/core/addOverlayLayer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import exitIntro from "./exitIntro";
import exitIntro from "../packages/tour/exitIntro";
import createElement from "../util/createElement";
import setStyle from "../util/setStyle";
import { IntroJs } from "../intro";
Expand Down
50 changes: 0 additions & 50 deletions src/core/introForElement.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/core/onKeyDown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { nextStep, previousStep } from "./steps";
import exitIntro from "./exitIntro";
import exitIntro from "../packages/tour/exitIntro";
import { IntroJs } from "../intro";
import isFunction from "../util/isFunction";
import { Tour } from "src/packages/tour/tour";

/**
* on keyCode:
Expand All @@ -18,18 +19,18 @@ import isFunction from "../util/isFunction";
* (3) e.keyCode
* https://github.com/jquery/jquery/blob/a6b0705294d336ae2f63f7276de0da1195495363/src/event.js#L638
*/
export default async function onKeyDown(intro: IntroJs, e: KeyboardEvent) {
export default async function onKeyDown(tour: Tour, e: KeyboardEvent) {
let code = e.code === undefined ? e.which : e.code;

// if e.which is null
if (code === null) {
code = e.charCode === null ? e.keyCode : e.charCode;
}

if ((code === "Escape" || code === 27) && intro._options.exitOnEsc === true) {
if ((code === "Escape" || code === 27) && tour.getOption('exitOnEsc') === true) {
//escape key pressed, exit the intro
//check if exit callback is defined
await exitIntro(intro, intro._targetElement);
await exitIntro(tour, tour.getTargetElement());
} else if (code === "ArrowLeft" || code === 37) {
//left arrow
await previousStep(intro);
Expand Down
6 changes: 0 additions & 6 deletions src/core/onResize.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/showElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import setShowElement from "../util/setShowElement";
import scrollParentToElement from "../util/scrollParentToElement";
import addClass from "../util/addClass";
import scrollTo from "../util/scrollTo";
import exitIntro from "./exitIntro";
import exitIntro from "../packages/tour/exitIntro";
import setAnchorAsButton from "../util/setAnchorAsButton";
import { IntroStep, nextStep, previousStep } from "./steps";
import setHelperLayerPosition from "./setHelperLayerPosition";
Expand Down
2 changes: 1 addition & 1 deletion src/core/steps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isFunction from "../util/isFunction";
import exitIntro from "./exitIntro";
import exitIntro from "../packages/tour/exitIntro";
import showElement from "./showElement";
import { IntroJs } from "../intro";

Expand Down
9 changes: 2 additions & 7 deletions src/intro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getDontShowAgain, setDontShowAgain } from "./core/dontShowAgain";
import exitIntro from "./core/exitIntro";
import exitIntro from "./packages/tour/exitIntro";
import {
hideHint,
hideHints,
Expand All @@ -10,7 +10,7 @@ import {
showHintDialog,
showHints,
} from "./core/hint";
import introForElement from "./core/introForElement";
import introForElement from "./packages/tour/introForElement";
import refresh from "./core/refresh";
import {
HintStep,
Expand Down Expand Up @@ -167,11 +167,6 @@ export class IntroJs {
return this._currentStep;
}

async exit(force: boolean) {
await exitIntro(this, this._targetElement, force);
return this;
}

refresh(refreshSteps?: boolean) {
refresh(this, refreshSteps);
return this;
Expand Down
15 changes: 6 additions & 9 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,18 @@ export function getDefaultOptions(): Options {
};
}

export function setOption<K extends keyof Options>(
options: Options,
export function setOption<T, K extends keyof T>(
options: T,
key: K,
value: Options[K]
): Options {
value: T[K]
): T {
options[key] = value;
return options;
}

export function setOptions(
options: Options,
partialOptions: Partial<Options>
): Options {
export function setOptions<T>(options: T, partialOptions: Partial<T>): T {
for (const [key, value] of Object.entries(partialOptions)) {
options = setOption(options, key as keyof Options, value);
options = setOption(options, key as keyof T, value as T[keyof T]);
}
return options;
}
8 changes: 8 additions & 0 deletions src/packages/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Package<TOption> {
getOption<K extends keyof TOption>(key: K): TOption[K];
setOptions(partialOptions: Partial<TOption>): this;
setOption<K extends keyof TOption>(key: K, value: TOption[K]): this;
clone(): ThisType<this>;
isActive(): boolean;
render(): this;
}
43 changes: 43 additions & 0 deletions src/packages/tour/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Tour } from "./tour";

export type introBeforeChangeCallback = (
this: Tour,
targetElement: HTMLElement,
currentStep: number,
direction: "backward" | "forward"
) => Promise<boolean> | boolean;

export type introChangeCallback = (
this: Tour,
targetElement: HTMLElement
) => void | Promise<void>;

export type introAfterChangeCallback = (
this: Tour,
targetElement: HTMLElement
) => void | Promise<void>;

export type introCompleteCallback = (
this: Tour,
currentStep: number,
reason: "skip" | "end" | "done"
) => void | Promise<void>;

export type introStartCallback = (
this: Tour,
targetElement: HTMLElement
) => void | Promise<void>;

export type introExitCallback = (this: Tour) => void | Promise<void>;

export type introSkipCallback = (
this: Tour,
currentStep: number
) => void | Promise<void>;

export type introBeforeExitCallback = (
this: Tour,
targetElement: HTMLElement
) => boolean | Promise<boolean>;

export type hintsAddedCallback = (this: Tour) => void | Promise<void>;
37 changes: 20 additions & 17 deletions src/core/exitIntro.ts → src/packages/tour/exitIntro.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import DOMEvent from "./DOMEvent";
import onKeyDown from "./onKeyDown";
import DOMEvent from "../../core/DOMEvent";
import onKeyDown from "../../core/onKeyDown";
import onResize from "./onResize";
import removeShowElement from "./removeShowElement";
import removeChild from "../util/removeChild";
import { IntroJs } from "../intro";
import isFunction from "../util/isFunction";
import removeShowElement from "../../core/removeShowElement";
import removeChild from "../../util/removeChild";
import isFunction from "../../util/isFunction";
import { Tour } from "./tour";
import {
introBeforeExitCallback,
introExitCallback,
} from "./callback";

/**
* Exit from intro
Expand All @@ -13,20 +17,19 @@ import isFunction from "../util/isFunction";
* @param {Boolean} force - Setting to `true` will skip the result of beforeExit callback
*/
export default async function exitIntro(
intro: IntroJs,
tour: Tour,
targetElement: HTMLElement,
beforeExitCallback?: introBeforeExitCallback,
exitCallback?: introExitCallback,
force: boolean = false
) {
let continueExit = true;

// calling onbeforeexit callback
//
// If this callback return `false`, it would halt the process
if (intro._introBeforeExitCallback !== undefined) {
continueExit = await intro._introBeforeExitCallback.call(
intro,
targetElement
);
if (isFunction(beforeExitCallback)) {
continueExit = await beforeExitCallback.call(tour, targetElement);
}

// skip this check if `force` parameter is `true`
Expand Down Expand Up @@ -70,14 +73,14 @@ export default async function exitIntro(
removeShowElement();

//clean listeners
DOMEvent.off(window, "keydown", onKeyDown, intro, true);
DOMEvent.off(window, "resize", onResize, intro, true);
DOMEvent.off(window, "keydown", onKeyDown, tour, true);
DOMEvent.off(window, "resize", onResize, tour, true);

//check if any callback is defined
if (isFunction(intro._introExitCallback)) {
await intro._introExitCallback.call(intro);
if (isFunction(exitCallback)) {
await exitCallback.call(tour);
}

// set the step to default
intro._currentStep = -1;
tour.setCurrentStep(-1);
}
Empty file added src/packages/tour/index.ts
Empty file.
50 changes: 50 additions & 0 deletions src/packages/tour/introForElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import addOverlayLayer from "../../core/addOverlayLayer";
import DOMEvent from "../../core/DOMEvent";
import { nextStep } from "../../core/steps";
import onKeyDown from "../../core/onKeyDown";
import onResize from "./onResize";
import fetchIntroSteps from "../../core/fetchIntroSteps";
import isFunction from "../../util/isFunction";
import { Tour } from "./tour";

/**
* Initiate a new introduction/guide from an element in the page
*
* @api private
*/
export default async function introForElement(
tour: Tour,
targetElm: HTMLElement
): Promise<Boolean> {
// don't start the tour if the instance is not active
if (!tour.isActive()) return false;

if (isFunction(tour._introStartCallback)) {
await tour._introStartCallback.call(tour, targetElm);
}

//set it to the introJs object
const steps = fetchIntroSteps(tour, targetElm);

if (steps.length === 0) {
return false;
}

tour.setSteps(steps);

//add overlay layer to the page
if (addOverlayLayer(tour, targetElm)) {
//then, start the show
await nextStep(tour);

targetElm.addEventListener;
if (tour.getOption("keyboardNavigation")) {
DOMEvent.on(window, "keydown", onKeyDown, tour, true);
}

//for window resize
DOMEvent.on(window, "resize", onResize, tour, true);
}

return false;
}
6 changes: 6 additions & 0 deletions src/packages/tour/onResize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import refresh from "../../core/refresh";
import { Tour } from "./tour";

export default function onResize(tour: Tour) {
refresh(tour);
}
Loading

0 comments on commit cf4a5af

Please sign in to comment.