forked from wypb/Adi.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.adi.js
163 lines (119 loc) · 2.93 KB
/
jquery.adi.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
158
159
160
161
162
163
/**
* ==============
* Adi.js
* ==============
*
* @author: Marius Balaj
* @website: http://mariusbalaj.com
* @contact: balajmarius93@gmail.com
*
*
*/
/**
* Check for jQuery
*/
if (typeof jQuery === 'undefined') {
throw new Error('Make sure jQuery is included before the jquery.adi.js');
}
;(function($) {
'use strict';
var Adi;
$.adi = function(args) {
/**
* Merge defaults with user options
*/
var options = $.extend({}, Adi.defaults, args);
return new Adi(options);
};
/**
* Constructor
*/
Adi = function(args) {
/**
* Merge this with user options
*/
$.extend(this, args);
if (this._check()) {
this._init();
this.active();
}
if(!this._check()) {
this.inactive();
}
};
/**
* Check for $.adblock
*/
Adi.prototype._check = function() {
return $.adblock === undefined
};
/**
* Start plugin
*/
Adi.prototype._init = function() {
this._append();
};
/**
* Set template
*/
Adi.prototype._setTemplate = function(title, content) {
return '<div class="jquery-adi">' +
'<div class="jquery-adi_content">' +
'<button class="jquery-adi_close"></button>' +
'<h2>' + title + '</h2>' +
'<p>' + content + '</p>' +
'</div>' +
'</div>';
};
/**
* Append html
*/
Adi.prototype._append = function(callback) {
this.$el = $(this._setTemplate(this.title, this.content)).appendTo($(document.body)).addClass(this.theme);
this._show();
};
/**
* Show modal
*/
Adi.prototype._show = function() {
var that = this;
this.$el.show();
this._center();
this._controls();
this.onOpen(this.$el);
};
/**
* Modal controls
*/
Adi.prototype._controls = function() {
var that = this;
function close() {
that.$el.hide();
that.onClose(that.$el);
}
this.$el.on('click', '.jquery-adi_close', close);
$(document).on('keyup', function(e) {
if (e.keyCode == 27)
close();
});
};
/**
* Center modal
*/
Adi.prototype._center = function() {
var $modal = this.$el.find('.jquery-adi_content');
$modal.css('margin-top', -Math.abs($modal.outerHeight() / 2));
};
/**
* Defaults
*/
Adi.defaults = {
title: 'Adblock detected!',
content: 'We noticed that you may have an Ad Blocker turned on. Please be aware that our site is best experienced with Ad Blockers turned off.',
theme: 'light',
onOpen: function() {},
onClose: function() {},
active: function() {},
inactive: function() {}
};
})(jQuery);