tarteaucitron.js 17 KB

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