popover.js 3.16 KB
Newer Older
1
/* ========================================================================
fat's avatar
fat committed
2
 * Bootstrap: popover.js v3.0.0
Chris Rebert's avatar
Chris Rebert committed
3
 * http://twbs.github.com/bootstrap/javascript.html#popovers
4
 * ========================================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 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


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')

fat's avatar
fat committed
32
33
34
35
36
37
  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>'
  })
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

    $tip.find('.popover-title:empty').hide()
fat's avatar
fat committed
62
  }
Jacob Thornton's avatar
Jacob Thornton committed
63

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

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

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

fat's avatar
fat committed
78
79
80
81
  Popover.prototype.tip = function () {
    if (!this.$tip) this.$tip = $(this.options.template)
    return this.$tip
  }
82

83

fat's avatar
fat committed
84
85
  // POPOVER PLUGIN DEFINITION
  // =========================
Jacob Thornton's avatar
Jacob Thornton committed
86

87
88
  var old = $.fn.popover

89
  $.fn.popover = function (option) {
90
    return this.each(function () {
fat's avatar
fat committed
91
      var $this   = $(this)
92
      var data    = $this.data('bs.popover')
fat's avatar
fat committed
93
94
      var options = typeof option == 'object' && option

95
      if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
96
97
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
98
99
  }

100
  $.fn.popover.Constructor = Popover
101

102

fat's avatar
fat committed
103
104
  // POPOVER NO CONFLICT
  // ===================
105
106
107
108
109
110

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

111
}(window.jQuery);