File size: 7,344 Bytes
8a554f1
 
 
 
438fea8
 
 
 
 
e5c37ad
5d08a61
 
e5c37ad
8a554f1
b4b2753
22c7264
 
 
 
8755ee2
a47eab3
8755ee2
0c119f7
 
5d08a61
 
 
0c119f7
 
bb8be8c
 
 
0b84e47
27c03db
5aa283f
 
 
bb8be8c
 
d5e94aa
5aa283f
bb8be8c
 
 
 
 
8755ee2
 
 
 
bb8be8c
 
 
 
d5e94aa
5aa283f
22fca24
 
ede8cb9
 
 
 
 
5aa283f
 
 
22fca24
 
 
5aa283f
a63a502
5aa283f
ede8cb9
 
 
22fca24
ede8cb9
22fca24
5aa283f
 
 
a63a502
 
 
28c1a4b
095d0ef
 
 
 
 
5aa283f
28c1a4b
 
 
095d0ef
28c1a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa283f
28c1a4b
 
22fca24
bb8be8c
 
e5c37ad
 
 
 
 
 
 
 
 
 
 
 
 
 
28c1a4b
 
 
 
 
cc0ef69
28c1a4b
 
 
 
 
 
 
5aa283f
 
 
 
 
8755ee2
 
 
 
 
bb8be8c
 
 
 
8755ee2
 
 
 
 
bb8be8c
 
 
b4b2753
 
8755ee2
 
 
 
d5e94aa
438fea8
d5e94aa
e5c37ad
 
 
 
d5e94aa
 
 
cc0ef69
 
 
 
5aa283f
 
 
 
27c03db
 
5aa283f
 
cc0ef69
 
27c03db
 
 
5aa283f
 
 
 
cc0ef69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aa283f
 
 
 
cc0ef69
 
5aa283f
 
8755ee2
cc0ef69
 
8755ee2
22c7264
cc0ef69
 
27c03db
37f5ac4
5aa283f
27c03db
b4b2753
 
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
import Overlay from './core/overlay';
import Element from './core/element';
import Popover from './core/popover';
import './common/polyfill';
import {
  CLASS_CLOSE_BTN,
  CLASS_NEXT_STEP_BTN,
  CLASS_PREV_STEP_BTN,
  ESC_KEY_CODE,
  ID_POPOVER, LEFT_KEY_CODE,
  OVERLAY_ANIMATE,
  OVERLAY_OPACITY,
  OVERLAY_PADDING, RIGHT_KEY_CODE,
} from './common/constants';

/**
 * Plugin class that drives the plugin
 */
export default class Sholo {
  /**
   * @param {Object} options
   */
  constructor(options = {}) {
    this.options = Object.assign({
      animate: OVERLAY_ANIMATE,     // Whether to animate or not
      opacity: OVERLAY_OPACITY,     // Overlay opacity
      padding: OVERLAY_PADDING,     // Spacing around the element from the overlay
    }, options);

    this.document = document;
    this.window = window;

    this.overlay = new Overlay(this.options, this.window, this.document);

    this.steps = [];            // steps to be presented if any
    this.currentStep = 0;       // index for the currently highlighted step

    this.onScroll = this.onScroll.bind(this);
    this.onResize = this.onResize.bind(this);
    this.onKeyUp = this.onKeyUp.bind(this);
    this.onClick = this.onClick.bind(this);

    // Event bindings
    this.bind();
  }

  /**
   * Binds any DOM events listeners
   * @todo: add throttling in all the listeners
   */
  bind() {
    this.document.addEventListener('scroll', this.onScroll, false);
    this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
    this.window.addEventListener('resize', this.onResize, false);
    this.window.addEventListener('keyup', this.onKeyUp, false);
    this.window.addEventListener('click', this.onClick, false);
  }

  /**
   * Removes the popover if clicked outside the highlighted element
   * or outside the
   * @param e
   */
  onClick(e) {
    if (!this.hasHighlightedElement()) {
      // Has no highlighted element so ignore the click
      return;
    }

    const highlightedElement = this.overlay.getHighlightedElement();
    const popover = this.document.getElementById(ID_POPOVER);

    const clickedHighlightedElement = highlightedElement.node.contains(e.target);
    const clickedPopover = popover && popover.contains(e.target);

    // Remove the overlay If clicked outside the highlighted element
    if (!clickedHighlightedElement && !clickedPopover) {
      this.overlay.clear();
      return;
    }

    const nextClicked = e.target.classList.contains(CLASS_NEXT_STEP_BTN);
    const prevClicked = e.target.classList.contains(CLASS_PREV_STEP_BTN);
    const closeClicked = e.target.classList.contains(CLASS_CLOSE_BTN);

    if (closeClicked) {
      this.reset();
      return;
    }

    if (nextClicked) {
      this.moveNext();
    } else if (prevClicked) {
      this.movePrevious();
    }
  }

