ui.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var UI_CELLS = [];
  2. function dCreate(domName) {
  3. return document.createElement(domName);
  4. }
  5. function uiCreateCell(cellData, x, y) {
  6. var cell = dCreate("div");
  7. cell.className = R.klass.cell.item;
  8. if (cellData.isBlack) {
  9. cell.classList.add(R.klass.cell.black);
  10. } else if (cellData.definitions !== null) {
  11. cell.classList.add(R.klass.cell.definition);
  12. var cellContent =dCreate("span");
  13. for (var i =0, nbDefinitions = cellData.definitions.length; i < nbDefinitions; i++) {
  14. var definition = cellData.definitions[i]
  15. ,domDefinition = dCreate("span");
  16. domDefinition.dataset.x = x;
  17. domDefinition.dataset.y = y;
  18. domDefinition.dataset.definition = i;
  19. domDefinition.className = R.klass.cell.definitions.item;
  20. domDefinition.innerHTML = definition.text.join("<br/>");
  21. cellContent.appendChild(domDefinition);
  22. }
  23. cell.appendChild(cellContent);
  24. } else {
  25. cell.classList.add(R.klass.cell.letter);
  26. }
  27. return cell;
  28. }
  29. function uiCreateGrid() {
  30. var frag = document.createDocumentFragment();
  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. document.body.textContent = "";
  73. document.body.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. i.dom.classList.add(R.klass.cell.found);
  82. }
  83. } else {
  84. i.dom.textContent = "";
  85. }
  86. }
  87. });
  88. }
  89. function gridClickDelegate(e) {
  90. var target = e.target;
  91. while (target && (target.dataset && !target.dataset.x))
  92. target = target.parentElement;
  93. if (target && target.dataset && target.dataset.x && target.dataset.y) {
  94. var clickedCell = UI_CELLS[parseInt(target.dataset.x, 10) +parseInt(target.dataset.y, 10) *GRID.width];
  95. if (clickedCell.data.isBlack) {
  96. unselect();
  97. } else if (clickedCell.data.definitions) {
  98. var first = true;
  99. unselect();
  100. if (clickedCell.data.definitions[target.dataset.definition]) {
  101. clickedCell.data.definitions[target.dataset.definition].word.forEach(function(coordinates) {
  102. select(coordinates[0], coordinates[1]);
  103. if (first && !UI_CELLS[coordinates[0] +coordinates[1] *GRID.width].data.found) {
  104. CURRENTINPUT = UI_CELLS[coordinates[0] +coordinates[1] *GRID.width];
  105. CURRENTINPUT.dom.classList.add(R.klass.cell.currentInput);
  106. }
  107. first = false;
  108. });
  109. }
  110. } else {
  111. var words = GRID.getWord(clickedCell.x, clickedCell.y);
  112. unselect();
  113. words.forEach(function (word) {
  114. word.forEach(function (coordinates) {
  115. select(coordinates[0], coordinates[1]);
  116. });
  117. });
  118. target.classList.add(R.klass.cell.currentInput);
  119. CURRENTINPUT = clickedCell;
  120. }
  121. }
  122. }