summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/utility.min.js1
-rw-r--r--js/wordsearch.js32
-rw-r--r--js/wordsearch.min.js1
3 files changed, 31 insertions, 3 deletions
diff --git a/js/utility.min.js b/js/utility.min.js
new file mode 100644
index 0000000..5f5a5f6
--- /dev/null
+++ b/js/utility.min.js
@@ -0,0 +1 @@
+if(typeof Math.rangeInt!="function"){Math.rangeInt=function(b,a){if(a==undefined){a=b;b=0}return Math.floor(Math.random()*(a-b+1))+b}}if(typeof Object.merge!="function"){Object.merge=function(c,b){for(var a in c){b[a]=c[a]}return b}}; \ No newline at end of file
diff --git a/js/wordsearch.js b/js/wordsearch.js
index ef33eb2..37dfa25 100644
--- a/js/wordsearch.js
+++ b/js/wordsearch.js
@@ -17,7 +17,7 @@
this.wrapEl = wrapEl;
// Add `.ws-area` to wrap element
- this.wrapEl.className += ' ws-area';
+ this.wrapEl.classList.add('ws-area');
// Default settings
var default_settings = {
@@ -305,7 +305,31 @@
WordSeach.prototype.clearHighlight = function() {
var selectedEls = document.querySelectorAll('.ws-selected');
for (var i = 0; i < selectedEls.length; i++) {
- selectedEls[i].className = 'ws-col';
+ selectedEls[i].classList.remove('ws-selected');
+ }
+ }
+
+ /**
+ * Lookup if the wordlist contains the selected
+ * @param {Array} selected
+ */
+ WordSeach.prototype.lookup = function(selected) {
+ var words = [''];
+
+ for (var i = 0; i < selected.length; i++) {
+ words[0] += selected[i].letter;
+ }
+ words.push(words[0].split('').reverse().join(''));
+
+ if (this.settings.words.indexOf(words[0]) > -1 ||
+ this.settings.words.indexOf(words[1]) > -1) {
+ for (var i = 0; i < selected.length; i++) {
+ var row = selected[i].row + 1,
+ col = selected[i].col + 1,
+ el = document.querySelector('.ws-area .ws-row:nth-child(' + row + ') .ws-col:nth-child(' + col + ')');
+
+ el.classList.add('ws-found');
+ }
}
}
@@ -338,7 +362,7 @@
col = current.col + 1,
el = document.querySelector('.ws-area .ws-row:nth-child(' + row + ') .ws-col:nth-child(' + col + ')');
- el.className = 'ws-col ws-selected';
+ el.className += ' ws-selected';
}
}
}
@@ -352,6 +376,8 @@
return function() {
_this.selectFrom = null;
_this.clearHighlight();
+ _this.lookup(_this.selected);
+ _this.selected = [];
}
}
diff --git a/js/wordsearch.min.js b/js/wordsearch.min.js
new file mode 100644
index 0000000..76854e3
--- /dev/null
+++ b/js/wordsearch.min.js
@@ -0,0 +1 @@
+(function(){Element.prototype.wordSeach=function(b){return new a(this,b)};function a(b,d){this.wrapEl=b;this.wrapEl.classList.add("ws-area");var c={"directions":["W","N","WN","EN"],"gridSize":10,"words":["one","two","three","four","five"],"debug":false};this.settings=Object.merge(d,c);if(this.parseWords(this.settings.gridSize)){var e=false;while(e==false){this.initialize();e=this.addWords()}if(!this.settings.debug){this.fillUpFools()}this.drawmatrix()}}a.prototype.parseWords=function(e){var b=true;for(var c=0;c<this.settings.words.length;c++){this.settings.words[c]=this.settings.words[c].toUpperCase();var d=this.settings.words[c];if(d.length>e){alert("The length of word `"+d+"` is overflow the gridSize.");console.error("The length of word `"+d+"` is overflow the gridSize.");b=false}}return b};a.prototype.addWords=function(){var e=true,c=0,f=true;while(e){var d=this.settings.directions[Math.rangeInt(this.settings.directions.length-1)],b=this.addWord(this.settings.words[c],d),f=true;if(b==false){e=false;f=false}c++;if(c>=this.settings.words.length){e=false}}return f};a.prototype.addWord=function(b,j){var d=true,g={"W":[0,1],"N":[1,0],"WN":[1,1],"EN":[1,-1]},m,e;switch(j){case"W":var m=Math.rangeInt(this.settings.gridSize-1),e=Math.rangeInt(this.settings.gridSize-b.length);break;case"N":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(this.settings.gridSize-1);break;case"WN":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(this.settings.gridSize-b.length);break;case"EN":var m=Math.rangeInt(this.settings.gridSize-b.length),e=Math.rangeInt(b.length-1,this.settings.gridSize-1);break;default:var h="UNKNOWN DIRECTION "+j+"!";alert(h);console.log(h);break}for(var f=0;f<b.length;f++){var l=m+f*g[j][0],c=e+f*g[j][1];var k=this.matrix[l][c].letter;if(k=="."||k==b[f]){this.matrix[l][c].letter=b[f]}else{d=false}}return d};a.prototype.initialize=function(){this.matrix=[];this.selectFrom=null;this.selected=[];this.initmatrix(this.settings.gridSize)};a.prototype.initmatrix=function(c){for(var e=0;e<c;e++){for(var b=0;b<c;b++){var d={letter:".",row:e,col:b};if(!this.matrix[e]){this.matrix[e]=[]}this.matrix[e][b]=d}}};a.prototype.drawmatrix=function(){for(var g=0;g<this.settings.gridSize;g++){var e=document.createElement("div");e.setAttribute("class","ws-row");this.wrapEl.appendChild(e);for(var d=0;d<this.settings.gridSize;d++){var f=document.createElement("canvas");f.setAttribute("class","ws-col");f.setAttribute("width",40);f.setAttribute("height",40);var b=f.width/2,h=f.height/2;var c=f.getContext("2d");c.font="400 28px Calibri";c.textAlign="center";c.textBaseline="middle";c.fillStyle="#333";c.fillText(this.matrix[g][d].letter,b,h);f.addEventListener("mousedown",this.onMousedown(this.matrix[g][d]));f.addEventListener("mouseover",this.onMouseover(this.matrix[g][d]));f.addEventListener("mouseup",this.onMouseup());e.appendChild(f)}}};a.prototype.fillUpFools=function(){for(var c=0;c<this.settings.gridSize;c++){for(var b=0;b<this.settings.gridSize;b++){if(this.matrix[c][b].letter=="."){this.matrix[c][b].letter=String.fromCharCode(Math.rangeInt(65,90))}}}};a.prototype.getItems=function(e,c,i,h){var d=[];if(e===i||c===h||Math.abs(i-e)==Math.abs(h-c)){var f=(e===i)?0:(i>e)?1:-1,g=(c===h)?0:(h>c)?1:-1,j=e,b=c;d.push(this.getItem(j,b));do{j+=f;b+=g;d.push(this.getItem(j,b))}while(j!==i||b!==h)}return d};a.prototype.getItem=function(c,b){return(this.matrix[c]?this.matrix[c][b]:undefined)};a.prototype.clearHighlight=function(){var b=document.querySelectorAll(".ws-selected");for(var c=0;c<b.length;c++){b[c].classList.remove("ws-selected")}};a.prototype.lookup=function(e){var g=[""];for(var c=0;c<e.length;c++){g[0]+=e[c].letter}g.push(g[0].split("").reverse().join(""));if(this.settings.words.indexOf(g[0])>-1||this.settings.words.indexOf(g[1])>-1){for(var c=0;c<e.length;c++){var f=e[c].row+1,b=e[c].col+1,d=document.querySelector(".ws-area .ws-row:nth-child("+f+") .ws-col:nth-child("+b+")");d.classList.add("ws-found")}}};a.prototype.onMousedown=function(b){var c=this;return function(){c.selectFrom=b}};a.prototype.onMouseover=function(b){var c=this;return function(){if(c.selectFrom){c.selected=c.getItems(c.selectFrom.row,c.selectFrom.col,b.row,b.col);c.clearHighlight();for(var e=0;e<c.selected.length;e++){var g=c.selected[e],h=g.row+1,d=g.col+1,f=document.querySelector(".ws-area .ws-row:nth-child("+h+") .ws-col:nth-child("+d+")");f.className+=" ws-selected"}}}};a.prototype.onMouseup=function(){var b=this;return function(){b.selectFrom=null;b.clearHighlight();b.lookup(b.selected);b.selected=[]}}})(); \ No newline at end of file