-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathErrorBar.js
182 lines (158 loc) · 6.2 KB
/
ErrorBar.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
import { openAsOverlay } from "../util/HtmlUtil";
import { type } from "../util/Util";
//TODO rename to "ViolationBar" or similar, as only one severity is about errors
export default class ErrorBar {
constructor(element, mediator) {
this.element = element;
this.mediator = mediator;
makeResizable(element);
this.table = element.querySelector('.errorTable');
this.toggleTableButton = document.getElementById('toggleErrorTable');
this.toggleTableButton.addEventListener('click', event => {
this.toggleTable();
});
this.numberOfViolations = document.getElementById('numberOfViolations');
makeColumnsResizable(this.table);
}
clear() {
while (this.table.rows.length > 1) {
this.table.deleteRow(1);
}
this.numberOfViolations.innerHTML = '';
}
displayRow({ severity, element, artifact, message, link, quickFixes }) {
const row = this.table.insertRow(-1);
row.addEventListener('dblclick', event => {
this.mediator.focusElement(element);
});
row.classList.add(severity.cssClass);
const severityCell = row.insertCell(-1), messageCell = row.insertCell(-1), linkCell = row.insertCell(-1), elementCell = row.insertCell(-1), artifactCell = row.insertCell(-1);
severityCell.classList.add('narrowColumn');
elementCell.innerHTML = type(element) + ' \"' + element.name + '\"';
artifactCell.innerHTML = artifact;
messageCell.innerHTML = message;
const linkElement = makeGuidelineLink(link);
linkCell.classList.add('narrowColumn');
linkCell.appendChild(linkElement);
if (quickFixes && quickFixes.length > 0) {
const quickFixesButton = document.createElement('button');
quickFixesButton.innerHTML = '💡';
quickFixesButton.style.border = 'none';
quickFixesButton.style.backgroundColor = 'transparent';
messageCell.appendChild(quickFixesButton);
quickFixesButton.addEventListener('click', event => {
event.stopPropagation();
const quickFixDiv = makeQuickFixDiv(quickFixes, () => {
this.mediator.focusElement(element);
});
openAsOverlay(quickFixDiv, event);
});
quickFixesButton.addEventListener('dblclick', event => {
event.stopPropagation();
});
}
}
toggleTable() {
this.element.classList.toggle('hidingTable');
}
displayNumberOfViolations(severity, number) {
const display = document.createElement('span');
display.innerHTML = severity.label + ': ' + number;
display.classList.add('barButton');
this.numberOfViolations.appendChild(display);
return display;
}
}
export function makeGuidelineLink(link) {
const linkElement = document.createElement('a');
linkElement.href = link;
linkElement.target = '_blank';
linkElement.innerHTML = '❓';
return linkElement;
}
export function makeQuickFixDiv(quickFixes, onFix = ()=>{}) {
const quickFixDiv = document.createElement('div');
quickFixDiv.classList.add("quickFixDiv");
const quickFixTable = document.createElement('table');
quickFixes.forEach(quickFix => {
const quickFixRow = quickFixTable.insertRow(-1);
quickFixRow.style.borderBottom = '0px';
const cell = quickFixRow.insertCell(-1);
// cell.classList.add('dd-dropdown-entry');
cell.classList.add('quickFixCell');
cell.style.padding = '3px';
cell.style.color = '#29487D';
cell.innerHTML = quickFix.label;
cell.addEventListener('click', event => {
onFix(quickFix);
quickFix.action(event);
event.stopPropagation();
});
cell.style.cursor = 'pointer';
});
quickFixTable.classList.add('errorTable');
quickFixTable.style.margin = '0';
quickFixTable.style.width = '100%';
quickFixDiv.appendChild(quickFixTable);
return quickFixDiv;
}
function makeResizable(elmnt) {
// TODO now we have two kinds of code for resizing: dividers and this here
addDragListener(elmnt, (dx, dy) => {
elmnt.style.height = elmnt.offsetHeight - dy + "px";
});
}
function addDragListener(element, callback) {
var deltaX = 0, deltaY = 0, currentX = 0, currentY = 0;
element.addEventListener('mousedown', dragMouseDown);
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
currentX = e.clientX;
currentY = e.clientY;
// stop listening when mouse is released
document.addEventListener('mouseup', closeDragElement);
// call a function whenever the cursor moves:
document.addEventListener('mousemove', elementDrag);
}
function elementDrag(e) {
if (element.classList.contains('hidingTable')) return;
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
deltaX = e.clientX - currentX;
deltaY = e.clientY - currentY;
currentX = e.clientX;
currentY = e.clientY;
callback(deltaX, deltaY);
}
function closeDragElement() {
document.removeEventListener('mouseup', closeDragElement);
document.removeEventListener('mousemove', elementDrag);
}
}
function makeColumnsResizable(table) {
const row = table.getElementsByTagName('tr')[0],
cols = row?.children;
for (let i = 0; i < cols.length - 1; i++) {
const div = createDiv();
const col = cols[i];
col.appendChild(div);
col.style.position = 'relative';
addDragListener(div, (dx, dy) => {
col.style.width = col.offsetWidth + dx + "px";
})
}
const tableBody = table.querySelector('tbody');
new ResizeObserver(() => {
table.querySelectorAll('.columnDivider').forEach(div => {
div.style.height = tableBody.offsetHeight + 'px';
});
}).observe(tableBody);
function createDiv() {
const div = document.createElement('div');
div.classList.add('columnDivider');
return div;
}
}