popover.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ========================================================================
  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. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // POPOVER PUBLIC CLASS DEFINITION
  11. // ===============================
  12. var Popover = function (element, options) {
  13. this.init('popover', element, options)
  14. }
  15. if (!$.fn.tooltip) throw new Error('Popover requires tooltip.js')
  16. Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
  17. placement: 'right',
  18. trigger: 'click',
  19. content: '',
  20. template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
  21. })
  22. // NOTE: POPOVER EXTENDS tooltip.js
  23. // ================================
  24. Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
  25. Popover.prototype.constructor = Popover
  26. Popover.prototype.getDefaults = function () {
  27. return Popover.DEFAULTS
  28. }
  29. Popover.prototype.setContent = function () {
  30. var $tip = this.tip()
  31. var title = this.getTitle()
  32. var content = this.getContent()
  33. $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
  34. $tip.find('.popover-content')[ // we use append for html objects to maintain js events
  35. this.options.html ? (typeof content == 'string' ? 'html' : 'append') : 'text'
  36. ](content)
  37. $tip.removeClass('fade top bottom left right in')
  38. // IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
  39. // this manually by checking the contents.
  40. if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
  41. }
  42. Popover.prototype.hasContent = function () {
  43. return this.getTitle() || this.getContent()
  44. }
  45. Popover.prototype.getContent = function () {
  46. var $e = this.$element
  47. var o = this.options
  48. return $e.attr('data-content')
  49. || (typeof o.content == 'function' ?
  50. o.content.call($e[0]) :
  51. o.content)
  52. }
  53. Popover.prototype.arrow = function () {
  54. return this.$arrow = this.$arrow || this.tip().find('.arrow')
  55. }
  56. Popover.prototype.tip = function () {
  57. if (!this.$tip) this.$tip = $(this.options.template)
  58. return this.$tip
  59. }
  60. // POPOVER PLUGIN DEFINITION
  61. // =========================
  62. var old = $.fn.popover
  63. $.fn.popover = function (option) {
  64. return this.each(function () {
  65. var $this = $(this)
  66. var data = $this.data('bs.popover')
  67. var options = typeof option == 'object' && option
  68. if (!data && option == 'destroy') return
  69. if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
  70. if (typeof option == 'string') data[option]()
  71. })
  72. }
  73. $.fn.popover.Constructor = Popover
  74. // POPOVER NO CONFLICT
  75. // ===================
  76. $.fn.popover.noConflict = function () {
  77. $.fn.popover = old
  78. return this
  79. }
  80. }(jQuery);