PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/javascript/jquery/jquery.popoverext.js

https://gitlab.com/tjaafar/SuiteCRM
JavaScript | 186 lines | 144 code | 37 blank | 5 comment | 24 complexity | d8f03476467cfffc7b91e31ed4f2383b MD5 | raw file
  1. !function( $ ) {
  2. "use strict"
  3. var PopoverExt = function ( element, options ) {
  4. this.init('popoverext', element, options)
  5. }
  6. /* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
  7. ========================================== */
  8. PopoverExt.prototype = $.extend({}, $.fn.popover.Constructor.prototype, {
  9. constructor: PopoverExt
  10. , init: function ( type, element, options ) {
  11. var eventIn
  12. , eventOut
  13. this.type = type
  14. this.$element = $(element)
  15. this.options = this.getOptions(options)
  16. this.enabled = true
  17. if (this.options.trigger != 'manual') {
  18. eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
  19. eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
  20. this.$element.on(eventIn, this.options.selector, $.proxy(this.enter, this))
  21. this.$element.on(eventOut, this.options.selector, $.proxy(this.leave, this))
  22. }
  23. //console.log(this.tip())
  24. var $tip = this.tip();
  25. this.options.selector ?
  26. (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
  27. this.fixTitle()
  28. }
  29. , setContent: function () {
  30. var $tip = this.tip()
  31. , title = this.getTitle()
  32. , content = this.getContent()
  33. , footer = this.getFooter();
  34. $tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title);
  35. $tip.find('.popover-content > *')[ $.type(content) == 'object' ? 'append' : 'html' ](content);
  36. $tip.find('.popover-footer')[ $.type(footer) == 'object' ? 'append' : 'html' ](footer);
  37. $tip.removeClass('fade top bottom left right in');
  38. }
  39. , hasContent: function () {
  40. return this.getTitle() || this.getContent()
  41. }
  42. , setId: function() {
  43. var $tip;
  44. if(this.options.id) {
  45. $tip = this.tip();
  46. $tip.attr("id",this.$element.attr("id") + "Popover");
  47. }
  48. }
  49. , show: function () {
  50. var $tip
  51. , inside
  52. , pos
  53. , actualWidth
  54. , actualHeight
  55. , placement
  56. , tp;
  57. if (this.hasContent() && this.enabled) {
  58. $tip = this.tip();
  59. this.setContent();
  60. this.setId();
  61. if (this.options.animation) {
  62. $tip.addClass('fade')
  63. }
  64. placement = typeof this.options.placement == 'function' ?
  65. this.options.placement.call(this, $tip[0], this.$element[0]) :
  66. this.options.placement;
  67. inside = /in/.test(placement)
  68. $tip
  69. .remove()
  70. .css({ top: 0, left: 0, display: 'block' })
  71. .appendTo(inside ? this.$element : document.body);
  72. if(this.options.hideOnBlur) {
  73. $tip
  74. .attr("tabindex","-1")
  75. .on("blur", $.proxy(this.hide, this))
  76. .trigger("focus");
  77. }
  78. pos = this.getPosition(inside)
  79. actualWidth = $tip[0].offsetWidth
  80. actualHeight = $tip[0].offsetHeight
  81. switch (inside ? placement.split(' ')[1] : placement) {
  82. case 'bottom':
  83. tp = {top: pos.top + pos.height + this.options.topOffset, left: pos.left + pos.width / 2 - actualWidth / 2}
  84. break
  85. case 'bottom left':
  86. tp = {top: pos.top + pos.height + this.options.topOffset, left: pos.left + this.options.leftOffset}
  87. break
  88. case 'bottom right':
  89. tp = {top: pos.top + pos.height + this.options.topOffset , left: (pos.left + pos.width) - actualWidth - this.options.leftOffset }
  90. break
  91. case 'top':
  92. tp = {top: pos.top - actualHeight + this.options.topOffset, left: pos.left + (pos.width / 2 - actualWidth / 2) + this.options.leftOffset}
  93. break
  94. case 'top right':
  95. tp = {top: pos.top - actualHeight + this.options.topOffset, left: (pos.left + pos.width) - actualWidth - this.options.leftOffset}
  96. break
  97. case 'top left':
  98. tp = {top: pos.top - actualHeight + this.options.topOffset, left: pos.left + this.options.leftOffset}
  99. break
  100. case 'left':
  101. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
  102. break
  103. case 'right':
  104. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
  105. break
  106. }
  107. $tip
  108. .css(tp)
  109. .addClass(placement)
  110. .addClass('in')
  111. if(this.options.fixed == true) {
  112. $tip.css("position","fixed");
  113. }
  114. }
  115. this.options.onShow();
  116. }
  117. , getFooter: function () {
  118. var footer
  119. , $e = this.$element
  120. , o = this.options;
  121. footer = $e.attr('data-footer')
  122. || (typeof o.content == 'function' ? o.footer.call($e[0]) : o.footer);
  123. footer = footer.toString().replace(/(^\s*|\s*$)/, "");
  124. return footer;
  125. }
  126. })
  127. /* POPOVER PLUGIN DEFINITION
  128. * ======================= */
  129. $.fn.popoverext = function ( option ) {
  130. return this.each(function () {
  131. var $this = $(this)
  132. , data = $this.data('popoverext')
  133. , options = typeof option == 'object' && option
  134. if (!data) $this.data('popoverext', (data = new PopoverExt(this, options)))
  135. if (typeof option == 'string') data[option]()
  136. })
  137. }
  138. $.fn.popoverext.Constructor = PopoverExt
  139. $.fn.popoverext.defaults = $.extend({} , $.fn.popover.defaults, {
  140. fixed: false,
  141. 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 class="popover-footer"></div></div></div>',
  142. id: false,
  143. footer: '',
  144. onShow:$.empty,
  145. leftOffset: 0,
  146. topOffset: 0,
  147. hideOnBlur: false
  148. })
  149. }( window.jQuery );