From 51a70c649f700aa2e243eafc0fbe4b42cf640f1a Mon Sep 17 00:00:00 2001 From: dim Date: Fri, 14 Apr 2017 10:15:22 +0200 Subject: [PATCH 1/3] Adding a precondition callback in options --- build/ouibounce.js | 5 ++++- build/ouibounce.min.js | 2 +- source/ouibounce.js | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/build/ouibounce.js b/build/ouibounce.js index b37b96a..7d73da0 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -24,7 +24,8 @@ return function ouibounce(el, custom_config) { cookieName = config.cookieName ? config.cookieName : 'viewedOuibounceModal', sitewide = config.sitewide === true ? ';path=/' : '', _delayTimer = null, - _html = document.documentElement; + _html = document.documentElement, + preConditionCallback = config.precondition; function setDefault(_property, _default) { return typeof _property === 'undefined' ? _default : _property; @@ -96,6 +97,8 @@ return function ouibounce(el, custom_config) { function fire() { if (isDisabled()) { return; } + if (preConditionCallback && typeof preConditionCallback === 'function' && !preConditionCallback()) { return; } + if (el) { el.style.display = 'block'; } callback(); diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index cd43043..6217061 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()||(L.addEventListener("mouseleave",u),L.addEventListener("mouseenter",r),L.addEventListener("keydown",c))}function u(e){e.clientY>v||(D=setTimeout(f,y))}function r(){D&&(clearTimeout(D),D=null)}function c(e){h||e.metaKey&&76===e.keyCode&&(h=!0,D=setTimeout(f,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")&&!p}function f(){s()||(!g||"function"!=typeof g||g())&&(e&&(e.style.display="block"),E(),m())}function m(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||{},p=l.aggressive||!1,v=o(l.sensitivity,20),k=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,g=l.precondition;setTimeout(t,k);var h=!1;return{fire:f,disable:m,isDisabled:s}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index 1bb73dd..21c3f78 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -12,7 +12,8 @@ function ouibounce(el, custom_config) { cookieName = config.cookieName ? config.cookieName : 'viewedOuibounceModal', sitewide = config.sitewide === true ? ';path=/' : '', _delayTimer = null, - _html = document.documentElement; + _html = document.documentElement, + preConditionCallback = config.precondition; function setDefault(_property, _default) { return typeof _property === 'undefined' ? _default : _property; @@ -84,6 +85,8 @@ function ouibounce(el, custom_config) { function fire() { if (isDisabled()) { return; } + if (preConditionCallback && typeof preConditionCallback === 'function' && !preConditionCallback()) { return; } + if (el) { el.style.display = 'block'; } callback(); From e475b8fae6d481ee0defc4416bf45642f0c46d77 Mon Sep 17 00:00:00 2001 From: Dimitri Gigot Date: Fri, 14 Apr 2017 10:28:18 +0200 Subject: [PATCH 2/3] Adding documentation for precondition Adding documentation for the precondition feature i just add. --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index ef59fa2..648c1fe 100644 --- a/README.md +++ b/README.md @@ -82,12 +82,14 @@ Ouibounce offers a few options, such as: - [Aggressive mode](#aggressive-mode) - [Timer](#set-a-min-time-before-ouibounce-fires) - [Delay](#delay) +- [Precondition](#precondition) - [Callback](#callback) - [Cookie expiration](#cookie-expiration) - [Cookie domain](#cookie-domain) - [Cookie name](#cookie-name) - [Sitewide cookie](#sitewide-cookie) - [Chaining options](#chaining-options) +- [Precondition](#precondition) ##### Sensitivity Ouibounce fires when the mouse cursor moves close to (or passes) the top of the viewport. You can define how far the mouse has to be before Ouibounce fires. The higher value, the more sensitive, and the more quickly the event will fire. _Defaults to 20._ @@ -125,6 +127,19 @@ _Example:_ ouibounce(document.getElementById('ouibounce-modal'), { delay: 100 }); ``` +##### Precondition +You can add a precondition, which is a function that will run once Ouibounce has been triggered, by using the `precondition` option. +The precondition allow you to prevent Ouibouce to show the modal if you don't want to. + +_Example:_ +```js +ouibounce(document.getElementById('ouibounce-modal'), { precondition: function() { + console.log('Should Ouibounce be fired?'); } + return true; // Ouibounce is not fired if false +}); +``` + + ##### Callback You can add a callback, which is a function that will run once Ouibounce has been triggered, by using the `callback` option. From db050c134d4e92b88f983c1f969f03c40d49fff3 Mon Sep 17 00:00:00 2001 From: Dimitri Gigot Date: Fri, 14 Apr 2017 10:30:17 +0200 Subject: [PATCH 3/3] Remove on of the link in options --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 648c1fe..12f94cf 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,6 @@ Ouibounce offers a few options, such as: - [Cookie name](#cookie-name) - [Sitewide cookie](#sitewide-cookie) - [Chaining options](#chaining-options) -- [Precondition](#precondition) ##### Sensitivity Ouibounce fires when the mouse cursor moves close to (or passes) the top of the viewport. You can define how far the mouse has to be before Ouibounce fires. The higher value, the more sensitive, and the more quickly the event will fire. _Defaults to 20._