var rows = 5; var cols = 5; var actField = null; var actMode = "design"; // design / fillin - //inicialiace cele aplikace po startu a pri vycisteni function initApplication() { setRaster(); clearAll(); } //vycisteni mrizky + vymazani vstupnich poli function clearAll() { clearRaster(); document.querySelector("#q_title").innerHTML = ""; document.querySelector("#q_task").innerHTML = ""; } function createNew() { document.getElementById("application").style.display = "block"; setRaster(); clearAll(); } function confirmClearAll() { if (confirm("Všechna vyplněná políčka budou smazána.\n Opravdu pokračovat?")) { clearAll() } } function confirmClearRaster() { if (confirm("Všechna vyplněná políčka budou smazána.\n Opravdu pokračovat?")) { clearRaster() } } //vycisteni mrizky function clearRaster() { var table = document.querySelector("table.crossword"); for (var i = 1; i < table.rows.length; i++) { var row = table.rows[i]; for (var j = 0; j < row.cells.length; j++) { var cell = row.cells[j]; if (j == 0) { cell.firstChild.innerHTML = " "; } else { cell.className = ""; cell.querySelector("input").value = ""; } } } document.querySelector("#mode-design").checked = true; setMode('design'); } //nastaveni rozmeru mrizky function setRaster() { rows = 5; cols = 5; actField = null; rows = parseInt(document.querySelector("#tb_rows").value) +1; if (isNaN(rows)) rows = 5; cols = parseInt(document.querySelector("#tb_cols").value) +1; if (isNaN(cols)) cols = 5; var table = document.querySelector("table.crossword"); var rowCount = table.rows.length; var cellCount = (rowCount > 0) ? table.rows[0].cells.length : 0; if (rowCount > rows) { //potreba umazat radky while (table.rows.length > rows) { table.deleteRow(-1);} } if (rows > rowCount) { while (table.rows.length < rows) { table.insertRow(-1); } } for (var i= 1; i < table.rows.length; i++) { var row = table.rows[i]; if (row.cells.length > cols) { //umazat bunky z radku while (row.cells.length > cols ) row.deleteCell(-1); } if (row.cells.length < cols ) { //pridat bunky z radku while (row.cells.length < cols ) { var cell = row.insertCell(-1); if (cell.cellIndex == 0) { cell.className = "legenda"; var div = cell.appendChild(document.createElement("div")) div.className = "legend" div.setAttribute("contenteditable","true"); div.innerHTML = " "; } else { //cell.className = "cwfield bd_top bd_left bd_right bd_bottom"; var field = cell.appendChild(document.createElement("input")); field.type = "text"; field.onkeyup = resolveKeys; field.setAttribute("maxlength", "2"); field.onclick = setFocus; field.onfocus = setFocus; //if (actMode == "design") cell.onclick = toggleField; //cell.innerHTML = "     "; } } } } } //nastaveni modu generovani/vyplnovani function setMode(mod) { if (mod == actMode ) return; actMode = mod; if (mod == "design") { initDesign(); } if (mod == "fillin") { initFilling(); } if (mod == "tajenka") { initSolution(); } } //oznacovani/vypinani pole pri designu doplnovacky function toggleField(evt) { var pole = evt.target; if (pole.nodeName.toLowerCase() == "input") pole = pole.parentNode; var field = pole.querySelector("input"); if (!field) { pole.className = "cwfield bd_top bd_left bd_right bd_bottom"; field = document.createElement("input"); field.onkeyup = resolveKeys; field.setAttribute("maxlength", "2"); field.onclick = setFocus; field.onfocus = setFocus; pole.removeChild(pole.firstChild); pole.appendChild(field); } else { pole.removeChild(field); pole.innerHTML = "     "; pole.className = ""; } } //oznacovani policek tajenky function toggleSolution(evt) { var pole = evt.target; if (pole.nodeName.toLowerCase() == "input") pole = pole.parentNode; var field = pole.querySelector("input"); if (!field) { return} else { if (pole.className.indexOf("tajenka") > -1) { pole.className = pole.className.replace("tajenka", ""); } else { pole.className += " tajenka"; } } } //inicializace vyplnovani doplnovacky function initFilling() { var fields = document.querySelectorAll("table.crossword td"); for (var i = 0; i < fields.length; i++) { fields[i].onclick = null; var row = Math.floor(i / cols); var col = i - row*cols; var pole = fields[i].querySelector("input"); if (pole) { pole.onkeyup = resolveKeys; pole.setAttribute("maxlength", "2"); pole.onclick = setFocus; pole.onfocus = setFocus; } } } //zahajeni navrhu doplnovacky function initDesign() { var fields = document.querySelectorAll("table.crossword td"); for (var i = 0; i < fields.length; i++) { if (fields[i].cellIndex == 0) continue; //fields[i].onclick = toggleField; var pole = fields[i].querySelector("input"); if (pole) { pole.onkeydown = null; pole.onclick = null; pole.onfocus = null; } } } //zahajeni oznaceni tajenky function initSolution() { var fields = document.querySelectorAll("table.crossword td"); for (var i = 0; i < fields.length; i++) { fields[i].onclick = toggleSolution; var pole = fields[i].querySelector("input"); if (pole) { pole.onkeydown = null; pole.onclick = null; pole.onfocus = null; } } } //aktivace policka pri vyplnovani function setFocus(evt) { var pole = evt.target; if (actField != null) actField.className = actField.className.replace(" focused", ""); actField = pole.parentNode; actField.className += " focused"; } var isShift = false; var punct = ""; //oznaceni hacku nebo carky kombinujci se sedalsim znakem //ovladani funkce klaves, pohyb po doplnovacce a vyplnovani function resolveKeys(evt) { //evt.preventDefault(); var charCode = (evt.which) ? evt.which : evt.keyCode; var pole = (typeof evt.target != 'undefined') ? evt.target : evt.srcElement; var cell = pole.parentNode; var row = cell.parentNode; if (charCode == 0 || charCode == 187) { //carka, hacek punct = (evt.shiftKey) ? "hacek" : "carka"; return; } if (charCode == 8) { //backspace console.log("backspace") pole.value = ""; cell.className = ""; goBackward(row, cell) return; } if (charCode == 46) { //delete pole.value = ""; cell.className = ""; goForward(row, cell); return; } if (charCode == 37) { //sipka vlevo if (cell.cellIndex > 0) { var nove = row.cells[cell.cellIndex-1].querySelector("input"); if (nove) goBackward(row, cell); } } if (charCode == 39) { //sipka vpravo if (cell.cellIndex < row.cells.length-1) { var nove = row.cells[cell.cellIndex+1].querySelector("input"); if (nove) goForward(row, cell); } } if (charCode == 38) { //sipka nahoru if (row.rowIndex > 0) { var nove = row.parentNode.rows[row.rowIndex-1].cells[cell.cellIndex].querySelector("input"); if (nove) nove.focus(); punct = ""; } } if (charCode == 40) { //sipka dolu if (row.rowIndex < row.parentNode.rows.length-1) { var nove = row.parentNode.rows[row.rowIndex+1].cells[cell.cellIndex].querySelector("input"); if (nove) nove.focus(); punct = ""; } } if (charCode == 32) { //mezera if (row.rowIndex < row.parentNode.rows.length-1) { pole.value = " "; goForward(row, cell); return; } } if ( (charCode > 47 && charCode < 91) || (charCode > 95 && charCode < 112) || charCode > 160) { //pismena cell.className = "cwfield bd_top bd_left bd_right bd_bottom"; var val = pole.value; if (val.length > 1) { pole.value = val.charAt(1) } goForward(row, cell) } } function goForward(row, cell) { if (cell.cellIndex + 1 >= row.cells.length) return; var nove = row.cells[cell.cellIndex + 1].querySelector("input"); if (nove) { nove.focus(); var range; if (nove.createTextRange) { range = nove.createTextRange(); range.move('character', 1); range.select(); } else { if (nove.selectionStart !== undefined) { nove.setSelectionRange(1, 1); } } } punct = ""; } function goBackward(row, cell) { if (cell.cellIndex == 0) return; var nove = row.cells[cell.cellIndex - 1].querySelector("input"); if (nove) { nove.focus(); var range; if (nove.createTextRange) { range = nove.createTextRange(); range.move('character', 1); range.select(); } else { if (nove.selectionStart !== undefined) { nove.setSelectionRange(1, 1); } } } punct = ""; } //generovani kodu doplnovacky pro otazku v ISu function getCWCode() { var mleft = 100, mright = 0, mtop = 100, mbottom = 0; var table = document.querySelector("table.crossword"); for (var i = 1; i < table.rows.length; i++) { var row = table.rows[i]; for (var j = 1; j < row.cells.length; j++) { var field = row.cells[j].querySelector("input"); if (field.value != "") { mleft = Math.min(mleft, j); mright = Math.max(mright, j); mtop = Math.min(i, mtop); mbottom = Math.max(i, mbottom); } } } var vystup = "\n"; // var isAuth = document.getElementById("qmode-auth").checked; //var baseURL = (isAuth) ? "https://is.muni.cz/auth" : "http://is.muni.cz"; //" + baseURL + " vystup += "