tarteaucitron.js 24 KB

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