kamrify commited on
Commit
6a7ee1e
·
1 Parent(s): 438fea8

Dynamically add the popover if not there

Browse files
assets/scripts/src/constants.js CHANGED
@@ -5,10 +5,28 @@ export const OVERLAY_ZINDEX = '999999999';
5
 
6
  export const ESC_KEY_CODE = 27;
7
 
8
- export const ID_POPOVER = 'sholo-popover-item';
9
  export const ID_OVERLAY = 'sholo-canvas-overlay';
10
 
 
11
  export const CLASS_POPOVER_TIP = 'sholo-popover-tip';
 
 
 
 
12
  export const CLASS_NEXT_STEP_BTN = 'sholo-next-btn';
13
  export const CLASS_PREV_STEP_BTN = 'sholo-prev-btn';
14
- export const CLASS_CLOSE_BTN = 'sholo-close-btn';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  export const ESC_KEY_CODE = 27;
7
 
 
8
  export const ID_OVERLAY = 'sholo-canvas-overlay';
9
 
10
+ export const ID_POPOVER = 'sholo-popover-item';
11
  export const CLASS_POPOVER_TIP = 'sholo-popover-tip';
12
+ export const CLASS_POPOVER_TITLE = 'sholo-popover-title';
13
+ export const CLASS_POPOVER_DESCRIPTION = 'sholo-popover-description';
14
+ export const CLASS_POPOVER_FOOTER = 'sholo-popover-footer';
15
+ export const CLASS_CLOSE_BTN = 'sholo-close-btn';
16
  export const CLASS_NEXT_STEP_BTN = 'sholo-next-btn';
17
  export const CLASS_PREV_STEP_BTN = 'sholo-prev-btn';
18
+
19
+ // language=HTML
20
+ export const POPOVER_HTML = `
21
+ <div id="${ID_POPOVER}">
22
+ <div class="${CLASS_POPOVER_TIP}"></div>
23
+ <div class="${CLASS_POPOVER_TITLE}">Popover Title</div>
24
+ <div class="${CLASS_POPOVER_DESCRIPTION}">Popover Description</div>
25
+ <div class="${CLASS_POPOVER_FOOTER}">
26
+ <a href="javascript:void(0)" class="${CLASS_CLOSE_BTN}">Close</a>
27
+ <span class="sholo-btn-group">
28
+ <a class="${CLASS_PREV_STEP_BTN}" href="javascript:void(0)">&larr; Previous</a>
29
+ <a class="${CLASS_NEXT_STEP_BTN}" href="javascript:void(0)">Next &rarr;</a>
30
+ </span>
31
+ </div>
32
+ </div>`;
assets/scripts/src/popover.js CHANGED
@@ -1,12 +1,12 @@
1
  import Element from './element';
2
- import { CLASS_POPOVER_TIP, ID_POPOVER } from './constants';
3
 
4
  /**
5
  * Popover that is displayed on top of the highlighted element
6
  */
