-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid-it-lab.html
More file actions
364 lines (317 loc) · 14.4 KB
/
grid-it-lab.html
File metadata and controls
364 lines (317 loc) · 14.4 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Grid-It Lab - Text-Kitchen</title>
<link rel="stylesheet" href="styles.css">
<style>
.grid-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
table-layout: fixed;
}
.grid-table td {
border: 1px solid #c0c0c0;
padding: 15px;
vertical-align: top;
word-wrap: break-word;
font-family: 'Courier New', monospace;
line-height: 1.6;
background: #ffffff;
}
.grid-table td:empty {
background: #f5f5f5;
}
.number-input-group {
display: flex;
gap: 20px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.number-input-item {
flex: 1;
min-width: 200px;
}
.number-input-item label {
display: block;
margin-bottom: 8px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
}
.number-input-item input[type="number"] {
width: 100%;
padding: 12px;
border: 2px solid #4a4a4a;
font-size: 1em;
background: #ffffff;
}
.number-input-item input[type="number"]:focus {
outline: none;
border-color: #c41e3a;
}
.words-per-cell-group {
margin-bottom: 20px;
}
</style>
</head>
<body>
<nav class="main-nav">
<a href="index.html">Home</a>
<a href="text-reverse-lab.html">Text Reversal</a>
<a href="line-break-lab.html">Line Break Lab</a>
<a href="eraser-lab.html">Eraser Lab</a>
<a href="alphabetizer-lab.html">Alphabetizer Lab</a>
<a href="markov-mashup-lab.html">Markov Mashup Lab</a>
<a href="lipogram-lab.html">Lipogram Lab</a>
<a href="long-tail-lab.html">Long Tail Lab</a>
<a href="grid-it-lab.html">Grid-It Lab</a>
<a href="letter-replacement-lab.html">Letter Swap</a>
<a href="chromacode-lab.html">Chromacode</a>
<a href="about.html">About</a>
</nav>
<div class="container">
<div class="header">
<h1>Grid-It Lab</h1>
<p>Organize text into a customizable grid with adjustable rows and columns</p>
</div>
<p style="margin-bottom: 30px; line-height: 1.8; color: #4a4a4a;">
[Add your description here about how the Grid-It Lab creates visual arrangements of text and its applications for spatial poetry or experimental layouts.]
</p>
<div class="upload-section">
<div class="upload-icon">📄</div>
<h2>Upload Text File</h2>
<p>Select a .txt file to organize into a grid</p>
<label class="file-input-wrapper">
Choose File
<input type="file" id="textFile" accept=".txt" />
</label>
</div>
<div class="options-section" id="optionsSection" style="display: none;">
<div class="option-group">
<h3>Grid Dimensions</h3>
<div class="number-input-group">
<div class="number-input-item">
<label for="columnsInput">Columns (leave empty for auto, max 10):</label>
<input type="number" id="columnsInput" min="1" max="50" placeholder="Auto (max 10)" />
</div>
<div class="number-input-item">
<label for="rowsInput">Rows (leave empty for auto):</label>
<input type="number" id="rowsInput" min="1" placeholder="Auto" />
</div>
</div>
</div>
<div class="option-group words-per-cell-group">
<h3>Words Per Cell</h3>
<div class="checkbox-container">
<input type="checkbox" id="fitAllCheckbox" />
<label for="fitAllCheckbox">Fit All (divide evenly across grid)</label>
</div>
<div class="slider-container" id="wordsPerCellContainer">
<div class="slider-label">
<span>Words per cell:</span>
<span id="wordsPerCellValue">1</span>
</div>
<input type="range" id="wordsPerCellSlider" class="slider" min="1" max="20" value="1" step="1">
</div>
</div>
<div class="option-group">
<h3>Reading Direction</h3>
<div class="radio-group">
<label class="radio-option" for="ltr">
<input type="radio" id="ltr" name="direction" value="ltr" checked />
<span>Left-to-Right</span>
</label>
<label class="radio-option" for="rtl">
<input type="radio" id="rtl" name="direction" value="rtl" />
<span>Right-to-Left</span>
</label>
</div>
</div>
<button class="apply-btn" id="generateBtn">Generate Grid</button>
</div>
<div class="results-section" id="resultsSection" style="display: none;">
<div class="output-header">
<div class="output-title">Grid View</div>
</div>
<div id="gridContainer"></div>
<div class="stats" id="statsDisplay"></div>
<button class="apply-btn" id="exportBtn" style="margin-top: 15px;">Export TXT</button>
</div>
</div>
<script>
let fileContent = '';
let words = [];
const fileInput = document.getElementById('textFile');
const optionsSection = document.getElementById('optionsSection');
const generateBtn = document.getElementById('generateBtn');
const resultsSection = document.getElementById('resultsSection');
const gridContainer = document.getElementById('gridContainer');
const statsDisplay = document.getElementById('statsDisplay');
fileInput.addEventListener('change', handleFileUpload);
generateBtn.addEventListener('click', generateGrid);
// Add event listeners for radio buttons
document.querySelectorAll('input[type="radio"]').forEach(radio => {
radio.addEventListener('change', updateRadioStyles);
});
// Initialize radio button styles
updateRadioStyles();
function updateRadioStyles() {
document.querySelectorAll('.radio-option').forEach(option => {
const radio = option.querySelector('input[type="radio"]');
if (radio.checked) {
option.classList.add('checked');
} else {
option.classList.remove('checked');
}
});
}
function handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
if (file.type === 'text/plain' || file.name.endsWith('.txt')) {
const reader = new FileReader();
reader.onload = function(e) {
fileContent = e.target.result;
words = fileContent.split(/\s+/).filter(word => word.trim().length > 0);
optionsSection.style.display = 'block';
resultsSection.style.display = 'none';
generateBtn.disabled = false;
};
reader.onerror = function(e) {
console.error('Error reading file:', e);
alert('Error reading file. Please try again.');
};
reader.readAsText(file);
} else {
alert('Please select a valid .txt file');
}
}
}
function generateGrid() {
if (words.length === 0) {
alert('Please upload a text file first');
return;
}
const columnsInput = document.getElementById('columnsInput').value;
const rowsInput = document.getElementById('rowsInput').value;
const fitAllChecked = document.getElementById('fitAllCheckbox').checked;
const wordsPerCellSliderValue = parseInt(document.getElementById('wordsPerCellSlider').value);
const direction = document.querySelector('input[name="direction"]:checked').value;
let columns, rows, wordsPerCell;
// Calculate grid dimensions
if (fitAllChecked) {
// "Fit All" mode: divide words evenly
columns = columnsInput ? parseInt(columnsInput) : 10;
rows = rowsInput ? parseInt(rowsInput) : Math.ceil(words.length / columns);
const totalCells = columns * rows;
wordsPerCell = Math.ceil(words.length / totalCells);
} else {
// Specific words per cell from slider
wordsPerCell = wordsPerCellSliderValue;
const totalWordsNeeded = words.length;
const cellsNeeded = Math.ceil(totalWordsNeeded / wordsPerCell);
if (columnsInput && rowsInput) {
columns = parseInt(columnsInput);
rows = parseInt(rowsInput);
} else if (columnsInput) {
columns = parseInt(columnsInput);
rows = Math.ceil(cellsNeeded / columns);
} else if (rowsInput) {
rows = parseInt(rowsInput);
columns = Math.ceil(cellsNeeded / rows);
} else {
// Default: max 10 columns, calculate rows
columns = Math.min(10, cellsNeeded);
rows = Math.ceil(cellsNeeded / columns);
}
}
// Create the grid
const table = document.createElement('table');
table.className = 'grid-table';
let wordIndex = 0;
const totalCells = columns * rows;
for (let row = 0; row < rows; row++) {
const tr = document.createElement('tr');
for (let col = 0; col < columns; col++) {
const td = document.createElement('td');
// Determine actual column index based on reading direction
const actualCol = direction === 'rtl' ? (columns - 1 - col) : col;
// Fill cells top-to-bottom, then left-to-right (or right-to-left)
const cellIndex = actualCol * rows + row;
if (wordsPerCellValue === 'fit') {
// Fit all mode: distribute evenly
const startIdx = cellIndex * wordsPerCell;
const endIdx = Math.min(startIdx + wordsPerCell, words.length);
const cellWords = words.slice(startIdx, endIdx);
td.textContent = cellWords.join(' ');
} else {
// Specific words per cell
const cellWords = [];
for (let i = 0; i < wordsPerCell; i++) {
const idx = cellIndex * wordsPerCell + i;
if (idx < words.length) {
cellWords.push(words[idx]);
}
}
td.textContent = cellWords.join(' ');
}
tr.appendChild(td);
}
table.appendChild(tr);
}
gridContainer.innerHTML = '';
gridContainer.appendChild(table);
// Calculate statistics
const wordsUsed = fitAllChecked
? words.length
: Math.min(words.length, totalCells * wordsPerCell);
const wordsNotUsed = words.length - wordsUsed;
statsDisplay.innerHTML = `
<strong>Grid Size:</strong> ${columns} columns × ${rows} rows (${totalCells} cells)<br>
<strong>Words Per Cell:</strong> ${fitAllChecked ? `~${wordsPerCell} (average)` : wordsPerCell}<br>
<strong>Total Words:</strong> ${words.length}<br>
<strong>Words in Grid:</strong> ${wordsUsed}<br>
${wordsNotUsed > 0 ? `<strong>Words Not Displayed:</strong> ${wordsNotUsed}<br>` : ''}
<strong>Reading Direction:</strong> ${direction === 'ltr' ? 'Left-to-Right' : 'Right-to-Left'}
`;
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
// Words per cell slider
const wordsPerCellSlider = document.getElementById('wordsPerCellSlider');
const wordsPerCellValue = document.getElementById('wordsPerCellValue');
const wordsPerCellContainer = document.getElementById('wordsPerCellContainer');
const fitAllCheckbox = document.getElementById('fitAllCheckbox');
if (wordsPerCellSlider && wordsPerCellValue) {
wordsPerCellSlider.addEventListener('input', (e) => {
wordsPerCellValue.textContent = e.target.value;
});
}
if (fitAllCheckbox && wordsPerCellContainer) {
fitAllCheckbox.addEventListener('change', (e) => {
wordsPerCellContainer.style.opacity = e.target.checked ? '0.5' : '1';
wordsPerCellSlider.disabled = e.target.checked;
});
}
document.getElementById('exportBtn').addEventListener('click', function() {
const table = gridContainer.querySelector('table');
if (!table) return;
const rows = Array.from(table.querySelectorAll('tr')).map(function(tr) {
return Array.from(tr.querySelectorAll('td')).map(function(td) {
return td.textContent;
}).join('\t');
});
const text = rows.join('\n');
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'grid-it-lab-output.txt';
a.click();
URL.revokeObjectURL(url);
});
</script>
</body>
</html>