-
Notifications
You must be signed in to change notification settings - Fork 9
/
jQuery.smartWebBanner.pre-1.7.min.js
7 lines (7 loc) · 5.17 KB
/
jQuery.smartWebBanner.pre-1.7.min.js
1
2
3
4
5
6
7
/*!
* jQuery Smart Web App Banner (Add to Home Screen)
* Copyright (c) 2014 Kurt Zenisek @ kzeni.com
* Version: 1.4-legacy (24-MAY-2014)
* Requires: jQuery v1.3.2 until v1.7 (newer version is available for jQuery 1.7+ that uses on() instead of live() [which was removed])
*/
;(function($){$.fn.smartWebBanner=function(options){var iPad=navigator.userAgent.match(/iPad/i)!=null;var iPhone=navigator.userAgent.match(/iPhone/i)!=null;var Safari=(/Safari/i).test(navigator.appVersion)&&!(/CriOS/i).test(navigator.appVersion);var standalone=navigator.standalone;function iOSversion(){if(iPhone||iPad){var v=(navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);return[parseInt(v[1],10),parseInt(v[2],10),parseInt(v[3]||0,10)]}else{return false}}var ver=iOSversion();if(ver[0]<=6)var iOS7=false;else var iOS7=true;var origHtmlMargin=parseFloat($('html').css('margin-top'));var bannerHeight;var originalTitle=document.title;var originalURL=window.location.href;if(typeof options=='string'){bannerHeight=$('#smartWebBanner').height();if(typeof opts=='undefined')var opts=$.fn.smartWebBanner.defaults;switch(options){case'show':if(!$('#smartWebBanner').hasClass('shown')){showBanner()}return false;case'hide':if($('#smartWebBanner').hasClass('shown')){origHtmlMargin=origHtmlMargin-bannerHeight;closeBanner()}return false}}else{var opts=$.extend({},$.fn.smartWebBanner.defaults,options)}if(opts.autoApp&&$('meta[name="apple-mobile-web-app-capable"]').length==0)$('head').append('<meta name="apple-mobile-web-app-capable" content="yes" />');function createBanner(){$('body').append('<div id="smartWebBanner"><a href="#" id="swb-close">X</a><a href="#" id="swb-icon"></a><div id="swb-info"><strong>'+opts.title+'</strong><span>'+opts.author+'</span></div><a href="#" id="swb-save">Save</a></div><div id="swb-instructions">Tap <span class="icon"></span> and then <strong>Add to Home Screen.</strong><div class="arrow"></div></div>');if(iPad)$('#smartWebBanner,#swb-instructions').addClass('ipad');if(!iPad&&!iPhone)$('#swb-instructions').html('<strong>It appears this isn\'t an iOS device.</strong> This is a preview of the iPhone popup design though.');if(opts.showFree)$('#smartWebBanner').addClass('free');if(opts.theme=='auto'){if(iOS7)$('#smartWebBanner,#swb-instructions').addClass('ios7');else $('#smartWebBanner,#swb-instructions').addClass('ios6')}if(opts.theme=='iOS 7')$('#smartWebBanner,#swb-instructions').addClass('ios7');if(opts.theme=='iOS 6')$('#smartWebBanner,#swb-instructions').addClass('ios6');if(opts.theme=='dark')$('#smartWebBanner,#swb-instructions').addClass('dark');if(opts.useIcon){if($('link[rel="apple-touch-icon-precomposed"]').length>0){iconURL=$('link[rel="apple-touch-icon-precomposed"]').attr('href')}else if($('link[rel="apple-touch-icon"]').length>0){iconURL=$('link[rel="apple-touch-icon"]').attr('href');if(opts.iconGloss!=false)$('#swb-icon').addClass('gloss')}if(opts.iconGloss==true)$('#swb-icon').addClass('gloss');if(opts.iconOverwrite!='')iconURL=opts.iconOverwrite;$('#swb-icon').css('background-image','url('+iconURL+')')}if(!opts.useIcon||!iconURL){$('#smartWebBanner').addClass('no-icon')}bannerHeight=$('#smartWebBanner').height()}function showBanner(){$('#smartWebBanner').stop().animate({top:0},opts.speedIn).addClass('shown');$('html').animate({marginTop:origHtmlMargin+bannerHeight},opts.speedIn);$('#swb-close').live('click',function(){closeBanner();return false});$('#swb-save,#swb-icon').live('click',function(){showInstructions();return false})}function closeBanner(){$('#smartWebBanner').stop().animate({top:-(bannerHeight+(bannerHeight/3))},opts.speedOut).removeClass('shown');$('html').animate({marginTop:origHtmlMargin},opts.speedOut);hideInstructions();setCookie('swb-closed','true',opts.daysHidden)}function showInstructions(){$('#swb-instructions').fadeIn(opts.popupSpeedIn);if(opts.titleSwap)document.title=opts.title;if(opts.url)history.replaceState(null,null,opts.url);if(opts.popupDuration!=0)setTimeout(function(){hideInstructions()},opts.popupDuration);setCookie('swb-saved','true',opts.daysReminder)}function hideInstructions(){$('#swb-instructions').fadeOut(opts.popupSpeedOut);if(opts.titleSwap)setTimeout(function(){document.title=originalTitle},12000);if(opts.url)setTimeout(function(){history.replaceState(null,null,originalURL)},12000)}function setCookie(name,value,exdays){var exdate=new Date();exdate.setDate(exdate.getDate()+exdays);var value=escape(value)+((exdays==null)?'':'; expires='+exdate.toUTCString());document.cookie=name+'='+value+'; path=/;'}function getCookie(name){var i,x,y,ARRcookies=document.cookie.split(";");for(i=0;i<ARRcookies.length;i++){x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);x=x.replace(/^\s+|\s+$/g,"");if(x==name){return unescape(y)}}}if(opts.debug||((iPhone||iPad)&&Safari&&!standalone&&typeof getCookie('swb-closed')=='undefined'&&typeof getCookie('swb-saved')=='undefined')){createBanner();showBanner()}};$.fn.smartWebBanner.defaults={title:'Web App',titleSwap:true,url:'',author:'Save to Home Screen',speedIn:300,speedOut:400,useIcon:true,iconOverwrite:'',iconGloss:'auto',showFree:true,daysHidden:15,daysReminder:90,popupDuration:6000,popupSpeedIn:200,popupSpeedOut:900,theme:'auto',autoApp:false,debug:false}})(jQuery);