-
Notifications
You must be signed in to change notification settings - Fork 0
/
mads.js
213 lines (164 loc) · 6.25 KB
/
mads.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
/*jshint es5: true, browser: true, undef:true, unused:true, indent: 4 */
var TG = TG || {};
/***
This module allows you to integrate ads served by MADS based on the Javascript SDK
It does lazy loading and initialization of ads, so the page won't be blocked from
rendering the content.
It will try to display ads within 500 milliseconds (configurable).
http://developer.madsone.com/JavaScript_AdTags
***/
TG.mads = (function (doc, win) {
"use strict";
var mads_js = "mads-js-" + Date.now(),
mads_js_loaded = 0,
tries = 0,
options = {
max_tries: 10,
timeout: 500
};
// A simple extend function so we can mix objects
function extend(destination, source) {
for (var k in source) {
if (source.hasOwnProperty(k)) {
destination[k] = source[k];
}
}
return destination;
}
return {
/**
* Getter for the global options object
*/
get options() {
return options;
},
/**
* Setter for the global options object
* @param params {Object} will merge the object's values with the global options
* existing keys will be overridden, new keys will be added
*/
set options(params) {
if (typeof params !== "object") {
throw "params must be an object";
}
extend(options, params);
},
/**
* loaded
* Returns a boolean wether MADS Javascript has been succesfully loaded
*/
get loaded() {
return (doc.getElementById(mads_js) && mads_js_loaded) ? true : false;
},
/**
* @method reset
* Will reset the loading state of ads, useful if you want to retry
* Use a try/catch block around load
*/
reset: function () {
if (! this.loaded) {
mads_js_loaded = 0;
tries = 0;
}
},
/**
* @method load
* @param callback {Function} function to execute when MADS has been succesfully loaded
* throws an error if it can't be loaded after the configured amount of tries,
* so use a try/catch if you want to handle this
*/
load: function (callback) {
var try_mads_sdk = function () {
try {
mads_js_loaded = (typeof win.MADSAdrequest !== "undefined") ? 1 : 0;
if (mads_js_loaded) callback(); else throw "MADS not yet loaded";
}
catch (e) {
if (tries < options.max_tries) {
setTimeout(try_mads_sdk, options.timeout);
tries++;
}
else {
throw "MADS couldn't be downloaded";
}
}
};
if (mads_js_loaded > 0) callback();
else if (mads_js_loaded === 0) {
mads_js_loaded = -1;
(function (s, id) {
var js, mjs = doc.getElementsByTagName(s)[0];
if (! doc.getElementById(id)) {
js = doc.createElement(s);
js.id = id;
js.src = "http://eu2.madsone.com/js/tags.js";
mjs.parentNode.insertBefore(js, mjs);
}
})("script", mads_js);
try_mads_sdk();
}
else {
try_mads_sdk();
}
},
/**
* @method showAd will load an advertisement and display it
* @param id {String} the ID of the element where the ad should be displayed
* if the id isn't found, throws an error
* @param params {Object} optional object containing any keys specific to the ad
* the params object will be merged with the global options, so you can set default zones etc. and override them per ad
* See http://developer.madsone.com/JavaScript_AdTags#Complete_list_of_ad_request_parameters for the complete list of options
* By default it will show a random MADS test advertisement
*/
showAd: function (id, params) {
var el = doc.getElementById(id);
if (! el) {
throw "Couldn't find element with id " + id;
}
if (el.children.length) {
throw "Already loaded " + id;
}
// check if we have valid options
params = typeof params === "object" ? params : {};
// setup default options
var settings = {
pid: "6252122059",
element_id: id,
async: true,
ad_type: "live"
};
// merge global options into settings
extend(settings, options);
// merge params into options
extend(settings, params);
this.load(function () {
win.MADSAdrequest.adrequest(settings);
});
},
/**
* @method showAllAds convenience function for showing ads in a range of elements with the same class name
* @param className the class, e.g. "js-mads-ad"
* You can set data attributes on each element containing any ad request parameters from
* http://developer.madsone.com/JavaScript_AdTags#Complete_list_of_ad_request_parameters
*
* Example:
* <div class="js-mads-ad" id="mads-ad" data-ad_type="live" data-user_age="34"></div>
*
*/
showAllAds: function (className) {
var ads = Array.prototype.slice.call(doc.getElementsByClassName(className), 0),
self = this;
if (ads.length) {
self.load(function () {
ads.forEach(function (el) {
var id = el.getAttribute("id");
if (id && ! el.children.length) {
var params = el.dataset;
self.showAd.call(self, id, params);
}
});
});
}
}
};
})(document, window);