bootstrap-popover.js 2.8 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ===========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-popover.js v2.0.4
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


21
!function ($) {
Jacob Thornton's avatar
Jacob Thornton committed
22

23
24
25
26
27
  "use strict"; // jshint ;_;


 /* POPOVER PUBLIC CLASS DEFINITION
  * =============================== */
28

Jacob Thornton's avatar
Jacob Thornton committed
29
  var Popover = function ( element, options ) {
30
    this.init('popover', element, options)
Jacob Thornton's avatar
Jacob Thornton committed
31
32
  }

33

34
35
  /* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
     ========================================== */
Jacob Thornton's avatar
Jacob Thornton committed
36

37
  Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
Jacob Thornton's avatar
Jacob Thornton committed
38

39
40
41
    constructor: Popover

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

46
47
      $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
      $tip.find('.popover-content > *')[this.options.html ? 'html' : 'text'](content)
48

49
      $tip.removeClass('fade top bottom left right in')
Jacob Thornton's avatar
Jacob Thornton committed
50
51
    }

52
53
54
55
  , hasContent: function () {
      return this.getTitle() || this.getContent()
    }

56
  , getContent: function () {
57
      var content
58
59
        , $e = this.$element
        , o = this.options
Jacob Thornton's avatar
Jacob Thornton committed
60

61
62
63
      content = $e.attr('data-content')
        || (typeof o.content == 'function' ? o.content.call($e[0]) :  o.content)

Jacob Thornton's avatar
Jacob Thornton committed
64
65
66
      return content
    }

67
  , tip: function () {
Jacob Thornton's avatar
Jacob Thornton committed
68
      if (!this.$tip) {
69
        this.$tip = $(this.options.template)
Jacob Thornton's avatar
Jacob Thornton committed
70
71
72
73
74
75
      }
      return this.$tip
    }

  })

76

Jacob Thornton's avatar
Jacob Thornton committed
77
78
79
 /* POPOVER PLUGIN DEFINITION
  * ======================= */

80
  $.fn.popover = function (option) {
81
82
83
84
85
86
87
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('popover')
        , options = typeof option == 'object' && option
      if (!data) $this.data('popover', (data = new Popover(this, options)))
      if (typeof option == 'string') data[option]()
    })
Jacob Thornton's avatar
Jacob Thornton committed
88
89
  }

90
  $.fn.popover.Constructor = Popover
91

92
  $.fn.popover.defaults = $.extend({} , $.fn.tooltip.defaults, {
93
    placement: 'right'
94
  , content: ''
Mark Otto's avatar
Mark Otto committed
95
  , template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'
96
97
  })

98
}(window.jQuery);