tarteaucitron.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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>';
  87. html += ' <div id="tarteaucitronServices">';
  88. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  89. service = s[tarteaucitron.job[index]];
  90. if (lastTitle !== service.type) {
  91. html += '<div class="tarteaucitronTitle">';
  92. html += ' ' + tarteaucitron.lang[service.type].title;
  93. html += ' <div class="tarteaucitronDetails">';
  94. html += ' ' + tarteaucitron.lang[service.type].details;
  95. html += ' </div>';
  96. html += '</div>';
  97. lastTitle = service.type;
  98. }
  99. html += '<div id="' + service.key + 'Line" class="tarteaucitronLine">';
  100. html += ' <div class="tarteaucitronName">';
  101. html += ' <b>' + service.name + '</b><br/>';
  102. html += ' <a href="' + service.uri + '" target="_blank">';
  103. html += ' ' + tarteaucitron.lang.more + ' : ' + service.uri.split('/')[2];
  104. html += ' </a>';
  105. html += ' </div>';
  106. html += ' <div class="tarteaucitronAsk">';
  107. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  108. html += ' ' + tarteaucitron.lang.allow;
  109. html += ' </div> ';
  110. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  111. html += ' ' + tarteaucitron.lang.deny;
  112. html += ' </div>';
  113. html += ' </div>';
  114. html += '</div>';
  115. html += '<div class="clear"></div>';
  116. }
  117. html += ' </div>';
  118. html += ' <div id="tarteaucitronDisclaimer">';
  119. html += ' ' + tarteaucitron.lang.disclaimer + '<br/><a href="https://opt-out.ferank.eu/" rel="nofollow" target="_blank">' + tarteaucitron.lang.credit + '</a>';
  120. html += ' </div>';
  121. html += '</div>';
  122. html += '<div id="tarteaucitronAlertBig">';
  123. html += ' <span id="tarteaucitronDisclaimerAlert">';
  124. html += ' ' + tarteaucitron.lang.alertBig;
  125. html += ' </span>';
  126. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.openPanel();">';
  127. html += ' ' + tarteaucitron.lang.personalize;
  128. html += ' </span>';
  129. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.closeAlert();">';
  130. html += ' ' + tarteaucitron.lang.close;
  131. html += ' </span>';
  132. html += '</div>';
  133. if (tarteaucitron.showAlertSmall === true) {
  134. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  135. html += ' <span id="tarteaucitronDot"></span>';
  136. html += ' ' + tarteaucitron.lang.alertSmall;
  137. html += '</div>';
  138. }
  139. div.id = 'tarteaucitronRoot';
  140. body.appendChild(div, body);
  141. div.innerHTML = html;
  142. // Step 4: load services
  143. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  144. service = s[tarteaucitron.job[index]];
  145. isAutostart = (!service.needConsent) ? true : false;
  146. isDenied = (cookie.indexOf(service.key + '=false') >= 0) ? true : false;
  147. isAllowed = (cookie.indexOf(service.key + '=true') >= 0) ? true : false;
  148. isResponded = (cookie.indexOf(service.key) >= 0) ? true : false;
  149. if ((!isResponded && (isAutostart || isNavigating)) || isAllowed) {
  150. if (!isAllowed) {
  151. tarteaucitron.cookie.create(service.key, true);
  152. }
  153. if (tarteaucitron.launch[service.key] !== true) {
  154. tarteaucitron.launch[service.key] = true;
  155. service.js();
  156. }
  157. tarteaucitron.state[service.key] = true;
  158. tarteaucitron.userInterface.color(service.key, true);
  159. } else if (isDenied) {
  160. if (typeof service.fallback === 'function') {
  161. service.fallback();
  162. }
  163. tarteaucitron.state[service.key] = false;
  164. tarteaucitron.userInterface.color(service.key, false);
  165. } else if (!isResponded) {
  166. if (typeof service.fallback === 'function') {
  167. service.fallback();
  168. }
  169. }
  170. if (tarteaucitron.state[service.key] === undefined && !alert) {
  171. alert = true;
  172. }
  173. }
  174. // Step 5: display the alert
  175. if (alert) {
  176. tarteaucitron.userInterface.openAlert();
  177. } else {
  178. tarteaucitron.userInterface.closeAlert();
  179. }
  180. if (document.location.hash === '#tarteaucitron' && tarteaucitron.autoOpen === true) {
  181. tarteaucitron.userInterface.openPanel();
  182. }
  183. });
  184. });
  185. },
  186. "userInterface": {
  187. "css": function (id, property, value) {
  188. "use strict";
  189. document.getElementById(id).style[property] = value;
  190. },
  191. "respond": function (el, status) {
  192. "use strict";
  193. var key = el.id.replace(new RegExp("(Eng[0-9]+|Allow|Deni)ed", "g"), '');
  194. // return if same state
  195. if (tarteaucitron.state[key] === status) {
  196. return;
  197. }
  198. // if not already launched... launch the service
  199. if (status === true) {
  200. if (tarteaucitron.launch[key] !== true) {
  201. tarteaucitron.launch[key] = true;
  202. tarteaucitron.services[key].js();
  203. }
  204. }
  205. tarteaucitron.state[key] = status;
  206. tarteaucitron.cookie.create(key, status);
  207. tarteaucitron.userInterface.color(key, status);
  208. },
  209. "color": function (key, status) {
  210. "use strict";
  211. var gray = '#808080',
  212. greenDark = '#1B870B',
  213. greenLight = '#E6FFE2',
  214. redDark = '#9C1A1A',
  215. redLight = '#FFE2E2',
  216. c = 'tarteaucitron',
  217. allAllowed = true,
  218. index;
  219. if (status === true) {
  220. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + greenDark);
  221. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  222. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  223. } else if (status === false) {
  224. tarteaucitron.userInterface.css(key + 'Line', 'borderLeft', '5px solid ' + redDark);
  225. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  226. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  227. }
  228. // check if all services are allowed
  229. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  230. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  231. allAllowed = false;
  232. break;
  233. }
  234. }
  235. if (allAllowed) {
  236. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', greenDark);
  237. } else {
  238. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', redDark);
  239. }
  240. },
  241. "openPanel": function () {
  242. "use strict";
  243. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  244. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  245. tarteaucitron.userInterface.closeAlert();
  246. // setting hash tag
  247. document.location.hash = 'tarteaucitron';
  248. },
  249. "closePanel": function () {
  250. "use strict";
  251. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  252. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  253. },
  254. "openAlert": function () {
  255. "use strict";
  256. var c = 'tarteaucitron';
  257. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  258. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  259. },
  260. "closeAlert": function () {
  261. "use strict";
  262. var c = 'tarteaucitron';
  263. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  264. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  265. }
  266. },
  267. "cookie": {
  268. "create": function (key, status) {
  269. "use strict";
  270. var d = new Date(),
  271. time = d.getTime(),
  272. expireTime = time + 31536000000, // 365 days
  273. regex = new RegExp("!" + key + "=(true|false)", "g"),
  274. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  275. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  276. d.setTime(expireTime);
  277. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  278. },
  279. "read": function () {
  280. "use strict";
  281. var nameEQ = "tarteaucitron=",
  282. ca = document.cookie.split(';'),
  283. i,
  284. c;
  285. for (i = 0; i < ca.length; i += 1) {
  286. c = ca[i];
  287. while (c.charAt(0) === ' ') {
  288. c = c.substring(1, c.length);
  289. }
  290. if (c.indexOf(nameEQ) === 0) {
  291. return c.substring(nameEQ.length, c.length);
  292. }
  293. }
  294. return '';
  295. },
  296. "purge": function (arr) {
  297. "use strict";
  298. var i;
  299. for (i = 0; i < arr.length; i += 1) {
  300. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/;';
  301. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname + ';';
  302. document.cookie = arr[i] + '=; expires=Thu, 01 Jan 2000 00:00:00 GMT; path=/; domain=.' + location.hostname.split('.').slice(-2).join('.') + ';';
  303. }
  304. }
  305. },
  306. "getLanguage": function () {
  307. "use strict";
  308. if (!navigator) { return 'en'; }
  309. var availableLanguages = 'en,fr',
  310. defaultLanguage = 'en',
  311. lang = navigator.language || navigator.browserLanguage ||
  312. navigator.systemLanguage || navigator.userLang || null,
  313. userLanguage = lang.substr(0, 2);
  314. if (availableLanguages.indexOf(userLanguage) === -1) {
  315. return defaultLanguage;
  316. }
  317. return userLanguage;
  318. },
  319. "addScript": function (url, id, callback) {
  320. "use strict";
  321. var script = document.createElement('script'),
  322. done = false;
  323. script.type = 'text/javascript';
  324. script.id = (id !== undefined) ? id : '';
  325. script.async = true;
  326. script.src = url;
  327. if (typeof callback === 'function') {
  328. script.onreadystatechange = script.onload = function () {
  329. var state = script.readyState;
  330. if (!done && (!state || /loaded|complete/.test(state))) {
  331. done = true;
  332. callback();
  333. }
  334. };
  335. }
  336. document.getElementsByTagName('head')[0].appendChild(script);
  337. },
  338. "fallback": function (matchClass, content) {
  339. "use strict";
  340. var elems = document.getElementsByTagName('*'),
  341. i,
  342. index = 0;
  343. for (i in elems) {
  344. if (elems[i] !== undefined) {
  345. for (index = 0; index < matchClass.length; index += 1) {
  346. if ((' ' + elems[i].className + ' ')
  347. .indexOf(' ' + matchClass[index] + ' ') > -1) {
  348. if (typeof content === 'function') {
  349. elems[i].innerHTML = content(elems[i]);
  350. } else {
  351. elems[i].innerHTML = content;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. },
  358. "engage": function (id) {
  359. "use strict";
  360. var html = '',
  361. r = Math.floor(Math.random() * 100000);
  362. html += '<div class="tac_activate">';
  363. html += ' <div class="tac_float">';
  364. html += ' <b>' + id + '</b> ' + tarteaucitron.lang.fallback + '<br/>';
  365. html += ' <div class="tarteaucitronAllow" id="Eng' + r + 'ed' + id + '" onclick="tarteaucitron.userInterface.respond(this, true);">';
  366. html += ' ' + tarteaucitron.lang.allow;
  367. html += ' </div>';
  368. html += ' </div>';
  369. html += '</div>';
  370. return html;
  371. }
  372. };