Browse Source

minor code factoring

* rename 'haltestelle.getInfo()' method to 'haltestelle.getAbfahrten()'
* move the ajax response processing from the anonymous callback function in the'haltestelle.getAbfahrten()' method into own 'haltestelle.processAbfahrten()' method
* adapt the eventListener to the renaming into the 'haltestelle.getAbfahrten()' method
master
T. Meissner 12 years ago
parent
commit
409916f414
1 changed files with 57 additions and 54 deletions
  1. +57
    -54
      scripts/haltestellen.js

+ 57
- 54
scripts/haltestellen.js View File

@ -67,6 +67,7 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) {
//variable definitions //variable definitions
var serverUrl; var serverUrl;
var target = document.getElementById("output");
var origin = window.location.origin.indexOf("www.goodcleanfun.de"); var origin = window.location.origin.indexOf("www.goodcleanfun.de");
// debug log // debug log
@ -85,83 +86,85 @@ function ajaxCall(dataUrl, outputElement, callback, responseType) {
// haltestelle object // haltestelle object
var haltestelle = { var haltestelle = {
getInfo : function(event) {
getAbfahrten : function(event) {
// debug log // debug log
if (DEBUG === 1) {console.log("getInfo function");}
if (DEBUG === 1) {console.log("getAbfahrten() method");}
// prevent submit default behaviour // prevent submit default behaviour
event.preventDefault(); event.preventDefault();
// get output area
var target = document.getElementById("output");
// construct ajax url
var hstName = document.getElementById("q").value; var hstName = document.getElementById("q").value;
var hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?ort=dresden&hst=" + hstName); var hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?ort=dresden&hst=" + hstName);
// get the data from the server with an ajax call // get the data from the server with an ajax call
ajaxCall(hstUrl, target, function(data) {
ajaxCall(hstUrl, target, haltestelle.processAbfahrten, "text");
},
processAbfahrten : function (data) {
// debug log
if (DEBUG === 1) {console.log("processAbfahrten() method");}
if (DEBUG === 1) {console.log("received data: " + data);}
//variable definitions
var i;
var y;
var htmlOutput;
var entry;
var dataLength;
// process of response only if it's not empty
if (data.indexOf("[]") === -1) {
// 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
// debug log // debug log
if (DEBUG === 1) {console.log("received data: " + data);}
//variable definitions
var i;
var y;
var htmlOutput;
var entry;
var dataLength;
// process of response only if it's not empty
if (data.indexOf("[]") === -1) {
// replace useless chars
// split string into array
data = data.replace(/\],\[/gi, '#');
data = data.replace(/\(.+?\)/gi, '');
data = data.replace('Ăź', 'ss');
data = data.replace(/<(.+?)>/gi, '$1');
data = data.slice(3,-3).split("#");
if (DEBUG === 1) {console.log("parsed data: " + data);}
// debug log
if (DEBUG === 1) {console.log("parsed data: " + data);}
dataLength = data.length;
dataLength = data.length;
// generate table header
htmlOutput = "<table>";
htmlOutput += "<tr>";
htmlOutput += "<th>Linie</th>";
htmlOutput += "<th>Richtung</th>";
htmlOutput += "<th>Abfahrt</th>";
htmlOutput += "</tr>";
// generate table header
htmlOutput = "<table>";
// generate table entries
for (i = 0; i < dataLength; i++) {
htmlOutput += "<tr>"; htmlOutput += "<tr>";
htmlOutput += "<th>Linie</th>";
htmlOutput += "<th>Richtung</th>";
htmlOutput += "<th>Abfahrt</th>";
htmlOutput += "</tr>";
// generate table entries
for (i = 0; i < dataLength; i++) {
htmlOutput += "<tr>";
entry = data[i].split(",");
entry = data[i].split(",");
// debug log
if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
for (y = 0; y < 3; y++) {
// debug log // debug log
if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
for (y = 0; y < 3; y++) {
// debug log
if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
htmlOutput += "<td>" + entry[y].slice(1,-1) + "</td>";
}
htmlOutput += "</tr>";
if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
htmlOutput += "<td>" + entry[y].slice(1,-1) + "</td>";
} }
// close table
htmlOutput += "</table>";
} else { // there was an empty response
htmlOutput = "<p>Haltestelleneingabe nicht eindeutig</p>";
htmlOutput += "</tr>";
} }
// close table
htmlOutput += "</table>";
} else { // there was an empty response
htmlOutput = "<p>Haltestelleneingabe nicht eindeutig</p>";
}
// print content into web page
target.innerHTML = htmlOutput;
}, "text");
// print content into web page
target.innerHTML = htmlOutput;
} }
}; };
//event listeners //event listeners
searchForm.addEventListener("submit", haltestelle.getInfo, false);
searchForm.addEventListener("submit", haltestelle.getAbfahrten, false);
})(); // end of anonymous function })(); // end of anonymous function

Loading…
Cancel
Save