Reordering questions with drag'n'drop (#1433)

* Reordering questions with drag'n'drop

* Add permission check

* Test permissions for question reordering

* Handle malformed requests for question reordering

* Show first up arrow and last down arrow

* Provide page offset

* Revert "Provide page offset"

This reverts commit 8090bd573f.

* Reorder questions endpoint with pagination support

* Rudimentary test for reordering endpoint

* Make reordering questions atomic

* cache questions

* Properly support pagination for reorder_questions

* appease linter

* Fix test
This commit is contained in:
Sohalt
2019-10-14 10:54:20 +02:00
committed by Raphael Michel
parent f10d1bd236
commit 91b586ce08
9 changed files with 3823 additions and 5 deletions

View File

@@ -0,0 +1,45 @@
/*global $, Sortable*/
$(function () {
$("[data-dnd-url]").each(function(){
var container = $(this),
url = container.data("dnd-url"),
up = container.find("a:has(.fa-arrow-up)"),
handle = $('<span class="btn btn-default btn-sm dnd-sort-handle"><i class="fa fa-arrows"></i></span>');
function hideArrows(container){
var up = container.find("a:has(.fa-arrow-up)"),
firstUp = up.first(),
down = container.find("a:has(.fa-arrow-down)"),
lastDown = down.last();
up.not(firstUp).css("display","none");
down.not(lastDown).css("display","none");
firstUp.css("display","inline-block");
lastDown.css("display","inline-block");
}
up.after(handle);
hideArrows(container);
Sortable.create(container.get(0), {
handle: ".dnd-sort-handle",
onSort: function(evt){
var container = $(evt.to),
ids = container.find("[data-dnd-id]").toArray().map(e => e.dataset.dndId);
hideArrows(container);
$.ajax(
{
'type': 'POST',
'url': url,
'headers': {'X-CSRFToken': $("input[name=csrfmiddlewaretoken]").val()},
'data': JSON.stringify({
ids: ids
}),
'contentType': "application/json",
'timeout': 30000
}
);
}
});
});
});