bootstrap-popover.js 2.73 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ===========================================================
2
 * bootstrap-popover.js v2.0.0
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * http://twitter.github.com/bootstrap/javascript.html#popover
 * ===========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * 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
 "use strict"

Jacob Thornton's avatar
Jacob Thornton committed
25
  var Popover = function ( element, options ) {
26
    this.init('popover', element, options)
Jacob Thornton's avatar
Jacob Thornton committed
27
28
  }

Jacob Thornton's avatar
Jacob Thornton committed
29
30
31
  /* NOTE: POPOVER EXTENDS BOOTSTRAP-TWIPSY.js
     ========================================= */

Jacob Thornton's avatar
Jacob Thornton committed
32
33
  Popover.prototype = $.extend({}, $.fn.twipsy.Twipsy.prototype, {

34
35
36
    constructor: Popover

  , setContent: function () {
Jacob Thornton's avatar
Jacob Thornton committed
37
      var $tip = this.tip()
38
39
40
41
42
43
        , title = this.getTitle()
        , content = this.getContent()

      $tip.find('.title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
      $tip.find('.content > *')[ $.type(content) == 'object' ? 'append' : 'html' ](content)

Jacob Thornton's avatar
Jacob Thornton committed
44
45
46
      $tip[0].className = 'popover'
    }

47
48
49
50
  , hasContent: function () {
      return this.getTitle() || this.getContent()
    }

51
  , getContent: function () {
52
      var content
53
54
        , $e = this.$element
        , o = this.options
Jacob Thornton's avatar
Jacob Thornton committed
55

56
57
58
59
      content = $e.attr('data-content')
        || (typeof o.content == 'function' ? o.content.call($e[0]) :  o.content)

      content = content.toString().replace(/(^\s*|\s*$)/, "")
60

Jacob Thornton's avatar
Jacob Thornton committed
61
62
63
64
65
      return content
    }

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

  })

73

Jacob Thornton's avatar
Jacob Thornton committed
74
75
76
 /* POPOVER PLUGIN DEFINITION
  * ======================= */

77
78
79
80
81
82
83
84
  $.fn.popover = function ( option ) {
    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
85
86
  }

87
88
  $.fn.popover.Popover = Popover

89
90
  $.fn.popover.defaults = $.extend({} , $.fn.twipsy.defaults, {
    placement: 'right'
91
  , content: ''
92
  , template: '<div class="popover"><div class="arrow"></div><div class="inner"><h3 class="title"></h3><div class="content"><p></p></div></div></div>'
93
94
  })

95
}( window.jQuery )