7
  export default class Popover extends Element {
8
  constructor(options = {
9
- padding: 10,
10
  }, window, document) {
11
  super();
12
 
@@ -14,17 +14,35 @@ export default class Popover extends Element {
14
  this.window = window;
15
  this.document = document;
16
 
17
- this.node = this.getPopover();
 
18
  }
19
 
20
- getPopover() {
21
- // @todo: Create if not there
22
- const popover = this.document.getElementById(ID_POPOVER);
23
- popover.style.position = 'absolute';
 
 
 
 
24
 
25
  return popover;
26
  }
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  getHeight() {
29
  return Math.max(this.node.scrollHeight, this.node.offsetHeight);
30
  }
@@ -35,8 +53,8 @@ export default class Popover extends Element {
35
 
36
  reset() {
37
  this.node.style.display = 'block';
38
- this.node.style.left = '';
39
- this.node.style.top = '';
40
  this.node.style.bottom = '';
41
  this.node.style.right = '';
42
 
@@ -49,11 +67,11 @@ export default class Popover extends Element {
49
  show(position) {
50
  this.reset();
51
 
52
- const popoverTip = this.node.querySelector(`.${CLASS_POPOVER_TIP}`);
53
-
54
  const pageHeight = this.getFullPageSize().height;
55
- const popoverHeight = this.getHeight();
 
56
  const popoverMargin = this.options.padding + 10;
 
57
 
58
  this.node.style.left = `${position.left - this.options.padding}px`;
59
 
 
1
  import Element from './element';
2
+ import { CLASS_POPOVER_TIP, ID_POPOVER, OVERLAY_PADDING, POPOVER_HTML } from './constants';
3
 
4
  /**
5
  * Popover that is displayed on top of the highlighted element
6
  */
7
  export default class Popover extends Element {
8
  constructor(options = {
9
+ padding: OVERLAY_PADDING,
10
  }, window, document) {
11
  super();
12
 
 
14
  this.window = window;
15
  this.document = document;
16
 
17
+ this.node = this.preparePopover();
18
+ this.hide();
19
  }
20
 
21
+ preparePopover() {
22
+ let popover = this.document.getElementById(ID_POPOVER);
23
+ if (popover) {
24
+ return popover;
25
+ }
26
+
27
+ popover = Popover.createFromString(POPOVER_HTML);
28
+ document.body.appendChild(popover);
29
 
30
  return popover;
31
  }
32
 
33
+ /**
34
+ * Turn a string into a node
35
+ * @param {String} htmlString to convert
36
+ * @return {Node} Converted node element
37
+ */
38
+ static createFromString(htmlString) {
39
+ const div = document.createElement('div');
40
+ div.innerHTML = htmlString.trim();
41
+
42
+ // Change this to div.childNodes to support multiple top-level nodes
43
+ return div.firstChild;
44
+ }
45
+
46
  getHeight() {
47
  return Math.max(this.node.scrollHeight, this.node.offsetHeight);
48
  }
 
53
 
54
  reset() {
55
  this.node.style.display = 'block';
56
+ this.node.style.left = '0';
57
+ this.node.style.top = '0';
58
  this.node.style.bottom = '';
59
  this.node.style.right = '';
60
 
 
67
  show(position) {
68
  this.reset();
69
 
 
 
70
  const pageHeight = this.getFullPageSize().height;
71
+
72
+ const popoverTip = this.node.querySelector(`.${CLASS_POPOVER_TIP}`);
73
  const popoverMargin = this.options.padding + 10;
74
+ const popoverHeight = this.getHeight();
75
 
76
  this.node.style.left = `${position.left - this.options.padding}px`;
77
 
assets/styles/scss/demo.scss CHANGED
@@ -48,6 +48,7 @@ section {
48
  /////////////////////////////////////////
49
  div#sholo-popover-item {
50
  display: none;
 
51
  background: white;
52
  margin: 0;
53
  padding: 15px;
 
48
  /////////////////////////////////////////
49
  div#sholo-popover-item {
50
  display: none;
51
+ position: absolute;
52
  background: white;
53
  margin: 0;
54
  padding: 15px;
index.html CHANGED
@@ -9,18 +9,6 @@
9
  <link rel="stylesheet" href="./assets/styles/css/demo.css">
10
  </head>
11
  <body>
12
- <div id="sholo-popover-item">
13
- <div class="sholo-popover-tip"></div>
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>
24
  <div class="page-wrap">
25
  <section class="section__header" data-sholo="Hey welcome to presenter!">
26
  <h1>Sholo</h1>
 
9
  <link rel="stylesheet" href="./assets/styles/css/demo.css">
10
  </head>
11
  <body>
 
 
 
 
 
 
 
 
 
 
 
 
12
  <div class="page-wrap">
13
  <section class="section__header" data-sholo="Hey welcome to presenter!">
14
  <h1>Sholo</h1>