popover.js 4.61 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-beta): 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-beta'
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
118
      this.tip = this.tip || $(this.config.template)[0]
      return this.tip
fat's avatar
fat committed
119
120
121
    }

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

      // we use append for html objects to maintain js events
125
126
      this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
      this.setElementContent($tip.find(Selector.CONTENT), this._getContent())
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
131
132
133
134
    }

    // private

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

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

    // static

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

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