popover.js 4.63 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): 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'
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

  const Default = $.extend({}, Tooltip.Default, {
    placement : 'right',
    trigger   : 'click',
    content   : '',
    template  : '<div class="popover" role="tooltip">'
34
              + '<div class="arrow"></div>'
Mark Otto's avatar
Mark Otto committed
35
36
              + '<h3 class="popover-header"></h3>'
              + '<div class="popover-body"></div></div>'
fat's avatar
fat committed
37
38
  })

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

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

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

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


  /**
   * ------------------------------------------------------------------------
   * 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
98
99
100
101
    static get EVENT_KEY() {
      return EVENT_KEY
    }

fat's avatar
fat committed
102
103
104
105
    static get DefaultType() {
      return DefaultType
    }

fat's avatar
fat committed
106
107
108
109
110
111
112

    // overrides

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

Johann-S's avatar
Johann-S committed
113
114
115
116
    addAttachmentClass(attachment) {
      $(this.getTipElement()).addClass(`${CLASS_PREFIX}-${attachment}`)
    }

fat's avatar
fat committed
117
    getTipElement() {
118
119
      this.tip = this.tip || $(this.config.template)[0]
      return this.tip
fat's avatar
fat committed
120
121
122
    }

    setContent() {
123
      const $tip = $(this.getTipElement())
fat's avatar
fat committed
124
125

      // we use append for html objects to maintain js events
126
127
      this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
      this.setElementContent($tip.find(Selector.CONTENT), this._getContent())
fat's avatar
fat committed
128

Starsam80's avatar
Starsam80 committed
129
      $tip.removeClass(`${ClassName.FADE} ${ClassName.SHOW}`)
fat's avatar
fat committed
130
131
132
133
134
135
    }

    // private

    _getContent() {
      return this.element.getAttribute('data-content')
Jacob Thornton's avatar
Jacob Thornton committed
136
        || (typeof this.config.content === 'function' ?
fat's avatar
fat committed
137
138
139
140
              this.config.content.call(this.element) :
              this.config.content)
    }

Johann-S's avatar
Johann-S committed
141
142
143
144
145
146
147
148
    _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
149
150
151
152
153

    // static

    static _jQueryInterface(config) {
      return this.each(function () {
154
155
        let data      = $(this).data(DATA_KEY)
        const _config = typeof config === 'object' ? config : null
fat's avatar
fat committed
156
157
158
159
160
161
162
163
164
165
166

        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
167
          if (typeof data[config] === 'undefined') {
168
169
            throw new Error(`No method named "${config}"`)
          }
fat's avatar
fat committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
          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

Johann-S's avatar
Johann-S committed
192
})($)
fat's avatar
fat committed
193
194

export default Popover