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


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

12
const Popover = (($) => {
fat's avatar
fat committed
13
14
15
16
17
18
19
20
21


  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */

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

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  const Default = {
    ...Tooltip.Default,
    ...{
      placement : 'right',
      trigger   : 'click',
      content   : '',
      template  : '<div class="popover" role="tooltip">'
                + '<div class="arrow"></div>'
                + '<h3 class="popover-header"></h3>'
                + '<div class="popover-body"></div></div>'
    }
  }

  const DefaultType = {
    ...Tooltip.DefaultType,
    ...{
      content : '(string|element|function)'
    }
  }
fat's avatar
fat committed
48

fat's avatar
fat committed
49
  const ClassName = {
Starsam80's avatar
Starsam80 committed
50
51
    FADE : 'fade',
    SHOW : 'show'
fat's avatar
fat committed
52
53
54
  }

  const Selector = {
Mark Otto's avatar
Mark Otto committed
55
56
    TITLE   : '.popover-header',
    CONTENT : '.popover-body'
fat's avatar
fat committed
57
58
59
  }

  const Event = {
fat's avatar
fat committed
60
61
62
63
64
65
66
67
68
69
    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
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
  }


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

  class Popover extends Tooltip {


    // getters

    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
104
105
106
107
    static get EVENT_KEY() {
      return EVENT_KEY
    }

fat's avatar
fat committed
108
109
110
111
    static get DefaultType() {
      return DefaultType
    }

fat's avatar
fat committed
112
113
114
115
116
117
118

    // overrides

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

Johann-S's avatar
Johann-S committed
119
120
121
122
    addAttachmentClass(attachment) {
      $(this.getTipElement()).addClass(`${CLASS_PREFIX}-${attachment}`)
    }

fat's avatar
fat committed
123
    getTipElement() {
124
125
      this.tip = this.tip || $(this.config.template)[0]
      return this.tip
fat's avatar
fat committed
126
127
128
    }

    setContent() {
129
      const $tip = $(this.getTipElement())
fat's avatar
fat committed
130
131

      // we use append for html objects to maintain js events
132
      this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
133
134
135
136
137
      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
138

Starsam80's avatar
Starsam80 committed
139
      $tip.removeClass(`${ClassName.FADE} ${ClassName.SHOW}`)
fat's avatar
fat committed
140
141
142
143
144
145
    }

    // private

    _getContent() {
      return this.element.getAttribute('data-content')
146
        || this.config.content
fat's avatar
fat committed
147
148
    }

Johann-S's avatar
Johann-S committed
149
150
151
152
153
154
155
156
    _cleanTipClass() {
      const $tip = $(this.getTipElement())
      const tabClass = $tip.attr('class').match(BSCLS_PREFIX_REGEX)
      if (tabClass !== null && tabClass.length > 0) {
        $tip.removeClass(tabClass.join(''))
      }
    }

fat's avatar
fat committed
157
158
159
160
161

    // static

    static _jQueryInterface(config) {
      return this.each(function () {
162
163
        let data      = $(this).data(DATA_KEY)
        const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
164
165
166
167
168
169
170
171
172
173
174

        if (!data && /destroy|hide/.test(config)) {
          return
        }

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

        if (typeof config === 'string') {
XhmikosR's avatar
XhmikosR committed
175
          if (typeof data[config] === 'undefined') {
176
177
            throw new Error(`No method named "${config}"`)
          }
fat's avatar
fat committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
          data[config]()
        }
      })
    }
  }


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

  $.fn[NAME]             = Popover._jQueryInterface
  $.fn[NAME].Constructor = Popover
  $.fn[NAME].noConflict  = function () {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Popover._jQueryInterface
  }

  return Popover

200
})($)
fat's avatar
fat committed
201
202

export default Popover