tarteaucitron.js 23 KB

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