popover.js 3.15 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: popover.js v3.1.1
3
 * http://getbootstrap.com/javascript/#popovers
4
 * ========================================================================
5
 * Copyright 2011-2014 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8
9


Zlatan Vasović's avatar
Zlatan Vasović committed
10
11
+function ($) {
  'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
12

fat's avatar
fat committed
13
14
  // POPOVER PUBLIC CLASS DEFINITION
  // ===============================
15

Jacob Thornton's avatar
Jacob Thornton committed
16
  var Popover = function (element, options) {
17
    this.init('popover', element, options)
Jacob Thornton's avatar
Jacob Thornton committed
18
19
  }

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

Zlatan Vasović's avatar
Zlatan Vasović committed
22
23
24
25
  Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
    placement: 'right',
    trigger: 'click',
    content: '',
26
    template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
fat's avatar
fat committed
27
  })
28

Jacob Thornton's avatar
Jacob Thornton committed
29

fat's avatar
fat committed
30
31
  // NOTE: POPOVER EXTENDS tooltip.js
  // ================================
Jacob Thornton's avatar
Jacob Thornton committed
32

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

fat's avatar
fat committed
35
  Popover.prototype.constructor = Popover
36

fat's avatar
fat committed
37
38
39
  Popover.prototype.getDefaults = function () {
    return Popover.DEFAULTS
  }
40

fat's avatar
fat committed
41
42
43
44
  Popover.prototype.setContent = function () {
    var $tip    = this.tip()
    var title   = this.getTitle()
    var content = this.getContent()
Jacob Thornton's avatar
Jacob Thornton committed
45

fat's avatar
fat committed
46
    $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
47
    $tip.find('.popover-content').empty()[ // we use append for html objects to maintain js events
48
49
      this.options.html ? (typeof content == 'string' ? 'html' : 'append') : 'text'
    ](content)
50

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

53
54
    // 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
55
    if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
fat's avatar
fat committed
56
  }
Jacob Thornton's avatar
Jacob Thornton committed
57

fat's avatar
fat committed
58
59
60
  Popover.prototype.hasContent = function () {
    return this.getTitle() || this.getContent()
  }
61

fat's avatar
fat committed
62
  Popover.prototype.getContent = function () {
fat's avatar
fat committed
63
64
    var $e = this.$element
    var o  = this.options
Jacob Thornton's avatar
Jacob Thornton committed
65

fat's avatar
fat committed
66
67
68
69
    return $e.attr('data-content')
      || (typeof o.content == 'function' ?
            o.content.call($e[0]) :
            o.content)
fat's avatar
fat committed
70
  }
Jacob Thornton's avatar
Jacob Thornton committed
71

Braden Whitten's avatar
Braden Whitten committed
72
  Popover.prototype.arrow = function () {
fat's avatar
fat committed
73
74
75
    return this.$arrow = this.$arrow || this.tip().find('.arrow')
  }

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

81

fat's avatar
fat committed
82
83
  // POPOVER PLUGIN DEFINITION
  // =========================
Jacob Thornton's avatar
Jacob Thornton committed
84

85
  function Plugin(option) {
86
    return this.each(function () {
fat's avatar
fat committed
87
      var $this   = $(this)
88
      var data    = $this.data('bs.popover')
fat's avatar
fat committed
89
90
      var options = typeof option == 'object' && option

fat's avatar
fat committed
91
      if (!data && option == 'destroy') return
92
      if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
93
94
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
95
96
  }

97
98
99
  var old = $.fn.popover

  $.fn.popover             = Plugin
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
}(jQuery);