-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathexercise_collections.js
More file actions
211 lines (185 loc) · 7.32 KB
/
exercise_collections.js
File metadata and controls
211 lines (185 loc) · 7.32 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
$(document).on('turbo-migration:load', function() {
if ($.isController('exercise_collections')) {
var dataElement = $('#data');
var exerciseList = $('#exercise-list');
if (dataElement.isPresent()) {
var data = dataElement.data('working-times');
var averageWorkingTimeValue = parseFloat(dataElement.data('average-working-time'));
var margin = {top: 30, right: 40, bottom: 30, left: 50},
width = 720 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scaleBand().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom(x);
var yAxisLeft = d3.axisLeft(y);
var tooltip = d3.select("#graph").append("div").attr("class", "exercise-id-tooltip");
var averageWorkingTime = d3.line()
.x(function (d) {
return x(d.index) + x.bandwidth() / 2;
})
.y(function () {
return y(averageWorkingTimeValue);
});
var minWorkingTime = d3.line()
.x(function (d) {
return x(d.index) + x.bandwidth() / 2;
})
.y(function () {
return y(0.1 * averageWorkingTimeValue);
});
var maxWorkingTime = d3.line()
.x(function (d) {
return x(d.index) + x.bandwidth() / 2;
})
.y(function () {
return y(2 * averageWorkingTimeValue);
});
var svg = d3.select('#graph')
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Get the data
data = Object.keys(data).map(function (key) {
return {
index: parseInt(key),
exercise_id: parseInt(data[key]['exercise_id']),
exercise_title: data[key]['exercise_title'],
working_time: parseFloat(data[key]['working_time'])
};
});
// Scale the range of the data
x.domain(data.map(function (d) {
return d.index;
}));
y.domain([0, d3.max(data, function (d) {
return d.working_time;
})]);
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.style("fill", "steelblue")
.call(yAxisLeft);
// Draw the bars
svg.selectAll("bar")
.data(data)
.enter()
.append("rect")
.attr("class", "value-bar")
.on("mousemove", function (event, d) {
tooltip
.style("left", event.pageX - 50 + "px")
.style("top", event.pageY + 50 + "px")
.style("display", "inline-block")
.html(`${I18n.t('activerecord.models.exercise.one')} ID: ${d.exercise_id}<br>` +
`${I18n.t('activerecord.attributes.exercise.title')}: ${d.exercise_title}<br>` +
`${I18n.t('exercises.statistics.average_worktime')}: ${d.working_time}s`);
})
.on("mouseout", function () {
tooltip.style("display", "none");
})
.on("click", function (_event, d) {
Turbo.visit(Routes.statistics_exercise_path(d.exercise_id));
})
.attr("x", function (d) {
return x(d.index);
})
.attr("width", x.bandwidth())
.attr("y", function (d) {
return y(d.working_time);
})
.attr("height", function (d) {
return height - y(d.working_time);
});
// Add the average working time path
svg.append("path")
.datum(data)
.attr("class", "line average-working-time")
.attr("d", averageWorkingTime);
// Add the anomaly paths (min/max average exercise working time)
svg.append("path")
.datum(data)
.attr("class", "line minimum-working-time")
.attr("d", minWorkingTime);
svg.append("path")
.datum(data)
.attr("class", "line maximum-working-time")
.attr("d", maxWorkingTime);
} else if (exerciseList.isPresent()) {
var exerciseSelect = $('#exercise-select');
var list = $("#sortable");
var updateExerciseList = function () {
// remove all options from the hidden select and add all selected exercises in the new order
exerciseSelect.find('option').remove();
var exerciseIdsInSortedOrder = list.sortable('toArray', {attribute: 'data-id'});
for (var i = 0; i < exerciseIdsInSortedOrder.length; i += 1) {
exerciseSelect.append('<option value="' + exerciseIdsInSortedOrder[i] + '" selected></option>')
}
}
list.sortable({
items: 'tr',
update: updateExerciseList
});
list.disableSelection();
var addExercisesForm = $('#exercise-selection');
var addExercisesButton = $('#add-exercises');
var removeExerciseButtons = $('.remove-exercise');
var sortButton = $('#sort-button');
var collectContainedExercises = function () {
return exerciseList.find('tbody > tr').toArray().map(function (item) {return $(item).data('id')});
}
var sortExercises = function() {
var listitems = $('tr', list);
listitems.sort(function (a, b) {
return ($(a).find('td:nth-child(2)').text().toUpperCase() > $(b).find('td:nth-child(2)').text().toUpperCase()) ? 1 : -1;
});
list.append(listitems);
list.sortable('refresh');
updateExerciseList();
}
var addExercise = function (id, title) {
var exercise = {id: _.escape(id), title: _.escape(title)}
var collectionExercises = collectContainedExercises();
if (collectionExercises.indexOf(exercise.id) === -1) {
// only add exercises that are not already contained in the collection
var template = '<tr data-id="' + exercise.id + '">' +
'<td><span class="fa-solid fa-bars"></span></td>' +
'<td>' + exercise.title + '</td>' +
`<td><a href="${Routes.exercise_path(exercise.id)}">${I18n.t('shared.show')}</td>` +
`<td><a class="remove-exercise" href="#">${I18n.t('shared.destroy')}</td></tr>`;
exerciseList.find('tbody').append(template);
$('#exercise-list').find('option[value="' + exercise.id + '"]').prop('selected', true);
}
}
addExercisesButton.on('click', function (e) {
e.preventDefault();
const selectedExercises = addExercisesForm.find('select')[0].selectedOptions;
for (var i = 0; i < selectedExercises.length; i++) {
addExercise(selectedExercises[i].value, selectedExercises[i].label);
}
bootstrap.Modal.getInstance($('#add-exercise-modal'))?.hide();
updateExerciseList();
addExercisesForm.find('select').val('').trigger("chosen:updated");
});
removeExerciseButtons.on('click', function (e) {
e.preventDefault();
var row = $(this).parent().parent();
var exerciseId = row.data('id');
$('#exercise-list').find('option[value="' + exerciseId + '"]').prop('selected', false);
row.remove();
updateExerciseList();
});
sortButton.on('click', function (e) {
e.preventDefault();
sortExercises();
});
}
}
});