popup.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * $Revision$
  3. * $Date$
  4. *
  5. * Copyright (C) 1999-2009 Jive Software. All rights reserved.
  6. *
  7. * This software is the proprietary information of Jive Software. Use is subject to license terms.
  8. */
  9. /*
  10. * $ lightbox_me
  11. * By: Buck Wilson
  12. * Version : 2.0
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. (function($) {
  27. $.fn.lightbox_me = function(options) {
  28. return this.each(function() {
  29. var
  30. opts = $.extend({}, $.fn.lightbox_me.defaults, options),
  31. $overlay = $('<div class="' + opts.classPrefix + '_overlay"/>'),
  32. $self = $(this),
  33. $iframe = $('<iframe src="' + /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' + '" style="z-index: ' + (opts.zIndex + 1) + '; display: none; border: none; margin: 0; padding: 0; position: absolute; width: 100%; height: 100%; top: 0; left: 0;"/>'),
  34. ie6 = ($.browser.msie && $.browser.version < 7);
  35. /*----------------------------------------------------
  36. DOM Building
  37. ---------------------------------------------------- */
  38. if (ie6) { $('body').append($iframe) } // iframe shim for ie6, to hide select elements
  39. $('body').append($self).append($overlay);
  40. /*----------------------------------------------------
  41. CSS stuffs
  42. ---------------------------------------------------- */
  43. // set css of the modal'd window
  44. setSelfPosition();
  45. $self.css({left: '50%', marginLeft: ($self.outerWidth() / 2) * -1, zIndex: (opts.zIndex + 3) });
  46. // set css of the overlay
  47. setOverlayHeight(); // pulled this into a function because it is called on window resize.
  48. $overlay
  49. .css({ position: 'absolute', width: '100%', top: 0, left: 0, right: 0, bottom: 0, zIndex: (opts.zIndex + 2), display: 'none' })
  50. .css(opts.overlayCSS);
  51. /*----------------------------------------------------
  52. Animate it in.
  53. ---------------------------------------------------- */
  54. $overlay.fadeIn(opts.overlaySpeed, function() {
  55. $self[opts.appearEffect](opts.lightboxSpeed, function() { setOverlayHeight(); opts.onLoad()});
  56. });
  57. /*----------------------------------------------------
  58. Bind Events
  59. ---------------------------------------------------- */
  60. $(window).resize(setOverlayHeight)
  61. .resize(setSelfPosition)
  62. .scroll(setSelfPosition)
  63. .keypress(observeEscapePress);
  64. $self.find(opts.closeSelector).add($overlay).click(function() { closeLightbox(); return false; });
  65. $self.bind('close', closeLightbox);
  66. $self.bind('resize', setSelfPosition);
  67. /*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  68. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
  69. /*----------------------------------------------------
  70. Private Functions
  71. ---------------------------------------------------- */
  72. /* Remove or hide all elements */
  73. function closeLightbox() {
  74. if (opts.destroyOnClose) {
  75. $self.add($overlay).remove();
  76. } else {
  77. $self.add($overlay).hide();
  78. }
  79. $iframe.remove();
  80. $(window).unbind('resize', setOverlayHeight);
  81. $(window).unbind('resize', setSelfPosition);
  82. opts.onClose();
  83. }
  84. /* Function to bind to the window to observe the escape key press */
  85. function observeEscapePress(e) {
  86. if(e.keyCode == 27 || (e.DOM_VK_ESCAPE == 27 && e.which==0)) closeLightbox();
  87. }
  88. /* Set the height of the overlay
  89. : if the document height is taller than the window, then set the overlay height to the document height.
  90. : otherwise, just set overlay height: 100%
  91. */
  92. function setOverlayHeight() {
  93. if ($(window).height() < $(document).height()) {
  94. $overlay.css({height: $(document).height() + 'px'});
  95. } else {
  96. $overlay.css({height: '100%'});
  97. if (ie6) {$('html,body').css('height','100%'); } // ie6 hack for height: 100%; TODO: handle this in IE7
  98. }
  99. }
  100. /* Set the position of the modal'd window ($self)
  101. : if $self is taller than the window, then make it absolutely positioned
  102. : otherwise fixed
  103. */
  104. function setSelfPosition() {
  105. var s = $self[0].style;
  106. if (($self.height() + 80 >= $(window).height()) && ($self.css('position') != 'absolute' || ie6)) {
  107. var topOffset = $(document).scrollTop() + 40;
  108. $self.css({position: 'absolute', top: topOffset + 'px', marginTop: 0})
  109. if (ie6) {
  110. s.removeExpression('top');
  111. }
  112. } else if ($self.height()+ 80 < $(window).height()) {
  113. if (ie6) {
  114. s.position = 'absolute';
  115. if (opts.centered) {
  116. s.setExpression('top', '(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"')
  117. s.marginTop = 0;
  118. } else {
  119. var top = (opts.modalCSS && opts.modalCSS.top) ? parseInt(opts.modalCSS.top) : 0;
  120. s.setExpression('top', '((blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"')
  121. }
  122. } else {
  123. if (opts.centered) {
  124. $self.css({ position: 'fixed', top: '50%', marginTop: ($self.outerHeight() / 2) * -1})
  125. } else {
  126. $self.css({ position: 'fixed'}).css(opts.modalCSS);
  127. }
  128. }
  129. }
  130. }
  131. });
  132. };
  133. $.fn.lightbox_me.defaults = {
  134. // animation
  135. appearEffect: "fadeIn",
  136. overlaySpeed: 300,
  137. lightboxSpeed: "fast",
  138. // close
  139. closeSelector: ".close",
  140. closeClick: true,
  141. closeEsc: true,
  142. // behavior
  143. destroyOnClose: false,
  144. // callbacks
  145. onLoad: function() {},
  146. onClose: function() {},
  147. // style
  148. classPrefix: 'lb',
  149. zIndex: 999,
  150. centered: false,
  151. modalCSS: {top: '40px'},
  152. overlayCSS: {background: 'black', opacity: .6}
  153. }
  154. })(jQuery);