Newer
Older
Sonia Zorba
committed
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2017 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
(function ($) {
function fillListContainer(container, values) {
var $container = $(container);
$container.empty();
for (var i = 0; i < values.length; i++) {
$container.append('<li class="bg-info"><input type="checkbox" />' + values[i] + '</li>');
}
}
function setupToggler(toggler, container) {
var $toggler = $(toggler);
$toggler.on('click', function (ev) {
var $container = $(container);
var check = $container.find('input[type="checkbox"]:checked').length
< $container.find('input[type="checkbox"]:not(:checked)').length;
$container.find('input[type="checkbox"]').prop('checked', check);
ev.preventDefault();
});
}
function setupShifter(shifter, source, target) {
var $shifter = $(shifter);
var $target = $(target);
$shifter.on('click', function (ev) {
$(source).find('input[type="checkbox"]:checked').each(function (index, element) {
$(element).prop('checked', false);
$(element).parent().appendTo($target);
});
ev.preventDefault();
});
}
TSM.openColumnsSorter = TSM.eventHandlerFactory(function (src, jsUpdate) {
// Reset event handlers
$('#columns-sorter a').off();
var model = JSON.parse(jsUpdate);
fillListContainer('#unsorted-columns', model.unSortedColumns);
fillListContainer('#sorted-columns', model.sortedColumns);
// Setting drag and sortable
$("#unsorted-columns, #sorted-columns").sortable({
connectWith: ".sortable"
}).disableSelection();
// Init toggle selection
setupToggler('#toggle-selection-unsorted', '#unsorted-columns');
setupToggler('#toggle-selection-sorted', '#sorted-columns');
// Init shift buttons
setupShifter('#shift-selection-unsorted', '#unsorted-columns', '#sorted-columns');
setupShifter('#shift-selection-sorted', '#sorted-columns', '#unsorted-columns');
// Showing modal
$('#columns-sorter').modal('show');
}, 'main:columns_sorter');
TSM.saveColumnsOrder = function () {
var sortedColumns = [];
$('#sorted-columns li').each(function (index, element) {
var columnName = $(element).text().trim();
sortedColumns.push(columnName);
});
$.ajax({
type: 'POST',
url: TSM.getRestPath('sort-columns'),
data: JSON.stringify(sortedColumns),
success: function (response) {
jsf.ajax.request('main', null, {
'javax.faces.behavior.event': 'action',
execute: '@none',
render: 'main:tables_wrapper',
onevent: function (event) {
if (event.status === 'success') {
$('#columns-sorter').modal('hide');
}
}
});
},
contentType: 'application/json',
dataType: 'json'
});
};