Check if elements exist before use them

This commit is contained in:
Amauri CHAMPEAUX 2015-03-10 17:16:07 +01:00
parent 592f606840
commit 5cad990705
1 changed files with 98 additions and 28 deletions

View File

@ -10,7 +10,7 @@ var scripts = document.getElementsByTagName('script'),
tarteaucitronNoAdBlocker = false; tarteaucitronNoAdBlocker = false;
var tarteaucitron = { var tarteaucitron = {
"version": 201.6, "version": 202,
"cdn": cdn, "cdn": cdn,
"user": {}, "user": {},
"lang": {}, "lang": {},
@ -49,10 +49,16 @@ var tarteaucitron = {
} }
}, false); }, false);
window.addEventListener("resize", function () { window.addEventListener("resize", function () {
if (document.getElementById('tarteaucitron').style.display === 'block') { if (document.getElementById('tarteaucitron') !== null) {
tarteaucitron.userInterface.jsSizing('main'); if (document.getElementById('tarteaucitron').style.display === 'block') {
} else if (document.getElementById('tarteaucitronCookiesListContainer').style.display === 'block') { tarteaucitron.userInterface.jsSizing('main');
tarteaucitron.userInterface.jsSizing('cookie'); }
}
if (document.getElementById('tarteaucitronCookiesListContainer') !== null) {
if (document.getElementById('tarteaucitronCookiesListContainer').style.display === 'block') {
tarteaucitron.userInterface.jsSizing('cookie');
}
} }
}, false); }, false);
} else { } else {
@ -75,10 +81,16 @@ var tarteaucitron = {
} }
}); });
window.attachEvent("onresize", function () { window.attachEvent("onresize", function () {
if (document.getElementById('tarteaucitron').style.display === 'block') { if (document.getElementById('tarteaucitron') !== null) {
tarteaucitron.userInterface.jsSizing('main'); if (document.getElementById('tarteaucitron').style.display === 'block') {
} else if (document.getElementById('tarteaucitronCookiesListContainer').style.display === 'block') { tarteaucitron.userInterface.jsSizing('main');
tarteaucitron.userInterface.jsSizing('cookie'); }
}
if (document.getElementById('tarteaucitronCookiesListContainer') !== null) {
if (document.getElementById('tarteaucitronCookiesListContainer').style.display === 'block') {
tarteaucitron.userInterface.jsSizing('cookie');
}
} }
}); });
} }
@ -666,41 +678,99 @@ var tarteaucitron = {
}, },
"jsSizing": function (type) { "jsSizing": function (type) {
"use strict"; "use strict";
var start = 10, var scrollbarMarginRight = 10,
parent, scrollbarWidthParent,
child, scrollbarWidthChild,
servicesHeight,
e = window, e = window,
a = 'inner'; a = 'inner',
windowInnerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
mainTop,
mainHeight,
closeButtonHeight,
headerHeight,
cookiesListHeight,
cookiesCloseHeight,
cookiesTitleHeight;
if (type === 'main') { if (type === 'main') {
// height services list container
tarteaucitron.userInterface.css('tarteaucitronScrollbarParent', 'height', 'auto');
tarteaucitron.userInterface.css('tarteaucitronScrollbarParent', 'height', (document.getElementById('tarteaucitron').offsetHeight - document.getElementById('tarteaucitronClosePanel').offsetHeight - document.getElementById('tarteaucitronMainLineOffset').offsetHeight + 1) + 'px');
// get the real window width for media query
if (window.innerWidth === undefined) { if (window.innerWidth === undefined) {
a = 'client'; a = 'client';
e = document.documentElement || document.body; e = document.documentElement || document.body;
} }
if (e[a + 'Width'] <= 767) { // height of the services list container
start = 12; if (document.getElementById('tarteaucitron') !== null && document.getElementById('tarteaucitronClosePanel') !== null && document.getElementById('tarteaucitronMainLineOffset') !== null) {
// reset
tarteaucitron.userInterface.css('tarteaucitronScrollbarParent', 'height', 'auto');
// calculate
mainHeight = document.getElementById('tarteaucitron').offsetHeight;
closeButtonHeight = document.getElementById('tarteaucitronClosePanel').offsetHeight;
headerHeight = document.getElementById('tarteaucitronMainLineOffset').offsetHeight;
// apply
servicesHeight = (mainHeight - closeButtonHeight - headerHeight + 1);
tarteaucitron.userInterface.css('tarteaucitronScrollbarParent', 'height', servicesHeight + 'px');
} }
// indent the global allow/deny buttons // align the main allow/deny button depending on scrollbar width
parent = document.getElementById('tarteaucitronScrollbarParent').offsetWidth; if (document.getElementById('tarteaucitronScrollbarParent') !== null && document.getElementById('tarteaucitronScrollbarChild') !== null) {
child = document.getElementById('tarteaucitronScrollbarChild').offsetWidth;
tarteaucitron.userInterface.css('tarteaucitronScrollbarAdjust', 'marginRight', ((parent - child) + start) + 'px'); // media query
if (e[a + 'Width'] <= 767) {
scrollbarMarginRight = 12;
} else if (e[a + 'Width'] <= 479) {
tarteaucitron.userInterface.css('tarteaucitronScrollbarAdjust', 'marginLeft', '11px');
}
scrollbarWidthParent = document.getElementById('tarteaucitronScrollbarParent').offsetWidth;
scrollbarWidthChild = document.getElementById('tarteaucitronScrollbarChild').offsetWidth;
tarteaucitron.userInterface.css('tarteaucitronScrollbarAdjust', 'marginRight', ((scrollbarWidthParent - scrollbarWidthChild) + scrollbarMarginRight) + 'px');
}
if (e[a + 'Width'] <= 479) { // center the main panel
tarteaucitron.userInterface.css('tarteaucitronScrollbarAdjust', 'marginLeft', '11px'); if (document.getElementById('tarteaucitron') !== null) {
// media query
if (e[a + 'Width'] <= 767) {
mainTop = 0;
} else {
mainTop = ((windowInnerHeight - document.getElementById('tarteaucitron').offsetHeight) / 2) - 21;
}
// correct
if (mainTop < 0) {
mainTop = 0;
}
tarteaucitron.userInterface.css('tarteaucitron', 'top', mainTop + 'px');
} }
} else if (type === 'cookie') { } else if (type === 'cookie') {
// height cookies list container
tarteaucitron.userInterface.css('tarteaucitronCookiesListContainer', 'bottom', (parseInt(document.getElementById('tarteaucitronAlertSmall').offsetHeight, 10) + 10) + 'px');
tarteaucitron.userInterface.css('tarteaucitronCookiesList', 'height', 'auto'); // put cookies list at bottom
tarteaucitron.userInterface.css('tarteaucitronCookiesList', 'height', (document.getElementById('tarteaucitronCookiesListContainer').offsetHeight - document.getElementById('tarteaucitronClosePanelCookie').offsetHeight - document.getElementById('tarteaucitronCookiesTitle').offsetHeight - 2) + 'px'); if (document.getElementById('tarteaucitronAlertSmall') !== null) {
tarteaucitron.userInterface.css('tarteaucitronCookiesListContainer', 'bottom', (document.getElementById('tarteaucitronAlertSmall').offsetHeight + 10) + 'px');
}
// height of cookies list
if (document.getElementById('tarteaucitronCookiesListContainer') !== null) {
// reset
tarteaucitron.userInterface.css('tarteaucitronCookiesList', 'height', 'auto');
// calculate
cookiesListHeight = document.getElementById('tarteaucitronCookiesListContainer').offsetHeight;
cookiesCloseHeight = document.getElementById('tarteaucitronClosePanelCookie').offsetHeight;
cookiesTitleHeight = document.getElementById('tarteaucitronCookiesTitle').offsetHeight;
// apply
tarteaucitron.userInterface.css('tarteaucitronCookiesList', 'height', (cookiesListHeight - cookiesCloseHeight - cookiesTitleHeight - 2) + 'px');
}
} }
} }
}, },