forked from JillElaine/jquery-idleTimeout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-idleTimeout-iframes.min.js
1 lines (1 loc) · 4.57 KB
/
jquery-idleTimeout-iframes.min.js
1
(function($){$.fn.idleTimeout=function(options){var defaults={idleTimeLimit:1200000,dialogDisplayLimit:180000,redirectUrl:"/logout",customCallback:false,activityEvents:"click keypress scroll wheel mousewheel mousemove",enableDialog:true,dialogTitle:"Session Expiration Warning",dialogText:"Because you have been inactive, your session is about to expire.",sessionKeepAliveTimer:600000},opts=$.extend(defaults,options),checkHeartbeat=2000,origTitle=document.title,sessionKeepAliveUrl=window.location.href,keepSessionAlive,activityDetector,idleTimer,remainingTimer,checkIdleTimeout,idleTimerLastActivity,startIdleTimer,stopIdleTimer,openWarningDialog,dialogTimer,checkDialogTimeout,startDialogTimer,stopDialogTimer,isDialogOpen,destroyWarningDialog,countdownDisplay,logoutUser,checkForIframes,includeIframes,attachEventIframe;keepSessionAlive=function(){if(opts.sessionKeepAliveTimer){var keepSession=function(){if(idleTimerLastActivity===store.get("idleTimerLastActivity")){$.get(sessionKeepAliveUrl);}};setInterval(keepSession,opts.sessionKeepAliveTimer);}};activityDetector=function(){$("body").on(opts.activityEvents,function(){if(isDialogOpen()!==true){startIdleTimer();}});};checkIdleTimeout=function(){var timeNow=$.now(),timeIdleTimeout=(store.get("idleTimerLastActivity")+opts.idleTimeLimit);if(timeNow>timeIdleTimeout){if(isDialogOpen()!==true){if(opts.enableDialog){openWarningDialog();}startDialogTimer();}}else{if(store.get("idleTimerLoggedOut")===true){logoutUser();}else{if(isDialogOpen()===true){destroyWarningDialog();stopDialogTimer();}}}};startIdleTimer=function(){stopIdleTimer();idleTimerLastActivity=$.now();store.set("idleTimerLastActivity",idleTimerLastActivity);idleTimer=setInterval(checkIdleTimeout,checkHeartbeat);};stopIdleTimer=function(){clearInterval(idleTimer);};openWarningDialog=function(){var dialogContent="<div id='idletimer_warning_dialog'><p>"+opts.dialogText+"</p><p style='display:inline'>Time remaining: <div style='display:inline' id='countdownDisplay'></div></p></div>";$(dialogContent).dialog({buttons:{"Stay Logged In":function(){destroyWarningDialog();stopDialogTimer();startIdleTimer();},"Log Out Now":function(){logoutUser();}},closeOnEscape:false,modal:true,title:opts.dialogTitle});$(".ui-dialog-titlebar-close").css("display","none");countdownDisplay();document.title=opts.dialogTitle;};checkDialogTimeout=function(){var timeNow=$.now(),timeDialogTimeout=(store.get("idleTimerLastActivity")+opts.idleTimeLimit+opts.dialogDisplayLimit);if((timeNow>timeDialogTimeout)||(store.get("idleTimerLoggedOut")===true)){logoutUser();}};startDialogTimer=function(){dialogTimer=setInterval(checkDialogTimeout,checkHeartbeat);};stopDialogTimer=function(){clearInterval(dialogTimer);clearInterval(remainingTimer);};isDialogOpen=function(){var dialogOpen=$("#idletimer_warning_dialog").is(":visible");if(dialogOpen===true){return true;}return false;};destroyWarningDialog=function(){$(".ui-dialog-content").dialog("destroy").remove();document.title=origTitle;};countdownDisplay=function(){var dialogDisplaySeconds=opts.dialogDisplayLimit/1000,mins,secs;remainingTimer=setInterval(function(){mins=Math.floor(dialogDisplaySeconds/60);if(mins<10){mins="0"+mins;}secs=dialogDisplaySeconds-(mins*60);if(secs<10){secs="0"+secs;}$("#countdownDisplay").html(mins+":"+secs);dialogDisplaySeconds-=1;},1000);};logoutUser=function(){store.set("idleTimerLoggedOut",true);if(opts.customCallback){opts.customCallback();}if(opts.redirectUrl){window.location.href=opts.redirectUrl;}};checkForIframes=function(){var docReadyCheck,isDocReady;docReadyCheck=function(){if(document.readyState==="complete"){clearInterval(isDocReady);includeIframes();}};isDocReady=setInterval(docReadyCheck,1000);};includeIframes=function(){var foundIframes=document.getElementsByTagName("iframe"),index,iframeItem;if(foundIframes.length>0){for(index=0;index<foundIframes.length;index++){iframeItem=foundIframes.item(index);if(iframeItem.attachEvent){iframeItem.attachEvent("onload",attachEventIframe(index));}else{iframeItem.addEventListener("load",attachEventIframe(index),false);}}}};attachEventIframe=function(index){var iframe=$("iframe:eq("+index+")").contents().find("html");iframe.on(opts.activityEvents,function(event){$("body").trigger(event);});};return this.each(function(){if(store.enabled){idleTimerLastActivity=$.now();store.set("idleTimerLastActivity",idleTimerLastActivity);store.set("idleTimerLoggedOut",false);}else{alert('Please disable "Private Mode", or upgrade to a modern browser. Or perhaps a dependent file missing. Please see: https://github.com/marcuswestin/store.js');}activityDetector();keepSessionAlive();startIdleTimer();checkForIframes();});};}(jQuery));