popover.js 4.59 KB
Newer Older
1
import $ from 'jquery'
fat's avatar
fat committed
2
3
4
5
import Tooltip from './tooltip'

/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
6
 * Bootstrap (v4.1.3): popover.js
fat's avatar
fat committed
7
8
9
10
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

11
const Popover = (($) => {
fat's avatar
fat committed
12
13
14
15
16
17
18
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

  const NAME                = 'popover'
Mark Otto's avatar
Mark Otto committed
19
  const VERSION             = '4.1.3'
fat's avatar
fat committed
20
  const DATA_KEY            = 'bs.popover'
fat's avatar
fat committed
21
  const EVENT_KEY           = `.${DATA_KEY}`
fat's avatar
fat committed
22
  const JQUERY_NO_CONFLICT  = $.fn[NAME]
Johann-S's avatar
Johann-S committed
23
  const CLASS_PREFIX        = 'bs-popover'
Johann-S's avatar
Johann-S committed
24
  const BSCLS_PREFIX_REGEX  = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
fat's avatar
fat committed
25

26
27
  const Default = {
    ...Tooltip.Default,
Vasilii Artemchuk's avatar
Vasilii Artemchuk committed
28
29
30
    placement : 'right',
    trigger   : 'click',
    content   : '',
XhmikosR's avatar
XhmikosR committed
31
32
33
34
    template  : '<div class="popover" role="tooltip">' +
                '<div class="arrow"></div>' +
                '<h3 class="popover-header"></h3>' +
                '<div class="popover-body"></div></div>'
35
36
37
38
  }

  const DefaultType = {
    ...Tooltip.DefaultType,
Vasilii Artemchuk's avatar
Vasilii Artemchuk committed
39
    content : '(string|element|function)'
40
  }
fat's avatar
fat committed
41

fat's avatar
fat committed
42
  const ClassName = {
Starsam80's avatar
Starsam80 committed
43
44
    FADE : 'fade',
    SHOW : 'show'
fat's avatar
fat committed
45
46
47
  }

  const Selector = {
Mark Otto's avatar
Mark Otto committed
48
49
    TITLE   : '.popover-header',
    CONTENT : '.popover-body'
fat's avatar
fat committed
50
51
52
  }

  const Event = {
fat's avatar
fat committed
53
54
55
56
57
58
59
60
61
62
    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}`
fat's avatar
fat committed
63
64
65
66
67
68
69
70
71
  }

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

  class Popover extends Tooltip {
XhmikosR's avatar
XhmikosR committed
72
    // Getters
fat's avatar
fat committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    static get VERSION() {
      return VERSION
    }

    static get Default() {
      return Default
    }

    static get NAME() {
      return NAME
    }

    static get DATA_KEY() {
      return DATA_KEY
    }

    static get Event() {
      return Event
    }

fat's avatar
fat committed
94
95
96
97
    static get EVENT_KEY() {
      return EVENT_KEY
    }

fat's avatar
fat committed
98
99
100
101
    static get DefaultType() {
      return DefaultType
    }

XhmikosR's avatar
XhmikosR committed
102
    // Overrides
fat's avatar
fat committed
103
104
105
106
107

    isWithContent() {
      return this.getTitle() || this._getContent()
    }

Johann-S's avatar
Johann-S committed
108
109
110
111
    addAttachmentClass(attachment) {
      $(this.getTipElement()).addClass(`${CLASS_PREFIX}-${attachment}`)
    }

fat's avatar
fat committed
112
    getTipElement() {
113
114
      this.tip = this.tip || $(this.config.template)[0]
      return this.tip
fat's avatar
fat committed
115
116
117
    }

    setContent() {
118
      const $tip = $(this.getTipElement())
fat's avatar
fat committed
119

XhmikosR's avatar
XhmikosR committed
120
      // We use append for html objects to maintain js events
121
      this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
122
123
124
125
126
      let content = this._getContent()
      if (typeof content === 'function') {
        content = content.call(this.element)
      }
      this.setElementContent($tip.find(Selector.CONTENT), content)
fat's avatar
fat committed
127

Starsam80's avatar
Starsam80 committed
128
      $tip.removeClass(`${ClassName.FADE} ${ClassName.SHOW}`)
fat's avatar
fat committed
129
130
    }

XhmikosR's avatar
XhmikosR committed
131
    // Private
fat's avatar
fat committed
132
133

    _getContent() {
XhmikosR's avatar
XhmikosR committed
134
135
      return this.element.getAttribute('data-content') ||
        this.config.content
fat's avatar
fat committed
136
137
    }

Johann-S's avatar
Johann-S committed
138
139
140
141
142
143
144
145
    _cleanTipClass() {
      const $tip = $(this.getTipElement())
      const tabClass = $tip.attr('class').match(BSCLS_PREFIX_REGEX)
      if (tabClass !== null && tabClass.length > 0) {
        $tip.removeClass(tabClass.join(''))
      }
    }

XhmikosR's avatar
XhmikosR committed
146
    // Static
fat's avatar
fat committed
147
148
149

    static _jQueryInterface(config) {
      return this.each(function () {
XhmikosR's avatar
XhmikosR committed
150
        let data = $(this).data(DATA_KEY)
151
        const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
152

153
        if (!data && /dispose|hide/.test(config)) {
fat's avatar
fat committed
154
155
156
157
158
159
160
161
162
          return
        }

        if (!data) {
          data = new Popover(this, _config)
          $(this).data(DATA_KEY, data)
        }

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

  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */

XhmikosR's avatar
XhmikosR committed
178
  $.fn[NAME] = Popover._jQueryInterface
fat's avatar
fat committed
179
  $.fn[NAME].Constructor = Popover
XhmikosR's avatar
XhmikosR committed
180
  $.fn[NAME].noConflict = () => {
fat's avatar
fat committed
181
182
183
184
185
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Popover._jQueryInterface
  }

  return Popover
186
})($)
fat's avatar
fat committed
187
188

export default Popover