tarteaucitron.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. phrases,
  44. textIntro,
  45. textMore,
  46. alert = false,
  47. index;
  48. // dedup, clean and sort job[]
  49. function cleanArray(arr) {
  50. var i,
  51. len = arr.length,
  52. out = [],
  53. obj = {};
  54. for (i = 0; i < len; i += 1) {
  55. if (!obj[arr[i]]) {
  56. obj[arr[i]] = {};
  57. if (tarteaucitron.services[arr[i]] !== undefined) {
  58. out.push(arr[i]);
  59. }
  60. }
  61. }
  62. return out;
  63. }
  64. tarteaucitron.job = cleanArray(tarteaucitron.job);
  65. tarteaucitron.job = tarteaucitron.job.sort(function (a, b) {
  66. if (s[a].type + s[a].key > s[b].type + s[b].key) { return 1; }
  67. if (s[a].type + s[a].key < s[b].type + s[b].key) { return -1; }
  68. return 0;
  69. });
  70. // if bypass: load all services and exit
  71. // for example, set tarteaucitron.user.bypass = true;
  72. // if the user is not in europa
  73. if (tarteaucitron.user.bypass === true) {
  74. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  75. service = s[tarteaucitron.job[index]];
  76. service.js();
  77. }
  78. return;
  79. }
  80. // Step 3: prepare the html
  81. html += '<div id="tarteaucitronBack"></div>';
  82. html += '<div id="tarteaucitron">';
  83. html += ' <div id="tarteaucitronClosePanel" onclick="tarteaucitron.userInterface.closePanel();">';
  84. html += ' ' + tarteaucitron.lang.close;
  85. html += ' </div>';
  86. html += ' <div id="tarteaucitronDisclaimer">';
  87. html += ' ' + tarteaucitron.lang.disclaimer;
  88. html += ' </div>';
  89. html += ' <div id="tarteaucitronServices">';
  90. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  91. service = s[tarteaucitron.job[index]];
  92. textIntro = service.lang[language].split(".").splice(0, 1).join(".");
  93. phrases = service.lang[language].split(".");
  94. phrases.splice(0, 1);
  95. textMore = phrases.join(".");
  96. if (lastTitle !== service.type) {
  97. html += '<div class="tarteaucitronTitle">';
  98. html += ' ' + tarteaucitron.lang[service.type];
  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 += ' ' + textIntro + '. ';
  106. html += ' <em id="' + service.key + 'More" onclick="tarteaucitron.userInterface.showMore(this);">';
  107. html += ' ' + tarteaucitron.lang.more;
  108. html += ' </em>';
  109. html += ' <div id="' + service.key + 'MoreText" class="tarteaucitronMore">';
  110. html += ' ' + textMore;
  111. html += ' <a href="' + service.uri + '" target="_blank">';
  112. html += ' ' + tarteaucitron.lang.source;
  113. html += ' </a>';
  114. html += ' </div>';
  115. html += ' </div>';
  116. html += ' <div class="tarteaucitronAsk">';
  117. html += ' <div id="' + service.key + 'Allowed" class="tarteaucitronAllow" onclick="tarteaucitron.userInterface.respond(this, true);">';
  118. html += ' ' + tarteaucitron.lang.allow;
  119. html += ' </div> ';
  120. html += ' <div id="' + service.key + 'Denied" class="tarteaucitronDeny" onclick="tarteaucitron.userInterface.respond(this, false);">';
  121. html += ' ' + tarteaucitron.lang.deny;
  122. html += ' </div>';
  123. html += ' </div>';
  124. html += '</div>';
  125. html += '<div class="clear"></div>';
  126. }
  127. html += ' </div>';
  128. html += '</div>';
  129. html += '<div id="tarteaucitronAlertBig">';
  130. html += ' <span id="tarteaucitronDisclaimerAlert">';
  131. html += ' ' + tarteaucitron.lang.alertBig;
  132. html += ' </span>';
  133. html += ' <span id="tarteaucitronPersonalize" onclick="tarteaucitron.userInterface.openPanel();">';
  134. html += ' ' + tarteaucitron.lang.personalize;
  135. html += ' </span>';
  136. html += ' <span id="tarteaucitronCloseAlert" onclick="tarteaucitron.userInterface.closeAlert();">';
  137. html += ' ' + tarteaucitron.lang.close;
  138. html += ' </span>';
  139. html += '</div>';
  140. html += '<div id="tarteaucitronAlertSmall" onclick="tarteaucitron.userInterface.openPanel();">';
  141. html += ' <span id="tarteaucitronDot"></span>';
  142. html += ' ' + tarteaucitron.lang.alertSmall;
  143. html += '</div>';
  144. div.id = 'tarteaucitronRoot';
  145. body.appendChild(div, body);
  146. div.innerHTML = html;
  147. // Step 4: load services
  148. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  149. service = s[tarteaucitron.job[index]];
  150. isAutostart = (!service.needConsent) ? true : false;
  151. isDenied = (cookie.indexOf(service.key + '=false') >= 0) ? true : false;
  152. isAllowed = (cookie.indexOf(service.key + '=true') >= 0) ? true : false;
  153. isResponded = (cookie.indexOf(service.key) >= 0) ? true : false;
  154. if ((!isResponded && (isAutostart || isNavigating)) || isAllowed) {
  155. if (!isAllowed) {
  156. tarteaucitron.cookie.create(service.key, true);
  157. }
  158. if (tarteaucitron.launch[service.key] !== true) {
  159. tarteaucitron.launch[service.key] = true;
  160. service.js();
  161. }
  162. tarteaucitron.state[service.key] = true;
  163. tarteaucitron.userInterface.color(service.key, true);
  164. } else if (isDenied) {
  165. if (typeof service.fallback === 'function') {
  166. service.fallback();
  167. }
  168. tarteaucitron.state[service.key] = false;
  169. tarteaucitron.userInterface.color(service.key, false);
  170. } else if (!isResponded) {
  171. if (typeof service.fallback === 'function') {
  172. service.fallback();
  173. }
  174. }
  175. if (tarteaucitron.state[service.key] === undefined && !alert) {
  176. alert = true;
  177. }
  178. }
  179. // Step 5: display the alert
  180. if (alert) {
  181. tarteaucitron.userInterface.openAlert();
  182. } else {
  183. tarteaucitron.userInterface.closeAlert();
  184. }
  185. if (document.location.hash === '#tarteaucitron' && tarteaucitron.autoOpen === true) {
  186. tarteaucitron.userInterface.openPanel();
  187. }
  188. });
  189. });
  190. },
  191. "userInterface": {
  192. "css": function (id, property, value) {
  193. "use strict";
  194. document.getElementById(id).style[property] = value;
  195. },
  196. "showMore": function (el) {
  197. "use strict";
  198. var key = el.id.replace(/More/, '');
  199. tarteaucitron.userInterface.css(key + 'MoreText', 'display', 'inline');
  200. el.style.display = 'none';
  201. },
  202. "respond": function (el, status) {
  203. "use strict";
  204. var key = el.id.replace(new RegExp("(Allow|Deni)ed", "g"), '');
  205. // return if same state
  206. if (tarteaucitron.state[key] === status) {
  207. return;
  208. }
  209. // if not already launched... launch the service
  210. if (status === true) {
  211. if (tarteaucitron.launch[key] !== true) {
  212. tarteaucitron.launch[key] = true;
  213. tarteaucitron.services[key].js();
  214. }
  215. }
  216. tarteaucitron.state[key] = status;
  217. tarteaucitron.cookie.create(key, status);
  218. tarteaucitron.userInterface.color(key, status);
  219. },
  220. "color": function (key, status) {
  221. "use strict";
  222. var gray = '#808080',
  223. greenDark = '#1B870B',
  224. greenLight = '#E6FFE2',
  225. redDark = '#9C1A1A',
  226. redLight = '#FFE2E2',
  227. c = 'tarteaucitron',
  228. allAllowed = true,
  229. index;
  230. if (status === true) {
  231. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', greenLight);
  232. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', greenDark);
  233. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', gray);
  234. } else if (status === false) {
  235. tarteaucitron.userInterface.css(key + 'Line', 'backgroundColor', redLight);
  236. tarteaucitron.userInterface.css(key + 'Allowed', 'backgroundColor', gray);
  237. tarteaucitron.userInterface.css(key + 'Denied', 'backgroundColor', redDark);
  238. }
  239. // check if all services are allowed
  240. for (index = 0; index < tarteaucitron.job.length; index += 1) {
  241. if (tarteaucitron.state[tarteaucitron.job[index]] === false) {
  242. allAllowed = false;
  243. break;
  244. }
  245. }
  246. if (allAllowed) {
  247. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', greenDark);
  248. } else {
  249. tarteaucitron.userInterface.css(c + 'Dot', 'backgroundColor', redDark);
  250. }
  251. },
  252. "openPanel": function () {
  253. "use strict";
  254. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'block');
  255. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'block');
  256. tarteaucitron.userInterface.closeAlert();
  257. // setting hash tag
  258. document.location.hash = 'tarteaucitron';
  259. },
  260. "closePanel": function () {
  261. "use strict";
  262. tarteaucitron.userInterface.css('tarteaucitron', 'display', 'none');
  263. tarteaucitron.userInterface.css('tarteaucitronBack', 'display', 'none');
  264. },
  265. "openAlert": function () {
  266. "use strict";
  267. var c = 'tarteaucitron';
  268. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'none');
  269. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'block');
  270. },
  271. "closeAlert": function () {
  272. "use strict";
  273. var c = 'tarteaucitron';
  274. tarteaucitron.userInterface.css(c + 'AlertSmall', 'display', 'block');
  275. tarteaucitron.userInterface.css(c + 'AlertBig', 'display', 'none');
  276. }
  277. },
  278. "cookie": {
  279. "create": function (key, status) {
  280. "use strict";
  281. var d = new Date(),
  282. time = d.getTime(),
  283. expireTime = time + 31536000000, // 365 days
  284. regex = new RegExp("!" + key + "=(true|false)", "g"),
  285. cookie = tarteaucitron.cookie.read().replace(regex, ""),
  286. value = 'tarteaucitron=' + cookie + '!' + key + '=' + status;
  287. d.setTime(expireTime);
  288. document.cookie = value + '; expires=' + d.toGMTString() + '; path=/;';
  289. },
  290. "read": function () {
  291. "use strict";
  292. var nameEQ = "tarteaucitron=",
  293. ca = document.cookie.split(';'),
  294. i,
  295. c;
  296. for (i = 0; i < ca.length; i += 1) {
  297. c = ca[i];
  298. while (c.charAt(0) === ' ') {
  299. c = c.substring(1, c.length);
  300. }
  301. if (c.indexOf(nameEQ) === 0) {
  302. return c.substring(nameEQ.length, c.length);
  303. }
  304. }
  305. return '';
  306. }
  307. },
  308. "getLanguage": function () {
  309. "use strict";
  310. if (!navigator) { return 'en'; }
  311. var availableLanguages = 'en,fr',
  312. defaultLanguage = 'en',
  313. lang = navigator.language || navigator.browserLanguage ||
  314. navigator.systemLanguage || navigator.userLang || null,
  315. userLanguage = lang.substr(0, 2);
  316. if (availableLanguages.indexOf(userLanguage) === -1) {
  317. return defaultLanguage;
  318. }
  319. return userLanguage;
  320. },
  321. "addScript": function (url, id, callback) {
  322. "use strict";
  323. var script = document.createElement('script'),
  324. done = false;
  325. script.type = 'text/javascript';
  326. script.id = (id !== undefined) ? id : '';
  327. script.async = true;
  328. script.src = url;
  329. if (typeof callback === 'function') {
  330. script.onreadystatechange = script.onload = function () {
  331. var state = script.readyState;
  332. if (!done && (!state || /loaded|complete/.test(state))) {
  333. done = true;
  334. callback();
  335. }
  336. };
  337. }
  338. document.getElementsByTagName('head')[0].appendChild(script);
  339. },
  340. "fallback": function (matchClass, content) {
  341. "use strict";
  342. var elems = document.getElementsByTagName('*'),
  343. i,
  344. index = 0;
  345. for (i in elems) {
  346. if (elems[i] !== undefined) {
  347. for (index = 0; index < matchClass.length; index += 1) {
  348. if ((' ' + elems[i].className + ' ')
  349. .indexOf(' ' + matchClass[index] + ' ') > -1) {
  350. if (typeof content === 'function') {
  351. elems[i].innerHTML = content(elems[i]);
  352. } else {
  353. elems[i].innerHTML = content;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. },
  360. /***
  361. * Fallback function for advertising services
  362. *
  363. * Currently, a banner to promote tarteaucitron.js is displayed,
  364. * fell free to change this by your own ads.
  365. *
  366. * Because eval() id devil, you can't pass <script>,
  367. * only html like <a><img /></a>
  368. */
  369. "promoteMe": function (el) {
  370. "use strict";
  371. var l = tarteaucitron.getLanguage(),
  372. w = el.offsetWidth,
  373. h = el.offsetHeight,
  374. s = '',
  375. r = '';
  376. if (w > 0 && h === 0) {
  377. h = 60;
  378. }
  379. if (w >= 728 && h <= 60) {
  380. h = 90;
  381. }
  382. if (w >= 970 && h >= 250) {
  383. s = '970250';
  384. } else if (w >= 970 && h >= 90) {
  385. s = '97090';
  386. } else if (w >= 728 && h >= 90) {
  387. s = '72890';
  388. } else if (w >= 468 && h >= 60) {
  389. s = '46860';
  390. } else if (w >= 336 && h >= 280) {
  391. s = '336280';
  392. } else if (w >= 300 && h >= 600) {
  393. s = '300600';
  394. } else if (w >= 300 && h >= 250) {
  395. s = '300250';
  396. } else if (w >= 250 && h >= 250) {
  397. s = '250250';
  398. } else if (w >= 200 && h >= 200) {
  399. s = '200200';
  400. } else if (w >= 160 && h >= 600) {
  401. s = '160600';
  402. } else if (w >= 120 && h >= 600) {
  403. s = '120600';
  404. } else if (w >= 120 && h >= 300) {
  405. s = '120300';
  406. } else if (w >= 120 && h >= 240) {
  407. s = '120240';
  408. } else {
  409. s = '8080';
  410. }
  411. r += '<a href="//opt-out.ferank.eu/" target="_blank" rel="nofollow">';
  412. r += ' <img src="//opt-out.ferank.eu/b/' + l + '/' + s + '.gif" border="0" />';
  413. r += '</a>';
  414. return r;
  415. }
  416. };