1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
define(['jquery', 'core/sortable_list', 'core/ajax', 'core/notification'], function($, SortableList, Ajax, Notification) {
|
|
|
17 |
return {
|
|
|
18 |
init: function(tableid, moveaction) {
|
|
|
19 |
// Initialise sortable for the given list.
|
|
|
20 |
var sort = new SortableList('#' + tableid + ' tbody');
|
|
|
21 |
sort.getElementName = function(element) {
|
|
|
22 |
return $.Deferred().resolve(element.attr('data-name'));
|
|
|
23 |
};
|
|
|
24 |
var origIndex;
|
|
|
25 |
$('#' + tableid + ' tbody tr').on(SortableList.EVENTS.DRAGSTART, function(_, info) {
|
|
|
26 |
// Remember position of the element in the beginning of dragging.
|
|
|
27 |
origIndex = info.sourceList.children().index(info.element);
|
|
|
28 |
// Resize the "proxy" element to be the same width as the main element.
|
|
|
29 |
setTimeout(function() {
|
|
|
30 |
$('.sortable-list-is-dragged').width(info.element.width());
|
|
|
31 |
}, 501);
|
|
|
32 |
}).on(SortableList.EVENTS.DROP, function(_, info) {
|
|
|
33 |
// When a list element was moved send AJAX request to the server.
|
|
|
34 |
var newIndex = info.targetList.children().index(info.element);
|
|
|
35 |
var t = info.element.find('[data-action=' + moveaction + ']');
|
|
|
36 |
if (info.positionChanged && t.length) {
|
|
|
37 |
var request = {
|
|
|
38 |
methodname: 'tool_xmldb_invoke_move_action',
|
|
|
39 |
args: {
|
|
|
40 |
action: moveaction,
|
|
|
41 |
dir: t.attr('data-dir'),
|
|
|
42 |
table: t.attr('data-table'),
|
|
|
43 |
field: t.attr('data-field'),
|
|
|
44 |
key: t.attr('data-key'),
|
|
|
45 |
index: t.attr('data-index'),
|
|
|
46 |
position: newIndex - origIndex
|
|
|
47 |
}
|
|
|
48 |
};
|
|
|
49 |
Ajax.call([request])[0].fail(Notification.exception);
|
|
|
50 |
}
|
|
|
51 |
});
|
|
|
52 |
}
|
|
|
53 |
};
|
|
|
54 |
});
|