workflow.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var GRID_PUBLIC_ID
  2. ,KNOWN_VERSION = 0
  3. ,GRID
  4. ,SELECTED = []
  5. ,CURRENTINPUT = null;
  6. function doGet(url, callback) {
  7. var xhr = new XMLHttpRequest();
  8. xhr.onreadystatechange = function(e) {
  9. if (xhr.readyState === 4) {
  10. var resp = null;
  11. if (xhr.status === 200) {
  12. resp = xhr.response;
  13. try {
  14. resp = JSON.parse(/** @type {string} */ (resp));
  15. } catch (e) {
  16. resp = null;
  17. }
  18. }
  19. callback(xhr.status, resp);
  20. }
  21. };
  22. xhr.open('GET', url, true);
  23. xhr.send(null);
  24. }
  25. function initPolling() {
  26. doGet("/api/poll?grid=" +GRID_PUBLIC_ID +"&v=" +KNOWN_VERSION, function(status, resp) {
  27. if (resp) {
  28. GRID = new Grid(resp);
  29. if (resp["grid"]) {
  30. KNOWN_VERSION = Math.max(GRID.update(resp["grid"]) || 0, KNOWN_VERSION);
  31. uiCreateGrid();
  32. }
  33. KNOWN_VERSION = Math.max(KNOWN_VERSION, resp["v"] || 0);
  34. } // TODO else cannot init party
  35. });
  36. }
  37. function pollNow() {
  38. // TODO avoid duplicate call / abort planned poll
  39. doGet("/api/poll?grid=" +GRID_PUBLIC_ID +"&v=" +KNOWN_VERSION, function(status, resp) {
  40. if (resp && resp["grid"]) {
  41. var newVersion = Math.max(GRID.update(resp["grid"]) || 0, KNOWN_VERSION);
  42. if (newVersion !== KNOWN_VERSION) {
  43. onGridUpdated();
  44. KNOWN_VERSION = newVersion;
  45. }
  46. }
  47. // TODO plan next poll
  48. });
  49. }
  50. function keyPressHandler(cell, key) {
  51. cell.dom.classList.add(R.klass.cell.pending);
  52. doGet("/api/put?grid=" +GRID_PUBLIC_ID +"&key=" +key +"&x=" +cell.x +"&y=" +cell.y, function(status, resp) {
  53. cell.dom.classList.remove(R.klass.cell.pending);
  54. if (status === 403 && !cell.data.found) {
  55. cell.dom.classList.add(R.klass.cell.wrong);
  56. // TODO wrong
  57. } else if (status === 204) {
  58. cell.dom.classList.remove(R.klass.cell.wrong);
  59. cell.data.letter = key;
  60. cell.data.found = true;
  61. onGridUpdated();
  62. } else {
  63. // out of sync ?
  64. pollNow();
  65. }
  66. });
  67. }
  68. function unselect() {
  69. SELECTED.forEach(function (cell) {
  70. cell.dom.classList.remove(R.klass.cell.selected);
  71. });
  72. if (CURRENTINPUT) {
  73. CURRENTINPUT.dom.classList.remove(R.klass.cell.currentInput);
  74. CURRENTINPUT = null;
  75. }
  76. SELECTED = [];
  77. }
  78. function select(x, y) {
  79. var cell = UI_CELLS[x +y *GRID.width];
  80. SELECTED.push(cell);
  81. cell.dom.classList.add(R.klass.cell.selected);
  82. }
  83. document.addEventListener('DOMContentLoaded', function() {
  84. GRID_PUBLIC_ID = (document.location.hash.substr(1));
  85. if (GRID_PUBLIC_ID == "") {
  86. document.location.href = "/";
  87. return;
  88. }
  89. document.addEventListener('click', gridClickDelegate);
  90. document.addEventListener('keypress', function(e) {
  91. if (CURRENTINPUT && !CURRENTINPUT.data.found) {
  92. var key = e.key.charAt(0).toUpperCase();
  93. if (key.match(/[A-Z]/)) {
  94. CURRENTINPUT.data.letter = key;
  95. keyPressHandler(CURRENTINPUT, key);
  96. onGridUpdated();
  97. }
  98. }
  99. });
  100. initPolling();
  101. });