// widget manager, kein jquery
CWWidgetManager = function () {
this.frames = {};
};
CWWidgetManager.prototype.init = function () {
var self = this;
console.log('CWDGM init');
if (window.addEventListener) {
window.addEventListener('message', function (event) {
if (typeof event.data.split == 'function') {
console.log(event);
console.log(event.data);
var tmp = event.data.split('|'); // das sind 3 teile a) keyword, b) dom-id des widgets, c) url für den listeniframe
console.log(tmp);
console.log(self.frames);
if (tmp[0] == 'CWsetIframeUrl') {
self.frames[tmp[1]].querySelectorAll('iframe')[0].setAttribute('src', tmp[2]);
} else if (tmp[0] == 'CWsetWindowUrl') {
window.location.href = tmp[1];
} else if (tmp[0] == 'CWscrollIframeTop') {
var elem = document.getElementById(tmp[1]);
if (elem) {
var dataID = elem.getAttribute('data-fahrzeug-id');
if (dataID) {
console.log('nicht scrollen, da id explizit angegeben wurde ');
} else {
console.log('hoch mit mir: ' + tmp[1], elem.offsetTop);
if (parseInt(tmp[2]) > 0) {
setTimeout(function () {
window.scrollTo(0, parseInt(tmp[2]) + elem.offsetTop - 100)
}, 500);
} else {
setTimeout(function () {
elem.scrollIntoView({behavior: "smooth", inline: "nearest"});
}, 500);
}
}
} else {
console.log('elem: ' + tmp[1] + ' nicht gefunden, kann nicht scrollen');
}
}
} else {
console.log('kann nix machen', event.data);
}
})
}
};
CWWidgetManager.prototype.addParamID = function (elem, url) {
var dataID = elem.getAttribute('data-fahrzeug-id');
var modus = elem.getAttribute('data-modus');
if (dataID !== null) {
dataID = parseInt(dataID);
if (!isNaN(dataID)) {
url += (url.split('?')[1] ? '&' : '?') + 'id_detail=' + dataID + '&suppress_scroll_top=1'; // wir machen hier einen zus parameter dran, der das hochscrollen verhindern soll
}
}
// kann man auch den modus dazupappen
if (modus === 'rental') {
url += (url.split('?')[1] ? '&' : '?') + 'fahrzeugart=3';
}
return url;
};
CWWidgetManager.prototype.loadHTML = function (id, url, width, height) {
var xmlhttp = new XMLHttpRequest();
var method = 'GET';
if (!id) {
console.log('loadFrame: no id', id);
return;
}
var elem = document.getElementById(id);
url = self.addParamID(elem, url);
console.log('loadHTML', id, elem, url);
if (elem !== null) {
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
elem.innerHTML = xmlhttp.responseText;
} else {
console.log('ajax error ' + xmlhttp.status);
}
}
};
xmlhttp.open(method, url, true);
xmlhttp.send();
}
};
CWWidgetManager.prototype.loadFrame = function (id, url, width, height, resize) {
var self = this;
if (!id) {
console.log('loadFrame: no id', id);
return true;
}
var elem = document.getElementById(id);
var marke = false;
url = self.addParamID(elem, url);
var style = 'style="';
if (width !== null) {
style = style + ' width: ' + width + ';';
}
if (height !== null) {
style = style + ' height: ' + height + ';';
}
style = style + '"';
console.log('loadFrame', id, elem, url, style);
var hash = location.hash;
var cwscroll = false;
/* console.log(hash.indexOf('#!')); */
if (hash.indexOf('#!') > -1) { // base64 "verschlüsselte" vollständige url auf caraworld
if (hash.indexOf('#!') > 0) { // dann hat es davor noch ne sprungmarke
var marke = hash.substring(0, hash.indexOf('#!'));
var el = document.querySelector(marke);
console.log('id', id, marke);
if (('#' + id) == marke) {
console.log('scroll', el, elem, elem.getAttribute('id'));
el.scrollIntoView(true);
}
url = url + '&hashparam=' + hash.substring(hash.indexOf('#!') + 2);
} else {
url = url + '&hashparam=' + hash.replace('#!', '');
}
cwscroll = true;
} else if (hash.indexOf('#id') > -1) { // variante um eine id direkt aufzurufen
url = url + '&id_detail=' + hash.replace('#id', '');
cwscroll = true;
} else if (url.includes('fahrzeugliste.php')) {
console.log('NACH OBEN (liste)', elem);
cwscroll = true;
marke = '#fahrzeugliste';
} else if (url.includes('fahrzeugdetailseite.php')) {
console.log('NACH OBEN (detail)', elem);
cwscroll = true;
marke = '#detail';
}
if (elem !== null) {
var myscroll = '';
if (!marke) {
marke = '#' + id;
var el = document.querySelector(marke);
if (el && cwscroll) {
console.log('scrollto', marke);
el.scrollIntoView(true);
} else {
// scrolling nach oben?
//myscroll = ' onload="self.scrollTo(0,0)"';
}
}
elem.innerHTML = '';
if (resize) {
if (typeof iFrameResize === 'function') {
iFrameResize([{log: true, autoResize: true, heightCalculationMethod: 'lowestElement'}], '#' + id + ' iframe');
}
}
self.frames[id] = elem;
}
};
// initialization
var CWDGM = new CWWidgetManager();
// kein jquery
var CWDGMready = function (fn) {
if (document.readyState && document.readyState != 'loading') {
fn();
} else if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', fn);
} else {
document.attachEvent('onreadystatechange', function () {
if (document.readyState != 'loading')
fn();
});
}
};
CWDGMready(function () {
CWDGM.init();
});
CWDGMready(function () {
CWDGM.loadFrame( 'cw-17080305', 'https://www.caraworld.de/frontend/virtual_host/cw4878/fahrzeugliste.php?frames=true&widget_id=17080305', '100%', null, true );
});