Some experiments with web applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. // variable to en-/disable debug
  2. // set to 1 to enable debug log
  3. var DEBUG;
  4. // wrap all in anonymous function to get out of global scope
  5. (function() {
  6. //variable definitions
  7. var serverUrl = gcf.getCgiBinPath(), // the url of the cgi-bin folder with the scripts we call with the ajaxCall() function
  8. target = document.getElementById("output"), // get the html area where we print out the data
  9. searchForm = document.getElementById("search-form"), // get the search form
  10. haltestelle; // object with methods to get and process the data
  11. // debug log
  12. if (DEBUG === 1) {console.log("anonymous function");}
  13. // haltestelle object
  14. haltestelle = {
  15. getAbfahrten : function (event) {
  16. // debug log
  17. if (DEBUG === 1) {console.log("getAbfahrten() method");}
  18. // prevent submit default behaviour
  19. event.preventDefault();
  20. // construct ajax url
  21. var hstName = document.getElementById("q").value,
  22. hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?query=abfahrten.do&ort=dresden&hst=" + hstName);
  23. // get the data from the server with an ajax call
  24. gcf.ajaxCall(hstUrl, target, this.processAbfahrten, "text");
  25. },
  26. processAbfahrten : function (data) {
  27. // debug log
  28. if (DEBUG === 1) {console.log("processAbfahrten() method");}
  29. if (DEBUG === 1) {console.log("received data: " + data);}
  30. //variable definitions
  31. var i,
  32. y,
  33. htmlOutput,
  34. entry,
  35. dataLength;
  36. // process of response only if it's not empty
  37. if (data.indexOf("[]") !== -1) { // there was an empty response
  38. this.getHaltestellen();
  39. } else {
  40. // replace useless chars & split string into array
  41. data = data.replace(/\(.+?\)/gi, ''); // remove all content in round parentheses
  42. data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection
  43. data = data.slice(3, -3).split("],["); // split at array boundaries to get an array of arrays
  44. // debug log
  45. if (DEBUG === 1) {console.log("parsed data: " + data);}
  46. dataLength = data.length;
  47. // generate table header
  48. htmlOutput = "<table>";
  49. htmlOutput += "<tr>";
  50. htmlOutput += "<th>Linie</th>";
  51. htmlOutput += "<th>Richtung</th>";
  52. htmlOutput += "<th>Abfahrt</th>";
  53. htmlOutput += "</tr>";
  54. // generate table entries
  55. for (i = 0; i < dataLength; i++) {
  56. htmlOutput += "<tr>";
  57. entry = data[i].split(",");
  58. // debug log
  59. if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
  60. for (y = 0; y < 3; y++) {
  61. // debug log
  62. if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
  63. htmlOutput += "<td>" + entry[y].slice(1, -1) + "</td>";
  64. }
  65. htmlOutput += "</tr>";
  66. }
  67. // close table
  68. htmlOutput += "</table>";
  69. // print content into web page
  70. target.innerHTML = htmlOutput;
  71. }
  72. },
  73. getHaltestellen : function () {
  74. // debug log
  75. if (DEBUG === 1) {console.log("getHaltestellen() method");}
  76. // prevent submit default behaviour
  77. //event.preventDefault();
  78. // construct ajax url
  79. var hstName = document.getElementById("q").value,
  80. hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?query=haltestelle.do&ort=dresden&hst=" + hstName);
  81. // get the data from the server with an ajax call
  82. gcf.ajaxCall(hstUrl, target, this.processHaltestellen, "text");
  83. },
  84. processHaltestellen : function (data) {
  85. // debug log
  86. if (DEBUG === 1) {console.log("processHaltestellen() method");}
  87. if (DEBUG === 1) {console.log("received data: " + data);}
  88. //variable definitions
  89. var i,
  90. entry,
  91. dataLength,
  92. htmlOutput;
  93. // process of response only if it's not empty
  94. if (data.indexOf("[]") !== -1) {
  95. htmlOutput = "<p>unbekannte Haltstelle, bitte erneut versuchen</p>";
  96. } else {
  97. // replace useless chars & split string into array
  98. data = data.replace(/\[\[\[.+?\]\],/gi, '['); // remove useless first city entry
  99. data = data.replace(/\(.+?\)/gi, ''); // remove all content in round parentheses
  100. data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection
  101. data = data.slice(4, -4).split("],["); // split at array boundaries to get an array of arrays
  102. // debug log
  103. if (DEBUG === 1) {console.log("parsed data: " + data);}
  104. dataLength = data.length;
  105. // generate table header
  106. htmlOutput = "<table>";
  107. // generate table entries
  108. for (i = 0; i < dataLength; i++) {
  109. htmlOutput += "<tr>";
  110. entry = data[i].split(",");
  111. // debug log
  112. if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
  113. htmlOutput += "<td>" + entry[0].slice(1, -1) + "</td>";
  114. htmlOutput += "</tr>";
  115. }
  116. // close table
  117. htmlOutput += "</table>";
  118. }
  119. // print content into web page
  120. target.innerHTML = htmlOutput;
  121. }
  122. };
  123. //event listeners
  124. searchForm.addEventListener("submit", haltestelle.getAbfahrten, false);
  125. //searchForm.addEventListener("submit", haltestelle.getHaltestellen, false);
  126. }()); // end of anonymous function