PageRenderTime 30ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/bPopup/0.10.0.1/jquery.bpopup.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 359 lines | 295 code | 36 blank | 28 comment | 61 complexity | f0a3f86ba3af393b9f3b9faed836c105 MD5 | raw file
  1. /*===================================================================================================================
  2. * @name: bPopup
  3. * @type: jQuery
  4. * @author: (c) Bjoern Klinggaard - @bklinggaard
  5. * @demo: http://dinbror.dk/bpopup
  6. * @version: 0.10.0
  7. * @requires jQuery 1.4.3
  8. *==================================================================================================================*/
  9. ;(function($) {
  10. 'use strict';
  11. $.fn.bPopup = function(options, callback) {
  12. if ($.isFunction(options)) {
  13. callback = options;
  14. options = null;
  15. }
  16. // OPTIONS
  17. var o = $.extend({}, $.fn.bPopup.defaults, options);
  18. // HIDE SCROLLBAR?
  19. if (!o.scrollBar)
  20. $('html').css('overflow', 'hidden');
  21. // VARIABLES
  22. var $popup = this
  23. , d = $(document)
  24. , w = window
  25. , $w = $(w)
  26. , wH = windowHeight()
  27. , wW = windowWidth()
  28. , prefix = '__b-popup'
  29. , isIOS6X = (/OS 6(_\d)+/i).test(navigator.userAgent) // Used for a temporary fix for ios6 timer bug when using zoom/scroll
  30. , buffer = 200
  31. , popups = 0
  32. , id
  33. , inside
  34. , fixedVPos
  35. , fixedHPos
  36. , fixedPosStyle
  37. , vPos
  38. , hPos
  39. , height
  40. , width
  41. , debounce
  42. , autoCloseTO
  43. ;
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. // PUBLIC FUNCTION - call it: $(element).bPopup().close();
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. $popup.close = function() {
  48. close();
  49. };
  50. $popup.reposition = function(animateSpeed) {
  51. reposition(animateSpeed);
  52. };
  53. return $popup.each(function() {
  54. if ($(this).data('bPopup')) return; //POPUP already exists?
  55. init();
  56. });
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. // HELPER FUNCTIONS - PRIVATE
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. function init() {
  61. triggerCall(o.onOpen);
  62. popups = ($w.data('bPopup') || 0) + 1, id = prefix + popups + '__',fixedVPos = o.position[1] !== 'auto', fixedHPos = o.position[0] !== 'auto', fixedPosStyle = o.positionStyle === 'fixed', height = $popup.outerHeight(true), width = $popup.outerWidth(true);
  63. o.loadUrl ? createContent() : open();
  64. };
  65. function createContent() {
  66. o.contentContainer = $(o.contentContainer || $popup);
  67. switch (o.content) {
  68. case ('iframe'):
  69. var iframe = $('<iframe class="b-iframe" ' + o.iframeAttr +'></iframe>');
  70. iframe.appendTo(o.contentContainer);
  71. height = $popup.outerHeight(true);
  72. width = $popup.outerWidth(true);
  73. open();
  74. iframe.attr('src', o.loadUrl); // setting iframe src after open due IE9 bug
  75. triggerCall(o.loadCallback);
  76. break;
  77. case ('image'):
  78. open();
  79. $('<img />')
  80. .load(function() {
  81. triggerCall(o.loadCallback);
  82. recenter($(this));
  83. }).attr('src', o.loadUrl).hide().appendTo(o.contentContainer);
  84. break;
  85. default:
  86. open();
  87. $('<div class="b-ajax-wrapper"></div>')
  88. .load(o.loadUrl, o.loadData, function(response, status, xhr) {
  89. triggerCall(o.loadCallback, status);
  90. recenter($(this));
  91. }).hide().appendTo(o.contentContainer);
  92. break;
  93. }
  94. };
  95. function open(){
  96. // MODAL OVERLAY
  97. if (o.modal) {
  98. $('<div class="b-modal '+id+'"></div>')
  99. .css({backgroundColor: o.modalColor, position: 'fixed', top: 0, right:0, bottom:0, left: 0, opacity: 0, zIndex: o.zIndex + popups})
  100. .appendTo(o.appendTo)
  101. .fadeTo(o.speed, o.opacity);
  102. }
  103. // POPUP
  104. calcPosition();
  105. $popup
  106. .data('bPopup', o).data('id',id)
  107. .css({
  108. 'left': o.transition == 'slideIn' || o.transition == 'slideBack' ? (o.transition == 'slideBack' ? d.scrollLeft() + wW : (hPos + width) *-1) : getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle))
  109. , 'position': o.positionStyle || 'absolute'
  110. , 'top': o.transition == 'slideDown' || o.transition == 'slideUp' ? (o.transition == 'slideUp' ? d.scrollTop() + wH : vPos + height * -1) : getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle))
  111. , 'z-index': o.zIndex + popups + 1
  112. }).each(function() {
  113. if(o.appending) {
  114. $(this).appendTo(o.appendTo);
  115. }
  116. });
  117. doTransition(true);
  118. };
  119. function close() {
  120. if (o.modal) {
  121. $('.b-modal.'+$popup.data('id'))
  122. .fadeTo(o.speed, 0, function() {
  123. $(this).remove();
  124. });
  125. }
  126. // Clean up
  127. unbindEvents();
  128. clearTimeout(autoCloseTO);
  129. // Close trasition
  130. doTransition();
  131. return false; // Prevent default
  132. };
  133. function reposition(animateSpeed){
  134. wH = windowHeight();
  135. wW = windowWidth();
  136. inside = insideWindow();
  137. if(inside){
  138. clearTimeout(debounce);
  139. debounce = setTimeout(function(){
  140. calcPosition();
  141. animateSpeed = animateSpeed || o.followSpeed;
  142. $popup
  143. .dequeue()
  144. .each(function() {
  145. if(fixedPosStyle) {
  146. $(this).css({ 'left': hPos, 'top': vPos });
  147. }
  148. else {
  149. $(this).animate({ 'left': o.follow[0] ? getLeftPos(true) : 'auto', 'top': o.follow[1] ? getTopPos(true) : 'auto' }, animateSpeed, o.followEasing);
  150. }
  151. });
  152. }, 50);
  153. }
  154. };
  155. //Eksperimental
  156. function recenter(content){
  157. var _width = content.width(), _height = content.height(), css = {};
  158. o.contentContainer.css({height:_height,width:_width});
  159. if (_height >= $popup.height()){
  160. css.height = $popup.height();
  161. }
  162. if(_width >= $popup.width()){
  163. css.width = $popup.width();
  164. }
  165. height = $popup.outerHeight(true)
  166. , width = $popup.outerWidth(true);
  167. calcPosition();
  168. o.contentContainer.css({height:'auto',width:'auto'});
  169. css.left = getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)),
  170. css.top = getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle));
  171. $popup
  172. .animate(
  173. css
  174. , 250
  175. , function() {
  176. content.show();
  177. inside = insideWindow();
  178. }
  179. );
  180. };
  181. function bindEvents() {
  182. $w.data('bPopup', popups);
  183. $popup.delegate('.bClose, .' + o.closeClass, 'click.'+id, close); // legacy, still supporting the close class bClose
  184. if (o.modalClose) {
  185. $('.b-modal.'+id).css('cursor', 'pointer').bind('click', close);
  186. }
  187. // Temporary disabling scroll/resize events on devices with IOS6+
  188. // due to a bug where events are dropped after pinch to zoom
  189. if (!isIOS6X && (o.follow[0] || o.follow[1])) {
  190. $w.bind('scroll.'+id, function() {
  191. if(inside){
  192. $popup
  193. .dequeue()
  194. .animate({ 'left': o.follow[0] ? getLeftPos(!fixedPosStyle) : 'auto', 'top': o.follow[1] ? getTopPos(!fixedPosStyle) : 'auto' }, o.followSpeed, o.followEasing);
  195. }
  196. }).bind('resize.'+id, function() {
  197. reposition();
  198. });
  199. }
  200. if (o.escClose) {
  201. d.bind('keydown.'+id, function(e) {
  202. if (e.which == 27) { //escape
  203. close();
  204. }
  205. });
  206. }
  207. };
  208. function unbindEvents() {
  209. if (!o.scrollBar) {
  210. $('html').css('overflow', 'auto');
  211. }
  212. $('.b-modal.'+id).unbind('click');
  213. d.unbind('keydown.'+id);
  214. $w.unbind('.'+id).data('bPopup', ($w.data('bPopup')-1 > 0) ? $w.data('bPopup')-1 : null);
  215. $popup.undelegate('.bClose, .' + o.closeClass, 'click.'+id, close).data('bPopup', null);
  216. };
  217. function doTransition(open) {
  218. switch (open ? o.transition : o.transitionClose || o.transition) {
  219. case "slideIn":
  220. animate({
  221. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() - (width || $popup.outerWidth(true)) - buffer
  222. });
  223. break;
  224. case "slideBack":
  225. animate({
  226. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() + wW + buffer
  227. });
  228. break;
  229. case "slideDown":
  230. animate({
  231. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() - (height || $popup.outerHeight(true)) - buffer
  232. });
  233. break;
  234. case "slideUp":
  235. animate({
  236. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() + wH + buffer
  237. });
  238. break;
  239. default:
  240. //Hardtyping 1 and 0 to ensure opacity 1 and not 0.9999998
  241. $popup.stop().fadeTo(o.speed, open ? 1 : 0, function(){onCompleteCallback(open);});
  242. }
  243. function animate(css){
  244. $popup
  245. .css({display: 'block',opacity: 1})
  246. .animate(css, o.speed, o.easing, function(){ onCompleteCallback(open); });
  247. };
  248. };
  249. function onCompleteCallback(open){
  250. if(open){
  251. bindEvents();
  252. triggerCall(callback);
  253. if(o.autoClose){
  254. autoCloseTO = setTimeout(close, o.autoClose);
  255. }
  256. } else {
  257. $popup.hide();
  258. triggerCall(o.onClose);
  259. if (o.loadUrl) {
  260. o.contentContainer.empty();
  261. $popup.css({height: 'auto', width: 'auto'});
  262. }
  263. }
  264. };
  265. function getLeftPos(includeScroll){
  266. return includeScroll ? hPos + d.scrollLeft() : hPos;
  267. };
  268. function getTopPos(includeScroll){
  269. return includeScroll ? vPos + d.scrollTop() : vPos;
  270. };
  271. function triggerCall(func, arg) {
  272. $.isFunction(func) && func.call($popup, arg);
  273. };
  274. function calcPosition(){
  275. vPos = fixedVPos ? o.position[1] : Math.max(0, ((wH- $popup.outerHeight(true)) / 2) - o.amsl)
  276. , hPos = fixedHPos ? o.position[0] : (wW - $popup.outerWidth(true)) / 2
  277. , inside = insideWindow();
  278. };
  279. function insideWindow(){
  280. return wH > $popup.outerHeight(true) && wW > $popup.outerWidth(true);
  281. };
  282. function windowHeight(){
  283. return w.innerHeight || $w.height();
  284. };
  285. function windowWidth(){
  286. return w.innerWidth || $w.width();
  287. };
  288. };
  289. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. // DEFAULT VALUES
  291. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. $.fn.bPopup.defaults = {
  293. amsl: 50
  294. , appending: true
  295. , appendTo: 'body'
  296. , autoClose: false
  297. , closeClass: 'b-close'
  298. , content: 'ajax' // ajax, iframe or image
  299. , contentContainer: false
  300. , easing: 'swing'
  301. , escClose: true
  302. , follow: [true, true] // x, y
  303. , followEasing: 'swing'
  304. , followSpeed: 500
  305. , iframeAttr: 'scrolling="no" frameborder="0"'
  306. , loadCallback: false
  307. , loadData: false
  308. , loadUrl: false
  309. , modal: true
  310. , modalClose: true
  311. , modalColor: '#000'
  312. , onClose: false
  313. , onOpen: false
  314. , opacity: 0.7
  315. , position: ['auto', 'auto'] // x, y,
  316. , positionStyle: 'absolute'// absolute or fixed
  317. , scrollBar: true
  318. , speed: 250 // open & close speed
  319. , transition: 'fadeIn' //transitions: fadeIn, slideDown, slideIn, slideBack
  320. , transitionClose: false
  321. , zIndex: 9997 // popup gets z-index 9999, modal overlay 9998
  322. };
  323. })(jQuery);