uiGrid.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var UI_CELLS = []
  2. ,SELECTED_INDEX = null;
  3. function uiCreateCell(cellData, x, y) {
  4. var cell = dCreate("div");
  5. cell.className = R.klass.cell.item;
  6. if (cellData.isBlack) {
  7. cell.classList.add(R.klass.cell.black);
  8. } else if (cellData.definitions !== null) {
  9. cell.classList.add(R.klass.cell.definition);
  10. var cellContent =dCreate("span");
  11. for (var i =0, nbDefinitions = cellData.definitions.length; i < nbDefinitions; i++) {
  12. var definition = cellData.definitions[i]
  13. ,domDefinition = dCreate("span");
  14. domDefinition.dataset.x = x;
  15. domDefinition.dataset.y = y;
  16. domDefinition.dataset.definition = i;
  17. domDefinition.className = R.klass.cell.definitions.item;
  18. domDefinition.innerHTML = definition.text.join("<br/>");
  19. cellContent.appendChild(domDefinition);
  20. }
  21. cell.appendChild(cellContent);
  22. } else {
  23. cell.classList.add(R.klass.cell.letter);
  24. }
  25. return cell;
  26. }
  27. function uiCreateGrid() {
  28. var frag = document.createDocumentFragment();
  29. dGet(R.id.scoreboard.header.title).textContent = GRID.title;
  30. dGet(R.id.scoreboard.header.difficulty).textContent = GRID.difficulty;
  31. for (var i =0; i < GRID.height; i++) {
  32. var line = dCreate("div");
  33. line.className = R.klass.line;
  34. frag.appendChild(line);
  35. for (var j =0; j < GRID.width; j++) {
  36. var cell = uiCreateCell(GRID.grid[j][i], j, i);
  37. cell.dataset.x = j;
  38. cell.dataset.y = i;
  39. line.appendChild(cell);
  40. UI_CELLS.push({
  41. x: j
  42. ,y: i
  43. ,dom: cell
  44. ,data: GRID.grid[j][i]
  45. });
  46. }
  47. }
  48. for (var i =0; i < GRID.height; i++) {
  49. for (var j =0; j < GRID.width; j++) {
  50. var cell = UI_CELLS[i *GRID.width +j];
  51. if (cell.data.definitions) {
  52. cell.data.definitions.forEach(function(d) {
  53. switch (d.direction) {
  54. case Definition.RIGHT_VERTICAL:
  55. UI_CELLS[i *GRID.width +j +1].dom.classList.add(R.klass.cell.definitions.rightVertical);
  56. break;
  57. case Definition.RIGHT_HORIZONTAL:
  58. UI_CELLS[i *GRID.width +j +1].dom.classList.add(R.klass.cell.definitions.rightHorizontal);
  59. break;
  60. case Definition.BOTTOM_VERTICAL:
  61. UI_CELLS[(i +1) *GRID.width +j].dom.classList.add(R.klass.cell.definitions.bottomVertical);
  62. break;
  63. case Definition.BOTTOM_HORIZONTAL:
  64. UI_CELLS[(i +1) *GRID.width +j].dom.classList.add(R.klass.cell.definitions.bottomHorizontal);
  65. break;
  66. }
  67. });
  68. }
  69. }
  70. }
  71. onGridUpdated();
  72. var gridContainer = dGet(R.id.grid);
  73. gridContainer.textContent = "";
  74. gridContainer.appendChild(frag);
  75. }
  76. function onGridUpdated() {
  77. UI_CELLS.forEach(function(i) {
  78. if (!i.data.definitions && !i.data.isBlack) {
  79. if (i.data.letter) {
  80. i.dom.textContent = i.data.letter;
  81. if (i.data.found) {
  82. var className = i.dom.className;
  83. i.dom.classList.add(R.klass.cell.found);
  84. i.dom.classList.remove(R.klass.cell.wrong);
  85. i.dom.style.color = i.data.found.color;
  86. }
  87. } else {
  88. i.dom.textContent = "";
  89. }
  90. }
  91. });
  92. }
  93. function moveInput(cell) {
  94. var input = dGet(R.id.input);
  95. input.style.top = cell.dom.offsetTop +"px";
  96. input.style.left = cell.dom.offsetLeft +"px";
  97. input.focus();
  98. }
  99. function getUiCell(x, y) {
  100. return UI_CELLS[x +y *GRID.width];
  101. }
  102. function isWordComplete(coordinates) {
  103. for (var i =0, nbCoords = coordinates.length; i < nbCoords; i++)
  104. if (!getUiCell(coordinates[i][0], coordinates[i][1]).data.complete)
  105. return false;
  106. return true;
  107. }
  108. function inputCell(x, y) {
  109. if (CURRENTINPUT)
  110. CURRENTINPUT.dom.classList.remove(R.klass.cell.currentInput);
  111. CURRENTINPUT = getUiCell(x, y);
  112. CURRENTINPUT.dom.classList.add(R.klass.cell.currentInput);
  113. // Display mobile / tablet keyboard
  114. moveInput(CURRENTINPUT);
  115. console.log("Current index: ", SELECTED_INDEX);
  116. }
  117. function uiSelectWritableCell(direction) {
  118. for (var i = SELECTED_INDEX +direction, nbCoords = SELECTED.length; i < nbCoords && i >= 0; i += direction) {
  119. if (!getUiCell(SELECTED[i].x, SELECTED[i].y).data.found) {
  120. inputCell(SELECTED[i].x, SELECTED[i].y);
  121. SELECTED_INDEX = i;
  122. return;
  123. }
  124. }
  125. }
  126. function gridClickDelegate(e) {
  127. var target = e.target;
  128. while (target && (target.dataset && !target.dataset.x))
  129. target = target.parentElement;
  130. if (target && target.dataset && target.dataset.x && target.dataset.y) {
  131. var clickedCell = UI_CELLS[parseInt(target.dataset.x, 10) +parseInt(target.dataset.y, 10) *GRID.width];
  132. if (clickedCell.data.isBlack) {
  133. unselect();
  134. } else {
  135. if (clickedCell.data.definitions) {
  136. var first = true;
  137. unselect();
  138. if (clickedCell.data.definitions[target.dataset.definition]) {
  139. var i =0;
  140. clickedCell.data.definitions[target.dataset.definition].word.forEach(function(coordinates) {
  141. select(coordinates[0], coordinates[1]);
  142. if (first && !getUiCell(coordinates[0], coordinates[1]).data.found) {
  143. SELECTED_INDEX = i;
  144. inputCell(coordinates[0], coordinates[1]);
  145. }
  146. first = false;
  147. i++;
  148. });
  149. }
  150. } else {
  151. var words = GRID.getWord(clickedCell.x, clickedCell.y);
  152. unselect();
  153. // If we have a direction not complete starting with clickedCell
  154. for (var i =0, nbWord = words.length; i < nbWord; i++) {
  155. for (var coordIndex =0, nbCoords = words[i].length; coordIndex < nbCoords; coordIndex++) {
  156. if (getUiCell(words[i][coordIndex][0], words[i][coordIndex][1]).data.found)
  157. continue;
  158. if (words[i][coordIndex][0] == clickedCell.x && words[i][coordIndex][1] == clickedCell.y) {
  159. words[i].forEach(function (coordinates) {
  160. select(coordinates[0], coordinates[1]);
  161. });
  162. SELECTED_INDEX = coordIndex;
  163. inputCell(clickedCell.x, clickedCell.y);
  164. return;
  165. }
  166. break;
  167. }
  168. }
  169. var index = 0;
  170. // We don't have a direction starting with clickedCell, try to find a new one
  171. for (var i =0, nbWords = words.length; i < nbWords; i++) {
  172. if (isWordComplete(words[i]))
  173. continue;
  174. for (var j =0, nbCoords = words[0].length; j < nbCoords; j++) {
  175. var coordinates = words[0][j];
  176. select(coordinates[0], coordinates[1]);
  177. if (coordinates[0] == clickedCell.x && coordinates[1] == clickedCell.y)
  178. SELECTED_INDEX = j;
  179. }
  180. target.classList.add(R.klass.cell.currentInput);
  181. inputCell(clickedCell.x, clickedCell.y);
  182. return;
  183. }
  184. }
  185. }
  186. }
  187. }