-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.smartModal.min.js
13 lines (13 loc) · 5.66 KB
/
jquery.smartModal.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
* jQuery smartModal
*
* Version: 2.1.0
* Author: Ben Marshall
* Author URL: http://www.benmarshall.me
* jQuery Plugin URL: http://plugins.jquery.com/smartModal/
* Plugin URL: http://www.benmarshall.me/jquery-smartmodal/
* GitHub: https://github.com/bmarshall511/jquery-smartModal
*
* Licensed under the MIT license
*/
(function(h){var f={overlayDelay:300,hideDelay:300,cookieExpires:365,debug:false,clickClose:true,animationDuration:800,animationEasing:"linear",gaTracking:false,shortkeys:true},b=false,d=false,i=false,c=0,a=[],j=[],k=[],g=h("<div />").addClass("smartmodal-overlay").attr("id","smartmodal-overlay").css("display","none"),e={init:function(){if(f.gaTracking){if(typeof(_gaq)!=="undefined"){i=true}else{if(f.debug){console.log("GA not loaded. Tracking disabled.")}}}if(window.localStorage){b=true}else{if(f.debug){console.log("Web storage not supported. Using jQuery.cookie plugin.")}}if(h.cookie){d=true}else{if(f.debug){console.log("jQuery.cookie plugin not loaded. Cookies have been disabled.")}}e.countModals();e.setupModals();e.eventHandler()},showModal:function(l){var t=h("#"+l),p=false,q=f.animationEasing,o=f.animationDuration,n,r,s=true,m=f.cookieExpires;if(!t.length){if(f.debug){console.log("#"+l+" not found.")}return false}t.addClass("smartmodal-modal");e.positionModal(l);if(!h("#smartmodal-overlay").length){h("body").append(g)}if(t.data("animation")){if(t.data("animation").indexOf("|")>=0){r=t.data("animation").split("|");if(r[0]){n=r[0]}if(r[1]){q=r[1]}if(r[2]){o=parseInt(r[2],10)}}else{n=t.data("animation")}p=true;e.positionModal(l,n)}g.fadeIn(f.overlayDelay);if(p){t.show().animate({left:e.calculatePos(t,"left"),top:e.calculatePos(t,"top")},{duration:o,specialEasing:{top:q}})}else{t.fadeIn(f.overlayDelay)}if(i&&t.data("name")){e.gaTrackEvent("jQuery.smartModal",t.data("name"),"Opened")}if(t.data("time")){if(t.data("close")&&t.data("close")==="manual"){s=false;h(".close",t).hide()}if(s){a[l]=window.setTimeout(function(){var u=false;if(t.hasClass("sticky")){t.removeClass("sticky");u=true}e.closeModal(l);if(u){t.addClass("sticky")}},(t.data("time")*1000))}if(h(".sec",t).length){h(".sec",t).text(t.data("time"));j[l]=window.setInterval(function(){var u=parseInt(h(".sec",t).text(),10)-1;if(u>=0){h(".sec",t).text(u)}else{if(!s&&h(".close",t).is(":hidden")){if(t.hasClass("sticky")&&t.data("time")){t.removeClass("sticky").addClass("wasSticky")}h(".close",t).show()}window.clearInterval(j[l])}},1000)}}if(t.hasClass("once")){if(b){localStorage["smartModal-"+l]="shown"}else{if(d){if(t.data("expires")){m=t.data("expires")}h.cookie("smartModal-"+l,"shown",{path:"/",expires:m})}}}},closeModal:function(m){if(h("#"+m).length){var l=h("#"+m);if(!l.hasClass("sticky")){if(l.hasClass("wasSticky")){l.removeClass("wasSticky").addClass("sticky")}if(j[m]){window.clearInterval(j[m])}if(a[m]){window.clearTimeout(a[m])}l.fadeOut(f.hideDelay,function(){if(!h(".smartmodal-modal:visible").length){e.removeOverlay()}});if(i&&l.data("name")){e.gaTrackEvent("jQuery.smartModal",l.data("name"),"Closed")}}}},removeOverlay:function(){if(h("#smartmodal-overlay").length){h("#smartmodal-overlay").fadeOut(f.hideDelay,function(){h(this).remove()})}},positionModal:function(n,m){if(n){if(h("#"+n).length){var l=h("#"+n);if(m){switch(m){case"top":l.css({top:-(l.height()),left:e.calculatePos(l,"left")});break;case"bottom":l.css({top:h(window).height()+l.height(),left:e.calculatePos(l,"left")});break;case"left":l.css({top:e.calculatePos(l,"top"),left:-(l.width())});break;case"right":l.css({top:e.calculatePos(l,"top"),left:h(window).width()+l.width()});break}}else{l.css({top:e.calculatePos(l,"top"),left:e.calculatePos(l,"left")})}}}},calculatePos:function(o,q){var n=h(window).width(),m=h(window).height(),p=o.width(),l=o.height();switch(q){case"left":return(n-p)/2;case"top":return(m-l)/2}},countModals:function(){c=h(".smartmodal").length;if(f.debug){console.log(c+" modals found.")}},eventHandler:function(){if(f.shortkeys){h(document).keyup(function(l){if(l.keyCode===27){h.each(h(".smartmodal-modal"),function(){if(!h(this).hasClass("sticky")){var m=h(this).attr("id");e.closeModal(m)}})}})}h(".smartmodal .close").bind("click",function(){var l=h(this).closest(".smartmodal").attr("id");e.closeModal(l)});h(window).resize(function(){h.each(h(".smartmodal"),function(){var l=h(this).attr("id");e.positionModal(l)})});if(f.clickClose){h("body").delegate("#smartmodal-overlay","click",function(l){l.preventDefault();h.each(h(".smartmodal-modal"),function(){e.closeModal(h(this).attr("id"))})})}},setupModals:function(){h(".smartmodal").each(function(){var m=h(this),o=true,n,l;if(!m.attr("id")){while(o){l="SM-"+Math.floor((Math.random()*c)+1);if(!h("#"+l).length){m.attr("id",l);o=false}}}n=m.attr("id");if(h.inArray(n,k)>-1){if(f.debug){console.log(" Multiple #"+n)}}k.push(n);m.hide();if(m.hasClass("once")){if(b){if(localStorage["smartModal-"+n]==="shown"){m=false;e.countModals()}}else{if(d){if(h.cookie("smartModal-"+n)==="shown"){m=false;e.countModals()}}}}if(m){if(m.hasClass("auto")){if(m.data("wait")){setTimeout(function(){e.showModal(n)},(m.data("wait")*1000))}else{e.showModal(n)}}}if(h("."+n).length){h("."+n).bind("click",function(p){p.preventDefault();e.showModal(n)})}})},gaTrackEvent:function(m,o,l,n){if(i){_gaq.push(["_trackEvent",m,o,l,n])}}};h.smartModal=function(l,m){if(typeof l==="object"){f=h.extend(f,l);e.init()}else{if(typeof l==="string"&&typeof m==="string"){switch(l){case"show":e.showModal(m);break;case"hide":e.closeModal(m);break;default:if(f.debug){console.log(l+"not valid.")}break}}else{if(typeof l==="string"&&typeof m==="object"){switch(l){case"init":if(m){f=h.extend(f,m)}e.init();break;case"settings":f=h.extend(f,m);break;default:if(f.debug){console.log(l+" not valid.")}break}}else{e.init()}}}}}(jQuery));