tarteaucitron.js 23 KB

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