-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgriddler.js
228 lines (226 loc) · 8.01 KB
/
griddler.js
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
226
227
228
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (var i = 0; i < count; i++) {
rpt += str;
}
return rpt;
}
}
$(document).ready(function(){
var griddler = $('#Griddler');
var rows = $('#rows');
var columns = $('#columns');
var currentColor = 1;
var codeTextarea = $('#GriddlerCode');
var columnNumbers = $('#ColumnNumbers')
var rowNumbers = $('#RowNumbers');
function CreateGrid(rows,columns) {
griddler.css('width',(columns*1.0625)+.0625+'em');
griddler.html(('<div class="row">' + '<div class="cell"></div>'.repeat(columns) + '</div>').repeat(rows));
ClearNumbers();
}
CreateGrid(40,40);
function ExportGriddler() {
var code = '';
griddler.children().each(function(){
var row = $(this);
code += 'r';
row.children().each(function(){
if($(this).hasClass('clicked'))
code += 'c';
else
code += 'e';
});
});
return code;
}
function ImportGriddler() {
var code = codeTextarea[0].value;
var griddlerhtml = '<div class="row">';
for (var i=1; i < code.length; i++)
{
var letter = code.charAt(i);
switch(letter) {
case 'r':
griddlerhtml += '</div><div class="row">';
break;
case 'c':
griddlerhtml += '<div class="cell clicked"></div>';
break;
case 'e':
griddlerhtml += '<div class="cell"></div>';
break;
default:;
}
}
griddler.html(griddlerhtml+'</div>');
griddler.css('width',(griddler.children().eq(0).children().length*1.0625)+.0625+'em');
codeTextarea[0].value = '';
ClearNumbers();
}
function RowToNumbers(row) {
var rowLen = row.length;
var numbers = '';
if(rowLen) {
var num = 1;
for(var i=0; i < rowLen; i++) {
var cell = row.eq(i);
if(i != rowLen-1) {
var nextCell = row.eq(i+1);
if(cell.index() + 1 - nextCell.index() == 0) {
num++;
}
else {
numbers += '<span class="num-box"><span class="num">'+num+'</span></span>';
num = 1;
}
}
else {
numbers += '<span class="num-box"><span class="num">'+num+'</span></span>';
}
}
}
else
numbers = '<span class="num-box"><span class="num">0</span></span>';
return '<div>'+numbers+'</div>';
}
function ColumnToNumbers(column) {
var columnLen = column.length;
var numbers = '';
if(columnLen) {
var num = 1;
for(var i=0; i < columnLen; i++) {
var cell = column.eq(i);
if(i != columnLen-1) {
var nextCell = column.eq(i+1);
if(cell.parent().index() + 1 - nextCell.parent().index() == 0) {
num++;
}
else {
numbers += '<span class="num-box"><span class="num">'+num+'</span></span>';
num = 1;
}
}
else {
numbers += '<span class="num-box"><span class="num">'+num+'</span></span>';
}
}
}
else
numbers = '<span class="num-box"><span class="num">0</span></span>';
return '<div>'+numbers+'</div>';
}
function GenerateNumbers() {
var rowNumbersCode = '';
griddler.children().each(function(index){
var row = $(this);
var cells = row.children();
var clicked = cells.filter('.clicked');
rowNumbersCode += RowToNumbers(clicked);
});
rowNumbers.html(rowNumbersCode).removeClass('empty');
var columnNumbersCode = '';
var columnsLen = griddler.children().eq(0).children().length;
for(var i=0; i < columnsLen; i++ ) {
columnNumbersCode += ColumnToNumbers(griddler.find('div.cell:nth-child('+(i+1)+')').filter('.clicked'));
}
columnNumbers.html(columnNumbersCode).removeClass('empty').css('margin-left',rowNumbers.outerWidth());
}
function ClearNumbers(){
columnNumbers.html('').addClass('empty');
rowNumbers.html('').addClass('empty');
}
$('button#submit').click(function(e){
CreateGrid(rows[0].value,columns[0].value)
e.preventDefault();
});
$('button#clear').click(function(e){
griddler.find('.cell.clicked').removeClass('clicked');
ClearNumbers();
e.preventDefault();
});
$('button#invert').click(function(e){
var clicked = griddler.find('.cell.clicked');
griddler.find('.cell').addClass('clicked');
clicked.removeClass('clicked')
e.preventDefault();
});
$('button#invisibility').click(function(e){
griddler.find('.clicked').removeClass('clicked');
e.preventDefault();
});
griddler.on('mousedown touchstart', '.cell', function(e){
var cell = $(this);
if(!cell.hasClass('clicked')) {
cell.addClass('clicked');
currentColor = 1;
}
else {
cell.removeClass('clicked');
currentColor = 0;
}
e.preventDefault();
}).on('mouseover', '.cell', function(){
var cell = $(this);
if(griddler.hasClass('dragged')) {
if(currentColor == 1) {
cell.addClass('clicked')
}
else {
cell.removeClass('clicked');
}
}
}).mousedown(function(e){
griddler.addClass('dragged');
e.preventDefault();
}).mouseup(function(){
griddler.removeClass('dragged');
}).on('touchstart', function(e){
griddler.addClass('dragged');
}).on('touchmove',function(e){
var cell = $(document.elementFromPoint(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY));
cell.trigger('mouseover');
e.preventDefault();
}).on('touchend',function(e){
griddler.removeClass('dragged') ;
});
$('button#export').click(function(e){
codeTextarea.val(ExportGriddler());
e.preventDefault();
});
$('button#import').click(function(e){
if(codeTextarea[0].value != '')
ImportGriddler();
e.preventDefault();
});
$('button#generate-numbers').click(function(e){
GenerateNumbers();
e.preventDefault();
});
});