tarteaucitron.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*jslint browser: true */
  2. // define correct path for files inclusion
  3. var scripts = document.getElementsByTagName('script'),
  4. path = scripts[scripts.length - 1].src.split('?')[0],
  5. cdn = path.split('/').slice(0, -1).join('/') + '/';
  6. var tarteaucitron = {
  7. "showAlertSmall": true, // show the small banner on bottom right?
  8. "autoOpen": false, // auto open the panel with #tarteaucitron hash?
  9. "grayArea": false, // activate the features of the gray area?
  10. "cdn": cdn,
  11. "user": {},
  12. "lang": {},
  13. "services": {},
  14. "state": [],
  15. "launch": [],
  16. "init": function () {
  17. "use strict";
  18. var cdn = tarteaucitron.cdn,
  19. language = tarteaucitron.getLanguage(),
  20. pathToLang = cdn + 'lang/tarteaucitron.' + language + '.js',
  21. pathToServices = cdn + 'tarteaucitron.services.js',
  22. linkElement = document.createElement('link');
  23. // Step 1: load css
  24. linkElement.rel = 'stylesheet';
  25. linkElement.type = 'text/css';
  26. linkElement.href = cdn + 'css/tarteaucitron.css';
  27. document.getElementsByTagName('head')[0].appendChild(linkElement);
  28. // Step 2: load language and services
  29. tarteaucitron.addScript(pathToLang, '', function () {
  30. tarteaucitron.addScript(pathToServices, '', function () {
  31. var body = document.body,
  32. div = document.createElement('div'),
  33. hostname = document.location.hostname,
  34. hostRef = document.referrer.split('/')[2],
  35. isNavigating = (hostRef === hostname) ? true : false,
  36. isAutostart,
  37. isDenied,
  38. isAllowed,
  39. isResponded,
  40. cookie = tarteaucitron.cookie.read(),
  41. s = tarteaucitron.services,
  42. service,
  43. html = '',
  44. lastTitle,
  45. alert = false,
  46. index;
  47. // dedup, clean and sort job[]
  48. function cleanArray(arr) {
  49. var i,
  50. len = arr.length,
  51. out = [],
  52. obj = {};
  53. for (i = 0; i < len; i += 1) {
  54. if (!obj[arr[i]]) {
  55. obj[arr[i]] = {};
  56. if (tarteaucitron.services[arr[i]] !== undefined) {
  57. out.push(arr[i]);
  58. }
  59. }
  60. }
  61. return out;
  62. }
  63. tarteaucitron.job = cleanArray(tarteaucitron.job);
  64. tarteaucitron.job = tarteaucitron.job.sort(function (a, b) {
  65. if (s[a].type + s[a].key > s[b].type + s[b].key) { return 1; }
  66. if (s[a].type + s[a].key < s[b].type + s[b].key) { return -1; }
  67. return 0;
  68. });
  69. // if bypass: load all services and exit
  70. // for example, set tarteaucitron.user.bypass = true;
  71. // if the user is not in europa
  72. if (tarteaucitron.user.bypass === true) {
  73. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  74. service = s[tarteaucitron.job[index]];
  75. service.js();
  76. }
  77. return;
  78. }
  79. // Step 3: prepare the html
  80. html += '<div id="tarteaucitronBack" onclick="tarteaucitron.userInterface.closePanel();"></div>';
  81. html += '<div id="tarteaucitron">';
  82. html += ' <div id="tarteaucitronClosePanel" onclick="tarteaucitron.userInterface.closePanel();">';
  83. html += ' ' + tarteaucitron.lang.close;
  84. html += ' </div>';
  85. html += ' <div id="tarteaucitronInfo">';
  86. html += ' ' + tarteaucitron.lang.info;
  87. html += ' <div id="tarteaucitronDisclaimer">';
  88. html += ' ' + tarteaucitron.lang.disclaimer;
  89. html += ' </div>';
  90. html += ' </div>';
  91. html += ' <div id="tarteaucitronServices">';
  92. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  93. service = s[tarteaucitron.job[index]];
  94. if (lastTitle !== service.type) {
  95. html += '<div class="tarteaucitronTitle">';
  96. html += ' ' + tarteaucitron.lang[service.type].title;
  97. html += ' <div class="tarteaucitronDetails">';
  98. html += ' ' + tarteaucitron.lang[service.type].details;
  99. html += ' </div>';
  100. html += '</div>';
  101. lastTitle = service.type;
  102. }
  103. html += '<div id="' + service.key + 'Line" class="tarteaucitronLine">';
  104. html += ' <div class="tarteaucitronName">';
  105. html += ' <b>' + service.name + '</b><br/>';
  106. html += ' <a href="' + service.uri + '" target="_blank">';
  107. html += ' ' + tarteaucitron.lang.more + ' : ' + service.uri.split('/')[2];
  108. html += ' </a>';
  109. html += ' </div>';
  110. html += ' <div class="tarteaucitronAsk">';
  111. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  112. html += ' ' + tarteaucitron.lang.allow;
  113. html += ' </div> ';
  114. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  115. html += ' ' + tarteaucitron.lang.deny;
  116. html += ' </div>';
  117. html += ' </div>';
  118. html += '</div>';
  119. html += '<div class="clear"></div>';
  120. }
  121. html += ' </div>';
  122. html += ' <div id="tarteaucitronFooter">';
  123. html += ' <a href="https://opt-out.ferank.eu/" rel="nofollow" target="_blank">' + tarteaucitron.lang.credit + '</a>';
  124. html += ' </div>';
  125. html += '</div>';
  126. html += '<div id="tarteaucitronAlertBig">';
  127. html += ' <span id="tarteaucitronDisclaimerAlert">';
  128. html += ' ' + tarteaucitron.lang.alertBig;
  129. html += ' </span>';
  130. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.acceptAll();">';
  131. html += ' ' + tarteaucitron.lang.acceptAll;
  132. html += ' </span>';
  133. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.openPanel();">';
  134. html += ' ' + tarteaucitron.lang.personalize;
  135. html += ' </span>';
  136. html += '</div>';
  137. if (tarteaucitron.showAlertSmall === true) {
  138. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  139. html += ' <span id="tarteaucitronDot"></span>';
  140. html += ' ' + tarteaucitron.lang.alertSmall;
  141. html += '</div>';
  142. }
  143. div.id = 'tarteaucitronRoot';
  144. body.appendChild(div, body);
  145. div.innerHTML = html;
  146. // Step 4: load services
  147. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  148. service = s[tarteaucitron.job[index]];
  149. isAutostart = (!service.needConsent) ? true : false;
  150. isDenied = (cookie.indexOf(service.key + '=false') >= 0) ? true : false;
  151. isAllowed = (cookie.indexOf(service.key + '=true') >= 0) ? true : false;
  152. isResponded = (cookie.indexOf(service.key) >= 0) ? true : false;
  153. if ((!isResponded && (isAutostart || isNavigating)) || isAllowed) {
  154. if (!isAllowed) {
  155. tarteaucitron.cookie.create(service.key, true);
  156. }
  157. if (tarteaucitron.launch[service.key] !== true) {
  158. tarteaucitron.launch[service.key] = true;
  159. service.js();
  160. }
  161. tarteaucitron.state[service.key] = true;
  162. tarteaucitron.userInterface.color(service.key, true);
  163. } else if (isDenied) {
  164. if (typeof service.fallback === 'function') {
  165. service.fallback();
  166. }
  167. tarteaucitron.state[service.key] = false;
  168. tarteaucitron.userInterface.color(service.key, false);
  169. } else if (!isResponded) {
  170. if (typeof service.grayJs === 'function' && tarteaucitron.grayArea === true) {
  171. service.grayJs();
  172. } else if (typeof service.fallback === 'function') {
  173. service.fallback();
  174. }
  175. }
  176. if (tarteaucitron.state[service.key] === undefined && !alert) {
  177. alert = true;
  178. }
  179. }
  180. // Step 5: display the alert
  181. if (alert) {
  182. tarteaucitron.userInterface.openAlert();
  183. } else {
  184. tarteaucitron.userInterface.closeAlert();
  185. }
  186. if (document.location.hash === '#tarteaucitron' && tarteaucitron.autoOpen === true) {
  187. tarteaucitron.userInterface.openPanel();
  188. }
  189. });
  190. });
  191. },
  192. "userInterface": {
  193. "css": function (id, property, value) {
  194. "use strict";
  195. document.getElementById(id).style[property] = value;
  196. },
  197. "acceptAll": function () {
  198. "use strict";
  199. var s = tarteaucitron.services,
  200. service,
  201. key,
  202. index = 0;
  203. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  204. service = s[tarteaucitron.job[index]];
  205. key = service.key;
  206. if (tarteaucitron.launch[key] !== true) {
  207. tarteaucitron.launch[key] = true;
  208. tarteaucitron.services[key].js();
  209. }
  210. tarteaucitron.state[key] = true;
  211. tarteaucitron.cookie.create(key, true);
  212. tarteaucitron.userInterface.color(key, true);
  213. tarteaucitron.userInterface.closeAlert();
  214. }
  215. },
  216. "respond": function (el, status) {
  217. "use strict";
  218. var key = el.id.replace(new RegExp("(Eng[0-9]+|Allow|Deni)ed", "g"), '');
  219. // return if same state
  220. if (tarteaucitron.state[key] === status) {
  221. return;
  222. }
  223. // if not already launched... launch the service
  224. if (status === true) {
  225. if (tarteaucitron.launch[key] !== true) {
  226. tarteaucitron.launch[key] = true;
  227. tarteaucitron.services[key].js();
  228. }
  229. }
  230. tarteaucitron.state[key] = status;
  231. tarteaucitron.cookie.create(key, status);
  232. tarteaucitron.userInterface.color(key, status);
  233. },
  234. "color": function (key, status) {
  235. "use strict";
  236. var gray = '#808080',
  237. greenDark = '#1B870B',
  238. greenLight = '#E6FFE2',
  239. redDark = '#9C1A1A',
  240. redLight = '#FFE2E2',
  241. c = 'tarteaucitron',
  242. allAllowed = true,
  243. index;
  244. if (status === true) {
  245. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + greenDark);
  246. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  247. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  248. } else if (status === false) {
  249. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + redDark);
  250. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  251. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  252. }
  253. // check if all services are allowed
  254. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  255. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  256. allAllowed = false;
  257. break;
  258. }
  259. }
  260. if (allAllowed) {
  261. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', greenDark);
  262. } else {
  263. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', redDark);
  264. }
  265. },
  266. "openPanel": function () {
  267. "use strict";
  268. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  269. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  270. tarteaucitron.userInterface.closeAlert();
  271. // setting hash tag
  272. document.location.hash = 'tarteaucitron';
  273. },
  274. "closePanel": function () {
  275. "use strict";
  276. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  277. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  278. },
  279. "openAlert": function () {
  280. "use strict";
  281. var c = 'tarteaucitron';
  282. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  283. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  284. },
  285. "closeAlert": function () {
  286. "use strict";
  287. var c = 'tarteaucitron';
  288. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  289. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  290. }
  291. },
  292. "cookie": {
  293. "create": function (key, status) {
  294. "use strict";
  295. var d = new Date(),
  296. time = d.getTime(),
  297. expireTime = time + 31536000000, // 365 days
  298. regex = new RegExp("!" + key + "=(true|false)", "g"),
  299. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  300. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  301. d.setTime(expireTime);
  302. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  303. },
  304. "read": function () {
  305. "use strict";
  306. var nameEQ = "tarteaucitron=",
  307. ca = document.cookie.split(';'),
  308. i,
  309. c;
  310. for (i = 0; i < ca.length; i += 1) {
  311. c = ca[i];
  312. while (c.charAt(0) === ' ') {
  313. c = c.substring(1, c.length);
  314. }
  315. if (c.indexOf(nameEQ) === 0) {
  316. return c.substring(nameEQ.length, c.length);
  317. }
  318. }
  319. return '';
  320. },
  321. "purge": function (arr) {
  322. "use strict";
  323. var i;
  324. for (i = 0; i < arr.length; i += 1) {
  325. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/;';
  326. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname + ';';
  327. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname.split('.').slice(-2).join('.') + ';';
  328. }
  329. }
  330. },
  331. "getLanguage": function () {
  332. "use strict";
  333. if (!navigator) { return 'en'; }
  334. var availableLanguages = 'en,fr',
  335. defaultLanguage = 'en',
  336. lang = navigator.language || navigator.browserLanguage ||
  337. navigator.systemLanguage || navigator.userLang || null,
  338. userLanguage = lang.substr(0, 2);
  339. if (availableLanguages.indexOf(userLanguage) === -1) {
  340. return defaultLanguage;
  341. }
  342. return userLanguage;
  343. },
  344. "addScript": function (url, id, callback) {
  345. "use strict";
  346. var script = document.createElement('script'),
  347. done = false;
  348. script.type = 'text/javascript';
  349. script.id = (id !== undefined) ? id : '';
  350. script.async = true;
  351. script.src = url;
  352. if (typeof callback === 'function') {
  353. script.onreadystatechange = script.onload = function () {
  354. var state = script.readyState;
  355. if (!done && (!state || /loaded|complete/.test(state))) {
  356. done = true;
  357. callback();
  358. }
  359. };
  360. }
  361. document.getElementsByTagName('head')[0].appendChild(script);
  362. },
  363. "fallback": function (matchClass, content) {
  364. "use strict";
  365. var elems = document.getElementsByTagName('*'),
  366. i,
  367. index = 0;
  368. for (i in elems) {
  369. if (elems[i] !== undefined) {
  370. for (index = 0; index < matchClass.length; index += 1) {
  371. if ((' ' + elems[i].className + ' ')
  372. .indexOf(' ' + matchClass[index] + ' ') > -1) {
  373. if (typeof content === 'function') {
  374. elems[i].innerHTML = content(elems[i]);
  375. } else {
  376. elems[i].innerHTML = content;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. },
  383. "engage": function (id) {
  384. "use strict";
  385. var html = '',
  386. r = Math.floor(Math.random() * 100000);
  387. html += '<div class="tac_activate">';
  388. html += ' <div class="tac_float">';
  389. html += ' <b>' + id + '</b> ' + tarteaucitron.lang.fallback + '<br/>';
  390. html += ' <div class="tarteaucitronAllow" id="Eng' + r + 'ed' + id + '" onclick="tarteaucitron.userInterface.respond(this, true);">';
  391. html += ' ' + tarteaucitron.lang.allow;
  392. html += ' </div>';
  393. html += ' </div>';
  394. html += '</div>';
  395. return html;
  396. }
  397. };