tarteaucitron.js 24 KB

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