tarteaucitron.js 24 KB

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