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


/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
6
 * Bootstrap (v4.0.0-alpha.6): popover.js
fat's avatar
fat committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

const Popover = (($) => {


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

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

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

fat's avatar
fat committed
38
  const DefaultType = $.extend({}, Tooltip.DefaultType, {
39
    content : '(string|element|function)'
fat's avatar
fat committed
40
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
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
  }


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

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

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

    // overrides

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

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

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

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

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

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

    // private

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

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

    // static

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

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

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

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

})(jQuery)

export default Popover