tarteaucitron.js 23 KB

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