popover.js 3.18 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: popover.js v3.2.0
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


10
11
+function ($) {
  'use strict';
Katie Zhu's avatar
Katie Zhu committed
12

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

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

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

Mark Otto's avatar
Mark Otto committed
22
  Popover.VERSION  = '3.2.0'
23

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

Jacob Thornton's avatar
Jacob Thornton committed
31

32
33
  // NOTE: POPOVER EXTENDS tooltip.js
  // ================================
34

35
  Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
36

37
  Popover.prototype.constructor = Popover
38

39
40
41
  Popover.prototype.getDefaults = function () {
    return Popover.DEFAULTS
  }
Jacob Thornton's avatar
Jacob Thornton committed
42

43
44
45
46
  Popover.prototype.setContent = function () {
    var $tip    = this.tip()
    var title   = this.getTitle()
    var content = this.getContent()
47

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

53
    $tip.removeClass('fade top bottom left right in')
Jacob Thornton's avatar
Jacob Thornton committed
54

55
56
57
58
    // IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
    // this manually by checking the contents.
    if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
  }
59

60
61
62
  Popover.prototype.hasContent = function () {
    return this.getTitle() || this.getContent()
  }
Jacob Thornton's avatar
Jacob Thornton committed
63

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

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

74
75
76
  Popover.prototype.arrow = function () {
    return (this.$arrow = this.$arrow || this.tip().find('.arrow'))
  }
77

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

Jacob Thornton's avatar
Jacob Thornton committed
83

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

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

93
94
95
96
97
      if (!data && option == 'destroy') return
      if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
98

99
  var old = $.fn.popover
100

101
102
  $.fn.popover             = Plugin
  $.fn.popover.Constructor = Popover
103
104


105
106
  // POPOVER NO CONFLICT
  // ===================
107

108
109
110
111
  $.fn.popover.noConflict = function () {
    $.fn.popover = old
    return this
  }
112

113
}(jQuery);