forked from carlsednaoui/ouibounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ouibounce.js
117 lines (93 loc) · 3.41 KB
/
ouibounce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory(require,exports,module);
} else {
root.ouibounce = factory();
}
}(this, function(require,exports,module) {
return function ouibounce(el, config) {
var config = config || {},
aggressive = config.aggressive || false,
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
callback = config.callback || function() {},
cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '',
cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '',
sitewide = config.sitewide === true ? ';path=/' : '',
_html = document.getElementsByTagName('html')[0];
function setDefault(_property, _default) {
return typeof _property === 'undefined' ? _default : _property;
}
function setDefaultCookieExpire(days) {
// transform days to milliseconds
var ms = days*24*60*60*1000;
var date = new Date();
date.setTime(date.getTime() + ms);
return "; expires=" + date.toGMTString();
}
setTimeout(attachOuiBounce, timer);
function attachOuiBounce() {
_html.addEventListener('mouseleave', handleMouseleave);
_html.addEventListener('keydown', handleKeydown);
}
function handleMouseleave(e) {
if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return;
fire();
callback();
}
var disableKeydown = false;
function handleKeydown(e) {
if (disableKeydown || checkCookieValue('viewedOuibounceModal', 'true') && !aggressive) return;
else if(!e.metaKey || e.keyCode != 76) return;
disableKeydown = true;
fire();
callback();
}
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[cookieName] === value;
}
function fire() {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
if (el) el.style.display = 'block';
disable();
}
function disable(options) {
var options = options || {};
// you can pass a specific cookie expiration when using the OuiBounce API
// ex: _ouiBounce.disable({ cookieExpire: 5 });
if (typeof options.cookieExpire !== 'undefined') {
cookieExpire = setDefaultCookieExpire(options.cookieExpire);
}
// you can pass use sitewide cookies too
// ex: _ouiBounce.disable({ cookieExpire: 5, sitewide: true });
if (options.sitewide === true) {
sitewide = ';path=/';
}
// you can pass a domain string when the cookie should be read subdomain-wise
// ex: _ouiBounce.disable({ cookieDomain: '.example.com' });
if (typeof options.cookieDomain !== 'undefined') {
cookieDomain = ';domain=' + options.cookieDomain;
}
document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide;
// remove listeners
_html.removeEventListener('mouseleave', handleMouseleave);
_html.removeEventListener('keydown', handleKeydown);
}
return {
fire: fire,
disable: disable
};
}
;
}));