  /**
   * Moves to the previous step if possible
   * otherwise resets the overlay
   */
  movePrevious() {
    this.currentStep -= 1;
    if (this.steps[this.currentStep]) {
      this.overlay.highlight(this.steps[this.currentStep]);
    } else {
      this.reset();
    }
  }

  /**
   * Moves to the next step if possible
   * otherwise resets the overlay
   */
  moveNext() {
    this.currentStep += 1;
    if (this.steps[this.currentStep]) {
      this.overlay.highlight(this.steps[this.currentStep]);
    } else {
      this.reset();
    }
  }

  /**
   * @returns {boolean}
   */
  hasNextStep() {
    return !!this.steps[this.currentStep + 1];
  }

  /**
   * @returns {boolean}
   */
  hasPreviousStep() {
    return !!this.steps[this.currentStep - 1];
  }

  /**
   * Resets the steps if any and clears the overlay
   */
  reset() {
    this.currentStep = 0;
    this.steps = [];
    this.overlay.clear();
  }

  /**
   * Checks if there is any highlighted element or not
   * @returns {boolean}
   */
  hasHighlightedElement() {
    const highlightedElement = this.overlay.getHighlightedElement();
    return highlightedElement && highlightedElement.node;
  }

  /**
   * Handler for the onScroll event on document
   * Refreshes without animation on scroll to make sure
   * that the highlighted part travels with the scroll
   */
  onScroll() {
    this.overlay.refresh(false);
  }

  /**
   * Handler for the onResize DOM event
   * Refreshes with animation on scroll to make sure that
   * the highlighted part travels with the width change of window
   */
  onResize() {
    // Refresh with animation
    this.overlay.refresh(true);
  }

  /**
   * Clears the overlay on escape key process
   * @param event
   */
  onKeyUp(event) {
    if (event.keyCode === ESC_KEY_CODE) {
      this.overlay.clear();
    } else if (event.keyCode === RIGHT_KEY_CODE && this.hasNextStep()) {
      this.moveNext();
    } else if (event.keyCode === LEFT_KEY_CODE && this.hasPreviousStep()) {
      this.movePrevious();
    }
  }

  /**
   * Defines steps to be highlighted
   * @param {array} steps
   */
  defineSteps(steps) {
    this.steps = [];

    steps.forEach((step, index) => {
      if (!step.element || typeof step.element !== 'string') {
        throw new Error(`Element (query selector string) missing in step ${index}`);
      }

      const element = this.prepareElementFromStep(step, steps, index);
      if (!element) {
        return;
      }

      this.steps.push(element);
    });
  }

  /**
   * Prepares the step received from the user and returns an instance
   * of Element
   *
   * @param currentStep Step that is being prepared
   * @param allSteps  List of all the steps
   * @param index Index of the current step
   * @returns {null|Element}
   */
  prepareElementFromStep(currentStep, allSteps = [], index = 0) {
    let querySelector = '';
    let elementOptions = {};

    if (typeof currentStep === 'string') {
      querySelector = currentStep;
    } else {
      querySelector = currentStep.element;
      elementOptions = Object.assign({}, this.options, currentStep);
    }

    const domElement = this.document.querySelector(querySelector);
    if (!domElement) {
      console.warn(`Element to highlight ${querySelector} not found`);
      return null;
    }

    let popover = null;
    if (elementOptions.popover && elementOptions.popover.description) {
      const popoverOptions = Object.assign(
        {},
        this.options,
        elementOptions.popover, {
          totalCount: allSteps.length,
          currentIndex: index,
          isFirst: index === 0,
          isLast: index === allSteps.length - 1,
        },
      );

      popover = new Popover(popoverOptions, this.window, this.document);
    }

    return new Element(domElement, elementOptions, popover, this.overlay, this.window, this.document);
  }

  /**
   * Initiates highlighting steps from first step
   * @param {number} index at which highlight is to be started
   */
  start(index = 0) {
    if (!this.steps || this.steps.length === 0) {
      throw new Error('There are no steps defined to iterate');
    }

    this.currentStep = index;
    this.overlay.highlight(this.steps[index]);
  }

  /**
   * Highlights the given element
   * @param {string|{element: string, popover: {}}} selector Query selector or a step definition
   */
  highlight(selector) {
    const element = this.prepareElementFromStep(selector);
    if (!element) {
      return;
    }

    this.overlay.highlight(element);
  }
}