Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "precondition" option #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ 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)
Expand Down Expand Up @@ -125,6 +126,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.

Expand Down
5 changes: 4 additions & 1 deletion build/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
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.

5 changes: 4 additions & 1 deletion source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down