tarteaucitron.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. alreadyLaunch = (alreadyLaunch === undefined) ? 0 : alreadyLaunch;
  7. var tarteaucitron = {
  8. "cdn": cdn,
  9. "user": {},
  10. "lang": {},
  11. "services": {},
  12. "state": [],
  13. "launch": [],
  14. "init": function (params) {
  15. "use strict";
  16. if (alreadyLaunch === 0) {
  17. alreadyLaunch = 1;
  18. if (window.addEventListener) {
  19. window.addEventListener("load", tarteaucitron.load(params), false);
  20. } else {
  21. window.attachEvent('onload', tarteaucitron.load(params));
  22. }
  23. }
  24. },
  25. "load": function (params) {
  26. "use strict";
  27. var cdn = tarteaucitron.cdn,
  28. language = tarteaucitron.getLanguage(),
  29. pathToLang = cdn + 'lang/tarteaucitron.' + language + '.js',
  30. timestamp = new Date().getTime(),
  31. pathToServices = cdn + 'tarteaucitron.services.js',
  32. parametersCom = '?uuid=' + tarteaucitron.uuid + '&c=' + encodeURIComponent(tarteaucitron.cookie.read()) + '&_' + timestamp,
  33. linkElement = document.createElement('link'),
  34. defaults = {
  35. "grayArea": false,
  36. "hashtag": '#tarteaucitron',
  37. "highPrivacy": false,
  38. "orientation": "top",
  39. "removeCredit": false,
  40. "showAlertSmall": true
  41. };
  42. // is the commercial version ?
  43. if (tarteaucitron.uuid !== '' && tarteaucitron.uuid !== undefined) {
  44. pathToServices = pathToServices + parametersCom;
  45. }
  46. // Step 0: get params
  47. if (params !== undefined) {
  48. tarteaucitron.extend(defaults, params);
  49. }
  50. // the hashtag need to be global
  51. tarteaucitron.hashtag = defaults.hashtag;
  52. // Step 1: load css
  53. linkElement.rel = 'stylesheet';
  54. linkElement.type = 'text/css';
  55. linkElement.href = cdn + 'css/tarteaucitron.css';
  56. document.getElementsByTagName('head')[0].appendChild(linkElement);
  57. // Step 2: load language and services
  58. tarteaucitron.addScript(pathToLang, '', function () {
  59. tarteaucitron.addScript(pathToServices, '', function () {
  60. var body = document.body,
  61. div = document.createElement('div'),
  62. hostname = document.location.hostname,
  63. hostRef = document.referrer.split('/')[2],
  64. isNavigating = (hostRef === hostname) ? true : false,
  65. isAutostart,
  66. isDenied,
  67. isAllowed,
  68. isResponded,
  69. cookie = tarteaucitron.cookie.read(),
  70. s = tarteaucitron.services,
  71. service,
  72. html = '',
  73. lastTitle,
  74. alert = false,
  75. index,
  76. orientation = 'Top';
  77. // dedup, clean and sort job[]
  78. function cleanArray(arr) {
  79. var i,
  80. len = arr.length,
  81. out = [],
  82. obj = {};
  83. for (i = 0; i < len; i += 1) {
  84. if (!obj[arr[i]]) {
  85. obj[arr[i]] = {};
  86. if (tarteaucitron.services[arr[i]] !== undefined) {
  87. out.push(arr[i]);
  88. }
  89. }
  90. }
  91. return out;
  92. }
  93. tarteaucitron.job = cleanArray(tarteaucitron.job);
  94. tarteaucitron.job = tarteaucitron.job.sort(function (a, b) {
  95. if (s[a].type + s[a].key > s[b].type + s[b].key) { return 1; }
  96. if (s[a].type + s[a].key < s[b].type + s[b].key) { return -1; }
  97. return 0;
  98. });
  99. // if bypass: load all services and exit
  100. // for example, set tarteaucitron.user.bypass = true;
  101. // if the user is not in europa
  102. if (tarteaucitron.user.bypass === true) {
  103. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  104. service = s[tarteaucitron.job[index]];
  105. service.js();
  106. }
  107. return;
  108. }
  109. // Step 3: prepare the html
  110. html += '<div id="tarteaucitronBack" onclick="tarteaucitron.userInterface.closePanel();"></div>';
  111. html += '<div id="tarteaucitron">';
  112. html += ' <div id="tarteaucitronClosePanel" onclick="tarteaucitron.userInterface.closePanel();">';
  113. html += ' ' + tarteaucitron.lang.close;
  114. html += ' </div>';
  115. html += ' <div id="tarteaucitronInfo">';
  116. html += ' ' + tarteaucitron.lang.info;
  117. html += ' <div id="tarteaucitronDisclaimer">';
  118. html += ' ' + tarteaucitron.lang.disclaimer;
  119. html += ' </div>';
  120. html += ' </div>';
  121. html += ' <div id="tarteaucitronServices">';
  122. html += '<div class="tarteaucitronLine tarteaucitronMainLine">';
  123. html += ' <div class="tarteaucitronName">';
  124. html += ' <b>' + tarteaucitron.lang.all + '</b>';
  125. html += ' </div>';
  126. html += ' <div class="tarteaucitronAsk">';
  127. html += ' <div id="tarteaucitronAllAllowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respondAll(true);">';
  128. html += ' ' + tarteaucitron.lang.allow;
  129. html += ' </div> ';
  130. html += ' <div id="tarteaucitronAllDenied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respondAll(false);">';
  131. html += ' ' + tarteaucitron.lang.deny;
  132. html += ' </div>';
  133. html += ' </div>';
  134. html += '</div>';
  135. html += '<div class="clear"></div>';
  136. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  137. service = s[tarteaucitron.job[index]];
  138. if (lastTitle !== service.type) {
  139. html += '<div class="tarteaucitronTitle">';
  140. html += ' ' + tarteaucitron.lang[service.type].title;
  141. html += ' <div class="tarteaucitronDetails">';
  142. html += ' ' + tarteaucitron.lang[service.type].details;
  143. html += ' </div>';
  144. html += '</div>';
  145. lastTitle = service.type;
  146. }
  147. html += '<div id="' + service.key + 'Line" class="tarteaucitronLine">';
  148. html += ' <div class="tarteaucitronName">';
  149. html += ' <b>' + service.name + '</b><br/>';
  150. html += ' <a href="' + service.uri + '" target="_blank">';
  151. html += ' ' + tarteaucitron.lang.more + ' : ' + service.uri.split('/')[2];
  152. html += ' </a>';
  153. html += ' </div>';
  154. html += ' <div class="tarteaucitronAsk">';
  155. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  156. html += ' ' + tarteaucitron.lang.allow;
  157. html += ' </div> ';
  158. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  159. html += ' ' + tarteaucitron.lang.deny;
  160. html += ' </div>';
  161. html += ' </div>';
  162. html += '</div>';
  163. html += '<div class="clear"></div>';
  164. }
  165. html += ' </div>';
  166. if (defaults.removeCredit === false) {
  167. html += ' <div id="tarteaucitronFooter">';
  168. html += ' <a href="https://opt-out.ferank.eu/" rel="nofollow" target="_blank">' + tarteaucitron.lang.credit + '</a>';
  169. html += ' </div>';
  170. }
  171. html += '</div>';
  172. // get the banner orientation
  173. if (defaults.orientation === 'bottom') {
  174. orientation = 'Bottom';
  175. }
  176. if (defaults.highPrivacy) {
  177. html += '<div id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '">';
  178. html += ' <span id="tarteaucitronDisclaimerAlert">';
  179. html += ' ' + tarteaucitron.lang.alertBigPrivacy;
  180. html += ' </span>';
  181. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.openPanel();">';
  182. html += ' ' + tarteaucitron.lang.personalize;
  183. html += ' </span>';
  184. html += '</div>';
  185. } else {
  186. html += '<div id="tarteaucitronAlertBig" class="tarteaucitronAlertBig' + orientation + '">';
  187. html += ' <span id="tarteaucitronDisclaimerAlert">';
  188. html += ' ' + tarteaucitron.lang.alertBig;
  189. html += ' </span>';
  190. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.respondAll(true);">';
  191. html += ' ' + tarteaucitron.lang.acceptAll;
  192. html += ' </span>';
  193. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.openPanel();">';
  194. html += ' ' + tarteaucitron.lang.personalize;
  195. html += ' </span>';
  196. html += '</div>';
  197. }
  198. if (defaults.showAlertSmall === true) {
  199. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  200. html += ' ' + tarteaucitron.lang.alertSmall;
  201. html += ' <div id="tarteaucitronDot">';
  202. html += ' <span id="tarteaucitronDotGreen"></span>';
  203. html += ' <span id="tarteaucitronDotYellow"></span>';
  204. html += ' <span id="tarteaucitronDotRed"></span>';
  205. html += ' </div>';
  206. html += '</div>';
  207. }
  208. div.id = 'tarteaucitronRoot';
  209. body.appendChild(div, body);
  210. div.innerHTML = html;
  211. // Step 4: load services
  212. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  213. service = s[tarteaucitron.job[index]];
  214. isAutostart = (!service.needConsent) ? true : false;
  215. isDenied = (cookie.indexOf(service.key + '=false') >= 0) ? true : false;
  216. isAllowed = (cookie.indexOf(service.key + '=true') >= 0) ? true : false;
  217. isResponded = (cookie.indexOf(service.key) >= 0) ? true : false;
  218. if ((!isResponded && (isAutostart || isNavigating) && !defaults.highPrivacy) || isAllowed) {
  219. if (!isAllowed) {
  220. tarteaucitron.cookie.create(service.key, true);
  221. }
  222. if (tarteaucitron.launch[service.key] !== true) {
  223. tarteaucitron.launch[service.key] = true;
  224. service.js();
  225. }
  226. tarteaucitron.state[service.key] = true;
  227. tarteaucitron.userInterface.color(service.key, true);
  228. } else if (isDenied) {
  229. if (typeof service.fallback === 'function') {
  230. service.fallback();
  231. }
  232. tarteaucitron.state[service.key] = false;
  233. tarteaucitron.userInterface.color(service.key, false);
  234. } else if (!isResponded) {
  235. if (typeof service.grayJs === 'function' && defaults.grayArea === true) {
  236. service.grayJs();
  237. } else if (typeof service.fallback === 'function') {
  238. service.fallback();
  239. }
  240. }
  241. if (tarteaucitron.state[service.key] === undefined && !alert) {
  242. alert = true;
  243. }
  244. }
  245. // Step 5: display the alert
  246. if (alert) {
  247. tarteaucitron.userInterface.openAlert();
  248. } else {
  249. tarteaucitron.userInterface.closeAlert();
  250. }
  251. if (document.location.hash === tarteaucitron.hashtag && tarteaucitron.hashtag !== '') {
  252. tarteaucitron.userInterface.openPanel();
  253. }
  254. });
  255. });
  256. },
  257. "userInterface": {
  258. "css": function (id, property, value) {
  259. "use strict";
  260. if (document.getElementById(id) !== null) {
  261. document.getElementById(id).style[property] = value;
  262. }
  263. },
  264. "respondAll": function (status) {
  265. "use strict";
  266. var s = tarteaucitron.services,
  267. service,
  268. key,
  269. index = 0;
  270. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  271. service = s[tarteaucitron.job[index]];
  272. key = service.key;
  273. if (tarteaucitron.launch[key] !== true && status === true) {
  274. tarteaucitron.launch[key] = true;
  275. tarteaucitron.services[key].js();
  276. }
  277. tarteaucitron.state[key] = status;
  278. tarteaucitron.cookie.create(key, status);
  279. tarteaucitron.userInterface.color(key, status);
  280. }
  281. },
  282. "respond": function (el, status) {
  283. "use strict";
  284. var key = el.id.replace(new RegExp("(Eng[0-9]+|Allow|Deni)ed", "g"), '');
  285. // return if same state
  286. if (tarteaucitron.state[key] === status) {
  287. return;
  288. }
  289. // if not already launched... launch the service
  290. if (status === true) {
  291. if (tarteaucitron.launch[key] !== true) {
  292. tarteaucitron.launch[key] = true;
  293. tarteaucitron.services[key].js();
  294. }
  295. }
  296. tarteaucitron.state[key] = status;
  297. tarteaucitron.cookie.create(key, status);
  298. tarteaucitron.userInterface.color(key, status);
  299. },
  300. "color": function (key, status) {
  301. "use strict";
  302. var gray = '#808080',
  303. greenDark = '#1B870B',
  304. greenLight = '#E6FFE2',
  305. redDark = '#9C1A1A',
  306. redLight = '#FFE2E2',
  307. yellowDark = '#FBDA26',
  308. c = 'tarteaucitron',
  309. nbDenied = 0,
  310. nbPending = 0,
  311. nbAllowed = 0,
  312. sum = tarteaucitron.job.length,
  313. index;
  314. if (status === true) {
  315. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + greenDark);
  316. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  317. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  318. } else if (status === false) {
  319. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + redDark);
  320. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  321. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  322. }
  323. // check if all services are allowed
  324. for (index = 0; index < sum; index += 1) {
  325. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  326. nbDenied += 1;
  327. } else if (tarteaucitron.state[tarteaucitron.job[index]] === undefined) {
  328. nbPending += 1;
  329. } else if (tarteaucitron.state[tarteaucitron.job[index]] === true) {
  330. nbAllowed += 1;
  331. }
  332. }
  333. tarteaucitron.userInterface.css(c + 'DotGreen', 'width', ((100 / sum) * nbAllowed) + '%');
  334. tarteaucitron.userInterface.css(c + 'DotYellow', 'width', ((100 / sum) * nbPending) + '%');
  335. tarteaucitron.userInterface.css(c + 'DotRed', 'width', ((100 / sum) * nbDenied) + '%');
  336. if (nbDenied === 0 && nbPending === 0) {
  337. tarteaucitron.userInterface.css(c + 'AllAllowed', 'backgroundColor', greenDark);
  338. tarteaucitron.userInterface.css(c + 'AllDenied', 'backgroundColor', gray);
  339. } else if (nbAllowed === 0 && nbPending === 0) {
  340. tarteaucitron.userInterface.css(c + 'AllAllowed', 'backgroundColor', gray);
  341. tarteaucitron.userInterface.css(c + 'AllDenied', 'backgroundColor', redDark);
  342. } else {
  343. tarteaucitron.userInterface.css(c + 'AllAllowed', 'backgroundColor', gray);
  344. tarteaucitron.userInterface.css(c + 'AllDenied', 'backgroundColor', gray);
  345. }
  346. // close the alert if all service have been reviewed
  347. if (nbPending === 0) {
  348. tarteaucitron.userInterface.closeAlert();
  349. }
  350. },
  351. "openPanel": function () {
  352. "use strict";
  353. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  354. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  355. // setting hash tag
  356. if (tarteaucitron.hashtag !== '') {
  357. document.location.hash = tarteaucitron.hashtag;
  358. }
  359. },
  360. "closePanel": function () {
  361. "use strict";
  362. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  363. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  364. },
  365. "openAlert": function () {
  366. "use strict";
  367. var c = 'tarteaucitron';
  368. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  369. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  370. },
  371. "closeAlert": function () {
  372. "use strict";
  373. var c = 'tarteaucitron';
  374. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  375. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  376. }
  377. },
  378. "cookie": {
  379. "create": function (key, status) {
  380. "use strict";
  381. var d = new Date(),
  382. time = d.getTime(),
  383. expireTime = time + 31536000000, // 365 days
  384. regex = new RegExp("!" + key + "=(true|false)", "g"),
  385. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  386. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  387. d.setTime(expireTime);
  388. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  389. },
  390. "read": function () {
  391. "use strict";
  392. var nameEQ = "tarteaucitron=",
  393. ca = document.cookie.split(';'),
  394. i,
  395. c;
  396. for (i = 0; i < ca.length; i += 1) {
  397. c = ca[i];
  398. while (c.charAt(0) === ' ') {
  399. c = c.substring(1, c.length);
  400. }
  401. if (c.indexOf(nameEQ) === 0) {
  402. return c.substring(nameEQ.length, c.length);
  403. }
  404. }
  405. return '';
  406. },
  407. "purge": function (arr) {
  408. "use strict";
  409. var i;
  410. for (i = 0; i < arr.length; i += 1) {
  411. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/;';
  412. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname + ';';
  413. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname.split('.').slice(-2).join('.') + ';';
  414. }
  415. }
  416. },
  417. "getLanguage": function () {
  418. "use strict";
  419. if (!navigator) { return 'en'; }
  420. var availableLanguages = 'en,fr',
  421. defaultLanguage = 'en',
  422. lang = navigator.language || navigator.browserLanguage ||
  423. navigator.systemLanguage || navigator.userLang || null,
  424. userLanguage = lang.substr(0, 2);
  425. if (availableLanguages.indexOf(userLanguage) === -1) {
  426. return defaultLanguage;
  427. }
  428. return userLanguage;
  429. },
  430. "getLocale": function () {
  431. "use strict";
  432. if (!navigator) { return 'en_US'; }
  433. var lang = navigator.language || navigator.browserLanguage ||
  434. navigator.systemLanguage || navigator.userLang || null,
  435. userLanguage = lang.substr(0, 2);
  436. if (userLanguage === 'fr') {
  437. return 'fr_FR';
  438. } else if (userLanguage === 'en') {
  439. return 'en_US';
  440. } else if (userLanguage === 'de') {
  441. return 'de_DE';
  442. } else if (userLanguage === 'es') {
  443. return 'es_ES';
  444. } else if (userLanguage === 'it') {
  445. return 'it_IT';
  446. } else if (userLanguage === 'pt') {
  447. return 'pt_PT';
  448. } else {
  449. return 'en_US';
  450. }
  451. },
  452. "addScript": function (url, id, callback) {
  453. "use strict";
  454. var script = document.createElement('script'),
  455. done = false;
  456. script.type = 'text/javascript';
  457. script.id = (id !== undefined) ? id : '';
  458. script.async = true;
  459. script.src = url;
  460. if (typeof callback === 'function') {
  461. script.onreadystatechange = script.onload = function () {
  462. var state = script.readyState;
  463. if (!done && (!state || /loaded|complete/.test(state))) {
  464. done = true;
  465. callback();
  466. }
  467. };
  468. }
  469. document.getElementsByTagName('head')[0].appendChild(script);
  470. },
  471. "fallback": function (matchClass, content) {
  472. "use strict";
  473. var elems = document.getElementsByTagName('*'),
  474. i,
  475. index = 0;
  476. for (i in elems) {
  477. if (elems[i] !== undefined) {
  478. for (index = 0; index < matchClass.length; index += 1) {
  479. if ((' ' + elems[i].className + ' ')
  480. .indexOf(' ' + matchClass[index] + ' ') > -1) {
  481. if (typeof content === 'function') {
  482. elems[i].innerHTML = content(elems[i]);
  483. } else {
  484. elems[i].innerHTML = content;
  485. }
  486. }
  487. }
  488. }
  489. }
  490. },
  491. "engage": function (id) {
  492. "use strict";
  493. var html = '',
  494. r = Math.floor(Math.random() * 100000);
  495. html += '<div class="tac_activate">';
  496. html += ' <div class="tac_float">';
  497. html += ' <b>' + id + '</b> ' + tarteaucitron.lang.fallback + '<br/>';
  498. html += ' <div class="tarteaucitronAllow" id="Eng' + r + 'ed' + id + '" onclick="tarteaucitron.userInterface.respond(this, true);">';
  499. html += ' ' + tarteaucitron.lang.allow;
  500. html += ' </div>';
  501. html += ' </div>';
  502. html += '</div>';
  503. return html;
  504. },
  505. "extend": function (a, b) {
  506. "use strict";
  507. var prop;
  508. for (prop in b) {
  509. if (b.hasOwnProperty(prop)) {
  510. a[prop] = b[prop];
  511. }
  512. }
  513. }
  514. };