Bug 22175: Make stock rotation table sortable
[koha-equinox.git] / koha-tmpl / intranet-tmpl / prog / js / pages / stockrotation.js
1 /* global KohaTable columns_settings */
2
3 function init() {
4     $('#ajax_status').hide();
5     $('#ajax_saving_msg').hide();
6     $('#ajax_saving_icon').hide();
7     $('#ajax_success_icon').hide();
8     $('#ajax_failed_icon').hide();
9     $('#ajax_failed_msg').hide();
10 }
11
12 $(document).ready(function() {
13     var apiEndpoint = '/api/v1/rotas/';
14     init();
15     $('#sortable_stages').sortable({
16         handle: '.drag_handle',
17         placeholder: 'drag_placeholder',
18         update: function(event, ui) {
19             init();
20             $('#sortable_stages').sortable('disable');
21             var rotaId = document.getElementById('sortable_stages').dataset.rotaId;
22             $('#ajax_saving_msg').text(
23                 document.getElementById('ajax_status').dataset.savingMsg
24             );
25             $('#ajax_saving_icon').show();
26             $('#ajax_saving_msg').show();
27             $('#ajax_status').fadeIn();
28             var stageId = ui.item[0].id.replace(/^stage_/, '');
29             var newIndex = ui.item.index();
30             var newPosition = newIndex + 1;
31             $.ajax({
32                 method: 'PUT',
33                 url: apiEndpoint + rotaId + '/stages/' + stageId + '/position',
34                 processData: false,
35                 contentType: 'application/json',
36                 data: newPosition
37             })
38             .done(function(data) {
39                 $('#ajax_success_msg').text(
40                     document.getElementById('ajax_status').dataset.successMsg
41                 );
42                 $('#ajax_saving_icon').hide();
43                 $('#ajax_success_icon').show();
44                 $('#ajax_success_msg').show();
45                 setTimeout(
46                     function() {
47                         $('#ajax_status').fadeOut();
48                     },
49                     700
50                 );
51             })
52             .fail(function(jqXHR, status, error) {
53                 $('#ajax_failed_msg').text(
54                     document.getElementById('ajax_status').dataset.failedMsg +
55                     error
56                 );
57                 $('#ajax_saving_icon').hide();
58                 $('#ajax_failed_icon').show();
59                 $('#ajax_failed_msg').show();
60                 $('#sortable_stages').sortable('cancel');
61             })
62             .always(function() {
63                 $('#sortable_stages').sortable('enable');
64             })
65         }
66     });
67
68     KohaTable("stock_rotation_manage_items", {
69         "aoColumnDefs": [
70             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
71             { "sType": "anti-the", "aTargets": [ "anti-the" ] }
72         ],
73         "sPaginationType": "four_button",
74         "autoWidth": false,
75         "exportColumns": [0,1,2,3,4,5]
76     }, stock_rotation_items_columns_settings);
77
78     KohaTable("stock_rotation", {
79         "aoColumnDefs": [
80             { "bSortable": false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
81             { "sType": "anti-the", "aTargets": [ "anti-the" ] }
82         ],
83         "sPaginationType": "four_button",
84         "autoWidth": false,
85         "exportColumns": [0,1,2,3,4]
86     }, stock_rotation_columns_settings);
87
88 });