|
| 1 | +// eslint-disable-next-line n/no-extraneous-import |
| 2 | +import { offset } from '@floating-ui/dom' |
| 3 | +import { useShepherd } from 'vue-shepherd' |
| 4 | + |
| 5 | +import { useStore } from 'vuex' |
| 6 | + |
| 7 | +import Product from '../services/product.js' |
| 8 | + |
| 9 | +// eslint-disable-next-line n/no-extraneous-import |
| 10 | +import 'shepherd.js/dist/css/shepherd.css' |
| 11 | +import './tour-theme.scss' |
| 12 | + |
| 13 | +function create (id, tourJson) { |
| 14 | + const store = useStore() |
| 15 | + Product.capture('ff-tour-start', { |
| 16 | + tour_id: id |
| 17 | + }) |
| 18 | + const tour = useShepherd({ |
| 19 | + useModalOverlay: true, |
| 20 | + defaultStepOptions: { |
| 21 | + arrow: true, |
| 22 | + classes: 'shepherd-theme-ff', |
| 23 | + scrollTo: false, |
| 24 | + cancelIcon: { |
| 25 | + enabled: true |
| 26 | + }, |
| 27 | + floatingUIOptions: { |
| 28 | + middleware: [offset({ mainAxis: 12, crossAxis: 0 })] |
| 29 | + } |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + function onCancel () { |
| 34 | + const index = tour.steps.indexOf(tour.currentStep) |
| 35 | + store.dispatch('ux/deactivateTour', id) |
| 36 | + Product.capture('ff-tour-cancel', { |
| 37 | + tour_id: id, |
| 38 | + tour_step: index |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + function onComplete () { |
| 43 | + store.dispatch('ux/deactivateTour', id) |
| 44 | + Product.capture('ff-tour-complete', { |
| 45 | + tour_id: id |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + function onBack () { |
| 50 | + tour.back() |
| 51 | + const index = tour.steps.indexOf(tour.currentStep) |
| 52 | + Product.capture('ff-tour-step-back', { |
| 53 | + tour_id: id, |
| 54 | + tour_step: index |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + function onNext () { |
| 59 | + tour.next() |
| 60 | + const index = tour.steps.indexOf(tour.currentStep) |
| 61 | + Product.capture('ff-tour-step-forward', { |
| 62 | + tour_id: id, |
| 63 | + tour_step: index |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + tour.on('cancel', onCancel) |
| 68 | + tour.on('complete', onComplete) |
| 69 | + |
| 70 | + // loop over steps and add them to the tour |
| 71 | + const steps = tourJson.length |
| 72 | + |
| 73 | + tourJson.forEach((step, i) => { |
| 74 | + const buttons = [] |
| 75 | + |
| 76 | + // which secondary button do we need? |
| 77 | + if (i === 0) { |
| 78 | + buttons.push({ |
| 79 | + text: 'Exit', |
| 80 | + action: tour.cancel, |
| 81 | + secondary: true |
| 82 | + }) |
| 83 | + } else { |
| 84 | + buttons.push({ |
| 85 | + text: 'Back', |
| 86 | + action: onBack, |
| 87 | + secondary: true |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + // which primary button do we need? |
| 92 | + if (i !== steps - 1) { |
| 93 | + buttons.push({ |
| 94 | + text: 'Next', |
| 95 | + action: onNext |
| 96 | + }) |
| 97 | + } else { |
| 98 | + buttons.push({ |
| 99 | + text: 'Finish', |
| 100 | + action: tour.complete |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + tour.addStep({ |
| 105 | + ...step, |
| 106 | + buttons |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + return tour |
| 111 | +} |
| 112 | + |
| 113 | +export default { |
| 114 | + create |
| 115 | +} |
0 commit comments