tarteaucitron.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*jslint browser: true */
  2. var tarteaucitron = {
  3. "cdn": "",
  4. "user": {},
  5. "lang": {},
  6. "services": {},
  7. "state": [],
  8. "launch": [],
  9. "init": function () {
  10. "use strict";
  11. var cdn = tarteaucitron.cdn,
  12. language = tarteaucitron.getLanguage(),
  13. pathToLang = cdn + 'lang/tarteaucitron.' + language + '.js',
  14. pathToServices = cdn + 'tarteaucitron.services.js',
  15. linkElement = document.createElement('link');
  16. // Step 1: load css
  17. linkElement.rel = 'stylesheet';
  18. linkElement.type = 'text/css';
  19. linkElement.href = cdn + 'css/tarteaucitron.css';
  20. document.getElementsByTagName('head')[0].appendChild(linkElement);
  21. // Step 2: load language and services
  22. tarteaucitron.addScript(pathToLang, '', function () {
  23. tarteaucitron.addScript(pathToServices, '', function () {
  24. var body = document.body,
  25. div = document.createElement('div'),
  26. hostname = document.location.hostname,
  27. hostRef = document.referrer.split('/')[2],
  28. isNavigating = (hostRef === hostname) ? true : false,
  29. isAutostart,
  30. isDenied,
  31. isAllowed,
  32. isResponded,
  33. cookie = tarteaucitron.cookie.read(),
  34. s = tarteaucitron.services,
  35. service,
  36. html = '',
  37. lastTitle,
  38. phrases,
  39. textIntro,
  40. textMore,
  41. alert = false,
  42. index;
  43. // dedup, clean and sort job[]
  44. function cleanArray(arr) {
  45. var i,
  46. len = arr.length,
  47. out = [],
  48. obj = {};
  49. for (i = 0; i < len; i += 1) {
  50. if (!obj[arr[i]]) {
  51. obj[arr[i]] = {};
  52. if (tarteaucitron.services[arr[i]] !== undefined) {
  53. out.push(arr[i]);
  54. }
  55. }
  56. }
  57. return out;
  58. }
  59. tarteaucitron.job = cleanArray(tarteaucitron.job);
  60. tarteaucitron.job = tarteaucitron.job.sort(function (a, b) {
  61. if (s[a].type + s[a].key > s[b].type + s[b].key) { return 1; }
  62. if (s[a].type + s[a].key < s[b].type + s[b].key) { return -1; }
  63. return 0;
  64. });
  65. // if bypass: load all services and exit
  66. // for example, set tarteaucitron.user.bypass = true;
  67. // if the user is not in europa
  68. if (tarteaucitron.user.bypass === true) {
  69. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  70. service = s[tarteaucitron.job[index]];
  71. service.js();
  72. }
  73. return;
  74. }
  75. // Step 3: prepare the html
  76. html += '<div id="tarteaucitronBack"></div>';
  77. html += '<div id="tarteaucitron">';
  78. html += ' <div id="tarteaucitronClosePanel" onclick="tarteaucitron.userInterface.closePanel();">';
  79. html += ' ' + tarteaucitron.lang.close;
  80. html += ' </div>';
  81. html += ' <div id="tarteaucitronDisclaimer">';
  82. html += ' ' + tarteaucitron.lang.disclaimer;
  83. html += ' </div>';
  84. html += ' <div id="tarteaucitronServices">';
  85. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  86. service = s[tarteaucitron.job[index]];
  87. textIntro = service.lang[language].split(".").splice(0, 1).join(".");
  88. phrases = service.lang[language].split(".");
  89. phrases.splice(0, 1);
  90. textMore = phrases.join(".");
  91. if (lastTitle !== service.type) {
  92. html += '<div class="tarteaucitronTitle">';
  93. html += ' ' + tarteaucitron.lang[service.type];
  94. html += '</div>';
  95. lastTitle = service.type;
  96. }
  97. html += '<div id="' + service.key + 'Line" class="tarteaucitronLine">';
  98. html += ' <div class="tarteaucitronName">';
  99. html += ' <b>' + service.name + '</b><br/>';
  100. html += ' ' + textIntro + '. ';
  101. html += ' <em id="' + service.key + 'More" onclick="tarteaucitron.userInterface.showMore(this);">';
  102. html += ' ' + tarteaucitron.lang.more;
  103. html += ' </em>';
  104. html += ' <div id="' + service.key + 'MoreText" class="tarteaucitronMore">';
  105. html += ' ' + textMore;
  106. html += ' <a href="' + service.uri + '" target="_blank">';
  107. html += ' ' + tarteaucitron.lang.source;
  108. html += ' </a>';
  109. html += ' </div>';
  110. html += ' </div>';
  111. html += ' <div class="tarteaucitronAsk">';
  112. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  113. html += ' ' + tarteaucitron.lang.allow;
  114. html += ' </div> ';
  115. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  116. html += ' ' + tarteaucitron.lang.deny;
  117. html += ' </div>';
  118. html += ' </div>';
  119. html += '</div>';
  120. html += '<div class="clear"></div>';
  121. }
  122. html += ' </div>';
  123. html += '</div>';
  124. html += '<div id="tarteaucitronAlertBig">';
  125. html += ' <span id="tarteaucitronDisclaimerAlert">';
  126. html += ' ' + tarteaucitron.lang.alertBig;
  127. html += ' </span>';
  128. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.openPanel();">';
  129. html += ' ' + tarteaucitron.lang.personalize;
  130. html += ' </span>';
  131. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.closeAlert();">';
  132. html += ' ' + tarteaucitron.lang.close;
  133. html += ' </span>';
  134. html += '</div>';
  135. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  136. html += ' <span id="tarteaucitronDot"></span>';
  137. html += ' ' + tarteaucitron.lang.alertSmall;
  138. html += '</div>';
  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') {
  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. "showMore": function (el) {
  192. "use strict";
  193. var key = el.id.replace(/More/, '');
  194. tarteaucitron.userInterface.css(key + 'MoreText', 'display', 'inline');
  195. el.style.display = 'none';
  196. },
  197. "respond": function (el, status) {
  198. "use strict";
  199. var key = el.id.replace(new RegExp("(Allow|Deni)ed", "g"), '');
  200. // return if same state
  201. if (tarteaucitron.state[key] === status) {
  202. return;
  203. }
  204. // if not already launched... launch the service
  205. if (status === true) {
  206. if (tarteaucitron.launch[key] !== true) {
  207. tarteaucitron.launch[key] = true;
  208. tarteaucitron.services[key].js();
  209. }
  210. }
  211. tarteaucitron.state[key] = status;
  212. tarteaucitron.cookie.create(key, status);
  213. tarteaucitron.userInterface.color(key, status);
  214. },
  215. "color": function (key, status) {
  216. "use strict";
  217. var gray = '#808080',
  218. greenDark = '#1B870B',
  219. greenLight = '#E6FFE2',
  220. redDark = '#9C1A1A',
  221. redLight = '#FFE2E2',
  222. c = 'tarteaucitron',
  223. allAllowed = true,
  224. index;
  225. if (status === true) {
  226. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', greenLight);
  227. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  228. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  229. } else if (status === false) {
  230. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', redLight);
  231. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  232. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  233. }
  234. // check if all services are allowed
  235. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  236. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  237. allAllowed = false;
  238. break;
  239. }
  240. }
  241. if (allAllowed) {
  242. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', greenDark);
  243. } else {
  244. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', redDark);
  245. }
  246. },
  247. "openPanel": function () {
  248. "use strict";
  249. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  250. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  251. tarteaucitron.userInterface.closeAlert();
  252. // setting hash tag
  253. document.location.hash = 'tarteaucitron';
  254. },
  255. "closePanel": function () {
  256. "use strict";
  257. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  258. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  259. },
  260. "openAlert": function () {
  261. "use strict";
  262. var c = 'tarteaucitron';
  263. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  264. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  265. },
  266. "closeAlert": function () {
  267. "use strict";
  268. var c = 'tarteaucitron';
  269. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  270. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  271. }
  272. },
  273. "cookie": {
  274. "create": function (key, status) {
  275. "use strict";
  276. var d = new Date(),
  277. time = d.getTime(),
  278. expireTime = time + 31536000000, // 365 days
  279. regex = new RegExp("!" + key + "=(true|false)", "g"),
  280. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  281. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  282. d.setTime(expireTime);
  283. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  284. },
  285. "read": function () {
  286. "use strict";
  287. var nameEQ = "tarteaucitron=",
  288. ca = document.cookie.split(';'),
  289. i,
  290. c;
  291. for (i = 0; i < ca.length; i += 1) {
  292. c = ca[i];
  293. while (c.charAt(0) === ' ') {
  294. c = c.substring(1, c.length);
  295. }
  296. if (c.indexOf(nameEQ) === 0) {
  297. return c.substring(nameEQ.length, c.length);
  298. }
  299. }
  300. return '';
  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. /***
  356. * Fallback function for advertising services
  357. *
  358. * Currently, a banner to promote tarteaucitron.js is displayed,
  359. * fell free to change this by your own ads.
  360. *
  361. * Because eval() id devil, you can't pass <script>,
  362. * only html like <a><img /></a>
  363. */
  364. "promoteMe": function (el) {
  365. "use strict";
  366. var l = tarteaucitron.getLanguage(),
  367. w = el.offsetWidth,
  368. h = el.offsetHeight,
  369. s = '',
  370. r = '';
  371. if (w > 0 && h === 0) {
  372. h = 60;
  373. }
  374. if (w >= 728 && h <= 60) {
  375. h = 90;
  376. }
  377. if (w >= 970 && h >= 250) {
  378. s = '970250';
  379. } else if (w >= 970 && h >= 90) {
  380. s = '97090';
  381. } else if (w >= 728 && h >= 90) {
  382. s = '72890';
  383. } else if (w >= 468 && h >= 60) {
  384. s = '46860';
  385. } else if (w >= 336 && h >= 280) {
  386. s = '336280';
  387. } else if (w >= 300 && h >= 600) {
  388. s = '300600';
  389. } else if (w >= 300 && h >= 250) {
  390. s = '300250';
  391. } else if (w >= 250 && h >= 250) {
  392. s = '250250';
  393. } else if (w >= 200 && h >= 200) {
  394. s = '200200';
  395. } else if (w >= 160 && h >= 600) {
  396. s = '160600';
  397. } else if (w >= 120 && h >= 600) {
  398. s = '120600';
  399. } else if (w >= 120 && h >= 300) {
  400. s = '120300';
  401. } else if (w >= 120 && h >= 240) {
  402. s = '120240';
  403. } else {
  404. s = '8080';
  405. }
  406. r += '<a href="//opt-out.ferank.eu/" target="_blank" rel="nofollow">';
  407. r += ' <img src="//opt-out.ferank.eu/b/' + l + '/' + s + '.gif" border="0" />';
  408. r += '</a>';
  409. return r;
  410. }
  411. };