|
Line 0
Link Here
|
| 0 |
- |
1 |
function init() { |
|
|
2 |
$('#ajax_status').hide(); |
| 3 |
$('#ajax_saving_msg').hide(); |
| 4 |
$('#ajax_saving_icon').hide(); |
| 5 |
$('#ajax_success_icon').hide(); |
| 6 |
$('#ajax_failed_icon').hide(); |
| 7 |
$('#ajax_failed_msg').hide(); |
| 8 |
} |
| 9 |
|
| 10 |
$(document).ready(function() { |
| 11 |
var apiEndpoint = '/api/v1/rotas/'; |
| 12 |
init(); |
| 13 |
$('#sortable_stages').sortable({ |
| 14 |
handle: '.drag_handle', |
| 15 |
placeholder: 'drag_placeholder', |
| 16 |
update: function(event, ui) { |
| 17 |
init(); |
| 18 |
$('#sortable_stages').sortable('disable'); |
| 19 |
var rotaId = document.getElementById('sortable_stages').dataset.rotaId; |
| 20 |
$('#ajax_saving_msg').text( |
| 21 |
document.getElementById('ajax_status').dataset.savingMsg |
| 22 |
); |
| 23 |
$('#ajax_saving_icon').show(); |
| 24 |
$('#ajax_saving_msg').show(); |
| 25 |
$('#ajax_status').fadeIn(); |
| 26 |
var stageId = ui.item[0].id.replace(/^stage_/, ''); |
| 27 |
var newIndex = ui.item.index(); |
| 28 |
var newPosition = newIndex + 1; |
| 29 |
$.ajax({ |
| 30 |
method: 'PUT', |
| 31 |
url: apiEndpoint + rotaId + '/stages/' + stageId + '/position', |
| 32 |
processData: false, |
| 33 |
contentType: 'application/json', |
| 34 |
data: newPosition |
| 35 |
}) |
| 36 |
.done(function(data) { |
| 37 |
$('#ajax_success_msg').text( |
| 38 |
document.getElementById('ajax_status').dataset.successMsg |
| 39 |
); |
| 40 |
$('#ajax_saving_icon').hide(); |
| 41 |
$('#ajax_success_icon').show(); |
| 42 |
$('#ajax_success_msg').show(); |
| 43 |
setTimeout( |
| 44 |
function() { |
| 45 |
$('#ajax_status').fadeOut(); |
| 46 |
}, |
| 47 |
700 |
| 48 |
); |
| 49 |
}) |
| 50 |
.fail(function(jqXHR, status, error) { |
| 51 |
$('#ajax_failed_msg').text( |
| 52 |
document.getElementById('ajax_status').dataset.failedMsg + |
| 53 |
error |
| 54 |
); |
| 55 |
$('#ajax_saving_icon').hide(); |
| 56 |
$('#ajax_failed_icon').show(); |
| 57 |
$('#ajax_failed_msg').show(); |
| 58 |
$('#sortable_stages').sortable('cancel'); |
| 59 |
}) |
| 60 |
.always(function() { |
| 61 |
$('#sortable_stages').sortable('enable'); |
| 62 |
}) |
| 63 |
} |
| 64 |
}); |
| 65 |
}); |