popover.js 3.34 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


Katie Zhu's avatar
Katie Zhu committed
10
11
12
13
14
(function (o_o) {
  typeof define  === 'function' && define.amd ? define(['jquery'], o_o) :
  typeof exports === 'object' ? o_o(require('jquery')) : o_o(this.jQuery)
})(function ($) {

Zlatan Vasović's avatar
Zlatan Vasović committed
15
  'use strict';
Jacob Thornton's avatar
Jacob Thornton committed
16

fat's avatar
fat committed
17
18
  // POPOVER PUBLIC CLASS DEFINITION
  // ===============================
19

Jacob Thornton's avatar
Jacob Thornton committed
20
  var Popover = function (element, options) {
21
    this.init('popover', element, options)
Jacob Thornton's avatar
Jacob Thornton committed
22
23
  }

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

fat's avatar
fat committed
26
27
  Popover.VERSION  = '3.1.1'

Zlatan Vasović's avatar
Zlatan Vasović committed
28
29
30
31
  Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
    placement: 'right',
    trigger: 'click',
    content: '',
32
    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
33
  })
34

Jacob Thornton's avatar
Jacob Thornton committed
35

fat's avatar
fat committed
36
37
  // NOTE: POPOVER EXTENDS tooltip.js
  // ================================
Jacob Thornton's avatar
Jacob Thornton committed
38

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

fat's avatar
fat committed
41
  Popover.prototype.constructor = Popover
42

fat's avatar
fat committed
43
44
45
  Popover.prototype.getDefaults = function () {
    return Popover.DEFAULTS
  }
46

fat's avatar
fat committed
47
48
49
50
  Popover.prototype.setContent = function () {
    var $tip    = this.tip()
    var title   = this.getTitle()
    var content = this.getContent()
Jacob Thornton's avatar
Jacob Thornton committed
51

fat's avatar
fat committed
52
    $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
53
    $tip.find('.popover-content').empty()[ // we use append for html objects to maintain js events
54
55
      this.options.html ? (typeof content == 'string' ? 'html' : 'append') : 'text'
    ](content)
56

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

59
60
    // 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
61
    if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').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

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

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

87

fat's avatar
fat committed
88
89
  // POPOVER PLUGIN DEFINITION
  // =========================
Jacob Thornton's avatar
Jacob Thornton committed
90

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

fat's avatar
fat committed
97
      if (!data && option == 'destroy') return
98
      if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
99
100
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
101
102
  }

103
104
105
  var old = $.fn.popover

  $.fn.popover             = Plugin
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
  }

Katie Zhu's avatar
Katie Zhu committed
117
});