popover.js 4.56 KB
Newer Older
fat's avatar
fat committed
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): popover.js
fat's avatar
fat committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
import {
  jQuery as $
} from './util/index'
11
12
import Data from './dom/data'
import SelectorEngine from './dom/selectorEngine'
13
14
import Tooltip from './tooltip'

Johann-S's avatar
Johann-S committed
15
16
17
18
19
20
21
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME                = 'popover'
XhmikosR's avatar
XhmikosR committed
22
const VERSION             = '4.3.1'
Johann-S's avatar
Johann-S committed
23
24
25
26
27
28
29
30
31
32
33
const DATA_KEY            = 'bs.popover'
const EVENT_KEY           = `.${DATA_KEY}`
const CLASS_PREFIX        = 'bs-popover'
const BSCLS_PREFIX_REGEX  = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')

const Default = {
  ...Tooltip.Default,
  placement : 'right',
  trigger   : 'click',
  content   : '',
  template  : '<div class="popover" role="tooltip">' +
34
              '<div class="popover-arrow"></div>' +
Johann-S's avatar
Johann-S committed
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
              '<h3 class="popover-header"></h3>' +
              '<div class="popover-body"></div></div>'
}

const DefaultType = {
  ...Tooltip.DefaultType,
  content : '(string|element|function)'
}

const ClassName = {
  FADE : 'fade',
  SHOW : 'show'
}

const Selector = {
  TITLE   : '.popover-header',
  CONTENT : '.popover-body'
}

const Event = {
  HIDE       : `hide${EVENT_KEY}`,
  HIDDEN     : `hidden${EVENT_KEY}`,
  SHOW       : `show${EVENT_KEY}`,
  SHOWN      : `shown${EVENT_KEY}`,
  INSERTED   : `inserted${EVENT_KEY}`,
  CLICK      : `click${EVENT_KEY}`,
  FOCUSIN    : `focusin${EVENT_KEY}`,
  FOCUSOUT   : `focusout${EVENT_KEY}`,
  MOUSEENTER : `mouseenter${EVENT_KEY}`,
  MOUSELEAVE : `mouseleave${EVENT_KEY}`
}

/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */

class Popover extends Tooltip {
  // Getters

  static get VERSION() {
    return VERSION
78
79
  }

Johann-S's avatar
Johann-S committed
80
81
  static get Default() {
    return Default
82
  }
fat's avatar
fat committed
83

Johann-S's avatar
Johann-S committed
84
85
  static get NAME() {
    return NAME
fat's avatar
fat committed
86
87
  }

Johann-S's avatar
Johann-S committed
88
89
  static get DATA_KEY() {
    return DATA_KEY
fat's avatar
fat committed
90
91
  }

Johann-S's avatar
Johann-S committed
92
93
  static get Event() {
    return Event
fat's avatar
fat committed
94
95
  }

Johann-S's avatar
Johann-S committed
96
97
98
  static get EVENT_KEY() {
    return EVENT_KEY
  }
fat's avatar
fat committed
99

Johann-S's avatar
Johann-S committed
100
101
102
  static get DefaultType() {
    return DefaultType
  }
fat's avatar
fat committed
103

Johann-S's avatar
Johann-S committed
104
  // Overrides
fat's avatar
fat committed
105

Johann-S's avatar
Johann-S committed
106
107
108
  isWithContent() {
    return this.getTitle() || this._getContent()
  }
fat's avatar
fat committed
109

Johann-S's avatar
Johann-S committed
110
  addAttachmentClass(attachment) {
111
    this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
Johann-S's avatar
Johann-S committed
112
  }
fat's avatar
fat committed
113

Johann-S's avatar
Johann-S committed
114
  setContent() {
115
    const tip = this.getTipElement()
fat's avatar
fat committed
116

117
118
    // we use append for html objects to maintain js events
    this.setElementContent(SelectorEngine.findOne(Selector.TITLE, tip), this.getTitle())
Johann-S's avatar
Johann-S committed
119
120
121
    let content = this._getContent()
    if (typeof content === 'function') {
      content = content.call(this.element)
fat's avatar
fat committed
122
    }
123
    this.setElementContent(SelectorEngine.findOne(Selector.CONTENT, tip), content)
fat's avatar
fat committed
124

125
126
    tip.classList.remove(ClassName.FADE)
    tip.classList.remove(ClassName.SHOW)
Johann-S's avatar
Johann-S committed
127
  }
fat's avatar
fat committed
128

Johann-S's avatar
Johann-S committed
129
  // Private
fat's avatar
fat committed
130

Johann-S's avatar
Johann-S committed
131
132
133
134
  _getContent() {
    return this.element.getAttribute('data-content') ||
      this.config.content
  }
fat's avatar
fat committed
135

Johann-S's avatar
Johann-S committed
136
  _cleanTipClass() {
137
138
    const tip = this.getTipElement()
    const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
139

Johann-S's avatar
Johann-S committed
140
    if (tabClass !== null && tabClass.length > 0) {
141
142
      tabClass.map((token) => token.trim())
        .forEach((tClass) => tip.classList.remove(tClass))
Johann-S's avatar
Johann-S committed
143
    }
Johann-S's avatar
Johann-S committed
144
  }
Johann-S's avatar
Johann-S committed
145

Johann-S's avatar
Johann-S committed
146
  // Static
fat's avatar
fat committed
147

Johann-S's avatar
Johann-S committed
148
149
  static _jQueryInterface(config) {
    return this.each(function () {
150
      let data      = Data.getData(this, DATA_KEY)
Johann-S's avatar
Johann-S committed
151
      const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
152

Johann-S's avatar
Johann-S committed
153
154
      if (!data && /dispose|hide/.test(config)) {
        return
155
      }
fat's avatar
fat committed
156

Johann-S's avatar
Johann-S committed
157
158
      if (!data) {
        data = new Popover(this, _config)
159
        Data.setData(this, DATA_KEY, data)
Johann-S's avatar
Johann-S committed
160
      }
fat's avatar
fat committed
161

Johann-S's avatar
Johann-S committed
162
163
164
      if (typeof config === 'string') {
        if (typeof data[config] === 'undefined') {
          throw new TypeError(`No method named "${config}"`)
fat's avatar
fat committed
165
        }
Johann-S's avatar
Johann-S committed
166
167
168
        data[config]()
      }
    })
fat's avatar
fat committed
169
  }
170
171
172
173

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
174
}
fat's avatar
fat committed
175

Johann-S's avatar
Johann-S committed
176
177
178
179
180
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
181

182
183
184
185
186
187
188
189
if (typeof $ !== 'undefined') {
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Popover._jQueryInterface
  $.fn[NAME].Constructor   = Popover
  $.fn[NAME].noConflict    = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Popover._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
190
}
fat's avatar
fat committed
191
192

export default Popover