This repository was archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.leadbox.js
157 lines (136 loc) · 4.94 KB
/
jquery.leadbox.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* LeadBox for jQuery (v1.4+)
* A simple modal plugin for lead capture purposes.
* For more details, please visit: https://github.com/zrod/leadbox
*
* Author: Rodrigo Z. Arthuso | arthuso.com
*
* Version 0.2 (12/15/2011)
* - Code cleanup, dropped support for IE6.
*
* Dual licensed under MIT and GPL.
*/
(function ($) {
jQuery.fn.leadbox = function(options) {
return this.each(function () {
if (options) {
options.interval = (isNaN(options.interval) ? $.leadbox.settings.interval : options.interval);
options.cookie_time = (isNaN(options.cookie_time) ? $.leadbox.settings.cookie_time : options.cookie_time);
$.extend($.leadbox.settings, options);
}
var actionClassElements,
el_c = $('#leadbox_container'),
el = this,
pos,
t;
if (el_c.length != 1 && $(el).length == 1 && $.leadbox.readCookie($.leadbox.settings.cookie) === false) {
$(el).css({'width': $.leadbox.settings.width, 'height': $.leadbox.settings.height}).wrap($('<div id="leadbox_container"></div>'));
el_c = $(el).parent();
// Load external content
if ($.leadbox.settings.remote && $.leadbox.settings.remote.indexOf('http://') != -1) {
$(el).html('<iframe src="' + $.leadbox.settings.remote + '" width="100%" height="100%" frameborder="0" scrolling="auto">Your browser does not support iframes</iframe>');
}
// Prepend close button
if ($.leadbox.settings.closeButton !== '') {
$(el).prepend('<div class="leadbox_close"><a href="javascript:$.leadbox.close();">' + $.leadbox.settings.closeButton + '</a></div>');
}
t = window.setTimeout(function() {
// Retrieve element's position
pos = $.leadbox.calcPosition(el);
// Center and display element
$(el_c).fadeIn();
$(el).css({left: pos.left, top: pos.top}).fadeIn('slow').click(function(e){ e.stopPropagation(); });
// Set a cookie so that it won't show up on every single request
$.leadbox.setCookie($.leadbox.settings.cookie, '1', $.leadbox.settings.cookie_time, $.leadbox.settings.cookie_path);
// Run callback
if (typeof($.leadbox.settings.onLoad) === 'function') $.leadbox.settings.onLoad();
}, $.leadbox.settings.interval);
// Center element after a resize event
$(window).resize(function() {
pos = $.leadbox.calcPosition(el);
$(el).css({left: pos.left, top: pos.top});
});
// Actions callback
if ($.leadbox.settings.actionClass !== '' && typeof($.leadbox.settings.onAction) === 'function') {
// Scan for elements containing the class defined in the settings
actionClassElements = $('#leadbox_container .' + $.leadbox.settings.actionClass);
if (actionClassElements.length > 0) {
$.each(actionClassElements, function(k, v) {
switch (v.tagName.toLowerCase()) {
case 'form':
$(v).submit(function() {
$.leadbox.settings.onAction();
return true;
});
break;
case 'a':
$(v).click(function() {
$.leadbox.settings.onAction();
});
break;
}
});
}
}
// Close leadbox on clicks outside its boundaries
if ($.leadbox.settings.closeOnExternalClick) {
$('body').not(el).bind('click', function(){
$.leadbox.close();
});
}
}
});
};
$.leadbox = {
settings: {
'interval': 10000,
'cookie': 'leadBox',
'cookie_time': 7,
'cookie_path': '/',
'width': 500,
'height': 220,
'closeOnExternalClick': false,
'closeButton': '',
'remote': '',
'actionClass': 'leadAction',
'onLoad': '',
'onAction': '',
'onClose': ''
},
calcPosition: function(el) {
var winWidth = $(window).width(),
winHeight = $(window).height(),
elWidth = $(el).outerWidth(),
elHeight = $(el).outerHeight(),
pos_left = Math.abs((winWidth / 2) - (elWidth / 2)),
pos_top = Math.abs((winHeight / 2) - (elHeight / 2));
return {left: pos_left, top: pos_top};
},
readCookie: function(n) {
if (document.cookie.length > 0) {
var cstrt = document.cookie.indexOf(n + '='),
cend;
if (cstrt != -1) {
cstrt = cstrt + n.length + 1;
cend = document.cookie.indexOf(';', cstrt);
if (cend == -1) cend = document.cookie.length;
return unescape(document.cookie.substring(cstrt, cend));
}
}
return false;
},
setCookie: function(n, v, ed, path) {
var vdate = new Date();
vdate.setDate(vdate.getDate() + ed);
document.cookie = n + '=' + v + ((ed === '' || ed === 0) ? '' : ';expires=' + vdate.toUTCString()) + ';path=' + path;
},
close: function () {
var el = $('#leadbox_container'),
child = el.find(':first-child').attr('id');
el.hide();
$('body').not(child).unbind('click');
// Run callback
if (typeof($.leadbox.settings.onClose) === 'function') $.leadbox.settings.onClose();
}
};
})(jQuery);