tarteaucitron.js 18 KB

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