-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwidget.js
213 lines (170 loc) · 7.25 KB
/
widget.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
_ _ __
| |_ __ _ ___| | __/ _| ___ _ __ ___ ___
| __/ _` / __| |/ / |_ / _ \| '__/ __/ _ \
| || (_| \__ \ <| _| (_) | | | (_| __/
\__\__,_|___/_|\_\_| \___/|_| \___\___|
===============================================================================
A generic widget include script which we will use to show further campaigns etc
<[email protected]> or http://taskforce.is for support
===============================================================================
@source: https://raw.github.com/tfrce/project-megaphone/gh-pages/widget.js
@licstart The following is the entire license notice for the
JavaScript code in this page.
Copyright (C) 2013 Taskforce <http://taskforce.is>
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU Affero
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any
later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU AGPL version 3 section 7, you may
distribute non-source (e.g., minimized or compacted) forms of that
code without the copy of the GNU AGPL normally required by section
4, provided you include this license notice and a URL through which
recipients can access the Corresponding Source.
@licend The above is the entire license notice for the JavaScript
code in this page.
*/
// Wrap widget in function to protect scope
var _tfrce_config = (typeof tfrce_config !== 'undefined') ? tfrce_config : {};
(function(window, widget_config){
// Do configuration
widget_config.show_style = widget_config.show_style || 'default';
widget_config.debug = widget_config.debug || false;
widget_config.disableDate = widget_config.disableDate || false;
widget_config.campaign = widget_config.campaign || 'thedaywefightback';
widget_config.cookieTimeout = widget_config.cookieTimeout || 172800;
// Setup
var active_campaign;
var ASSET_URL, COOKIE_TIMEOUT;
if(widget_config.debug) {
ASSET_URL = '../thedaywefightback/';
COOKIE_TIMEOUT = 20;
} else {
ASSET_URL = '//d1ux67szpr7bp0.cloudfront.net/project-megaphone/thedaywefightback/';
COOKIE_TIMEOUT = widget_config.cookieTimeout;
}
// Cookie helpers, taken from w3schools
function setCookie(c_name,value,seconds) {
var exdate = new Date(new Date().getTime() + seconds*1000);
var c_value=escape(value) + ((seconds==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) { c_start = c_value.indexOf(c_name + "="); };
if (c_start == -1) {
c_value = null;
} else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) { c_end = c_value.length; }
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
// Define checks
var checks = {
betweenDate: function(startDate, endDate) {
var startEpoch = startDate.getTime();
var endEpoch = endDate.getTime();
var currentEpoch = new Date().getTime();
if(currentEpoch > startEpoch && currentEpoch < endEpoch) {
return true;
} else {
return false;
}
},
isMobile: function() {
var ismobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i);
return ismobile ? false : true;
},
hasSeenCampaign: function (cookieName) {
var cookie = getCookie(cookieName)
if(cookie === null) {
return false;
} else {
return true
}
}
}
// Define campaigns
var campaign = {
thedaywefightback: {
cookieName: 'thedaywefightback_hasseen',
startDate: new Date(2013, 9, 15, 0),
endDate: new Date(2013, 9, 26, 12),
hide: function (el, callback) {
document.body.removeChild(el);
setCookie(active_campaign.cookieName, 'true', COOKIE_TIMEOUT);
if(callback) { callback(); };
},
styles: {
default: {
campaign_container: 'position:fixed;width:100%;bottom:0;left:0;z-index:100000; padding: 0 20px;-webkit-box-sizing: border-box; -moz-box-sizing: border-box;',
iframe_container: 'position: relative; height: 400px; max-width:1201px; margin: 0px auto 8px auto; background: #444;border-radius: 10px;',
iframe: 'width: 100%;height: 100%;border: 0;margin:0;padding:0;border-radius: 10px;',
closeButton: 'border: 0;height: 28px;width: 28px;cursor: pointer;position: absolute;top:33px;right:20px;background: url("' + ASSET_URL +'images/close-button.png");'
}
},
show: function () {
var style = active_campaign.styles[active_campaign.config.show_style]
if(style.overlay) {
var overlay = document.createElement('div');
overlay.style.cssText = style.overlay;
document.body.appendChild(overlay);
}
// Create a container
var campaign_container = document.createElement('div');
campaign_container.style.cssText = style.campaign_container;
// Create a container for the iframe so we can do padding and border-radius properly
var iframe_container = document.createElement('div');
iframe_container.style.cssText = style.iframe_container;
// Append Iframe and campaign container to document
campaign_container.appendChild(iframe_container);
document.body.appendChild(campaign_container);
var iframe = document.createElement('iframe');
iframe.style.cssText = style.iframe;
// Set the source of the iframe to the configured show_style type
iframe.src = ASSET_URL + active_campaign.config.show_style + '.html';
iframe_container.appendChild(iframe);
// Setup a close button
var closeButton = document.createElement('button');
closeButton.style.cssText = style.closeButton;
iframe_container.appendChild(closeButton);
closeButton.onclick = function() {
active_campaign.hide(campaign_container);
if(style.overlay) {
document.body.removeChild(overlay);
}
}
},
init: function (config) {
active_campaign.config = config;
// Check cookie for this campaign
if(checks.hasSeenCampaign(active_campaign.cookieName)) {
//return false;
}
// Check between date
if(!checks.betweenDate(active_campaign.startDate, active_campaign.endDate) && !active_campaign.config.disableDate) {
//return false;
}
// Check if is mobile
if(!checks.isMobile()){
return false;
}
active_campaign.show();
}
}
}
// Load campaign if exist
if(typeof campaign[widget_config.campaign] !== 'undefined') {
active_campaign = campaign[widget_config.campaign];
active_campaign.init(widget_config);
} else {
return false;
}
})(window, _tfrce_config);