Simulate graph states in the browser
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.

42 lines
1.0KB

  1. var api = {};
  2. api.poll = function() {
  3. var xmlhttp = new XMLHttpRequest();
  4. xmlhttp.onreadystatechange = function() {
  5. if (xmlhttp.readyState == XMLHttpRequest.DONE) {
  6. if (xmlhttp.status == 200) {
  7. api.update(xmlhttp.responseText);
  8. }
  9. }
  10. };
  11. // Send the request
  12. xmlhttp.open("GET", window.location.href+"/graph", true);
  13. xmlhttp.send();
  14. };
  15. api.update = function(s) {
  16. json = JSON.parse(s);
  17. graph.update(json);
  18. }
  19. api.edit = function(edit) {
  20. var xmlhttp = new XMLHttpRequest();
  21. xmlhttp.onreadystatechange = function() {
  22. if (xmlhttp.readyState == XMLHttpRequest.DONE) {
  23. if (xmlhttp.status == 200) {
  24. api.update(xmlhttp.responseText);
  25. }
  26. }
  27. };
  28. // Send the request
  29. xmlhttp.open("POST", window.location.href+"/edit", true);
  30. xmlhttp.setRequestHeader("Content-Type", "application/json");
  31. xmlhttp.send(JSON.stringify(edit));
  32. }
  33. // Launch the polling loop
  34. setInterval(api.poll, 1000);