bootstrap-popover.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ===========================================================
  2. * bootstrap-popover.js v2.0.4
  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.isHTML(title) ? 'html' : 'text'](title)
  35. $tip.find('.popover-content > *')[this.isHTML(content) ? '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. })
  56. /* POPOVER PLUGIN DEFINITION
  57. * ======================= */
  58. $.fn.popover = function (option) {
  59. return this.each(function () {
  60. var $this = $(this)
  61. , data = $this.data('popover')
  62. , options = typeof option == 'object' && option
  63. if (!data) $this.data('popover', (data = new Popover(this, options)))
  64. if (typeof option == 'string') data[option]()
  65. })
  66. }
  67. $.fn.popover.Constructor = Popover
  68. $.fn.popover.defaults = $.extend({} , $.fn.tooltip.defaults, {
  69. placement: 'right'
  70. , content: ''
  71. , 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>'
  72. })
  73. }(window.jQuery);