bootstrap-popover.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* ===========================================================
  2. * bootstrap-popover.js v2.0.2
  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"
  21. var Popover = function ( element, options ) {
  22. this.init('popover', element, options)
  23. }
  24. /* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
  25. ========================================== */
  26. Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
  27. constructor: Popover
  28. , setContent: function () {
  29. var $tip = this.tip()
  30. , title = this.getTitle()
  31. , content = this.getContent()
  32. $tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
  33. $tip.find('.popover-content > *')[ $.type(content) == 'object' ? 'append' : 'html' ](content)
  34. $tip.removeClass('fade top bottom left right in')
  35. }
  36. , hasContent: function () {
  37. return this.getTitle() || this.getContent()
  38. }
  39. , getContent: function () {
  40. var content
  41. , $e = this.$element
  42. , o = this.options
  43. content = $e.attr('data-content')
  44. || (typeof o.content == 'function' ? o.content.call($e[0]) : o.content)
  45. content = content.toString().replace(/(^\s*|\s*$)/, "")
  46. return content
  47. }
  48. , tip: function() {
  49. if (!this.$tip) {
  50. this.$tip = $(this.options.template)
  51. }
  52. return this.$tip
  53. }
  54. })
  55. /* POPOVER PLUGIN DEFINITION
  56. * ======================= */
  57. $.fn.popover = function ( option ) {
  58. return this.each(function () {
  59. var $this = $(this)
  60. , data = $this.data('popover')
  61. , options = typeof option == 'object' && option
  62. if (!data) $this.data('popover', (data = new Popover(this, options)))
  63. if (typeof option == 'string') data[option]()
  64. })
  65. }
  66. $.fn.popover.Constructor = Popover
  67. $.fn.popover.defaults = $.extend({} , $.fn.tooltip.defaults, {
  68. placement: 'right'
  69. , content: ''
  70. , 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>'
  71. })
  72. }( window.jQuery );