popover.js 3.13 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ===========================================================
fat's avatar
fat committed
2
 * Bootstrap: popover.js v3.0.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#popovers
Jacob Thornton's avatar
Jacob Thornton committed
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
18
 *
 * 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.
 * =========================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19
20


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

Jacob Thornton's avatar
Jacob Thornton committed
37

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

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

fat's avatar
fat committed
43
  Popover.prototype.constructor = Popover
44

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

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

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

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

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

fat's avatar
fat committed
64
65
  Popover.prototype.getContent = function () {
    var content = typeof this.options.content == 'function' ?
fat's avatar
fat committed
66
      this.options.content.call(this.$element[0]) :
fat's avatar
fat committed
67
      this.options.content
Jacob Thornton's avatar
Jacob Thornton committed
68

fat's avatar
fat committed
69
70
    return content || this.$element.attr('data-content')
  }
Jacob Thornton's avatar
Jacob Thornton committed
71

fat's avatar
fat committed
72
73
74
75
  Popover.prototype.tip = function () {
    if (!this.$tip) this.$tip = $(this.options.template)
    return this.$tip
  }
76

fat's avatar
fat committed
77
78
79
  Popover.prototype.destroy = function () {
    this.hide().$element.off('.' + this.type).removeData(this.type)
  }
Jacob Thornton's avatar
Jacob Thornton committed
80

81

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

85
86
  var old = $.fn.popover

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

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

98
  $.fn.popover.Constructor = Popover
99

100

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

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

109
}(window.jQuery);