uiGrid.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var UI_CELLS = [];
  2. function uiCreateCell(cellData, x, y) {
  3. var cell = dCreate("div");
  4. cell.className = R.klass.cell.item;
  5. if (cellData.isBlack) {
  6. cell.classList.add(R.klass.cell.black);
  7. } else if (cellData.definitions !== null) {
  8. cell.classList.add(R.klass.cell.definition);
  9. var cellContent =dCreate("span");
  10. for (var i =0, nbDefinitions = cellData.definitions.length; i < nbDefinitions; i++) {
  11. var definition = cellData.definitions[i]
  12. ,domDefinition = dCreate("span");
  13. domDefinition.dataset.x = x;
  14. domDefinition.dataset.y = y;
  15. domDefinition.dataset.definition = i;
  16. domDefinition.className = R.klass.cell.definitions.item;
  17. domDefinition.innerHTML = definition.text.join("<br/>");
  18. cellContent.appendChild(domDefinition);
  19. }
  20. cell.appendChild(cellContent);
  21. } else {
  22. cell.classList.add(R.klass.cell.letter);
  23. }
  24. return cell;
  25. }
  26. function uiCreateGrid() {
  27. var frag = document.createDocumentFragment();
  28. dGet(R.id.scoreboard.header.title).textContent = GRID.title;
  29. dGet(R.id.scoreboard.header.difficulty).textContent = GRID.difficulty;
  30. for (var i =0; i < GRID.height; i++) {
  31. var line = dCreate("div");
  32. line.className = R.klass.line;
  33. frag.appendChild(line);
  34. for (var j =0; j < GRID.width; j++) {
  35. var cell = uiCreateCell(GRID.grid[j][i], j, i);
  36. cell.dataset.x = j;
  37. cell.dataset.y = i;
  38. line.appendChild(cell);
  39. UI_CELLS.push({
  40. x: j
  41. ,y: i
  42. ,dom: cell
  43. ,data: GRID.grid[j][i]
  44. });
  45. }
  46. }
  47. for (var i =0; i < GRID.height; i++) {
  48. for (var j =0; j < GRID.width; j++) {
  49. var cell = UI_CELLS[i *GRID.width +j];
  50. if (cell.data.definitions) {
  51. cell.data.definitions.forEach(function(d) {
  52. switch (d.direction) {
  53. case Definition.RIGHT_VERTICAL:
  54. UI_CELLS[i *GRID.width +j +1].dom.classList.add(R.klass.cell.definitions.rightVertical);
  55. break;
  56. case Definition.RIGHT_HORIZONTAL:
  57. UI_CELLS[i *GRID.width +j +1].dom.classList.add(R.klass.cell.definitions.rightHorizontal);
  58. break;
  59. case Definition.BOTTOM_VERTICAL:
  60. UI_CELLS[(i +1) *GRID.width +j].dom.classList.add(R.klass.cell.definitions.bottomVertical);
  61. break;
  62. case Definition.BOTTOM_HORIZONTAL:
  63. UI_CELLS[(i +1) *GRID.width +j].dom.classList.add(R.klass.cell.definitions.bottomHorizontal);
  64. break;
  65. }
  66. });
  67. }
  68. }
  69. }
  70. onGridUpdated();
  71. var gridContainer = dGet(R.id.grid);
  72. gridContainer.textContent = "";
  73. gridContainer.appendChild(frag);
  74. }
  75. function onGridUpdated() {
  76. UI_CELLS.forEach(function(i) {
  77. if (!i.data.definitions && !i.data.isBlack) {
  78. if (i.data.letter) {
  79. i.dom.textContent = i.data.letter;
  80. if (i.data.found) {
  81. var className = i.dom.className;
  82. i.dom.classList.add(R.klass.cell.found);
  83. i.dom.classList.remove(R.klass.cell.wrong);
  84. i.dom.style.color = i.data.found.color;
  85. }
  86. } else {
  87. i.dom.textContent = "";
  88. }
  89. }
  90. });
  91. }
  92. function moveInput(cell) {
  93. var input = dGet(R.id.input);
  94. input.style.top = cell.dom.offsetTop +"px";
  95. input.style.left = cell.dom.offsetLeft +"px";
  96. input.focus();
  97. }
  98. function gridClickDelegate(e) {
  99. var target = e.target;
  100. while (target && (target.dataset && !target.dataset.x))
  101. target = target.parentElement;
  102. if (target && target.dataset && target.dataset.x && target.dataset.y) {
  103. var clickedCell = UI_CELLS[parseInt(target.dataset.x, 10) +parseInt(target.dataset.y, 10) *GRID.width];
  104. if (clickedCell.data.isBlack) {
  105. unselect();
  106. } else {
  107. if (clickedCell.data.definitions) {
  108. var first = true;
  109. unselect();
  110. if (clickedCell.data.definitions[target.dataset.definition]) {
  111. clickedCell.data.definitions[target.dataset.definition].word.forEach(function(coordinates) {
  112. select(coordinates[0], coordinates[1]);
  113. if (first && !UI_CELLS[coordinates[0] +coordinates[1] *GRID.width].data.found) {
  114. CURRENTINPUT = UI_CELLS[coordinates[0] +coordinates[1] *GRID.width];
  115. CURRENTINPUT.dom.classList.add(R.klass.cell.currentInput);
  116. }
  117. first = false;
  118. });
  119. }
  120. } else {
  121. var words = GRID.getWord(clickedCell.x, clickedCell.y);
  122. unselect();
  123. words.forEach(function (word) {
  124. word.forEach(function (coordinates) {
  125. select(coordinates[0], coordinates[1]);
  126. });
  127. });
  128. target.classList.add(R.klass.cell.currentInput);
  129. CURRENTINPUT = clickedCell;
  130. }
  131. // Display mobile / tablet keyboard
  132. moveInput(CURRENTINPUT);
  133. }
  134. }
  135. }