From e6f5af65028c65183726d4346c00d1d150524238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rogier?= Date: Thu, 26 Nov 2015 16:40:59 +0100 Subject: [PATCH] Add canFire option Check if Ouibounce can be fired before opening triggering the callback --- README.md | 10 ++++++++++ build/ouibounce.js | 3 ++- build/ouibounce.min.js | 2 +- source/ouibounce.js | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ef59fa2..6a076a6 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Ouibounce offers a few options, such as: - [Timer](#set-a-min-time-before-ouibounce-fires) - [Delay](#delay) - [Callback](#callback) +- [CanFire](#canFire) - [Cookie expiration](#cookie-expiration) - [Cookie domain](#cookie-domain) - [Cookie name](#cookie-name) @@ -133,6 +134,15 @@ _Example:_ ouibounce(document.getElementById('ouibounce-modal'), { callback: function() { console.log('Ouibounce fired!'); } }); ``` +##### Canfire +You can add a callback with the `canFire` option, which will check if Ouibounce can be triggered. +By default, this callback returns true; + +_Example:_ +```js +ouibounce(document.getElementById('ouibounce-modal'), { canFire: function() { console.log("Actually, I don't want to fire Ouibounce!"); return false; } }); +``` + ##### Cookie expiration Ouibounce sets a cookie by default to prevent the modal from appearing more than once per user. You can add a cookie expiration (in days) using `cookieExpire` to adjust the time period before the modal will appear again for a user. By default, the cookie will expire at the end of the session, which for most browsers is when the browser is closed entirely. diff --git a/build/ouibounce.js b/build/ouibounce.js index b37b96a..2ad6e97 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -18,6 +18,7 @@ return function ouibounce(el, custom_config) { sensitivity = setDefault(config.sensitivity, 20), timer = setDefault(config.timer, 1000), delay = setDefault(config.delay, 0), + canFire = config.canFire || function() { return true; }, callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', @@ -94,7 +95,7 @@ return function ouibounce(el, custom_config) { // You can use ouibounce without passing an element // https://github.com/carlsednaoui/ouibounce/issues/30 function fire() { - if (isDisabled()) { return; } + if (isDisabled() || !canFire()) { return; } if (el) { el.style.display = 'block'; } diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index cd43043..c908ffc 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(e,n,o){return function(e,n){"use strict";function o(e,n){return"undefined"==typeof e?n:e}function i(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function t(){s()||(L.addEventListener("mouseleave",u),L.addEventListener("mouseenter",r),L.addEventListener("keydown",c))}function u(e){e.clientY>k||(D=setTimeout(m,y))}function r(){D&&(clearTimeout(D),D=null)}function c(e){g||e.metaKey&&76===e.keyCode&&(g=!0,D=setTimeout(m,y))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var i=e[o].split("=");n[i[0]]=i[1]}return n}function s(){return d(T,"true")&&!v}function m(){s()||(e&&(e.style.display="block"),E(),f())}function f(e){var n=e||{};"undefined"!=typeof n.cookieExpire&&(b=i(n.cookieExpire)),n.sitewide===!0&&(w=";path=/"),"undefined"!=typeof n.cookieDomain&&(x=";domain="+n.cookieDomain),"undefined"!=typeof n.cookieName&&(T=n.cookieName),document.cookie=T+"=true"+b+x+w,L.removeEventListener("mouseleave",u),L.removeEventListener("mouseenter",r),L.removeEventListener("keydown",c)}var l=n||{},v=l.aggressive||!1,k=o(l.sensitivity,20),p=o(l.timer,1e3),y=o(l.delay,0),E=l.callback||function(){},b=i(l.cookieExpire)||"",x=l.cookieDomain?";domain="+l.cookieDomain:"",T=l.cookieName?l.cookieName:"viewedOuibounceModal",w=l.sitewide===!0?";path=/":"",D=null,L=document.documentElement;setTimeout(t,p);var g=!1;return{fire:m,disable:f,isDisabled:s}}}); \ No newline at end of file +!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(e,n,o){return function(e,n){"use strict";function o(e,n){return"undefined"==typeof e?n:e}function i(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function t(){s()||(g.addEventListener("mouseleave",u),g.addEventListener("mouseenter",r),g.addEventListener("keydown",c))}function u(e){e.clientY>k||(L=setTimeout(m,y))}function r(){L&&(clearTimeout(L),L=null)}function c(e){h||e.metaKey&&76===e.keyCode&&(h=!0,L=setTimeout(m,y))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var i=e[o].split("=");n[i[0]]=i[1]}return n}function s(){return d(w,"true")&&!v}function m(){!s()&&E()&&(e&&(e.style.display="block"),b(),f())}function f(e){var n=e||{};"undefined"!=typeof n.cookieExpire&&(x=i(n.cookieExpire)),n.sitewide===!0&&(D=";path=/"),"undefined"!=typeof n.cookieDomain&&(T=";domain="+n.cookieDomain),"undefined"!=typeof n.cookieName&&(w=n.cookieName),document.cookie=w+"=true"+x+T+D,g.removeEventListener("mouseleave",u),g.removeEventListener("mouseenter",r),g.removeEventListener("keydown",c)}var l=n||{},v=l.aggressive||!1,k=o(l.sensitivity,20),p=o(l.timer,1e3),y=o(l.delay,0),E=l.canFire||function(){return!0},b=l.callback||function(){},x=i(l.cookieExpire)||"",T=l.cookieDomain?";domain="+l.cookieDomain:"",w=l.cookieName?l.cookieName:"viewedOuibounceModal",D=l.sitewide===!0?";path=/":"",L=null,g=document.documentElement;setTimeout(t,p);var h=!1;return{fire:m,disable:f,isDisabled:s}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index 1bb73dd..ebada62 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -6,6 +6,7 @@ function ouibounce(el, custom_config) { sensitivity = setDefault(config.sensitivity, 20), timer = setDefault(config.timer, 1000), delay = setDefault(config.delay, 0), + canFire = config.canFire || function() { return true; }, callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', @@ -82,7 +83,7 @@ function ouibounce(el, custom_config) { // You can use ouibounce without passing an element // https://github.com/carlsednaoui/ouibounce/issues/30 function fire() { - if (isDisabled()) { return; } + if (isDisabled() || !canFire()) { return; } if (el) { el.style.display = 'block'; }