1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
<!doctype html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Magic squares playgound">
<title>Magic Squares</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
input[type=text]
{
font-size:52px;
width: 57px;
height: 57px;
border: 1px solid grey;
margin-right: 2px;
text-align: center;
}
.row {
padding: 2px;
}
.list {
display: inline-block;
overflow-x: scroll;
width: 50%;
white-space: nowrap;
}
.best-square pre {
display: inline-block;
padding-left: 50px;
}
.best-square {
border: 1px solid grey;
}
</style>
<script src="js/words.js"></script>
<script>
function submit(){
var size = get_size();
var words = [];
for(var row = 0 ; row < size; row++){
words.push("");
}
for(var row = 0 ; row < size; row++){
for(var i = 0 ; i < size; i++){
var element = document.getElementById("col"+row+"_"+i);
words[row] += element.value.substring(0,1);
}
}
var name = document.getElementById("name").value;
var details = {"words": words.join("\n"), "name": name}
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
//console.log(JSON.stringify());
fetch(new Request("/wordsquares", {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
}))
.then((response) => {
console.log(response);
window.location = "/";
});
}
function filter_row(cols){
return words.filter(word => {
if(word.length != cols.length){
return false;
}
word = word.toUpperCase();
for(var i = 0; i < cols.length; i++){
if( (cols[i] != "" && word.substring(i, i+1) != cols[i])){
return false;
}
}
return true;
}).join(" ");
}
function update_input(from, to){
var fromEl = document.querySelector(from);
fromEl.value = fromEl.value.toUpperCase();
//$(from).val($(from).val().toUpperCase())
to.forEach(element => {
document.querySelector(element).value = fromEl.value;
//fromEl.value = fromEl.fromEl.toUpperCase();
//$(element).val($(from).val());
});
}
function update_filter(row){
var size = get_size();
var cols = [];
for(var i = 0 ; i < size; i++){
var element = document.getElementById("col"+row+"_"+i);
cols.push(element.value.substring(0,1));
//cols.push($("#col"+row+"_"+i).val().substring(0,1));
}
document.getElementById("filter"+row).textContent = filter_row(cols);
//$("#filter"+row).text(filter_row(cols));
}
function clear_input(){
var size = get_size();
document.querySelectorAll("input[type=text]").forEach(element => {
element.value = "";
})
//$('input[type=text]').val("");
for(var i = 0 ; i < size; i++){
update_filter(i);
}
}
function get_size(){
return parseInt(document.getElementById("size_input").value);
}
function resize(){
var size = get_size();
var square = document.getElementById("square");
square.innerHTML = "";
// var filter = document.getElementById("filter");
// filter.innerHTML = "";
for(var i = 0; i < size; i++){
var divRow = document.createElement("div");
divRow.setAttribute("id", "row"+i);
divRow.setAttribute("class", "row");
square.appendChild(divRow);
for(var j = 0; j < size; j++){
var colInput = document.createElement("input");
colInput.setAttribute("id", "col"+i+"_"+j);
colInput.setAttribute("value", "");
colInput.setAttribute("type", "text");
colInput.setAttribute("data-row", i);
colInput.setAttribute("data-col", j);
divRow.appendChild(colInput);
var listener = function(element){
var row = parseInt(element.target.getAttribute("data-row"));
var col = parseInt(element.target.getAttribute("data-col"));
update_input("#col"+row+"_"+col, ["#col"+col+"_"+row])
update_filter(row);
update_filter(col);
// focus on next input
// col++;
// if(col == size){
// console.log("end of row");
// row++;
// col = 0;
// if(row == size){
// row = 0;
// }
// }
// document.getElementById("col"+row+"_"+col).focus();
}
colInput.addEventListener("keyup", listener);
colInput.addEventListener("change", listener);
colInput.addEventListener("paste", listener);
}
var divFilter = document.createElement("div");
divFilter.setAttribute("id", "filter"+i);
divFilter.setAttribute("class", "list");
divRow.appendChild(divFilter);
}
}
window.onload = function(){
var feed = new Vue({
el: '#app',
data: {
show: window.location.hash == '#success',
best : []
},
created() {
fetch(new Request('/wordsquares/best')).then(response => response.json())
.then(response => this.best = response.best);
console.log(this.best);
}
});
resize();
clear_input()
var inputChange = function(){ resize(); };
document.getElementById("size_input").addEventListener("keyup", inputChange);
document.getElementById("size_input").addEventListener("change", inputChange);
document.getElementById("size_input").addEventListener("paste", inputChange);
}
</script>
</head>
<body >
<div id="app">
<div id="square"></div>
<div>
<button onclick="clear_input()">Clear</button>
<input id="size_input" width="5" type="number" value="4"></input>
<div>
<input id="name" placeholder="Your name">
<button onclick="submit()">Submit</button>
<p id="status" v-if='show'>Success, thank you!</p>
</div>
</div>
<!-- <div id="filter"></div> -->
<div class="feed">
<span>Best Submitted:</span>
<div class="best-square" v-for="item in best">
<pre>
{{item.words}}
</pre>
<span>{{item.name}}</span>
</div>
</div>
</div>
</body>
</html>
|