kamrify commited on
Commit
5aa283f
·
1 Parent(s): 7e830c6

Buttons in the popover

Browse files
.eslintrc.json CHANGED
@@ -3,6 +3,9 @@
3
  "env": {
4
  "browser": true
5
  },
 
 
 
6
  "rules": {
7
  "no-console": "off",
8
  "no-underscore-dangle": "off",
 
3
  "env": {
4
  "browser": true
5
  },
6
+ "ecmaFeatures": {
7
+ "experimentalObjectRestSpread": true
8
+ },
9
  "rules": {
10
  "no-console": "off",
11
  "no-underscore-dangle": "off",
assets/scripts/src/overlay.js CHANGED
@@ -57,7 +57,7 @@ export default class Overlay {
57
  * @param animate bool
58
  */
59
  highlight(element, animate = true) {
60
- if (!element) {
61
  return;
62
  }
63
 
 
57
  * @param animate bool
58
  */
59
  highlight(element, animate = true) {
60
+ if (!element || !element.node) {
61
  return;
62
  }
63
 
assets/scripts/src/sholo.js CHANGED
@@ -21,10 +21,13 @@ export default class Sholo {
21
  this.document = document;
22
  this.window = window;
23
 
 
 
 
24
  this.onScroll = this.onScroll.bind(this);
25
  this.onResize = this.onResize.bind(this);
26
  this.onKeyUp = this.onKeyUp.bind(this);
27
- this.onMouseUp = this.onMouseUp.bind(this);
28
 
29
  // Event bindings
30
  this.bind();
@@ -39,7 +42,7 @@ export default class Sholo {
39
  this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
40
  this.window.addEventListener('resize', this.onResize, false);
41
  this.window.addEventListener('keyup', this.onKeyUp, false);
42
- this.window.addEventListener('mouseup', this.onMouseUp, false);
43
  }
44
 
45
  /**
@@ -47,23 +50,36 @@ export default class Sholo {
47
  * or outside the
48
  * @param e
49
  */
50
- onMouseUp(e) {
51
- const highlightedElement = this.overlay.getHighlightedElement();
52
- const popover = document.getElementById('sholo-popover-item');
53
-
54
- if (!highlightedElement || !highlightedElement.node) {
55
  return;
56
  }
57
 
 
 
 
58
  const clickedHighlightedElement = highlightedElement.node.contains(e.target);
59
  const clickedPopover = popover && popover.contains(e.target);
60
 
61
  // Remove the overlay If clicked outside the highlighted element
62
  if (!clickedHighlightedElement && !clickedPopover) {
63
  this.overlay.clear();
 
 
 
 
 
 
 
64
  }
65
  }
66
 
 
 
 
 
 
67
  /**
68
  * Handler for the onScroll event on document
69
  * Refreshes without animation on scroll to make sure
@@ -93,26 +109,50 @@ export default class Sholo {
93
  }
94
  }
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  /**
97
  * Highlights the given selector
98
  * @param selector
99
  */
100
  highlight(selector) {
101
- let domElement;
 
 
 
 
102
 
 
103
  if (typeof selector === 'string') {
104
- domElement = document.querySelector(selector);
105
- } else if (typeof selector === 'object') {
106
- domElement = selector;
107
- } else {
108
- throw new Error('Element can only be string or the dom element');
109
  }
110
 
111
- if (domElement) {
112
- const element = new Element(domElement, this.options);
113
- this.overlay.highlight(element);
114
- } else {
115
- this.overlay.clear();
116
  }
 
 
117
  }
118
  }
 
21
  this.document = document;
22
  this.window = window;
23
 
24
+ this.steps = []; // steps to be presented if any
25
+ this.currentStep = 0; // index for the currently highlighted step
26
+
27
  this.onScroll = this.onScroll.bind(this);
28
  this.onResize = this.onResize.bind(this);
29
  this.onKeyUp = this.onKeyUp.bind(this);
30
+ this.onClick = this.onClick.bind(this);
31
 
32
  // Event bindings
33
  this.bind();
 
42
  this.document.addEventListener('DOMMouseScroll', this.onScroll, false);
43
  this.window.addEventListener('resize', this.onResize, false);
44
  this.window.addEventListener('keyup', this.onKeyUp, false);
45
+ this.window.addEventListener('click', this.onClick, false);
46
  }
47
 
48
  /**
 
50
  * or outside the
51
  * @param e
52
  */
53
+ onClick(e) {
54
+ if (!this.hasHighlightedElement()) {
55
+ // Has no highlighted element so ignore the click
 
 
56
  return;
57
  }
58
 
59
+ const highlightedElement = this.overlay.getHighlightedElement();
60
+ const popover = document.getElementById('sholo-popover-item');
61
+
62
  const clickedHighlightedElement = highlightedElement.node.contains(e.target);
63
  const clickedPopover = popover && popover.contains(e.target);
64
 
65
  // Remove the overlay If clicked outside the highlighted element
66
  if (!clickedHighlightedElement && !clickedPopover) {
67
  this.overlay.clear();
68
+ return;
69
+ }
70
+
71
+ const nextClicked = e.target.classList.contains('sholo-next-btn');
72
+ if (nextClicked) {
73
+ this.currentStep += 1;
74
+ this.overlay.highlight(this.steps[this.currentStep]);
75
  }
76
  }
77
 
78
+ hasHighlightedElement() {
79
+ const highlightedElement = this.overlay.getHighlightedElement();
80
+ return highlightedElement && highlightedElement.node;
81
+ }
82
+
83
  /**
84
  * Handler for the onScroll event on document
85
  * Refreshes without animation on scroll to make sure
 
109
  }
110
  }
111
 
112
+ defineSteps(steps) {
113
+ this.steps = [];
114
+
115
+ steps.forEach((step, index) => {
116
+ if (!step.element) {
117
+ throw new Error(`Element (query selector or a dom element) missing in step ${index}`);
118
+ }
119
+
120
+ const domElement = Sholo.findDomElement(step.element);
121
+ const element = new Element(domElement, Object.assign({}, this.options, step));
122
+
123
+ this.steps.push(element);
124
+ });
125
+ }
126
+
127
+ start() {
128
+ if (!this.steps || this.steps.length === 0) {
129
+ throw new Error('There are no steps defined to iterate');
130
+ }
131
+
132
+ this.currentStep = 0;
133
+ this.overlay.highlight(this.steps[0]);
134
+ }
135
+
136
  /**
137
  * Highlights the given selector
138
  * @param selector
139
  */
140
  highlight(selector) {
141
+ const domElement = Sholo.findDomElement(selector);
142
+
143
+ const element = new Element(domElement, this.options);
144
+ this.overlay.highlight(element);
145
+ }
146
 
147
+ static findDomElement(selector) {
148
  if (typeof selector === 'string') {
149
+ return document.querySelector(selector);
 
 
 
 
150
  }
151
 
152
+ if (typeof selector === 'object') {
153
+ return selector;
 
 
 
154
  }
155
+
156
+ throw new Error('Element can only be string or the dom element');
157
  }
158
  }
assets/styles/scss/demo.scss CHANGED
@@ -118,7 +118,7 @@ div#sholo-popover-item {
118
  margin: 10px 0 0;
119
  }
120
 
121
- .sholo-close {
122
  float: left;
123
  }
124
 
 
118
  margin: 10px 0 0;
119
  }
120
 
121
+ .sholo-close-btn {
122
  float: left;
123
  }
124
 
index.html CHANGED
@@ -14,10 +14,10 @@
14
  <div class="sholo-popover-title">Did you know?</div>
15
  <div class="sholo-popover-description">You can make step by step introductions with sholo!</div>
16
  <div class="sholo-popover-footer">
17
- <a href="javascript:void(0)" class="sholo-close">Close</a>
18
  <span class="sholo-btn-group">
19
- <a href="javascript:void(0)">&larr; Previous</a>
20
- <a href="javascript:void(0)">Next &rarr;</a>
21
  </span>
22
  </div>
23
  </div>
@@ -75,10 +75,15 @@
75
  padding: 5
76
  });
77
 
 
 
 
 
 
78
 
79
  document.querySelector('.btn__example')
80
  .addEventListener('click', function () {
81
- sholo.highlight('.section__header');
82
  });
83
  </script>
84
  </body>
 
14
  <div class="sholo-popover-title">Did you know?</div>
15
  <div class="sholo-popover-description">You can make step by step introductions with sholo!</div>
16
  <div class="sholo-popover-footer">
17
+ <a href="javascript:void(0)" class="sholo-close-btn">Close</a>
18
  <span class="sholo-btn-group">
19
+ <a class="sholo-prev-btn" href="javascript:void(0)">&larr; Previous</a>
20
+ <a class="sholo-next-btn" href="javascript:void(0)">Next &rarr;</a>
21
  </span>
22
  </div>
23
  </div>
 
75
  padding: 5
76
  });
77
 
78
+ sholo.defineSteps([
79
+ { element: '.section__header' },
80
+ { element: '.section__examples' },
81
+ { element: '.section__contributing' },
82
+ ]);
83
 
84
  document.querySelector('.btn__example')
85
  .addEventListener('click', function () {
86
+ sholo.start();
87
  });
88
  </script>
89
  </body>