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.

167 lines
5.0 KiB

  1. // variable to en-/disable debug
  2. // set to 1 to enable debug log
  3. var DEBUG;
  4. // xmlhttp object function
  5. function getHTTPObject() {
  6. // debug log
  7. if (DEBUG === 1) {console.log("xml http object function");}
  8. // variable definitions
  9. var xhr;
  10. // check for availibility if xmlhttprequest object
  11. if(window.XMLHttpRequest) {
  12. xhr = new XMLHttpRequest();
  13. } else if(window.ActiveXObject) {
  14. xhr = new ActiveXObject("Msxml2.XMLHTTP");
  15. }
  16. return xhr;
  17. }
  18. // ajax call function
  19. function ajaxCall(dataUrl, outputElement, callback, responseType) {
  20. // debug log
  21. if (DEBUG === 1) {console.log("ajax function");}
  22. // variable definitions
  23. var response;
  24. var request = getHTTPObject(); // get the xmlhttp object which is supported
  25. outputElement.innerHTML = "Lade Daten ...";
  26. request.onreadystatechange = function() {
  27. if(request.readyState === 4 && request.status === 200) {
  28. //save ajax response
  29. if (responseType === "json") {
  30. response = JSON.parse(request.responseText);
  31. } else if (responseType === "xml") {
  32. response = request.responseXML;
  33. } else {
  34. response = request.responseText;
  35. }
  36. // check if callback is a function
  37. if(typeof callback === "function") {
  38. callback(response);
  39. }
  40. }
  41. };
  42. request.open("get", dataUrl, true);
  43. request.send(null);
  44. }
  45. // wrap all in anonymous function to get out of global scope
  46. (function() {
  47. //variable definitions
  48. var serverUrl;
  49. var origin = window.location.origin.indexOf("www.goodcleanfun.de");
  50. // debug log
  51. if (DEBUG === 1) {console.log("anonymous function");}
  52. // server url
  53. if (origin === -1) {
  54. serverUrl = "http://goodcleanfun.de/cgi-bin/";
  55. } else {
  56. serverUrl = "http://www.goodcleanfun.de/cgi-bin/";
  57. }
  58. // get the search form
  59. var searchForm = document.getElementById("search-form");
  60. // haltestelle object
  61. var haltestelle = {
  62. getInfo : function(event) {
  63. // debug log
  64. if (DEBUG === 1) {console.log("getInfo function");}
  65. // prevent submit default behaviour
  66. event.preventDefault();
  67. // get output area
  68. var target = document.getElementById("output");
  69. var hstName = document.getElementById("q").value;
  70. var hstUrl = encodeURI(serverUrl + "abfahrtsmonitor.py?ort=dresden&hst=" + hstName);
  71. // get the data from the server with an ajax call
  72. ajaxCall(hstUrl, target, function(data) {
  73. // debug log
  74. if (DEBUG === 1) {console.log("received data: " + data);}
  75. //variable definitions
  76. var i;
  77. var y;
  78. var htmlOutput;
  79. var entry;
  80. var dataLength;
  81. // process of response only if it's not empty
  82. if (data.indexOf("[]") === -1) {
  83. // replace useless chars
  84. // split string into array
  85. data = data.replace(/\],\[/gi, '#');
  86. data = data.replace(/\(.+?\)/gi, '');
  87. data = data.replace('ß', 'ss');
  88. data = data.replace(/<(.+?)>/gi, '$1');
  89. data = data.slice(3,-3).split("#");
  90. // debug log
  91. if (DEBUG === 1) {console.log("parsed data: " + data);}
  92. dataLength = data.length;
  93. // generate table header
  94. htmlOutput = "<table>";
  95. htmlOutput += "<tr>";
  96. htmlOutput += "<th>Linie</th>";
  97. htmlOutput += "<th>Richtung</th>";
  98. htmlOutput += "<th>Abfahrt</th>";
  99. htmlOutput += "</tr>";
  100. // generate table entries
  101. for (i = 0; i < dataLength; i++) {
  102. htmlOutput += "<tr>";
  103. entry = data[i].split(",");
  104. // debug log
  105. if (DEBUG === 1) {console.log("part " + i + " of parsed data: " + entry);}
  106. for (y = 0; y < 3; y++) {
  107. // debug log
  108. if (DEBUG === 1) {console.log("part " + y + ": " + entry[y]);}
  109. htmlOutput += "<td>" + entry[y].slice(1,-1) + "</td>";
  110. }
  111. htmlOutput += "</tr>";
  112. }
  113. // close table
  114. htmlOutput += "</table>";
  115. } else { // there was an empty response
  116. htmlOutput = "<p>Haltestelleneingabe nicht eindeutig</p>";
  117. }
  118. // print content into web page
  119. target.innerHTML = htmlOutput;
  120. }, "text");
  121. }
  122. };
  123. //event listeners
  124. searchForm.addEventListener("submit", haltestelle.getInfo, false);
  125. })(); // end of anonymous function