Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
467 lines (397 loc) · 15.5 KB
/
Copy pathscript.js
File metadata and controls
467 lines (397 loc) · 15.5 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Configuration for different comparison types
const COMPARISON_TYPES = {
importance: {
itemLabel: 'items to compare',
placeholder: 'e.g., Price, Quality, Speed, Reliability',
sliderLabels: [
'Much more important',
'More important',
'Equally important',
'More important',
'Much more important'
],
question: 'Which is more important?',
resultLabel: 'Importance Score',
downloadFileName: 'importance_comparison_matrix.csv'
},
likelihood: {
itemLabel: 'possible outcomes',
placeholder: 'e.g., Outcome A, Outcome B, Outcome C',
sliderLabels: [
'Much more likely',
'More likely',
'Equally likely',
'More likely',
'Much more likely'
],
question: 'Which is more likely?',
resultLabel: 'Likelihood Score',
downloadFileName: 'likelihood_comparison_matrix.csv'
}
};
// Global variables
let elements = [];
let comparisons = [];
let currentComparison = 0;
let matrix = [];
// Initialize the app
document.addEventListener('DOMContentLoaded', function() {
const radioButtons = document.querySelectorAll('input[name="comparisonType"]');
radioButtons.forEach(radio => {
radio.addEventListener('change', updateUILanguage);
});
updateUILanguage();
// Add slider event listener
const slider = document.getElementById('comparisonSlider');
slider.addEventListener('input', updateSliderSelection);
});
function updateUILanguage() {
const selectedType = document.querySelector('input[name="comparisonType"]:checked').value;
const typeConfig = COMPARISON_TYPES[selectedType];
document.getElementById('elementsLabel').textContent = `Enter ${typeConfig.itemLabel}`;
document.getElementById('elements').placeholder = typeConfig.placeholder;
const questionEl = document.getElementById('comparisonQuestion');
if (questionEl) {
questionEl.textContent = typeConfig.question;
}
updateSliderLabels();
}
function updateSliderLabels() {
const selectedType = document.querySelector('input[name="comparisonType"]:checked').value;
const labels = COMPARISON_TYPES[selectedType].sliderLabels;
document.getElementById('leftLabel').textContent = labels[0];
document.getElementById('centerLabel').textContent = labels[2];
document.getElementById('rightLabel').textContent = labels[4];
}
function updateStepIndicator(stepNumber) {
document.querySelectorAll('.step').forEach((step, index) => {
if (index < stepNumber) {
step.classList.add('active');
} else {
step.classList.remove('active');
}
});
}
function startComparison() {
const elementsInput = document.getElementById('elements').value;
elements = elementsInput.split(',')
.map(e => e.trim())
.filter(e => e.length > 0);
if (elements.length < 2) {
alert('Please enter at least 2 items to compare');
return;
}
// Initialize matrix
matrix = Array(elements.length).fill(0)
.map(() => Array(elements.length).fill(1));
// Generate all possible pairs
comparisons = [];
for (let i = 0; i < elements.length - 1; i++) {
for (let j = i + 1; j < elements.length; j++) {
comparisons.push([i, j]);
}
}
currentComparison = 0;
document.getElementById('setup').style.display = 'none';
document.getElementById('comparison').style.display = 'block';
updateStepIndicator(2);
// Add progress information
const progressInfo = `
<div class="comparison-progress">
<div class="progress-bar">
<div class="progress-fill" style="width: 0%"></div>
</div>
<div class="progress-text">0/${comparisons.length} comparisons completed</div>
</div>
`;
document.getElementById('comparison').insertAdjacentHTML('afterbegin', progressInfo);
showNextComparison();
}
function updateSliderSelection() {
const slider = document.getElementById('comparisonSlider');
const value = parseInt(slider.value);
const selectedType = document.querySelector('input[name="comparisonType"]:checked').value;
const labels = COMPARISON_TYPES[selectedType].sliderLabels;
// Update visual feedback based on slider position
document.getElementById('leftLabel').style.fontWeight = value < 2 ? '600' : '400';
document.getElementById('centerLabel').style.fontWeight = value === 2 ? '600' : '400';
document.getElementById('rightLabel').style.fontWeight = value > 2 ? '600' : '400';
}
function submitComparison() {
const [i, j] = comparisons[currentComparison];
const sliderValue = parseInt(document.getElementById('comparisonSlider').value);
// Convert slider value to matrix values
const values = [5, 3, 1, 1/3, 1/5];
const ratio = values[sliderValue];
matrix[i][j] = ratio;
matrix[j][i] = 1/ratio;
currentComparison++;
// Update progress bar
const progressPercentage = (currentComparison / comparisons.length) * 100;
const progressFill = document.querySelector('.comparison-progress .progress-fill');
const progressText = document.querySelector('.comparison-progress .progress-text');
progressFill.style.width = `${progressPercentage}%`;
progressText.textContent = `${currentComparison}/${comparisons.length} comparisons completed`;
if (currentComparison < comparisons.length) {
showNextComparison();
} else {
showResults();
}
}
function showNextComparison() {
const [i, j] = comparisons[currentComparison];
document.getElementById('optionA').textContent = elements[i];
document.getElementById('optionB').textContent = elements[j];
// Reset slider to center
const slider = document.getElementById('comparisonSlider');
slider.value = 2;
updateSliderSelection();
}
function showResults() {
document.getElementById('comparison').style.display = 'none';
document.getElementById('results').style.display = 'block';
updateStepIndicator(3);
// Calculate scores
const scores = matrix.map(row =>
row.reduce((a, b) => a + b, 0) / elements.length
);
// Calculate percentages
const totalScore = scores.reduce((a, b) => a + b, 0);
const percentages = scores.map(score => (score / totalScore) * 100);
// Create results table
let html = '<table class="result-table"><tr><th></th>';
elements.forEach(e => {
html += `<th>${e}</th>`;
});
html += '<th>Score</th><th>Percentage</th></tr>';
for (let i = 0; i < elements.length; i++) {
html += `<tr><th>${elements[i]}</th>`;
for (let j = 0; j < elements.length; j++) {
html += `<td>${matrix[i][j].toFixed(2)}</td>`;
}
html += `<td>${scores[i].toFixed(2)}</td>`;
html += `<td>${percentages[i].toFixed(1)}%</td></tr>`;
}
html += '</table>';
// Add ranked summary
const rankedResults = elements.map((element, index) => ({
element,
score: scores[index],
percentage: percentages[index]
})).sort((a, b) => b.percentage - a.percentage);
html += '<h3>Ranking</h3><ul class="summary-list">';
rankedResults.forEach((result, index) => {
html += `
<li>
<span><strong>#${index + 1}:</strong> ${result.element}</span>
<div style="display: flex; align-items: center;">
<span>${result.percentage.toFixed(1)}%</span>
<div class="progress-bar">
<div class="progress-fill" style="width: ${result.percentage}%"></div>
</div>
</div>
</li>`;
});
html += '</ul>';
document.getElementById('resultMatrix').innerHTML = html;
}
function downloadCSV() {
const selectedType = document.querySelector('input[name="comparisonType"]:checked').value;
const typeConfig = COMPARISON_TYPES[selectedType];
let csvContent = "Elements," + elements.join(",") + ",Score,Percentage\n";
const scores = matrix.map(row =>
row.reduce((a, b) => a + b, 0) / elements.length
);
const totalScore = scores.reduce((a, b) => a + b, 0);
const percentages = scores.map(score => (score / totalScore) * 100);
for (let i = 0; i < elements.length; i++) {
let row = [
elements[i],
...matrix[i].map(v => v.toFixed(2)),
scores[i].toFixed(2),
percentages[i].toFixed(1)
];
csvContent += row.join(",") + "\n";
}
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement("a");
const url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", typeConfig.downloadFileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function startEvaluation() {
// Store current matrix and elements if not already stored
window.savedMatrix = matrix;
window.savedElements = elements;
// Hide results and show evaluation section
document.getElementById('results').style.display = 'none';
document.getElementById('evaluation').style.display = 'block';
updateStepIndicator(4);
// Clear previous options
const optionsInput = document.getElementById('optionsInput');
optionsInput.value = '';
// Clear evaluation matrix and show initial message
const evaluationMatrix = document.getElementById('evaluationMatrix');
evaluationMatrix.innerHTML = '<p class="input-help">Enter your options above to start the evaluation</p>';
// Add event listener for options input
optionsInput.addEventListener('input', handleOptionsInput);
}
function handleOptionsInput() {
const optionsInput = document.getElementById('optionsInput');
const options = optionsInput.value
.split(',')
.map(opt => opt.trim())
.filter(opt => opt.length > 0);
if (options.length >= 2) {
createEvaluationMatrix(options);
} else {
document.getElementById('evaluationMatrix').innerHTML =
'<p class="input-help">Enter at least 2 options to compare</p>';
}
}
function createEvaluationMatrix(options) {
const criteria = window.savedElements;
const weights = calculateWeights();
const totalRatings = options.length * criteria.length;
let html = `
<div class="progress-container">
<div class="progress-bar">
<div class="progress-fill" style="width: 0%"></div>
</div>
<div class="progress-text">0/${totalRatings} ratings completed</div>
</div>
<table class="evaluation-table">
<thead>
<tr>
<th>Criteria (Weight)</th>
${options.map(opt => `<th>${opt}</th>`).join('')}
</tr>
</thead>
<tbody>`;
criteria.forEach((criterion, i) => {
html += `
<tr>
<td>
${criterion}
<small>(${(weights[i] * 100).toFixed(1)}%)</small>
</td>
${options.map(opt => `
<td>
<input type="number"
min="1"
max="5"
step="0.5"
class="rating-input"
data-criterion="${criterion}"
data-option="${opt}"
onchange="updateEvaluationProgress(${totalRatings})"
placeholder="1-5">
</td>
`).join('')}
</tr>`;
});
html += `
</tbody>
</table>
<div class="rating-help">
<p>Rate each option from 1 to 5 for each criterion:</p>
<ul>
<li>1 = Poor</li>
<li>2 = Fair</li>
<li>3 = Good</li>
<li>4 = Very Good</li>
<li>5 = Excellent</li>
</ul>
</div>
<div class="button-container">
<button onclick="calculateFinalResults()" class="btn btn-primary">
Calculate Results
</button>
</div>`;
document.getElementById('evaluationMatrix').innerHTML = html;
}
function calculateWeights() {
const matrix = window.savedMatrix;
const n = matrix.length;
const rowSums = matrix.map(row =>
row.reduce((a, b) => a + b, 0)
);
const total = rowSums.reduce((a, b) => a + b, 0);
return rowSums.map(sum => sum / total);
}
function updateEvaluationProgress(totalRatings) {
const inputs = document.querySelectorAll('.rating-input');
const validInputs = Array.from(inputs).filter(input => {
const value = parseFloat(input.value);
return !isNaN(value) && value >= 1 && value <= 5;
}).length;
const progressPercent = (validInputs / totalRatings) * 100;
const progressFill = document.querySelector('.progress-fill');
const progressText = document.querySelector('.progress-text');
if (progressFill && progressText) {
progressFill.style.width = `${progressPercent}%`;
progressText.textContent = `${validInputs}/${totalRatings} ratings completed`;
}
}
function calculateFinalResults() {
const options = document.getElementById('optionsInput').value
.split(',')
.map(opt => opt.trim())
.filter(opt => opt.length > 0);
const criteria = window.savedElements;
const weights = calculateWeights();
// Validate all inputs
const inputs = document.querySelectorAll('.rating-input');
let allValid = true;
inputs.forEach(input => {
const value = parseFloat(input.value);
if (!value || value < 1 || value > 5) {
input.style.borderColor = 'red';
allValid = false;
} else {
input.style.borderColor = '';
}
});
if (!allValid) {
alert('Please fill in all ratings with values between 1 and 5');
return;
}
// Calculate weighted scores
const results = options.map(option => {
let totalScore = 0;
criteria.forEach((criterion, i) => {
const input = document.querySelector(`input[data-criterion="${criterion}"][data-option="${option}"]`);
const rating = parseFloat(input.value);
totalScore += rating * weights[i];
});
return { option, score: totalScore };
});
// Sort and display results
results.sort((a, b) => b.score - a.score);
displayFinalResults(results);
}
function displayFinalResults(results) {
document.getElementById('evaluation').style.display = 'none';
document.getElementById('finalResults').style.display = 'block';
const maxScore = Math.max(...results.map(r => r.score));
let html = '<div class="final-rankings">';
results.forEach((result, index) => {
const percentage = (result.score / maxScore) * 100;
html += `
<div class="ranking-item">
<div class="ranking-details">
<h3>#${index + 1}: ${result.option}</h3>
<div class="score-bar-container">
<div class="score-bar" style="width: ${percentage}%"></div>
</div>
</div>
<div class="ranking-score">${result.score.toFixed(2)}</div>
</div>
`;
});
html += '</div>';
document.getElementById('rankingResults').innerHTML = html;
}