UNPKG

angular-shepherd

Version:

An Angular wrapper for the site tour library Shepherd.

1 lines 10 kB
{"version":3,"file":"angular-shepherd.mjs","sources":["../../src/lib/utils/dom.ts","../../src/lib/utils/buttons.ts","../../src/lib/shepherd.service.ts","../../src/public_api.ts","../../src/angular-shepherd.ts"],"sourcesContent":["/**\n * Helper method to check if element is hidden, since we cannot use :visible without jQuery\n * @param element The element to check for visibility\n * @returns true if element is hidden\n */\nexport function elementIsHidden(element: HTMLElement): boolean {\n return element.offsetWidth === 0 && element.offsetHeight === 0;\n}\n","import type { StepOptionsButton } from 'shepherd.js';\nimport type { ShepherdService } from '../shepherd.service';\n\nexport type AngularShepherdButton = StepOptionsButton & {\n type?: 'back' | 'cancel' | 'next';\n};\n\n/**\n * Creates a button of the specified type, with the given classes and text\n *\n * @param button.type The type of button cancel, back, or next\n * @param button.classes Classes to apply to the button\n * @param button.text The text for the button\n * @param button.action The action to call\n */\nexport function makeButton(\n this: ShepherdService,\n button: AngularShepherdButton\n) {\n const { classes, disabled, label, secondary, type, text } = button;\n const builtInButtonTypes = ['back', 'cancel', 'next'];\n\n if (!type) {\n return button;\n }\n\n if (builtInButtonTypes.indexOf(type) === -1) {\n throw new Error(\n `'type' property must be one of 'back', 'cancel', or 'next'`\n );\n }\n\n return {\n action: this[type].bind(this),\n classes,\n disabled,\n label,\n secondary,\n text\n };\n}\n","import { Injectable } from '@angular/core';\nimport Shepherd, {\n type TourOptions,\n type StepOptions,\n type Tour\n} from 'shepherd.js';\nimport { elementIsHidden } from './utils/dom';\nimport { makeButton } from './utils/buttons';\n\ninterface RequiredElement {\n message: string;\n selector: string;\n title: string;\n}\n@Injectable({\n providedIn: 'root'\n})\nexport class ShepherdService {\n confirmCancel: TourOptions['confirmCancel'] = false;\n confirmCancelMessage?: TourOptions['confirmCancelMessage'];\n defaultStepOptions: StepOptions = {};\n errorTitle?: string;\n exitOnEsc: TourOptions['exitOnEsc'] = true;\n isActive = false;\n keyboardNavigation: TourOptions['keyboardNavigation'] = true;\n messageForUser: string | null = null;\n modal = false;\n requiredElements: Array<RequiredElement> = [];\n tourName: TourOptions['tourName'] = undefined;\n tourObject: Tour | null = null;\n\n constructor() {}\n\n /**\n * Get the tour object and call back\n */\n back() {\n this.tourObject?.back();\n }\n\n /**\n * Cancel the tour\n */\n cancel() {\n this.tourObject?.cancel();\n }\n\n /**\n * Complete the tour\n */\n complete() {\n this.tourObject?.complete();\n }\n\n /**\n * Hides the current step\n */\n hide() {\n this.tourObject?.hide();\n }\n\n /**\n * Advance the tour to the next step\n */\n next() {\n this.tourObject?.next();\n }\n\n /**\n * Show a specific step, by passing its id\n * @param id The id of the step you want to show\n */\n show(id: string | number) {\n this.tourObject?.show(id);\n }\n\n /**\n * Start the tour\n */\n start() {\n this.isActive = true;\n this.tourObject?.start();\n }\n\n /**\n * This function is called when a tour is completed or cancelled to initiate cleanup.\n * @param completeOrCancel 'complete' or 'cancel'\n */\n onTourFinish(completeOrCancel: string) {\n this.isActive = false;\n }\n\n /**\n * Take a set of steps and create a tour object based on the current configuration\n * @param steps An array of steps\n */\n addSteps(steps: Array<StepOptions>) {\n this._initialize();\n const tour = this.tourObject;\n\n // Return nothing if there are no steps or if somehow there is no tour.\n if (!tour || !steps || !Array.isArray(steps) || steps.length === 0) {\n return;\n }\n\n if (!this.requiredElementsPresent()) {\n tour.addStep({\n buttons: [\n {\n text: 'Exit',\n action: tour.cancel\n }\n ],\n id: 'error',\n title: this.errorTitle,\n text: [this.messageForUser as string]\n });\n return;\n }\n\n steps.forEach((step) => {\n if (step.buttons) {\n step.buttons = step.buttons.map(makeButton.bind(this), this);\n }\n\n tour.addStep(step);\n });\n }\n\n /**\n * Observes the array of requiredElements, which are the elements that must be present at the start of the tour,\n * and determines if they exist, and are visible, if either is false, it will stop the tour from executing.\n */\n private requiredElementsPresent() {\n let allElementsPresent = true;\n\n /* istanbul ignore next: also can't test this due to things attached to root blowing up tests */\n this.requiredElements.forEach((element) => {\n const selectedElement = document.querySelector(element.selector);\n\n if (\n allElementsPresent &&\n (!selectedElement || elementIsHidden(selectedElement as HTMLElement))\n ) {\n allElementsPresent = false;\n this.errorTitle = element.title;\n this.messageForUser = element.message;\n }\n });\n\n return allElementsPresent;\n }\n\n /**\n * Initializes the tour, creates a new Shepherd.Tour. sets options, and binds events.\n */\n private _initialize() {\n const tourObject = new Shepherd.Tour({\n confirmCancel: this.confirmCancel,\n confirmCancelMessage: this.confirmCancelMessage,\n defaultStepOptions: this.defaultStepOptions,\n keyboardNavigation: this.keyboardNavigation,\n tourName: this.tourName,\n useModalOverlay: this.modal,\n exitOnEsc: this.exitOnEsc\n });\n\n tourObject.on('complete', this.onTourFinish.bind(this, 'complete'));\n tourObject.on('cancel', this.onTourFinish.bind(this, 'cancel'));\n\n this.tourObject = tourObject;\n }\n}\n","/*\n * Public API Surface of shepherd\n */\n\nexport * from './lib/shepherd.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;AAAA;;;;AAIG;AACG,SAAU,eAAe,CAAC,OAAoB,EAAA;IAClD,OAAO,OAAO,CAAC,WAAW,KAAK,CAAC,IAAI,OAAO,CAAC,YAAY,KAAK,CAAC;AAChE;;ACAA;;;;;;;AAOG;AACG,SAAU,UAAU,CAExB,MAA6B,EAAA;AAE7B,IAAA,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,MAAM;IAClE,MAAM,kBAAkB,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC;IAErD,IAAI,CAAC,IAAI,EAAE;AACT,QAAA,OAAO,MAAM;IACf;IAEA,IAAI,kBAAkB,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,KAAK,CACb,CAAA,0DAAA,CAA4D,CAC7D;IACH;IAEA,OAAO;QACL,MAAM,EAAE,IAAI,CAAC,IAAI,CA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