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.

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