Browse Source

further develop support for getting station names

* if we got no data from the server in the haltestelle.processabfahrten() method, we call haltestelle.getHaltestellen() method
* when we got data from the server in the haltestelle.getHaltestellen() method, we call the haltestelle.processHaltestellen() method
* in the haltestelle.processHaltestellen() method we process the gotten data and print it into the output area of the html file
master
T. Meissner 12 years ago
parent
commit
3fc5ce41fe
1 changed files with 58 additions and 13 deletions
  1. +58
    -13
      scripts/haltestellen.js

+ 58
- 13
scripts/haltestellen.js View File

@ -1,6 +1,6 @@
// variable to en-/disable debug // variable to en-/disable debug
// set to 1 to enable debug log // set to 1 to enable debug log
var DEBUG = 1;
var DEBUG;
// xmlhttp object function // xmlhttp object function
@ -119,14 +119,17 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) {
dataLength; dataLength;
// process of response only if it's not empty // 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 // 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(/\(.+?\)/gi, ''); // remove all content in round parentheses
data = data.replace('ß', 'ss'); // remove some special characters data = data.replace('ß', 'ss'); // remove some special characters
data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection 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 // debug log
if (DEBUG === 1) {console.log("parsed data: " + data);} if (DEBUG === 1) {console.log("parsed data: " + data);}
@ -134,7 +137,7 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) {
dataLength = data.length; dataLength = data.length;
// generate table header // generate table header
htmlOutput = "<table>";
htmlOutput = "<table>";
htmlOutput += "<tr>"; htmlOutput += "<tr>";
htmlOutput += "<th>Linie</th>"; htmlOutput += "<th>Linie</th>";
htmlOutput += "<th>Richtung</th>"; htmlOutput += "<th>Richtung</th>";
@ -150,27 +153,26 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) {
for (y = 0; y < 3; y++) { for (y = 0; y < 3; y++) {
// debug log // debug log
if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);} if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
htmlOutput += "<td>" + entry[y].slice(1,-1) + "</td>";
htmlOutput += "<td>" + entry[y].slice(1, -1) + "</td>";
} }
htmlOutput += "</tr>"; htmlOutput += "</tr>";
} }
// close table // close table
htmlOutput += "</table>"; htmlOutput += "</table>";
} else { // there was an empty response
htmlOutput = "<p>Haltestelleneingabe nicht eindeutig</p>";
// print content into web page
target.innerHTML = htmlOutput;
} }
// print content into web page
target.innerHTML = htmlOutput;
}, },
getHaltestellen : function (event) {
getHaltestellen : function () {
// debug log // debug log
if (DEBUG === 1) {console.log("getHaltestellen() method");} if (DEBUG === 1) {console.log("getHaltestellen() method");}
// prevent submit default behaviour // prevent submit default behaviour
event.preventDefault();
//event.preventDefault();
// construct ajax url // construct ajax url
var hstName = document.getElementById("q").value, 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("processHaltestellen() method");}
if (DEBUG === 1) {console.log("received data: " + data);} 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 = "<p>unbekannte Haltstelle, bitte erneut versuchen</p>";
} 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 = "<table>";
// generate table entries
for (i = 0; i < dataLength; i++) {
htmlOutput += "<tr>";
entry = data[i].split(",");
// debug log
if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
htmlOutput += "<td>" + entry[0].slice(1, -1) + "</td>";
htmlOutput += "</tr>";
}
// close table
htmlOutput += "</table>";
}
// print content into web page
target.innerHTML = htmlOutput;
} }
}; };
//event listeners //event listeners
searchForm.addEventListener("submit", haltestelle.getAbfahrten, false); searchForm.addEventListener("submit", haltestelle.getAbfahrten, false);
searchForm.addEventListener("submit", haltestelle.getHaltestellen, false);
//searchForm.addEventListener("submit", haltestelle.getHaltestellen, false);
}()); // end of anonymous function }()); // end of anonymous function

Loading…
Cancel
Save