forked from ponlawat-w/moodle-block_hubcourseranking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanualranking.js
56 lines (50 loc) · 1.62 KB
/
manualranking.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
require(['jquery'], $ => {
$(document).ready(() => {
$manualrankings = $('[data-formtype=manualranking]');
$.each($manualrankings, (_, instance) => {
const $instance = $(instance);
const $input = $instance.find('input');
if (!$input.length) {
throw 'input not found';
}
let ORDER = $input.val().split(';').filter(x => x);
const updateOrder = () => {
$instance.find('.label').remove();
$instance.find('.bg-primary').removeClass('bg-primary text-white');
$.each(ORDER, (index, id) => {
const $row = $instance.find(`.manualranking-row[data-courseid=${id}]`);
if ($row.length) {
const $order = $row.find('.manualranking-order');
$order.html($('<span>')
.html(index + 1)
.addClass('label label-success'))
.css('font-size', '1em');
$row.addClass('bg-primary text-white');
}
});
$input.val(ORDER.join(';'));
};
const toggleOrder = id => {
const index = ORDER.findIndex(o => parseInt(o) === parseInt(id));
if (index > -1) {
ORDER.splice(index, 1);
} else {
ORDER.push(id);
}
updateOrder();
};
$instance.find('.manualranking-row').click(e => {
const $targetrow = $(e.target).closest('tr');
if (!$targetrow.length) {
return;
}
toggleOrder($targetrow.attr('data-courseid'));
});
$instance.find('.manualranking-deselect').click(() => {
ORDER = [];
updateOrder();
});
updateOrder();
});
});
});