popover.js 3.41 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: popover.js v3.1.0
3
 * http://getbootstrap.com/javascript/#popovers
4
 * ========================================================================
Zlatan Vasović's avatar
Zlatan Vasović committed
5
 * Copyright 2013 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19
20


XhmikosR's avatar
XhmikosR committed
21
+function ($) { 'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
22

fat's avatar
fat committed
23
24
  // POPOVER PUBLIC CLASS DEFINITION
  // ===============================
25

Jacob Thornton's avatar
Jacob Thornton committed
26
  var Popover = function (element, options) {
27
    this.init('popover', element, options)
Jacob Thornton's avatar
Jacob Thornton committed
28
29
  }

fat's avatar
fat committed
30
31
  if (!$.fn.tooltip) throw new Error('Popover requires tooltip.js')

Zlatan Vasović's avatar
Zlatan Vasović committed
32
33
34
35
36
  Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
    placement: 'right',
    trigger: 'click',
    content: '',
    template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
fat's avatar
fat committed
37
  })
38

Jacob Thornton's avatar
Jacob Thornton committed
39

fat's avatar
fat committed
40
41
  // NOTE: POPOVER EXTENDS tooltip.js
  // ================================
Jacob Thornton's avatar
Jacob Thornton committed
42

fat's avatar
fat committed
43
  Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
44

fat's avatar
fat committed
45
  Popover.prototype.constructor = Popover
46

fat's avatar
fat committed
47
48
49
  Popover.prototype.getDefaults = function () {
    return Popover.DEFAULTS
  }
50

fat's avatar
fat committed
51
52
53
54
  Popover.prototype.setContent = function () {
    var $tip    = this.tip()
    var title   = this.getTitle()
    var content = this.getContent()
Jacob Thornton's avatar
Jacob Thornton committed
55

fat's avatar
fat committed
56
57
    $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
    $tip.find('.popover-content')[this.options.html ? 'html' : 'text'](content)
58

fat's avatar
fat committed
59
    $tip.removeClass('fade top bottom left right in')
60

61
62
    // IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
    // this manually by checking the contents.
Jacob Thornton's avatar
Jacob Thornton committed
63
    if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
fat's avatar
fat committed
64
  }
Jacob Thornton's avatar
Jacob Thornton committed
65

fat's avatar
fat committed
66
67
68
  Popover.prototype.hasContent = function () {
    return this.getTitle() || this.getContent()
  }
69

fat's avatar
fat committed
70
  Popover.prototype.getContent = function () {
fat's avatar
fat committed
71
72
    var $e = this.$element
    var o  = this.options
Jacob Thornton's avatar
Jacob Thornton committed
73

fat's avatar
fat committed
74
75
76
77
    return $e.attr('data-content')
      || (typeof o.content == 'function' ?
            o.content.call($e[0]) :
            o.content)
fat's avatar
fat committed
78
  }
Jacob Thornton's avatar
Jacob Thornton committed
79

Braden Whitten's avatar
Braden Whitten committed
80
  Popover.prototype.arrow = function () {
fat's avatar
fat committed
81
82
83
    return this.$arrow = this.$arrow || this.tip().find('.arrow')
  }

fat's avatar
fat committed
84
85
86
87
  Popover.prototype.tip = function () {
    if (!this.$tip) this.$tip = $(this.options.template)
    return this.$tip
  }
88

89

fat's avatar
fat committed
90
91
  // POPOVER PLUGIN DEFINITION
  // =========================
Jacob Thornton's avatar
Jacob Thornton committed
92

93
94
  var old = $.fn.popover

95
  $.fn.popover = function (option) {
96
    return this.each(function () {
fat's avatar
fat committed
97
      var $this   = $(this)
98
      var data    = $this.data('bs.popover')
fat's avatar
fat committed
99
100
      var options = typeof option == 'object' && option

101
      if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
102
103
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
104
105
  }

106
  $.fn.popover.Constructor = Popover
107

108

fat's avatar
fat committed
109
110
  // POPOVER NO CONFLICT
  // ===================
111
112
113
114
115
116

  $.fn.popover.noConflict = function () {
    $.fn.popover = old
    return this
  }

117
}(jQuery);