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.

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