Skip to content

Commit

Permalink
Nice refactor, close #11
Browse files Browse the repository at this point in the history
- Add public functions to fire and disable OuiBounce on demand
- Refactor code to use newly available functions
- Add comments to checkCookie fn
  • Loading branch information
carlsednaoui committed Mar 25, 2014
1 parent 9b3ee90 commit 3ec64f7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 40 deletions.
49 changes: 30 additions & 19 deletions build/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
;

Expand Down
2 changes: 1 addition & 1 deletion build/ouibounce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 30 additions & 19 deletions source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
5 changes: 4 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ <h3>This is a OuiBounce modal!</h3>
<!-- Example page JS -->
<!-- Used to fire the modal -->
<script>
ouiBounce(document.getElementById('ouibounce-modal'), {

// if you want to use the 'fire' or 'disable' fn,
// you need to save OuiBounce to an object
var _ouiBounce = ouiBounce(document.getElementById('ouibounce-modal'), {
aggressive: true,
timer: 0,
callback: function() { console.log('ouiBounce fired!'); }
Expand Down

0 comments on commit 3ec64f7

Please sign in to comment.