tarteaucitron.js 17 KB

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