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

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

https://gitlab.com/Mirros/cdnjs
JavaScript | 372 lines | 308 code | 36 blank | 28 comment | 64 complexity | d7291a4ef76a90676dfffdd0128b64ac 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.11.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.x || inside.y){
  138. clearTimeout(debounce);
  139. debounce = setTimeout(function(){
  140. calcPosition();
  141. animateSpeed = animateSpeed || o.followSpeed;
  142. var css = {};
  143. if(inside.x)
  144. css.left = o.follow[0] ? getLeftPos(true) : 'auto';
  145. if(inside.y)
  146. css.top = o.follow[1] ? getTopPos(true) : 'auto';
  147. $popup
  148. .dequeue()
  149. .each(function() {
  150. if(fixedPosStyle) {
  151. $(this).css({ 'left': hPos, 'top': vPos });
  152. }
  153. else {
  154. $(this).animate(css, animateSpeed, o.followEasing);
  155. }
  156. });
  157. }, 50);
  158. }
  159. };
  160. //Eksperimental
  161. function recenter(content){
  162. var _width = content.width(), _height = content.height(), css = {};
  163. o.contentContainer.css({height:_height,width:_width});
  164. if (_height >= $popup.height()){
  165. css.height = $popup.height();
  166. }
  167. if(_width >= $popup.width()){
  168. css.width = $popup.width();
  169. }
  170. height = $popup.outerHeight(true)
  171. , width = $popup.outerWidth(true);
  172. calcPosition();
  173. o.contentContainer.css({height:'auto',width:'auto'});
  174. css.left = getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)),
  175. css.top = getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle));
  176. $popup
  177. .animate(
  178. css
  179. , 250
  180. , function() {
  181. content.show();
  182. inside = insideWindow();
  183. }
  184. );
  185. };
  186. function bindEvents() {
  187. $w.data('bPopup', popups);
  188. $popup.delegate('.bClose, .' + o.closeClass, 'click.'+id, close); // legacy, still supporting the close class bClose
  189. if (o.modalClose) {
  190. $('.b-modal.'+id).css('cursor', 'pointer').bind('click', close);
  191. }
  192. // Temporary disabling scroll/resize events on devices with IOS6+
  193. // due to a bug where events are dropped after pinch to zoom
  194. if (!isIOS6X && (o.follow[0] || o.follow[1])) {
  195. $w.bind('scroll.'+id, function() {
  196. if(inside.x || inside.y){
  197. var css = {};
  198. if(inside.x)
  199. css.left = o.follow[0] ? getLeftPos(!fixedPosStyle) : 'auto';
  200. if(inside.y)
  201. css.top = o.follow[1] ? getTopPos(!fixedPosStyle) : 'auto';
  202. $popup
  203. .dequeue()
  204. .animate(css, o.followSpeed, o.followEasing);
  205. }
  206. }).bind('resize.'+id, function() {
  207. reposition();
  208. });
  209. }
  210. if (o.escClose) {
  211. d.bind('keydown.'+id, function(e) {
  212. if (e.which == 27) { //escape
  213. close();
  214. }
  215. });
  216. }
  217. };
  218. function unbindEvents() {
  219. if (!o.scrollBar) {
  220. $('html').css('overflow', 'auto');
  221. }
  222. $('.b-modal.'+id).unbind('click');
  223. d.unbind('keydown.'+id);
  224. $w.unbind('.'+id).data('bPopup', ($w.data('bPopup')-1 > 0) ? $w.data('bPopup')-1 : null);
  225. $popup.undelegate('.bClose, .' + o.closeClass, 'click.'+id, close).data('bPopup', null);
  226. };
  227. function doTransition(open) {
  228. switch (open ? o.transition : o.transitionClose || o.transition) {
  229. case "slideIn":
  230. animate({
  231. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() - (width || $popup.outerWidth(true)) - buffer
  232. });
  233. break;
  234. case "slideBack":
  235. animate({
  236. left: open ? getLeftPos(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() + wW + buffer
  237. });
  238. break;
  239. case "slideDown":
  240. animate({
  241. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() - (height || $popup.outerHeight(true)) - buffer
  242. });
  243. break;
  244. case "slideUp":
  245. animate({
  246. top: open ? getTopPos(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() + wH + buffer
  247. });
  248. break;
  249. default:
  250. //Hardtyping 1 and 0 to ensure opacity 1 and not 0.9999998
  251. $popup.stop().fadeTo(o.speed, open ? 1 : 0, function(){onCompleteCallback(open);});
  252. }
  253. function animate(css){
  254. $popup
  255. .css({display: 'block',opacity: 1})
  256. .animate(css, o.speed, o.easing, function(){ onCompleteCallback(open); });
  257. };
  258. };
  259. function onCompleteCallback(open){
  260. if(open){
  261. bindEvents();
  262. triggerCall(callback);
  263. if(o.autoClose){
  264. autoCloseTO = setTimeout(close, o.autoClose);
  265. }
  266. } else {
  267. $popup.hide();
  268. triggerCall(o.onClose);
  269. if (o.loadUrl) {
  270. o.contentContainer.empty();
  271. $popup.css({height: 'auto', width: 'auto'});
  272. }
  273. }
  274. };
  275. function getLeftPos(includeScroll){
  276. return includeScroll ? hPos + d.scrollLeft() : hPos;
  277. };
  278. function getTopPos(includeScroll){
  279. return includeScroll ? vPos + d.scrollTop() : vPos;
  280. };
  281. function triggerCall(func, arg) {
  282. $.isFunction(func) && func.call($popup, arg);
  283. };
  284. function calcPosition(){
  285. vPos = fixedVPos ? o.position[1] : Math.max(0, ((wH- $popup.outerHeight(true)) / 2) - o.amsl)
  286. , hPos = fixedHPos ? o.position[0] : (wW - $popup.outerWidth(true)) / 2
  287. , inside = insideWindow();
  288. };
  289. function insideWindow(){
  290. return {
  291. x: wW > $popup.outerWidth(true),
  292. y: wH > $popup.outerHeight(true)
  293. }
  294. };
  295. function windowHeight(){
  296. return $w.height();
  297. };
  298. function windowWidth(){
  299. return $w.width();
  300. };
  301. };
  302. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  303. // DEFAULT VALUES
  304. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  305. $.fn.bPopup.defaults = {
  306. amsl: 50
  307. , appending: true
  308. , appendTo: 'body'
  309. , autoClose: false
  310. , closeClass: 'b-close'
  311. , content: 'ajax' // ajax, iframe or image
  312. , contentContainer: false
  313. , easing: 'swing'
  314. , escClose: true
  315. , follow: [true, true] // x, y
  316. , followEasing: 'swing'
  317. , followSpeed: 500
  318. , iframeAttr: 'scrolling="no" frameborder="0"'
  319. , loadCallback: false
  320. , loadData: false
  321. , loadUrl: false
  322. , modal: true
  323. , modalClose: true
  324. , modalColor: '#000'
  325. , onClose: false
  326. , onOpen: false
  327. , opacity: 0.7
  328. , position: ['auto', 'auto'] // x, y,
  329. , positionStyle: 'absolute'// absolute or fixed
  330. , scrollBar: true
  331. , speed: 250 // open & close speed
  332. , transition: 'fadeIn' //transitions: fadeIn, slideDown, slideIn, slideBack
  333. , transitionClose: false
  334. , zIndex: 9997 // popup gets z-index 9999, modal overlay 9998
  335. };
  336. })(jQuery);