tarteaucitron.js 23 KB

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