kamrify commited on
Commit
3f937e4
·
1 Parent(s): ad0769b

Add callbacks for onNext, onPrevious

Browse files
Files changed (2) hide show
  1. demo/scripts/demo.js +5 -5
  2. src/index.js +32 -12
demo/scripts/demo.js CHANGED
@@ -1,5 +1,5 @@
1
  /* eslint-disable */
2
- document.addEventListener("DOMContentLoaded", function () {
3
 
4
  const tourSteps = [
5
  {
@@ -8,7 +8,7 @@ document.addEventListener("DOMContentLoaded", function () {
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
10
  nextBtnText: 'Okay, Start!'
11
- }
12
  }, {
13
  element: '#logo_emoji',
14
  popover: {
@@ -102,7 +102,7 @@ document.addEventListener("DOMContentLoaded", function () {
102
  animate: false,
103
  opacity: 0.8,
104
  padding: 5,
105
- showButtons: true,
106
  });
107
 
108
  boringTourDriver.defineSteps(tourSteps);
@@ -145,7 +145,7 @@ document.addEventListener("DOMContentLoaded", function () {
145
  /////////////////////////////////////////////
146
  // Form focus examples
147
  /////////////////////////////////////////////
148
- const focusDriver = new Driver({padding: 0});
149
  const inputIds = ['creation-input', 'creation-input-2', 'creation-input-3', 'creation-input-4'];
150
  inputIds.forEach(inputId => {
151
  // Highlight the section on focus
@@ -375,7 +375,7 @@ document.addEventListener("DOMContentLoaded", function () {
375
  featureIntroductionDriver.start();
376
  });
377
 
378
- const newURL = location.href.split("?")[0];
379
  if (newURL !== location.href) {
380
  window.location = newURL;
381
  window.location.href = newURL;
 
1
  /* eslint-disable */
2
+ document.addEventListener('DOMContentLoaded', function () {
3
 
4
  const tourSteps = [
5
  {
 
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
10
  nextBtnText: 'Okay, Start!'
11
+ },
12
  }, {
13
  element: '#logo_emoji',
14
  popover: {
 
102
  animate: false,
103
  opacity: 0.8,
104
  padding: 5,
105
+ showButtons: true
106
  });
107
 
108
  boringTourDriver.defineSteps(tourSteps);
 
145
  /////////////////////////////////////////////
146
  // Form focus examples
147
  /////////////////////////////////////////////
148
+ const focusDriver = new Driver({ padding: 0 });
149
  const inputIds = ['creation-input', 'creation-input-2', 'creation-input-3', 'creation-input-4'];
150
  inputIds.forEach(inputId => {
151
  // Highlight the section on focus
 
375
  featureIntroductionDriver.start();
376
  });
377
 
378
+ const newURL = location.href.split('?')[0];
379
  if (newURL !== location.href) {
380
  window.location = newURL;
381
  window.location.href = newURL;
src/index.js CHANGED
@@ -45,6 +45,10 @@ export default class Driver {
45
  },
46
  onReset: () => { // When overlay is about to be cleared
47
  },
 
 
 
 
48
  ...options,
49
  };
50
 
@@ -166,12 +170,21 @@ export default class Driver {
166
  * @public
167
  */
168
  movePrevious() {
169
- this.currentStep -= 1;
170
- if (this.steps[this.currentStep]) {
171
- this.overlay.highlight(this.steps[this.currentStep]);
172
- } else {
173
  this.reset();
 
174
  }
 
 
 
 
 
 
 
 
175
  }
176
 
177
  /**
@@ -180,12 +193,21 @@ export default class Driver {
180
  * @public
181
  */
182
  moveNext() {
183
- this.currentStep += 1;
184
- if (this.steps[this.currentStep]) {
185
- this.overlay.highlight(this.steps[this.currentStep]);
186
- } else {
187
  this.reset();
 
 
 
 
 
 
188
  }
 
 
 
189
  }
190
 
191
  /**
@@ -285,12 +307,10 @@ export default class Driver {
285
 
286
  if (isStepDefinition) {
287
  querySelector = currentStep.element;
288
- elementOptions = {
289
- ...this.options,
290
- ...currentStep,
291
- };
292
  }
293
 
 
294
  const domElement = isDomElement(querySelector) ? querySelector : this.document.querySelector(querySelector);
295
  if (!domElement) {
296
  console.warn(`Element to highlight ${querySelector} not found`);
 
45
  },
46
  onReset: () => { // When overlay is about to be cleared
47
  },
48
+ onNext: () => { // When next button is clicked
49
+ },
50
+ onPrevious: () => { // When previous button is clicked
51
+ },
52
  ...options,
53
  };
54
 
 
170
  * @public
171
  */
172
  movePrevious() {
173
+ const currentStep = this.steps[this.currentStep];
174
+ const previousStep = this.steps[this.currentStep - 1];
175
+
176
+ if (!previousStep) {
177
  this.reset();
178
+ return;
179
  }
180
+
181
+ // If there is an event binding on the current step
182
+ if (currentStep.options.onPrevious) {
183
+ currentStep.options.onPrevious();
184
+ }
185
+
186
+ this.overlay.highlight(previousStep);
187
+ this.currentStep -= 1;
188
  }
189
 
190
  /**
 
193
  * @public
194
  */
195
  moveNext() {
196
+ const currentStep = this.steps[this.currentStep];
197
+ const nextStep = this.steps[this.currentStep + 1];
198
+
199
+ if (!nextStep) {
200
  this.reset();
201
+ return;
202
+ }
203
+
204
+ // If there is an event binding on the current step
205
+ if (currentStep.options.onNext) {
206
+ currentStep.options.onNext();
207
  }
208
+
209
+ this.overlay.highlight(nextStep);
210
+ this.currentStep += 1;
211
  }
212
 
213
  /**
 
307
 
308
  if (isStepDefinition) {
309
  querySelector = currentStep.element;
310
+ elementOptions = { ...this.options, ...currentStep };
 
 
 
311
  }
312
 
313
+ // If the given element is a query selector or a DOM element?
314
  const domElement = isDomElement(querySelector) ? querySelector : this.document.querySelector(querySelector);
315
  if (!domElement) {
316
  console.warn(`Element to highlight ${querySelector} not found`);