Repository navigation
Expand file tree
/
Copy pathhtml-renderer.js
More file actions
415 lines (367 loc) · 16.9 KB
/
Copy pathhtml-renderer.js
File metadata and controls
415 lines (367 loc) · 16.9 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// ============================================================================
// HTML RENDERER MODULE
// Updated: 2025-02-02 02:37:00 - Fixed badge generation groups
// ============================================================================
console.log(' html-renderer.js loading - v3');
import {
SIZE_OPTIONS,
TYPE_OPTIONS,
FLAG_OPTIONS,
OPERAND_FLAGS,
SIZE_NEEDED_TYPES
} from './core-constants.js';
import { canExpand } from './validation.js';
import { getGroupLines, isGroupLeader } from './groups.js';
/**
* Gets size options HTML for a given type
* @param {string} type - The condition type
* @param {string} selectedSize - The currently selected size
* @returns {string} HTML options string
*/
export function getSizeOptions(type, selectedSize) {
let options = '';
if (type === 'BCD') {
const sizes = SIZE_OPTIONS.BCD;
sizes.forEach((size) => {
options += `<option value="${size}" ${selectedSize === size ? 'selected' : ''}>${size}</option>`;
});
} else if (type === 'Float') {
const sizes = SIZE_OPTIONS.Float;
sizes.forEach((size) => {
options += `<option value="${size}" ${selectedSize === size ? 'selected' : ''}>${size}</option>`;
});
} else if (
type === 'Mem' ||
type === 'Delta' ||
type === 'Prior' ||
type === 'Invert'
) {
const sizes = SIZE_OPTIONS.Mem;
sizes.forEach((size) => {
options += `<option value="${size}" ${selectedSize === size ? 'selected' : ''}>${size}</option>`;
});
}
return options;
}
/**
* Gets type options HTML for a given flag
* @param {string} flag - The condition flag
* @param {string} currentType - The currently selected type
* @returns {string} HTML options string
*/
export function getTypeOptions(flag, currentType) {
if (flag === 'I:') {
// Add Address flag - only allow Mem, Prior, Value, and Recall
const allowedTypes = TYPE_OPTIONS.AddAddress;
let options = '';
allowedTypes.forEach((type) => {
options += `<option value="${type}" ${currentType === type ? 'selected' : ''}>${type}</option>`;
});
return options;
} else {
// All other flags - allow all types
const allTypes = TYPE_OPTIONS.All;
let options = '';
allTypes.forEach((type) => {
options += `<option value="${type}" ${currentType === type ? 'selected' : ''}>${type}</option>`;
});
return options;
}
}
/**
* Creates the HTML for a condition row
* @param {Object} condition - The condition object
* @returns {string} HTML string for the condition row
*/
export function createConditionRowHTML(condition) {
const isOperandFlag = OPERAND_FLAGS.includes(condition.flag);
const hasOperand = condition.cmp && condition.cmp !== '';
let cmpOptions = '';
if (isOperandFlag) {
cmpOptions = `
<option value="" ${condition.cmp === '' ? 'selected' : ''}></option>
<option value="*" ${condition.cmp === '*' ? 'selected' : ''}>*</option>
<option value="/" ${condition.cmp === '/' ? 'selected' : ''}>/</option>
<option value="%" ${condition.cmp === '%' ? 'selected' : ''}>%</option>
<option value="+" ${condition.cmp === '+' ? 'selected' : ''}>+</option>
<option value="-" ${condition.cmp === '-' ? 'selected' : ''}>-</option>
<option value="&" ${condition.cmp === '&' ? 'selected' : ''}>&</option>
<option value="^" ${condition.cmp === '^' ? 'selected' : ''}>^</option>
`;
} else {
cmpOptions = `
<option value="=" ${condition.cmp === '=' ? 'selected' : ''}>=</option>
<option value="<" ${condition.cmp === '<' ? 'selected' : ''}><</option>
<option value="<=" ${condition.cmp === '<=' ? 'selected' : ''}><=</option>
<option value=">" ${condition.cmp === '>' ? 'selected' : ''}>></option>
<option value=">=" ${condition.cmp === '>=' ? 'selected' : ''}>>=</option>
<option value="!=" ${condition.cmp === '!=' ? 'selected' : ''}>!=</option>
`;
}
const leftSizeOptions = getSizeOptions(condition.type, condition.size);
const rightSizeOptions = getSizeOptions(
condition.compareType,
condition.compareSize,
);
const leftNeedsSize = SIZE_NEEDED_TYPES.includes(condition.type);
const leftNeedsInput = condition.type !== 'Recall';
const rightNeedsSize = SIZE_NEEDED_TYPES.includes(condition.compareType);
const rightNeedsInput = condition.compareType !== 'Recall';
return `
<div>
<select onchange="window.updateBitfieldCondition(${condition.lineId}, 'flag', this.value); window.renderBitfieldConditions();">
${FLAG_OPTIONS.map(flag =>
`<option value="${flag.value}" ${condition.flag === flag.value ? 'selected' : ''}>${flag.label}</option>`
).join('')}
</select>
</div>
<div>
<select id="type-select-${condition.lineId}" onchange="window.validateTypeChange(${condition.lineId}, this.value); window.renderBitfieldConditions();">
${getTypeOptions(condition.flag, condition.type)}
</select>
</div>
<div class="${!leftNeedsSize ? 'hidden' : ''}">
<select onchange="window.updateBitfieldCondition(${condition.lineId}, 'size', this.value)" ${!leftNeedsSize ? 'disabled' : ''}>
${leftSizeOptions || '<option>-</option>'}
</select>
</div>
<div class="${!leftNeedsInput ? 'hidden' : ''}">
<input type="text" value="${condition.memory}"
onchange="window.updateBitfieldCondition(${condition.lineId}, 'memory', this.value); window.recomputeExpandState(${condition.lineId}); window.renderBitfieldConditions();"
oninput="window.updateBitfieldCondition(${condition.lineId}, 'memory', this.value); window.recomputeExpandState(${condition.lineId});"
${!leftNeedsInput ? 'disabled' : ''}>
</div>
<div>
<select onchange="window.updateBitfieldCondition(${condition.lineId}, 'cmp', this.value); window.renderBitfieldConditions();">
${cmpOptions}
</select>
</div>
<div class="${isOperandFlag && !hasOperand ? 'hidden' : ''}">
<select onchange="window.updateBitfieldCondition(${condition.lineId}, 'compareType', this.value); window.renderBitfieldConditions();" ${isOperandFlag && !hasOperand ? 'disabled' : ''}>
<option value="Value" ${condition.compareType === 'Value' ? 'selected' : ''}>Value</option>
<option value="Mem" ${condition.compareType === 'Mem' ? 'selected' : ''}>Mem</option>
<option value="Delta" ${condition.compareType === 'Delta' ? 'selected' : ''}>Delta</option>
<option value="Prior" ${condition.compareType === 'Prior' ? 'selected' : ''}>Prior</option>
<option value="BCD" ${condition.compareType === 'BCD' ? 'selected' : ''}>BCD</option>
<option value="Float" ${condition.compareType === 'Float' ? 'selected' : ''}>Float</option>
<option value="Invert" ${condition.compareType === 'Invert' ? 'selected' : ''}>Invert</option>
<option value="Recall" ${condition.compareType === 'Recall' ? 'selected' : ''}>Recall</option>
</select>
</div>
<div class="${(isOperandFlag && !hasOperand) || !rightNeedsSize ? 'hidden' : ''}">
<select onchange="window.updateBitfieldCondition(${condition.lineId}, 'compareSize', this.value)" ${(isOperandFlag && !hasOperand) || !rightNeedsSize ? 'disabled' : ''}>
${rightSizeOptions || '<option>-</option>'}
</select>
</div>
<div class="${(isOperandFlag && !hasOperand) || !rightNeedsInput ? 'hidden' : ''}">
<input type="text" value="${condition.value}"
onchange="window.updateBitfieldCondition(${condition.lineId}, 'value', this.value); window.renderBitfieldConditions();"
${(isOperandFlag && !hasOperand) || !rightNeedsInput ? 'disabled' : ''}>
</div>
<div class="${isOperandFlag ? 'hidden' : ''}">
<input type="number" min="0" value="${condition.hits}" onchange="window.updateBitfieldCondition(${condition.lineId}, 'hits', this.value)" ${isOperandFlag ? 'disabled' : ''}>
</div>
`;
}
/**
* Formats a condition for display
* @param {Object} condition - The condition to format
* @returns {string} Formatted display string
*/
export function formatConditionDisplay(condition) {
let display = '';
if (condition.flag) display += condition.flag + ' ';
display += condition.type + ' ';
display += condition.size + ' ';
display += condition.memory;
if (condition.cmp) {
display += ' ' + condition.cmp + ' ';
display += condition.compareType + ' ';
if (condition.compareSize) display += condition.compareSize + ' ';
display += condition.value;
}
return display;
}
/**
* Renders all bitfield conditions to the DOM
* @param {Array} bitfieldConditions - Array of conditions
* @param {Map} bitfieldExpansions - Expansion map
* @param {Map} linkGroupColors - Group colors map
* @param {Function} linkCondition - Function to link conditions
* @param {Function} unlinkCondition - Function to unlink conditions
* @param {Function} canLinkCondition - Function to check if linking is possible
* @param {Function} addConditionAtIndex - Function to add condition at index
* @param {Function} copyCondition - Function to copy condition
* @param {Function} removeBitfieldCondition - Function to remove condition
* @param {Function} expandCondition - Function to expand condition
* @param {Function} reopenExpansion - Function to reopen expansion
*/
export function renderBitfieldConditions(
bitfieldConditions,
bitfieldExpansions,
linkGroupColors,
linkCondition,
unlinkCondition,
canLinkCondition,
addConditionAtIndex,
copyCondition,
removeBitfieldCondition,
expandCondition,
reopenExpansion
) {
const container = document.getElementById('bitfieldConditionsList');
if (!container) {
console.error('bitfieldConditionsList container not found!');
return;
}
container.innerHTML = '';
// Group conditions by groupId to render together
const processedIndices = new Set();
const groupMap = new Map(); // Map<groupId, condition[]>
bitfieldConditions.forEach((condition, index) => {
if (!groupMap.has(condition.groupId)) {
groupMap.set(condition.groupId, []);
}
groupMap.get(condition.groupId).push({ condition, index });
});
bitfieldConditions.forEach((condition, index) => {
if (processedIndices.has(index)) return;
const groupLines = groupMap.get(condition.groupId);
const isMultiLineGroup = groupLines.length > 1;
if (isMultiLineGroup) {
// Render entire link group
const colorIndex = linkGroupColors.get(condition.groupId);
const colorClass = colorIndex === 0 ? 'forest-green' : 'maroon';
const groupContainer = document.createElement('div');
groupContainer.className = `link-group-container ${colorClass}`;
groupLines.forEach(
({ condition: groupCondition, index: conditionIndex }) => {
processedIndices.add(conditionIndex);
const isLeader = isGroupLeader(bitfieldConditions, groupCondition);
// Create condition row
const row = document.createElement('div');
row.className =
'bitfield-condition-row' +
(groupCondition.expanded ? ' read-only' : '') +
' show-expand';
row.setAttribute('data-id', groupCondition.lineId);
row.innerHTML = createConditionRowHTML(groupCondition);
groupContainer.appendChild(row);
// Show buttons based on position in link group
const copyRow = document.createElement('div');
copyRow.className = 'bitfield-copy-row';
if (isLeader) {
// Leader - show all buttons
copyRow.innerHTML = `
${
groupCondition.expanded
? (() => {
// Get the generated groups count from the condition (stored before expansion was deleted)
const generatedGroups = groupCondition.generatedGroupsCount || 1;
const badgeText = `Generating ${generatedGroups} ${generatedGroups === 1 ? 'group' : 'groups'}`;
return `<div class="expansion-badge" onclick="window.reopenExpansion(${groupCondition.lineId})">${badgeText}</div>`;
})()
: `<button type="button" class="expand-btn" onclick="console.log('Expand clicked for line:', ${groupCondition.lineId}); window.expandCondition(${groupCondition.lineId})" ${!canExpand(groupCondition) ? 'disabled' : ''}>Expand</button>`
}
<button class="link-btn" onclick="window.linkCondition(${groupCondition.lineId})" ${!canLinkCondition(groupCondition.lineId) ? 'disabled' : ''}>Link</button>
<button class="add-line-btn" onclick="window.addConditionAtIndex(${conditionIndex})">Add</button>
<button class="copy-condition-btn" onclick="window.copyCondition(${groupCondition.lineId})">Copy</button>
<button class="remove-btn" onclick="window.removeBitfieldCondition(${groupCondition.lineId})">×</button>
`;
} else {
// Non-leader - only show Unlink button
copyRow.innerHTML = `
<button class="link-btn" onclick="window.unlinkCondition(${groupCondition.lineId})">Unlink</button>
`;
}
groupContainer.appendChild(copyRow);
},
);
container.appendChild(groupContainer);
// Handle expansion for this group - check if any condition in the group has an expansion
const groupExpansion = groupLines.find(
({ condition: groupCondition }) =>
bitfieldExpansions[groupCondition.lineId],
);
if (groupExpansion) {
const expansionId = groupExpansion.condition.lineId;
const expansionDiv = document.createElement('div');
if (
bitfieldExpansions[expansionId] &&
bitfieldExpansions[expansionId].showingCustom
) {
expansionDiv.innerHTML = bitfieldExpansions[expansionId].customHtml;
} else {
expansionDiv.innerHTML = bitfieldExpansions[expansionId].html;
}
container.appendChild(expansionDiv);
}
} else {
// Render individual condition (not linked)
processedIndices.add(index);
const row = document.createElement('div');
row.className =
'bitfield-condition-row' +
(condition.expanded ? ' read-only' : '') +
' show-expand';
row.setAttribute('data-id', condition.lineId);
row.innerHTML = createConditionRowHTML(condition);
container.appendChild(row);
const copyRow = document.createElement('div');
copyRow.className = 'bitfield-copy-row';
copyRow.innerHTML = `
${
condition.expanded
? (() => {
// Get the generated groups count from the condition (stored before expansion was deleted)
const generatedGroups = condition.generatedGroupsCount || 1;
const badgeText = `Generating ${generatedGroups} ${generatedGroups === 1 ? 'group' : 'groups'}`;
return `<div class="expansion-badge" onclick="window.reopenExpansion(${condition.lineId})">${badgeText}</div>`;
})()
: `<button type="button" class="expand-btn" onclick="console.log('Expand clicked for line:', ${condition.lineId}); window.expandCondition(${condition.lineId})" ${!canExpand(condition) ? 'disabled' : ''}>Expand</button>`
}
<button class="link-btn" onclick="window.linkCondition(${condition.lineId})" ${!canLinkCondition(condition.lineId) ? 'disabled' : ''}>Link</button>
<button class="add-line-btn" onclick="window.addConditionAtIndex(${index})">Add</button>
<button class="copy-condition-btn" onclick="window.copyCondition(${condition.lineId})">Copy</button>
<button class="remove-btn" onclick="window.removeBitfieldCondition(${condition.lineId})">×</button>
`;
container.appendChild(copyRow);
// Handle expansion for individual condition
if (bitfieldExpansions[condition.lineId]) {
console.log('Rendering expansion for line:', condition.lineId);
const expansionDiv = document.createElement('div');
if (bitfieldExpansions[condition.lineId].showingCustom) {
expansionDiv.innerHTML =
bitfieldExpansions[condition.lineId].customHtml;
} else {
expansionDiv.innerHTML = bitfieldExpansions[condition.lineId].html;
console.log(
'Expansion HTML set for line:',
condition.lineId,
'length:',
expansionDiv.innerHTML.length,
);
}
container.appendChild(expansionDiv);
console.log(
'Expansion div added to container for line:',
condition.lineId,
);
}
}
});
}
/**
* Flashes a type dropdown to show validation error
* @param {number} lineId - The line ID
*/
export function flashTypeDropdown(lineId) {
const selectElement = document.getElementById(`type-select-${lineId}`);
if (!selectElement) return;
// Add CSS animation class
selectElement.classList.add('type-validation-flash');
// Remove class after animation completes
setTimeout(() => {
selectElement.classList.remove('type-validation-flash');
}, 1000);
}