popover.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ========================================================================
  2. * Bootstrap: popover.js v3.0.3
  3. * http://getbootstrap.com/javascript/#popovers
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // POPOVER PUBLIC CLASS DEFINITION
  21. // ===============================
  22. var Popover = function (element, options) {
  23. this.init('popover', element, options)
  24. }
  25. if (!$.fn.tooltip) throw new Error('Popover requires tooltip.js')
  26. Popover.DEFAULTS = $.extend({} , $.fn.tooltip.Constructor.DEFAULTS, {
  27. placement: 'right'
  28. , trigger: 'click'
  29. , content: ''
  30. , template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
  31. })
  32. // NOTE: POPOVER EXTENDS tooltip.js
  33. // ================================
  34. Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
  35. Popover.prototype.constructor = Popover
  36. Popover.prototype.getDefaults = function () {
  37. return Popover.DEFAULTS
  38. }
  39. Popover.prototype.setContent = function () {
  40. var $tip = this.tip()
  41. var title = this.getTitle()
  42. var content = this.getContent()
  43. $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
  44. $tip.find('.popover-content')[this.options.html ? 'html' : 'text'](content)
  45. $tip.removeClass('fade top bottom left right in')
  46. // IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
  47. // this manually by checking the contents.
  48. if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
  49. }
  50. Popover.prototype.hasContent = function () {
  51. return this.getTitle() || this.getContent()
  52. }
  53. Popover.prototype.getContent = function () {
  54. var $e = this.$element
  55. var o = this.options
  56. return $e.attr('data-content')
  57. || (typeof o.content == 'function' ?
  58. o.content.call($e[0]) :
  59. o.content)
  60. }
  61. Popover.prototype.arrow = function () {
  62. return this.$arrow = this.$arrow || this.tip().find('.arrow')
  63. }
  64. Popover.prototype.tip = function () {
  65. if (!this.$tip) this.$tip = $(this.options.template)
  66. return this.$tip
  67. }
  68. // POPOVER PLUGIN DEFINITION
  69. // =========================
  70. var old = $.fn.popover
  71. $.fn.popover = function (option) {
  72. return this.each(function () {
  73. var $this = $(this)
  74. var data = $this.data('bs.popover')
  75. var options = typeof option == 'object' && option
  76. if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
  77. if (typeof option == 'string') data[option]()
  78. })
  79. }
  80. $.fn.popover.Constructor = Popover
  81. // POPOVER NO CONFLICT
  82. // ===================
  83. $.fn.popover.noConflict = function () {
  84. $.fn.popover = old
  85. return this
  86. }
  87. }(jQuery);