diff --git a/scripts/haltestellen.js b/scripts/haltestellen.js index 18e8727..5158a93 100644 --- a/scripts/haltestellen.js +++ b/scripts/haltestellen.js @@ -1,6 +1,6 @@ // variable to en-/disable debug // set to 1 to enable debug log -var DEBUG = 1; +var DEBUG; // xmlhttp object function @@ -119,14 +119,17 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { dataLength; // process of response only if it's not empty - if (data.indexOf("[]") === -1) { + if (data.indexOf("[]") !== -1) { // there was an empty response + + haltestelle.getHaltestellen(); + + } else { // replace useless chars & split string into array - data = data.replace(/\],\[/gi, '#'); // insert a special char to mark internal array boundaries data = data.replace(/\(.+?\)/gi, ''); // remove all content in round parentheses data = data.replace('ß', 'ss'); // remove some special characters data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection - data = data.slice(3,-3).split("#"); // split on the inserted char to get an array of arrays + data = data.slice(3, -3).split("],["); // split at array boundaries to get an array of arrays // debug log if (DEBUG === 1) {console.log("parsed data: " + data);} @@ -134,7 +137,7 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { dataLength = data.length; // generate table header - htmlOutput = ""; + htmlOutput = "
"; htmlOutput += ""; htmlOutput += ""; htmlOutput += ""; @@ -150,27 +153,26 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { for (y = 0; y < 3; y++) { // debug log if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);} - htmlOutput += ""; + htmlOutput += ""; } htmlOutput += ""; } // close table htmlOutput += "
LinieRichtung" + entry[y].slice(1,-1) + "" + entry[y].slice(1, -1) + "
"; - } else { // there was an empty response - htmlOutput = "

Haltestelleneingabe nicht eindeutig

"; + // print content into web page + target.innerHTML = htmlOutput; + } - // print content into web page - target.innerHTML = htmlOutput; }, - getHaltestellen : function (event) { + getHaltestellen : function () { // debug log if (DEBUG === 1) {console.log("getHaltestellen() method");} // prevent submit default behaviour - event.preventDefault(); + //event.preventDefault(); // construct ajax url var hstName = document.getElementById("q").value, @@ -187,12 +189,55 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) { if (DEBUG === 1) {console.log("processHaltestellen() method");} if (DEBUG === 1) {console.log("received data: " + data);} + //variable definitions + var i, + entry, + dataLength, + htmlOutput; + + // process of response only if it's not empty + if (data.indexOf("[]") !== -1) { + htmlOutput = "

unbekannte Haltstelle, bitte erneut versuchen

"; + } else { + + // replace useless chars & split string into array + data = data.replace(/\[\[\[.+?\]\],/gi, '['); // remove useless first city entry + data = data.replace(/\(.+?\)/gi, ''); // remove all content in round parentheses + data = data.replace('ß', 'ss'); // remove some special characters + data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection + data = data.slice(4, -4).split("],["); // split at array boundaries to get an array of arrays + + // debug log + if (DEBUG === 1) {console.log("parsed data: " + data);} + + dataLength = data.length; + + // generate table header + htmlOutput = ""; + + // generate table entries + for (i = 0; i < dataLength; i++) { + htmlOutput += ""; + entry = data[i].split(","); + // debug log + if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);} + + htmlOutput += ""; + htmlOutput += ""; + } + // close table + htmlOutput += "
" + entry[0].slice(1, -1) + "
"; + + } + + // print content into web page + target.innerHTML = htmlOutput; } }; //event listeners searchForm.addEventListener("submit", haltestelle.getAbfahrten, false); - searchForm.addEventListener("submit", haltestelle.getHaltestellen, false); + //searchForm.addEventListener("submit", haltestelle.getHaltestellen, false); }()); // end of anonymous function