diff --git a/build/ouibounce.js b/build/ouibounce.js index 2f47f7c..7c1ea8d 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -15,42 +15,53 @@ return function ouiBounce(el, config) { aggressive = config.aggressive || false, sensitivity = setDefault(config.sensitivity, 20), timer = setDefault(config.timer, 1000), - callback = config.callback || function() {}; - - setTimeout(attachOuiBounce.bind(el), timer); - + callback = config.callback || function() {}, + _html = document.getElementsByTagName('html')[0]; + function setDefault(_property, _default) { return typeof _property === 'undefined' ? _default : _property; } + setTimeout(attachOuiBounce, timer); function attachOuiBounce() { - var _this = this, - _html = document.getElementsByTagName('html')[0]; - _html.addEventListener('mouseout', handleMouseout); + } - function handleMouseout(e) { - if (e.clientY > sensitivity || (getCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; - _this.style.display = 'block'; - callback(); - - // set cookie and disable mouseout event - document.cookie = 'viewedOuibounceModal=true'; - _html.removeEventListener('mouseout', handleMouseout); - } + function handleMouseout(e) { + if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; + fire(); + callback(); } - function getCookieValue(k, v) { - // return cookies in an object + function checkCookieValue(cookieName, value) { + // cookies are separated by '; ' var cookies = document.cookie.split('; ').reduce(function(prev, curr) { + // split by '=' to get key, value pairs var el = curr.split('='); + + // add the cookie to fn object prev[el[0]] = el[1]; return prev; }, {}); - return cookies[k] === v; + return cookies[cookieName] === value; } + + function fire() { + el.style.display = 'block'; + disable(); + } + + function disable() { + document.cookie = 'viewedOuibounceModal=true'; + _html.removeEventListener('mouseout', handleMouseout); + } + + return { + fire: fire, + disable: disable + }; } ; diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index 2c50517..d62d137 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t(require,exports,module):e.ouiBounce=t()}(this,function(){return function(e,t){function n(e,t){return"undefined"==typeof e?t:e}function o(){function e(o){o.clientY>r||i("viewedOuibounceModal","true")&&!u||(t.style.display="block",d(),document.cookie="viewedOuibounceModal=true",n.removeEventListener("mouseout",e))}var t=this,n=document.getElementsByTagName("html")[0];n.addEventListener("mouseout",e)}function i(e,t){var n=document.cookie.split("; ").reduce(function(e,t){var n=t.split("=");return e[n[0]]=n[1],e},{});return n[e]===t}var t=t||{},u=t.aggressive||!1,r=n(t.sensitivity,20),c=n(t.timer,1e3),d=t.callback||function(){};setTimeout(o.bind(e),c)}}); \ No newline at end of file +!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t(require,exports,module):e.ouiBounce=t()}(this,function(){return function(e,t){function n(e,t){return"undefined"==typeof e?t:e}function o(){a.addEventListener("mouseout",i)}function i(e){e.clientY>f||u("viewedOuibounceModal","true")&&!d||(r(),l())}function u(e,t){var n=document.cookie.split("; ").reduce(function(e,t){var n=t.split("=");return e[n[0]]=n[1],e},{});return n[e]===t}function r(){e.style.display="block",c()}function c(){document.cookie="viewedOuibounceModal=true",a.removeEventListener("mouseout",i)}var t=t||{},d=t.aggressive||!1,f=n(t.sensitivity,20),s=n(t.timer,1e3),l=t.callback||function(){},a=document.getElementsByTagName("html")[0];return setTimeout(o,s),{fire:r,disable:c}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index 5206591..8c6c772 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -3,40 +3,51 @@ function ouiBounce(el, config) { aggressive = config.aggressive || false, sensitivity = setDefault(config.sensitivity, 20), timer = setDefault(config.timer, 1000), - callback = config.callback || function() {}; - - setTimeout(attachOuiBounce.bind(el), timer); - + callback = config.callback || function() {}, + _html = document.getElementsByTagName('html')[0]; + function setDefault(_property, _default) { return typeof _property === 'undefined' ? _default : _property; } + setTimeout(attachOuiBounce, timer); function attachOuiBounce() { - var _this = this, - _html = document.getElementsByTagName('html')[0]; - _html.addEventListener('mouseout', handleMouseout); + } - function handleMouseout(e) { - if (e.clientY > sensitivity || (getCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; - _this.style.display = 'block'; - callback(); - - // set cookie and disable mouseout event - document.cookie = 'viewedOuibounceModal=true'; - _html.removeEventListener('mouseout', handleMouseout); - } + function handleMouseout(e) { + if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; + fire(); + callback(); } - function getCookieValue(k, v) { - // return cookies in an object + function checkCookieValue(cookieName, value) { + // cookies are separated by '; ' var cookies = document.cookie.split('; ').reduce(function(prev, curr) { + // split by '=' to get key, value pairs var el = curr.split('='); + + // add the cookie to fn object prev[el[0]] = el[1]; return prev; }, {}); - return cookies[k] === v; + return cookies[cookieName] === value; } + + function fire() { + el.style.display = 'block'; + disable(); + } + + function disable() { + document.cookie = 'viewedOuibounceModal=true'; + _html.removeEventListener('mouseout', handleMouseout); + } + + return { + fire: fire, + disable: disable + }; } diff --git a/test/index.html b/test/index.html index 9483e8f..d0e05d4 100644 --- a/test/index.html +++ b/test/index.html @@ -59,7 +59,10 @@