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.

200 lines
6.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 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('ß', 'ss'); // remove some special characters
  52. data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection
  53. data = data.slice(3, -3).split("],["); // split at array boundaries to get an array of arrays
  54. // debug log
  55. if (DEBUG === 1) {console.log("parsed data: " + data);}
  56. dataLength = data.length;
  57. // generate table header
  58. htmlOutput = "<table>";
  59. htmlOutput += "<tr>";
  60. htmlOutput += "<th>Linie</th>";
  61. htmlOutput += "<th>Richtung</th>";
  62. htmlOutput += "<th>Abfahrt</th>";
  63. htmlOutput += "</tr>";
  64. // generate table entries
  65. for (i = 0; i < dataLength; i++) {
  66. htmlOutput += "<tr>";
  67. entry = data[i].split(",");
  68. // debug log
  69. if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
  70. for (y = 0; y < 3; y++) {
  71. // debug log
  72. if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
  73. htmlOutput += "<td>" + entry[y].slice(1, -1) + "</td>";
  74. }
  75. htmlOutput += "</tr>";
  76. }
  77. // close table
  78. htmlOutput += "</table>";
  79. // print content into web page
  80. target.innerHTML = htmlOutput;
  81. }
  82. },
  83. getHaltestellen : function () {
  84. // debug log
  85. if (DEBUG === 1) {console.log("getHaltestellen() method");}
  86. // prevent submit default behaviour
  87. //event.preventDefault();
  88. // construct ajax url
  89. var hstName = document.getElementById("q").value,
  90. hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?query=haltestelle.do&ort=dresden&hst=" + hstName);
  91. // get the data from the server with an ajax call
  92. gcf.ajaxCall(hstUrl, target, this.processHaltestellen, "text");
  93. },
  94. processHaltestellen : function (data) {
  95. // debug log
  96. if (DEBUG === 1) {console.log("processHaltestellen() method");}
  97. if (DEBUG === 1) {console.log("received data: " + data);}
  98. //variable definitions
  99. var i,
  100. entry,
  101. dataLength,
  102. htmlOutput;
  103. // process of response only if it's not empty
  104. if (data.indexOf("[]") !== -1) {
  105. htmlOutput = "<p>unbekannte Haltstelle, bitte erneut versuchen</p>";
  106. } else {
  107. // replace useless chars & split string into array
  108. data = data.replace(/\[\[\[.+?\]\],/gi, '['); // remove useless first city entry
  109. data = data.replace(/\(.+?\)/gi, ''); // remove all content in round parentheses
  110. data = data.replace('ß', 'ss'); // remove some special characters
  111. data = data.replace(/<(.+?)>/gi, '$1'); // remove tag parentheses to prevent code injection
  112. data = data.slice(4, -4).split("],["); // split at array boundaries to get an array of arrays
  113. // debug log
  114. if (DEBUG === 1) {console.log("parsed data: " + data);}
  115. dataLength = data.length;
  116. // generate table header
  117. htmlOutput = "<table>";
  118. // generate table entries
  119. for (i = 0; i < dataLength; i++) {
  120. htmlOutput += "<tr>";
  121. entry = data[i].split(",");
  122. // debug log
  123. if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
  124. htmlOutput += "<td>" + entry[0].slice(1, -1) + "</td>";
  125. htmlOutput += "</tr>";
  126. }
  127. // close table
  128. htmlOutput += "</table>";
  129. }
  130. // print content into web page
  131. target.innerHTML = htmlOutput;
  132. }
  133. };
  134. //event listeners
  135. searchForm.addEventListener("submit", haltestelle.getAbfahrten, false);
  136. //searchForm.addEventListener("submit", haltestelle.getHaltestellen, false);
  137. }()); // end of anonymous function