/* * JSxword.js - Javascript functions for RudeXWord * */ // Several Constants var Q_WIDTH = 30 + 1; // not sure if adding 1 is right. maybe because of the array var Q_HEIGHT = 30 + 1; var Q_MOUSEOVERBGCOLOR = "#dddddd"; var HTML_TARGET = "document.getElementById(\"xword\").innerHTML"; var SOLUTION_HTML_TARGET = "document.getElementById(\"solution\").innerHTML"; var CRSR_BLINK_SPEED = 250; var CRSR_BLINK_COLOR1 = "#aa3366"; var CRSR_BLINK_COLOR2 = "#ff6699"; var SYM_LETTER = "."; var SYM_WORDBEGIN = "I"; var SYM_WORDEND = "S"; var SYM_NOTHING = " "; var XWORDGRID_FIELDNAME = "XWordGrid"; // More or less useful variables var cursorIRQ; var currentFocusFieldId; var SolutionStr = ""; var qBoxIRQ; var tmpQnr; // to see if the question changed // start function $(document).ready( function() { $(".letter").bind("click", function () { fieldFocus($(this).attr("id")); }) .bind("mouseover", function () { mouseOver($(this).attr("id")) }) .bind("mouseout", function () { $(this).css("backgroundColor", "") }); $(window).keydown(function(event){ evalKeyboardEvent(event); }); // obviously, IE7 needs an own event binding $(".letter").bind("keydown", function () { //alert("ie7"); evalKeyboardEvent(window.event); }); // $('#Solution').val(""); }); // string-array helper classes function replaceCharAt(sourceString, pos, byChar) { return sourceString.substring(0,pos) + byChar + sourceString.substring(pos+1,sourceString.length); } // replace Char in a string function replaceCharInArray(arr, xpos, ypos, byChar) { arr[ypos] = replaceCharAt(arr[ypos], xpos, byChar); return arr; } // in an array of string, gets char at stringpos x in array element y function getCharInArray(arr, xpos, ypos) { return arr[ypos].charAt(xpos); } // make arr[x] of String to TextArea function arrayToTextArea(arr) { ares = ""; for(i=0;i<(arr.length - 1);++i) { ares += arr[i] + "\n"; } ares += arr[(arr.length)-1]; return ares; } // make arr[x][y] to textArea function twoDimArrayToTextArea(arr) { ares = ""; for(i=0;i<(arr.length - 1);++i) { for(j=0;j<(arr[i].length-1);++j) { ares += arr[i][j]; } ares += "\n"; } return ares; } function textAreaToArray(ta) { // count the elements: nv = ta.count(...) nv = 100; //##tmpvalue arr = new Array(nv); arr = ta.split("\r\n") return arr } // attr="X" or "Y", fid= field ID in format "FH42V32E", or "S" for solutionfieldnumber // returns either the value behind H or V function getCoordFromFID(attr,fid) { res = -1; if(attr == "X") { bsign = "H"; esign = "V" } else if(attr == "Y") { bsign = "V"; esign = "E" } else if(attr == "S") { bsign = "S"; esign = "E" } b = fid.indexOf(bsign); e = fid.indexOf(esign); if((b>0) && (e>0) && (b 0) { numcand = fid.substring(b+1,b+1+spos); } res=parseInt(numcand); } return res; } // if a document is new, initialize the grid field. if not, load it into the grid function initOrLoadXWordGrid() { // init the field where the xword grid is remembered // only init when field is empty xf = eval("document.forms['xword']."+XWORDGRID_FIELDNAME) if (xf) { xfv = xf.value; xfa = textAreaToArray(xfv); makenew = true; if (xfa.length == parseInt(Q_HEIGHT)) if (xfa[0].length == parseInt(Q_WIDTH)) makenew = false if (makenew == true) { // wrong format, so // if the field HAS an entry (test if width or height is > e.g. 3), put an error if ((xfa.length > 3) || (xfa[0].length > 3)) { alert("Found a default entry, but not loading: should be sized h="+Q_HEIGHT+" w="+Q_WIDTH+", but is h="+ xfa.length+" w="+xfa[0].length+"."); } // make a new save field NTA_ROWS = Q_HEIGHT+2; // no idea why 2 NTA_COLS = Q_WIDTH+2; na = new Array(NTA_ROWS); for(h=0;h<=NTA_ROWS;++h) { la = new Array(NTA_COLS); for(l=0;l<=NTA_COLS;++l) { // initialization with blank la[l] = " "; } na[h] = la; } // write the content in the xwordgridfield xf.value = twoDimArrayToTextArea(na); } else { // format ok } } } function initXWord() { checkXWordIntegrity(); initOrLoadXWordGrid(); } function hideQuestionbox() { $('#questionbox').css("visibility", "hidden").text(""); } function disallowHideQuestionbox() { window.clearTimeout(qBoxIRQ); } function allowHideQuestionbox() { qBoxIRQ = window.setTimeout("hideQuestionbox()",400); } //display Question function displayQuestion(qnr) { tdObj = $("#qf"+qnr); //rootOffsetX=document.getElementById('xword').offsetLeft; //rootOffsetY=document.getElementById('xword').offsetTop; tdObjOffset = tdObj.position(); thisOffsetX = tdObjOffset.left; thisOffsetY = tdObjOffset.top; boxObj = $('#questionbox'); boxObj.html("
" + qnr + "
" + questions[qnr-1][0] + "

