bootstrap-popover.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ===========================================================
  2. * bootstrap-popover.js v2.1.1
  3. * http://twitter.github.com/bootstrap/javascript.html#popovers
  4. * ===========================================================
  5. * Copyright 2012 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 ($) {
  20. "use strict"; // jshint ;_;
  21. /* POPOVER PUBLIC CLASS DEFINITION
  22. * =============================== */
  23. var Popover = function (element, options) {
  24. this.init('popover', element, options)
  25. }
  26. /* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
  27. ========================================== */
  28. Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
  29. constructor: Popover
  30. , setContent: function () {
  31. var $tip = this.tip()
  32. , title = this.getTitle()
  33. , content = this.getContent()
  34. $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
  35. $tip.find('.popover-content > *')[this.options.html ? 'html' : 'text'](content)
  36. $tip.removeClass('fade top bottom left right in')
  37. }
  38. , hasContent: function () {
  39. return this.getTitle() || this.getContent()
  40. }
  41. , getContent: function () {
  42. var content
  43. , $e = this.$element
  44. , o = this.options
  45. content = $e.attr('data-content')
  46. || (typeof o.content == 'function' ? o.content.call($e[0]) : o.content)
  47. return content
  48. }
  49. , tip: function () {
  50. if (!this.$tip) {
  51. this.$tip = $(this.options.template)
  52. }
  53. return this.$tip
  54. }
  55. , destroy: function () {
  56. this.hide().$element.off('.' + this.type).removeData(this.type)
  57. }
  58. })
  59. /* POPOVER PLUGIN DEFINITION
  60. * ======================= */
  61. $.fn.popover = function (option) {
  62. return this.each(function () {
  63. var $this = $(this)
  64. , data = $this.data('popover')
  65. , options = typeof option == 'object' && option
  66. if (!data) $this.data('popover', (data = new Popover(this, options)))
  67. if (typeof option == 'string') data[option]()
  68. })
  69. }
  70. $.fn.popover.Constructor = Popover
  71. $.fn.popover.defaults = $.extend({} , $.fn.tooltip.defaults, {
  72. placement: 'right'
  73. , trigger: 'click'
  74. , content: ''
  75. , 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>'
  76. })
  77. }(window.jQuery);