-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathsubmission_statistics.js
More file actions
157 lines (135 loc) · 4.86 KB
/
Copy pathsubmission_statistics.js
File metadata and controls
157 lines (135 loc) · 4.86 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
$(document).on('turbo-migration:load', function(event) {
var currentSubmission = 0;
var active_file = undefined;
var fileTrees = [];
var editor = undefined;
var fileTypeById = {};
var showActiveFile = function() {
$('tr.active').removeClass('active');
$('tr#submission-' + currentSubmission).addClass('active');
var session = editor.getSession();
var fileType = fileTypeById[active_file.file_type_id];
session.setMode(fileType.editor_mode);
session.setTabSize(fileType.indent_size);
session.setValue(active_file.content);
session.setUseSoftTabs(true);
session.setUseWrapMode(true);
// The event ready.jstree is fired too early and thus doesn't work.
var selectFileInJsTree = function() {
if (!filetree.hasClass('jstree-loading')) {
filetree.jstree("deselect_all");
filetree.jstree("select_node", active_file.file_id);
} else {
setTimeout(selectFileInJsTree, 250);
}
};
filetree = $(fileTrees[currentSubmission]);
selectFileInJsTree();
// Finally change jstree element to prevent flickering
showFileTree(currentSubmission);
};
var initializeFileTree = function() {
$('.files').each(function(index, element) {
const jsTreeConfig = $(element).data('entries')
jsTreeConfig.core.themes = {...jsTreeConfig.core.themes, name: window.getCurrentTheme() === "dark" ? "default-dark" : "default"}
const fileTree = $(element).jstree(jsTreeConfig);
$(element).on('click', 'li.jstree-leaf', function() {
var id = parseInt($(this).attr('id'));
_.each(files[currentSubmission], function(file) {
if (file.file_id === id) {
active_file = file;
}
});
showActiveFile();
});
CodeOceanEditor.installFileTreeEventHandlers(fileTree);
fileTrees.push(fileTree);
});
};
var showFileTree = function(index) {
$('.files').hide();
$(fileTrees[index]).show();
};
if ($.isController('exercises') && $('#timeline').isPresent() && event.detail.url.includes("/statistics")) {
var slider = $('#submissions-slider>input');
var submissions = $('#data').data('submissions');
var files = $('#data').data('files');
var filetypes = $('#data').data('file-types');
var playButton = $('#play-button');
var playInterval = undefined;
editor = ace.edit('current-file');
editor.setShowPrintMargin(false);
editor.setTheme(CodeOceanEditor.THEME);
editor.$blockScrolling = Infinity;
editor.setReadOnly(true);
_.each(filetypes, function (filetype) {
filetype = JSON.parse(filetype);
fileTypeById[filetype.id] = filetype;
});
$('tr[data-id]>.clickable').each(function(index, element) {
element = $(element);
element.parent().attr('id', 'submission-' + index);
element.click(function() {
slider.val(index);
slider.change()
});
});
const handleAceThemeChangeEvent = function() {
editor.setTheme(CodeOceanEditor.THEME);
};
$(document).on('theme:change:ace', handleAceThemeChangeEvent.bind(this));
const onSliderChange = function(event) {
currentSubmission = slider.val();
var currentFiles = files[currentSubmission];
var fileIndex = 0;
_.each(currentFiles, function(file, index) {
if (file.name === active_file.name) {
fileIndex = index;
}
});
active_file = currentFiles[fileIndex];
showActiveFile();
};
slider.on('change', onSliderChange);
const stopReplay = function() {
clearInterval(playInterval);
playInterval = undefined;
playButton.find('span.fa-solid').removeClass('fa-pause').addClass('fa-play')
};
playButton.on('click', function(event) {
if (playInterval === undefined) {
// Reset slider if showing newest submission.
if (parseInt(slider.val()) === submissions.length - 1) {
slider.val(0);
onSliderChange();
}
playInterval = setInterval(function() {
if ($.isController('exercises') && $('#timeline').isPresent() && slider.val() < submissions.length - 1) {
slider.val(parseInt(slider.val()) + 1);
slider.change()
} else {
stopReplay();
}
}, 1000);
playButton.find('span.fa-solid').removeClass('fa-play').addClass('fa-pause')
} else {
stopReplay();
}
});
active_file = files[0][0];
initializeFileTree();
showActiveFile();
// Start with newest submission
slider.val(submissions.length - 1);
onSliderChange();
const unloadSubmissionStatistics = function() {
if (playInterval) {
stopReplay();
}
$(document).off('theme:change:ace');
editor.destroy();
}
$(document).on('turbo:visit', unloadSubmissionStatistics);
$(window).on('beforeunload', unloadSubmissionStatistics);
}
});