" + questions[qnr-1][1] + "
"); if (qnr != tmpQnr) { cx = thisOffsetX; cy = thisOffsetY; cOuter = $('#grid_OuterTable').position(); cyOuter = cOuter.top; cy = cy - cyOuter; if(cy > 250) { dy = -200 } else dy = 30; if(cx > 250) { dx = -300 } else dx = 80; oy = document.body.scrollTop; // scrolled down ox = document.body.scrollLeft; // scrolled right oy = 0; ox = 0; boxObj.css("top", cyOuter + cy + dy + oy); boxObj.css("left", cx + dx + ox); tmpQnr = qnr; } boxObj.css("visibility", "visible"); disallowHideQuestionbox(); } // If a letter is found, enter it into the solution string // If then all letters are solved, show the otherwise hidden send button function enterLetterInSolutionString(ind, letter) { SolutionStr = document.forms['xword'].Solution.value; // Find the appropriate letter in the word grid and replace it cnt = 0; var i = 0; for(cnt=0;cnt=37 && kc<=40) { dx = 0; dy = 0; if(kc==37) dx=-1; if(kc==38) dy=-1; if(kc==39) dx=1; if(kc==40) dy=1; newx = getCoordFromFID("X",fid) + dx; newy = getCoordFromFID("Y",fid) + dy; newid = "CH"+newx+"V"+newy+"E"; //newids = "CH"+newx+"V"+newy+"S"+news+"E"; newfocusfield = $("[id^=CH"+newx+"V"+newy+"]"); //newfocusfield = document.getElementById(newid); newid=newfocusfield.attr("id"); if (newfocusfield != null) { // only allow navigating to field of class "letter" if(newfocusfield.hasClass("letter")) { fieldFocus(newid); } // again we will fill the keyCode with some nothing-doing value if(event.which) event.keyCode = 37; else window.event.keyCode = 37; return; } } // delete and backspace will be treated as spacebar if (kc==46 || kc==8) kc = 32; newLetter = String.fromCharCode(kc); validLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "; if (validLetters.indexOf(newLetter) >= 0) { newLetterTranslated = newLetter; if (newLetter == " ") newLetterTranslated = " "; // Is the current focused field a solution index field? // If yes, also enter the value in the solution field and the HTML field. sind = fid.indexOf("S"); if (sind >= 0) { eind = fid.indexOf("E"); solutionIndex = fid.substring(sind+1,eind); enterLetterInSolutionString(solutionIndex-1, newLetter); solutionObj = $("#CS"+solutionIndex); if (solutionObj) { solutionObj.html(newLetterTranslated); } } // In any case, enter the letter into the grid field. cfObj.html(newLetterTranslated); ff = eval("document.forms['xword']."+XWORDGRID_FIELDNAME); ff.value = arrayToTextArea(replaceCharInArray(textAreaToArray(ff.value),getCoordFromFID("X",fid),getCoordFromFID("Y",fid), newLetter)); // A little workaround to avoid that, on the IE, the pressed SPACEBAR is interpreted as "Scroll down". // We overwrite the event key. Don't ask why it's 37. 37 just doesn't do ANYthing, that's why. In a way, it's like the 42. if (kc==32) { window.event.keyCode = 37; } } } } } function cursorActivity() { fid = currentFocusFieldId; fid = "C" + fid.substr(1); fieldObj = $("#"+fid); fieldObj.addClass("xwCrsrState1").toggleClass("xwCrsrState2"); } function fieldUnfocus() { fid = currentFocusFieldId; if (fid != null) { fid = "C" + fid.substr(1); $("#"+fid).removeClass("xwCrsrState1").removeClass("xwCrsrState2"); clearInterval(cursorIRQ); } } function fieldFocus(fid) { // cursor activity fieldUnfocus(); fid = "F" + fid.substr(1); currentFocusFieldId = fid; cursorIRQ = setInterval("cursorActivity()",CRSR_BLINK_SPEED); } function mouseOver(fid) { $("#"+fid).css("background-color", Q_MOUSEOVERBGCOLOR); }