forked from Kolatzek/WPdoodlez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossword.min.js
1 lines (1 loc) · 14.6 KB
/
crossword.min.js
1
if(document.getElementById("cwd-crossword")){var board,wordArr,wordBank,wordsActive,mode,bgColor=crossword_vars.cwdcw_bg_color,borderColor=crossword_vars.cwdcw_border_color,txtColor=crossword_vars.cwdcw_txt_color,crossword_ansver_var=crossword_vars.cwdcw_ansver,crossword_ansver_incorect=crossword_vars.cwdcw_ansver_incorect,crossword_align_question=crossword_vars.cwdcw_align_question,crossword_width_question=crossword_vars.cwdcw_question_width,crossword_question_color=crossword_vars.cwdcw_question_txt_color,counter_color=crossword_vars.cwdcw_counter_color,optional_bgColor=optional_crossword_vars.cwdcw_optional_bg_color,optional_borderColor=optional_crossword_vars.cwdcw_optional_border_color,optional_textColor=optional_crossword_vars.cwdcw_optional_text_color,optional_questionColor=optional_crossword_vars.cwdcw_optional_question_color,optional_counterColor=optional_crossword_vars.cwdcw_optional_counter_color,correct_ansver_var=optional_crossword_vars.cwdcw_correct_ansver,incorrect_ansver=optional_crossword_vars.cwdcw_incorrect_ansver,crossword_ansver="yes",Bounds={top:0,right:0,bottom:0,left:0,Update:function(e,r){this.top=Math.min(r,this.top),this.right=Math.max(e,this.right),this.bottom=Math.max(r,this.bottom),this.left=Math.min(e,this.left)},Clean:function(){this.top=999,this.right=0,this.bottom=0,this.left=999}};function Play(){for(var e=document.getElementsByClassName("cwd-letter"),r=0;r<e.length;r++)e[r].innerHTML="<input autocomplete='offi' class='cwd-char' type='text' maxlength='1' style='color:#000;'></input>";mode=0,ToggleInputBoxes(!1),jQuery(".cwd-square.cwd-letter.cwd-first").each(function(e,r){var t=jQuery(this).attr("data-c");jQuery(this).append("<div class='cwd-counter'>"+t.replace(" , ",",").replace(/ /g,"")+"</div>")})}function Create(){if(0===mode)ToggleInputBoxes(!0),document.getElementById("cwd-crossword").innerHTML=BoardToHtml(" "),mode=1;else{GetWordsFromInput();for(var e=0,r=!1;e<10&&!r;e++)CleanVars(),r=PopulateBoard();document.getElementById("cwd-crossword").innerHTML=r?BoardToHtml(" "):"Failed to find crossword."}}function ToggleInputBoxes(e){for(var r=document.getElementsByClassName("cwd-word"),t=document.getElementsByClassName("cwd-clue"),a=0;a<r.length;a++)!0===e?(RemoveClass(r[a],"cwd-hide"),RemoveClass(t[a],"cwd-clueReadOnly"),t[a].disabled=""):(AddClass(r[a],"cwd-hide"),AddClass(t[a],"cwd-clueReadOnly"),t[a].disabled="readonly")}function GetWordsFromInput(){wordArr=[];for(var e,r=0,t=document.getElementsByClassName("cwd-word");r<t.length;r++)null!==(e=t[r].value.toUpperCase())&&e.length>1&&wordArr.push(e)}function CleanVars(){Bounds.Clean(),wordBank=[],wordsActive=[],board=[];for(var e=0;e<32;e++){board.push([]);for(var r=0;r<32;r++)board[e].push(null)}}function PopulateBoard(){PrepareBoard();for(var e=0,r=!0,t=wordBank.length;e<t&&r;e++)r=AddWordToBoard();return r}function PrepareBoard(){wordBank=[];for(var e=0,r=wordArr.length;e<r;e++)wordBank.push(new WordObj(wordArr[e]));for(e=0;e<wordBank.length;e++)for(var t=0,a=wordBank[e];t<a.char.length;t++)for(var o=0,n=a.char[t];o<wordBank.length;o++)for(var s=0,i=wordBank[o];o!==e&&s<i.char.length;s++)a.totalMatches+=n===i.char[s]?1:0}function AddWordToBoard(){var e,r,t,a,o,n,s;if(wordsActive.length<1){for(t=0,e=0,r=wordBank.length;e<r;e++)wordBank[e].totalMatches<wordBank[t].totalMatches&&(t=e);wordBank[t].successfulMatches=[{x:12,y:12,dir:0}]}else for(t=-1,e=0,r=wordBank.length;e<r;e++){(a=wordBank[e]).effectiveMatches=0,a.successfulMatches=[];for(var i=0,d=a.char.length;i<d;i++){o=a.char[i];for(var h=0,l=wordsActive.length;h<l;h++)for(var c=0,w=(n=wordsActive[h]).char.length;c<w;c++)if(o===n.char[c]){a.effectiveMatches++;var u={x:n.x,y:n.y,dir:0};0===n.dir?(u.dir=1,u.x+=c,u.y-=i):(u.dir=0,u.y+=c,u.x-=i);for(var v=!0,f=-1,p=a.char.length+1;f<p;f++){var y=[];if(f!==i){if(0===u.dir){if((Q=u.x+f)<0||Q>=board.length){v=!1;break}y.push(board[Q][u.y]),y.push(board[Q][u.y+1]),y.push(board[Q][u.y-1])}else{if((b=u.y+f)<0||b>board[u.x].length){v=!1;break}y.push(board[u.x][b]),y.push(board[u.x+1][b]),y.push(board[u.x-1][b])}if(f>-1&&f<p-1){if(y[0]!==a.char[f]){if(null!==y[0]){v=!1;break}if(null!==y[1]){v=!1;break}if(null!==y[2]){v=!1;break}}}else if(null!==y[0]){v=!1;break}}}!0===v&&a.successfulMatches.push(u)}}if((s=a.totalMatches-a.effectiveMatches)<9999&&a.successfulMatches.length>0)s=9999,t=e;else if(s<=0)return!1}if(-1===t)return!1;var g=wordBank.splice(t,1);wordsActive.push(g[0]);var x=wordsActive.length-1,_=Math.random(),j=wordsActive[x].successfulMatches,m=j[Math.floor(_*j.length)];for(wordsActive[x].x=m.x,wordsActive[x].y=m.y,wordsActive[x].dir=m.dir,e=0,r=wordsActive[x].char.length;e<r;e++){var Q=m.x,b=m.y;0===m.dir?board[Q+=e][b]=wordsActive[x].char[e]:(b+=e,board[Q][b]=wordsActive[x].char[e]),Bounds.Update(Q,b)}return!0}function BoardToHtml(e){for(var r=Bounds.top-1,t="";r<Bounds.bottom+2;r++){t+="<div class='cwd-row'>";for(var a=Bounds.left-1;a<Bounds.right+2;a++)t+=BoardCharToElement(board[a][r],a,r);t+="</div>"}return t}function BoardCharToElement(e,r,t){for(var a="",o="",n="",s=[],i=[],d=0;d<wordsActive.length;d++)if(wordsActive[d].x==r&&wordsActive[d].y==t){a="cwd-first",n=d,i.push(n);for(var h=0,l=document.getElementsByClassName("cwd-word");h<l.length;h++)l[h].value.toUpperCase()==wordsActive[d].string&&(o=parseInt(l[h].dataset.counter),s.push(o))}s.reverse();var c=e?["cwd-square","cwd-letter "+a]:["cwd-square"],w=e?[r]:[""],u=e?[t]:[""],v=e?[i.join(",")]:[""];return EleStr("div",[{a:"class",v:c},{a:"data-c",v:s.join(",")},{a:"data-x",v:w},{a:"data-y",v:u},{a:"data-el",v:v}],e)}function WordObj(e){this.string=e,this.char=e.split(""),this.totalMatches=0,this.effectiveMatches=0,this.successfulMatches=[]}function RegisterEvents(){document.getElementById("cwd-crossword").onfocus=function(){return!1}}function EleStr(e,r,t){t=t||"";for(var a=0,o="<"+e+" ";a<r.length;a++)o+=r[a].a+"='"+ArrayToString(r[a].v," ")+"' ";return o+">"+t+"</"+e+">"}function ArrayToString(e,r){if(null===e||e.length<1)return"";null===r&&(r=",");for(var t=e[0],a=1;a<e.length;a++)t+=r+e[a];return t}function AddClass(e,r){e.className=e.className.replaceAll(" "+r,"")+" "+r}function RemoveClass(e,r){e.className=e.className.replaceAll(" "+r,"")}function ToggleClass(e,r){var t=e.className.replaceAll(" "+r,"");e.className=t.length===e.className.length?t+" "+r:t}RegisterEvents(),String.prototype.replaceAll=function(e,r){var t=new RegExp(e,"g");return this.replace(t,r)},Create(),Play(),jQuery(document).ready(function(){jQuery(".cwd-square.cwd-letter .cwd-char").keyup(function(e){jQuery(".cwd-square.cwd-letter").removeClass("cwd-correct"),jQuery(".cwd-square.cwd-letter").removeClass("cwd-error"),jQuery(".cwd-square.cwd-letter.cwd-first").each(function(e,r){for(var t=jQuery(r)[0].dataset.el.split(","),a=0;a<t.length;a++){var o=t[a],n=wordsActive[o],s=n.char,i=n.dir,d=n.x,h=n.y,l=(jQuery(r)[0].dataset.c,""),c="",w=!0;if(l=0==i?d:h,0==i)for(var u=0,v=l;v<s.length+l;v++){(f=jQuery('.cwd-square.cwd-letter[data-x="'+v+'"][data-y="'+h+'"] .cwd-char').val().toUpperCase())?f!=s[u]&&(w=!1):c="empty",u++}else for(u=0,v=l;v<s.length+l;v++){var f;(f=jQuery('.cwd-square.cwd-letter[data-y="'+v+'"][data-x="'+d+'"] .cwd-char').val().toUpperCase())?f!=s[u]&&(w=!1):c="empty",u++}if("empty"!=c&&1==w)if(0==i&&"yes"==crossword_ansver)for(v=l;v<s.length+l;v++)"yes"==crossword_ansver_var?jQuery('.cwd-square.cwd-letter[data-x="'+v+'"][data-y="'+h+'"] ').addClass("cwd-correct").css({"background-color":"#b3ffb4"}):jQuery('.cwd-square.cwd-letter[data-x="'+v+'"][data-y="'+h+'"] ').addClass("cwd-correct").css({"background-color":"none"});else for(v=l;v<s.length+l;v++)"yes"==crossword_ansver_var?jQuery('.cwd-square.cwd-letter[data-y="'+v+'"][data-x="'+d+'"] ').addClass("cwd-correct").css({"background-color":"#b3ffb4"}):jQuery('.cwd-square.cwd-letter[data-y="'+v+'"][data-x="'+d+'"] ').addClass("cwd-correct").css({"background-color":"none"});if("no"==crossword_ansver&&jQuery(".cwd-square.cwd-letter").removeClass("cwd-correct"),"empty"!=c&&0==w)if(0==i&&"yes"==crossword_ansver_incorect)for(v=l;v<s.length+l;v++)jQuery('.cwd-square.cwd-letter[data-x="'+v+'"][data-y="'+h+'"] ').addClass("cwd-error");else for(v=l;v<s.length+l;v++)jQuery('.cwd-square.cwd-letter[data-y="'+v+'"][data-x="'+d+'"] ').addClass("cwd-error");"no"==crossword_ansver_incorect&&jQuery(".cwd-square.cwd-letter").removeClass("cwd-error")}}),jQuery(".cwd-square.cwd-letter").length==jQuery(".cwd-square.cwd-letter.cwd-correct").length&&jQuery("#overlay").fadeIn(400,function(){jQuery("#modal_form_crossword").css("display","block").animate({opacity:1,top:"50%"},200)})}),jQuery(".cwd-square.cwd-letter").css({"border-color":"#"+borderColor,"background-color":"#"+bgColor}),jQuery(".cwd-crossword-questions .cwd-line .cwd-clue").css("color","#"+crossword_question_color),jQuery(".cwd-crossword .cwd-counter").css("color","#"+counter_color),jQuery("#modal_close, #overlay").click(function(){var e=jQuery("#modal_form_crossword iframe").attr("src");jQuery("#modal_form_crossword iframe").attr("src",""),jQuery("#modal_form_crossword iframe").attr("src",e),jQuery("#modal_form_crossword").animate({opacity:0,top:"45%"},200,function(){jQuery(this).css("display","none"),jQuery("#overlay").fadeOut(400)})})}),jQuery(document).ready(function(){if(lastMoveDirection=null,jQuery(".cwd-char").hover(function(){""==jQuery(this).val()?jQuery(this).css({cursor:"text"}):jQuery(this).css({cursor:"pointer"})},function(){}),jQuery(".cwd-char").on("click touchstart",function(){this.select(),this.setSelectionRange(0,99999)}),jQuery(".cwd-char").on("keydown",function(e){var r=parseInt(jQuery(this).parent().attr("data-x")),t=parseInt(jQuery(this).parent().attr("data-y"));if(46==e.which||8==e.which)if(e.preventDefault(),this.value="",jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").length>0)jQuery(window).width()>769&&jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus();else{var a=jQuery(this).parent("div.cwd-letter").attr("data-x");jQuery(window).width()>769&&jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+a+'"]').find("input").focus()}37==e.which&&(r--,jQuery(this).parent().siblings('[data-x = "'+r+'"][data-y = "'+t+'"]').find("input").focus()),39==e.which&&(r++,jQuery(this).parent().siblings('[data-x = "'+r+'"][data-y = "'+t+'"]').find("input").focus()),38==e.which&&(t--,jQuery(this).parent().parent().prev().find('[data-x = "'+r+'"][data-y = "'+t+'"]').find("input").focus()),40==e.which&&(t++,jQuery(this).parent().parent().next().find('[data-x = "'+r+'"][data-y = "'+t+'"]').find("input").focus())}),jQuery(window).width()<769)for(var e=document.getElementById("cwd-crossword").childNodes,r=0;r<e.length;r++)for(var t=e[r].childNodes,a=0;a<t.length;a++)for(var o=t[a].childNodes,n=0;n<o.length;n++)"INPUT"==o[n].nodeName&&o[n].addEventListener("input",function(e){if(e.preventDefault(),"x"==lastMoveDirection&&this.parentNode.nextSibling.classList.contains("cwd-letter"))lastMoveDirection="x",this.parentNode.nextSibling.querySelector(".cwd-char").focus(),this.parentNode.nextSibling.querySelector(".cwd-char").setSelectionRange(0,99999);else{lastMoveDirection="y";var r=this.parentNode.getAttribute("data-x");if(jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").length>0){var t=jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input");t.focus(),t.get(0).setSelectionRange(0,99999)}else lastMoveDirection="x",this.parentNode.nextSibling.querySelector(".cwd-char").focus(),this.parentNode.nextSibling.querySelector(".cwd-char").setSelectionRange(0,99999)}},!1);else s=window.navigator.userAgent,((i=s.indexOf("MSIE"))>0?parseInt(s.substring(i+5,s.indexOf(".",i))):navigator.userAgent.match(/Trident\/7\./)||navigator.userAgent.match(/ Edge\//)?11:0)>0?jQuery(".cwd-char").on("keyup",function(e){var r=jQuery(this).parent("div.cwd-letter").attr("data-x");window.getSelection&&""!=window.getSelection().toString()?"x"==lastMoveDirection&&jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").length>0?(lastMoveDirection="x",jQuery(this).val(e.char).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):jQuery(this).val(e.char).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").length>0?(lastMoveDirection="y",jQuery(this).val(e.char).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").focus().select()):(lastMoveDirection="x",jQuery(this).val(e.char).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):"x"==lastMoveDirection&&jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").length>0?(lastMoveDirection="x",jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").length>0?(lastMoveDirection="y",jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").focus().select()):(lastMoveDirection="x",jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select())}):jQuery(".cwd-char").on("keypress",function(e){var r=jQuery(this).parent("div.cwd-letter").attr("data-x");window.getSelection&&""!=window.getSelection().toString()?"x"==lastMoveDirection&&jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").length>0?(lastMoveDirection="x",jQuery(this).val(e.char).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):jQuery(this).val(e.char).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").length>0?(lastMoveDirection="y",jQuery(this).val(e.char).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").focus().select()):(lastMoveDirection="x",jQuery(this).val(e.char).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):"x"==lastMoveDirection&&jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").length>0?(lastMoveDirection="x",jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select()):jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").length>0?(lastMoveDirection="y",jQuery(this).parent("div.cwd-letter").parent("div.cwd-row").next("div.cwd-row").find('[data-x="'+r+'"]').find("input").focus().select()):(lastMoveDirection="x",jQuery(this).parent("div.cwd-letter").next("div.cwd-letter").find("input").focus().select())});var s,i;jQuery(".cwd-letter").hover(function(){jQuery(this).find(".cwd-counter").hide()},function(){jQuery(this).find(".cwd-counter").show()})})}