2017-06-23 14:50:12 +08:00
|
|
|
//= require jquery-ui/widgets/sortable
|
2016-11-30 18:42:05 +08:00
|
|
|
|
2016-12-24 02:56:54 +08:00
|
|
|
// Extend datatables API with searchable options
|
|
|
|
// (http://stackoverflow.com/questions/39912395/datatables-dynamically-set-columns-searchable)
|
|
|
|
$.fn.dataTable.Api.register('isColumnSearchable()', function(colSelector) {
|
|
|
|
var idx = this.column(colSelector).index();
|
|
|
|
return this.settings()[0].aoColumns[idx].bSearchable;
|
|
|
|
});
|
|
|
|
$.fn.dataTable.Api.register('setColumnSearchable()', function(colSelector, value) {
|
|
|
|
if (value !== this.isColumnSearchable(colSelector)) {
|
|
|
|
var idx = this.column(colSelector).index();
|
|
|
|
this.settings()[0].aoColumns[idx].bSearchable = value;
|
|
|
|
if (value === true) {
|
|
|
|
this.rows().invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
var rowsSelected = [];
|
|
|
|
|
|
|
|
// Tells whether we're currently viewing or editing table
|
2016-12-02 23:18:11 +08:00
|
|
|
var currentMode = 'viewMode';
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// Tells what action will execute by pressing on save button (update/create)
|
2016-12-02 23:18:11 +08:00
|
|
|
var saveAction = 'update';
|
2016-02-12 23:52:43 +08:00
|
|
|
var selectedSample;
|
|
|
|
|
2016-12-05 18:34:36 +08:00
|
|
|
// Helps saving correct table state
|
|
|
|
var myData;
|
|
|
|
var loadFirstTime = true;
|
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
var table;
|
|
|
|
var originalHeader;
|
|
|
|
|
2017-06-06 18:46:56 +08:00
|
|
|
var view_assigned;
|
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
function dataTableInit() {
|
|
|
|
// Make a copy of original samples table header
|
|
|
|
originalHeader = $('#samples thead').children().clone();
|
2017-06-06 18:46:56 +08:00
|
|
|
view_assigned = 'assigned';
|
2016-12-02 23:18:11 +08:00
|
|
|
table = $('#samples').DataTable({
|
|
|
|
order: [[2, 'desc']],
|
2016-11-28 23:28:22 +08:00
|
|
|
dom: "R<'row'<'col-sm-9-custom toolbar'l><'col-sm-3-custom'f>>tpi",
|
2016-02-12 23:52:43 +08:00
|
|
|
stateSave: true,
|
|
|
|
processing: true,
|
|
|
|
serverSide: true,
|
2016-12-20 20:41:19 +08:00
|
|
|
sScrollX: '100%',
|
|
|
|
sScrollXInner: '100%',
|
2016-12-19 23:59:09 +08:00
|
|
|
scrollY: '64vh',
|
|
|
|
scrollCollapse: true,
|
2016-02-12 23:52:43 +08:00
|
|
|
colReorder: {
|
2016-12-10 00:37:22 +08:00
|
|
|
fixedColumnsLeft: 2,
|
|
|
|
realtime: false
|
2016-12-02 23:18:11 +08:00
|
|
|
},
|
|
|
|
destroy: true,
|
|
|
|
ajax: {
|
|
|
|
url: $('#samples').data('source'),
|
2017-06-06 18:46:56 +08:00
|
|
|
data: function ( d ) {
|
|
|
|
d.assigned = view_assigned;
|
|
|
|
},
|
2016-12-02 23:18:11 +08:00
|
|
|
global: false,
|
|
|
|
type: 'POST'
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
|
|
|
columnDefs: [{
|
2016-12-02 23:18:11 +08:00
|
|
|
targets: 0,
|
|
|
|
searchable: false,
|
|
|
|
orderable: false,
|
|
|
|
className: 'dt-body-center',
|
|
|
|
sWidth: '1%',
|
|
|
|
render: function() {
|
|
|
|
return "<input type='checkbox'>";
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
}, {
|
2016-12-02 23:18:11 +08:00
|
|
|
targets: 1,
|
|
|
|
searchable: false,
|
|
|
|
orderable: true,
|
|
|
|
sWidth: '1%'
|
2016-09-23 23:45:02 +08:00
|
|
|
}, {
|
2016-12-02 23:18:11 +08:00
|
|
|
targets: 2,
|
|
|
|
render: function(data, type, row) {
|
2017-01-16 22:52:02 +08:00
|
|
|
return "<a href='" + row.sampleInfoUrl + "'" +
|
2017-01-20 18:05:05 +08:00
|
|
|
"class='sample-info-link'>" + data + '</a>';
|
2016-12-02 23:18:11 +08:00
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
}],
|
2016-12-02 23:18:11 +08:00
|
|
|
rowCallback: function(row, data) {
|
|
|
|
// Get row ID
|
|
|
|
var rowId = data.DT_RowId;
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
// If row ID is in the list of selected row IDs
|
|
|
|
if ($.inArray(rowId, rowsSelected) !== -1) {
|
|
|
|
$(row).find('input[type="checkbox"]').prop('checked', true);
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
$(row).addClass('selected');
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
|
|
|
columns: (function() {
|
2016-12-02 23:18:11 +08:00
|
|
|
var numOfColumns = $('#samples').data('num-columns');
|
|
|
|
var columns = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < numOfColumns; i++) {
|
|
|
|
var visible = (i <= 6);
|
|
|
|
columns.push({
|
|
|
|
data: String(i),
|
|
|
|
defaultContent: '',
|
2016-12-24 02:56:54 +08:00
|
|
|
visible: visible,
|
|
|
|
searchable: visible
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return columns;
|
2016-02-12 23:52:43 +08:00
|
|
|
})(),
|
2016-12-02 23:18:11 +08:00
|
|
|
fnDrawCallback: function() {
|
|
|
|
animateSpinner(this, false);
|
|
|
|
changeToViewMode();
|
|
|
|
updateButtons();
|
2017-05-04 22:30:29 +08:00
|
|
|
// Show number of selected samples info
|
|
|
|
$('#samples_info').append('<span id="selected_info"></span>');
|
|
|
|
$('#selected_info').html(' ('+rowsSelected.length+' entries selected)');
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
2016-12-08 22:44:50 +08:00
|
|
|
preDrawCallback: function() {
|
2018-01-19 00:41:35 +08:00
|
|
|
rowsSelected = [];
|
2016-12-08 22:44:50 +08:00
|
|
|
animateSpinner(this);
|
2017-01-16 22:52:02 +08:00
|
|
|
$('.sample-info-link').off('click');
|
2016-12-08 22:44:50 +08:00
|
|
|
},
|
2016-12-09 22:21:26 +08:00
|
|
|
stateLoadCallback: function(settings) {
|
2016-12-08 22:44:50 +08:00
|
|
|
// Send an Ajax request to the server to get the data. Note that
|
|
|
|
// this is a synchronous request since the data is expected back from the
|
|
|
|
// function
|
2017-01-25 19:12:27 +08:00
|
|
|
var team = $('#samples').attr('data-team-id');
|
2016-12-09 22:21:26 +08:00
|
|
|
var user = $('#samples').attr('data-user-id');
|
2016-12-08 22:44:50 +08:00
|
|
|
|
2016-12-09 22:21:26 +08:00
|
|
|
$.ajax({
|
2017-01-25 19:12:27 +08:00
|
|
|
url: '/state_load/' + team + '/' + user,
|
|
|
|
data: {team: team},
|
2016-12-08 22:44:50 +08:00
|
|
|
async: false,
|
2016-12-09 22:21:26 +08:00
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
success: function(json) {
|
2016-12-08 22:44:50 +08:00
|
|
|
myData = json.state;
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2016-12-09 22:21:26 +08:00
|
|
|
});
|
|
|
|
return myData;
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
2016-12-09 22:21:26 +08:00
|
|
|
stateSaveCallback: function(settings, data) {
|
2016-12-08 22:44:50 +08:00
|
|
|
// Send an Ajax request to the server with the state object
|
2017-01-25 19:12:27 +08:00
|
|
|
var team = $('#samples').attr('data-team-id');
|
2016-12-09 22:21:26 +08:00
|
|
|
var user = $('#samples').attr('data-user-id');
|
2016-12-08 22:44:50 +08:00
|
|
|
// Save correct data
|
|
|
|
if (loadFirstTime == true) {
|
|
|
|
data = myData;
|
2016-12-02 23:18:11 +08:00
|
|
|
}
|
2016-12-05 18:34:36 +08:00
|
|
|
|
2016-12-09 22:21:26 +08:00
|
|
|
$.ajax({
|
2017-01-25 19:12:27 +08:00
|
|
|
url: '/state_save/' + team + '/' + user,
|
|
|
|
data: {team: team, state: data},
|
2016-12-09 22:21:26 +08:00
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST'
|
|
|
|
});
|
2016-12-08 22:44:50 +08:00
|
|
|
loadFirstTime = false;
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
2016-12-05 18:34:36 +08:00
|
|
|
fnInitComplete: function(oSettings, json) {
|
2016-12-08 22:44:50 +08:00
|
|
|
// Reload correct column order and visibility (if you refresh page)
|
|
|
|
for (var i = 0; i < table.columns()[0].length; i++) {
|
2017-05-11 17:16:12 +08:00
|
|
|
var visibility = false;
|
|
|
|
if (myData.columns[i]) {
|
|
|
|
visibility = myData.columns[i].visible;
|
|
|
|
}
|
2016-12-09 22:21:26 +08:00
|
|
|
if (typeof (visibility) === 'string') {
|
|
|
|
visibility = (visibility === 'true');
|
2016-12-06 16:40:28 +08:00
|
|
|
}
|
2016-12-08 22:44:50 +08:00
|
|
|
table.column(i).visible(visibility);
|
2016-12-24 02:56:54 +08:00
|
|
|
table.setColumnSearchable(i, visibility);
|
2016-12-08 22:44:50 +08:00
|
|
|
}
|
2016-12-10 00:37:22 +08:00
|
|
|
oSettings._colReorder.fnOrder(myData.ColReorder);
|
2016-12-14 00:25:01 +08:00
|
|
|
table.on('mousedown', function() {
|
|
|
|
$('#samples-columns-dropdown').removeClass('open');
|
|
|
|
});
|
2017-01-16 21:24:31 +08:00
|
|
|
initHeaderTooltip();
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
// Append button to inner toolbar in table
|
|
|
|
$('div.toolbarButtons').appendTo('div.toolbar');
|
|
|
|
$('div.toolbarButtons').show();
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
$('.delete_samples_submit').click(function() {
|
|
|
|
animateLoading();
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
$('#assignSamples, #unassignSamples').click(function() {
|
|
|
|
animateLoading();
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
// Handle click on table cells with checkboxes
|
2017-01-16 22:52:02 +08:00
|
|
|
$('#samples').on('click', 'tbody td, thead th:first-child', function(e) {
|
|
|
|
if (!$(e.target).is('.sample-info-link')) {
|
|
|
|
// Skip if clicking on samples info link
|
|
|
|
$(this).parent().find('input[type="checkbox"]').trigger('click');
|
|
|
|
}
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Handle clicks on checkbox
|
|
|
|
$('#samples tbody').on('click', "input[type='checkbox']", function(e) {
|
|
|
|
if (currentMode !== 'viewMode') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Get row ID
|
|
|
|
var $row = $(this).closest('tr');
|
|
|
|
var data = table.row($row).data();
|
|
|
|
var rowId = data.DT_RowId;
|
|
|
|
|
|
|
|
// Determine whether row ID is in the list of selected row IDs
|
|
|
|
var index = $.inArray(rowId, rowsSelected);
|
|
|
|
|
|
|
|
// If checkbox is checked and row ID is not in list of selected row IDs
|
|
|
|
if (this.checked && index === -1) {
|
|
|
|
rowsSelected.push(rowId);
|
|
|
|
// Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
|
|
|
|
} else if (!this.checked && index !== -1) {
|
|
|
|
rowsSelected.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.checked) {
|
|
|
|
$row.addClass('selected');
|
|
|
|
} else {
|
|
|
|
$row.removeClass('selected');
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDataTableSelectAllCtrl(table);
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
updateButtons();
|
2017-05-04 22:30:29 +08:00
|
|
|
|
|
|
|
// Update number of selected samples info
|
|
|
|
$('#selected_info').html(' ('+ rowsSelected.length +' entries selected)');
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Handle click on "Select all" control
|
2017-01-05 19:08:03 +08:00
|
|
|
$('.dataTables_scrollHead input[name="select_all"]').on('click', function(e) {
|
2016-12-02 23:18:11 +08:00
|
|
|
if (this.checked) {
|
|
|
|
$('#samples tbody input[type="checkbox"]:not(:checked)').trigger('click');
|
|
|
|
} else {
|
|
|
|
$('#samples tbody input[type="checkbox"]:checked').trigger('click');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent click event from propagating to parent
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle table draw event
|
|
|
|
table.on('draw', function() {
|
|
|
|
updateDataTableSelectAllCtrl(table);
|
2017-01-07 00:31:17 +08:00
|
|
|
sampleInfoListener();
|
2017-01-24 21:33:23 +08:00
|
|
|
|
2017-01-24 17:56:10 +08:00
|
|
|
// Prevent sample row toggling when selecting user smart annotation link
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
2017-01-07 00:31:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
table.on('column-reorder', function() {
|
|
|
|
sampleInfoListener();
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
table = dataTableInit();
|
|
|
|
|
2016-12-20 20:41:19 +08:00
|
|
|
// Timeout for table header scrolling
|
|
|
|
setTimeout(function () {
|
|
|
|
table.columns.adjust();
|
|
|
|
}, 10);
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
// Updates "Select all" control in a data table
|
2016-12-02 23:18:11 +08:00
|
|
|
function updateDataTableSelectAllCtrl(table) {
|
2017-01-05 19:08:03 +08:00
|
|
|
var $table = table.table().node();
|
|
|
|
var $header = table.table().header();
|
|
|
|
var $chkbox_all = $('tbody input[type="checkbox"]', $table);
|
|
|
|
var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
|
|
|
|
var chkbox_select_all = $('input[name="select_all"]', $header).get(0);
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// If none of the checkboxes are checked
|
|
|
|
if($chkbox_checked.length === 0){
|
|
|
|
chkbox_select_all.checked = false;
|
|
|
|
if('indeterminate' in chkbox_select_all){
|
|
|
|
chkbox_select_all.indeterminate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all of the checkboxes are checked
|
|
|
|
} else if ($chkbox_checked.length === $chkbox_all.length){
|
|
|
|
chkbox_select_all.checked = true;
|
|
|
|
if('indeterminate' in chkbox_select_all){
|
|
|
|
chkbox_select_all.indeterminate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If some of the checkboxes are checked
|
|
|
|
} else {
|
|
|
|
chkbox_select_all.checked = true;
|
|
|
|
if('indeterminate' in chkbox_select_all){
|
|
|
|
chkbox_select_all.indeterminate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append selected samples to form
|
|
|
|
$("form#form-samples").submit(function(e){
|
|
|
|
var form = this;
|
|
|
|
|
|
|
|
if (currentMode == "viewMode")
|
|
|
|
appendSamplesIdToForm(form);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Append selected samples and headers form
|
|
|
|
$("form#form-export").submit(function(e){
|
|
|
|
var form = this;
|
|
|
|
|
|
|
|
if (currentMode == "viewMode") {
|
|
|
|
// Remove all hidden fields
|
|
|
|
$("#form-export").find("input[name=sample_ids\\[\\]]").remove();
|
|
|
|
$("#form-export").find("input[name=header_ids\\[\\]]").remove();
|
|
|
|
|
|
|
|
// Append samples
|
|
|
|
appendSamplesIdToForm(form);
|
|
|
|
|
|
|
|
// Append visible column information
|
|
|
|
$("table#samples thead tr").children("th").each(function(i) {
|
|
|
|
var th = $(this);
|
|
|
|
var val;
|
|
|
|
|
|
|
|
if ($(th).attr("id") == "sample-name")
|
|
|
|
val = -1;
|
|
|
|
else if ($(th).attr("id") == "sample-type")
|
|
|
|
val = -2;
|
|
|
|
else if ($(th).attr("id") == "sample-group")
|
|
|
|
val = -3;
|
|
|
|
else if ($(th).attr("id") == "added-by")
|
|
|
|
val = -4;
|
|
|
|
else if ($(th).attr("id") == "added-on")
|
|
|
|
val = -5;
|
|
|
|
else if ($(th).hasClass("custom-field"))
|
|
|
|
val = th.attr("id");
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
$(form).append(
|
|
|
|
$('<input>')
|
|
|
|
.attr('type', 'hidden')
|
|
|
|
.attr('name', 'header_ids[]')
|
|
|
|
.val(val)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function appendSamplesIdToForm(form) {
|
|
|
|
$.each(rowsSelected, function(index, rowId){
|
|
|
|
$(form).append(
|
|
|
|
$('<input>')
|
|
|
|
.attr('type', 'hidden')
|
|
|
|
.attr('name', 'sample_ids[]')
|
|
|
|
.val(rowId)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-16 21:24:31 +08:00
|
|
|
function initHeaderTooltip() {
|
|
|
|
// Fix compatibility of fixed table header and column names modal-tooltip
|
2017-01-25 20:44:26 +08:00
|
|
|
$('.modal-tooltip').off();
|
2017-01-16 21:24:31 +08:00
|
|
|
$('.modal-tooltip').hover(function() {
|
|
|
|
var $tooltip = $(this).find('.modal-tooltiptext');
|
|
|
|
var offsetLeft = $tooltip.offset().left;
|
2017-01-24 21:30:32 +08:00
|
|
|
(offsetLeft + 200) > $(window).width() ? offsetLeft -= 150 : offsetLeft;
|
2017-01-16 21:24:31 +08:00
|
|
|
var offsetTop = $tooltip.offset().top;
|
|
|
|
$('body').append($tooltip);
|
|
|
|
$tooltip.css('background-color', '#d2d2d2');
|
|
|
|
$tooltip.css('border-radius', '6px');
|
|
|
|
$tooltip.css('color', '#333');
|
|
|
|
$tooltip.css('display', 'block');
|
|
|
|
$tooltip.css('left', offsetLeft + 'px');
|
|
|
|
$tooltip.css('padding', '5px');
|
|
|
|
$tooltip.css('position', 'absolute');
|
|
|
|
$tooltip.css('text-align', 'center');
|
|
|
|
$tooltip.css('top', offsetTop + 'px');
|
|
|
|
$tooltip.css('visibility', 'visible');
|
|
|
|
$tooltip.css('width', '200px');
|
|
|
|
$tooltip.css('word-wrap', 'break-word');
|
|
|
|
$(this).data('dropdown-tooltip', $tooltip);
|
|
|
|
}, function() {
|
|
|
|
$(this).append($(this).data('dropdown-tooltip'));
|
|
|
|
$(this).data('dropdown-tooltip').removeAttr('style');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:45:02 +08:00
|
|
|
//Show sample info
|
|
|
|
function sampleInfoListener() {
|
2016-09-28 21:46:41 +08:00
|
|
|
$(".sample_info").on("click", function(e){
|
2017-01-09 16:38:42 +08:00
|
|
|
$("#modal-info-sample").remove();
|
2016-09-23 23:45:02 +08:00
|
|
|
var that = $(this);
|
|
|
|
$.ajax({
|
|
|
|
method: "GET",
|
|
|
|
url: that.attr("data-href") + '.json',
|
|
|
|
dataType: "json"
|
|
|
|
}).done(function(xhr, settings, data) {
|
2016-09-28 21:46:41 +08:00
|
|
|
$("body").append($.parseHTML(data.responseJSON.html));
|
2016-09-23 23:45:02 +08:00
|
|
|
$("#modal-info-sample").modal('show',{
|
2016-09-28 21:46:41 +08:00
|
|
|
backdrop: true,
|
|
|
|
keyboard: false,
|
2016-09-23 23:45:02 +08:00
|
|
|
}).on('hidden.bs.modal', function () {
|
2016-09-28 21:46:41 +08:00
|
|
|
$(this).find(".modal-body #sample-info-table").DataTable().destroy();
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#sample-info-table').DataTable({
|
|
|
|
dom: "RBltpi",
|
|
|
|
stateSave: false,
|
|
|
|
buttons: [],
|
|
|
|
processing: true,
|
|
|
|
colReorder: {
|
|
|
|
fixedColumnsLeft: 1000000 // Disable reordering
|
|
|
|
},
|
|
|
|
columnDefs: [{
|
|
|
|
targets: 0,
|
|
|
|
searchable: false,
|
|
|
|
orderable: false
|
|
|
|
}],
|
|
|
|
fnDrawCallback: function(settings, json) {
|
|
|
|
animateSpinner(this, false);
|
|
|
|
},
|
|
|
|
preDrawCallback: function(settings) {
|
|
|
|
animateSpinner(this);
|
|
|
|
}
|
2016-09-23 23:45:02 +08:00
|
|
|
});
|
|
|
|
}).fail(function(error){
|
|
|
|
// TODO
|
|
|
|
}).always(function(data){
|
|
|
|
// TODO
|
2016-10-03 16:40:32 +08:00
|
|
|
});
|
2016-09-28 21:46:41 +08:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
2016-10-03 16:40:32 +08:00
|
|
|
});
|
2016-10-03 16:15:46 +08:00
|
|
|
}
|
2016-09-23 23:45:02 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
// Edit sample
|
|
|
|
function onClickEdit() {
|
|
|
|
if (rowsSelected.length != 1) return;
|
|
|
|
|
|
|
|
var row = table.row("#" + rowsSelected[0]);
|
|
|
|
var node = row.node();
|
|
|
|
var rowData = row.data();
|
|
|
|
|
|
|
|
$(node).find("td input").trigger("click");
|
|
|
|
selectedSample = node;
|
|
|
|
|
|
|
|
clearAllErrors();
|
|
|
|
changeToEditMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
saveAction = "update";
|
|
|
|
|
|
|
|
$.ajax({
|
2017-01-20 18:05:05 +08:00
|
|
|
url: rowData["sampleEditUrl"],
|
2016-02-12 23:52:43 +08:00
|
|
|
type: "GET",
|
|
|
|
dataType: "json",
|
|
|
|
success: function (data) {
|
|
|
|
// Show save and cancel buttons in first two columns
|
|
|
|
$(node).children("td").eq(0).html($("#saveSample").clone());
|
|
|
|
$(node).children("td").eq(1).html($("#cancelSave").clone());
|
|
|
|
|
|
|
|
// Sample name column
|
|
|
|
var colIndex = getColumnIndex("#sample-name");
|
|
|
|
if (colIndex) {
|
|
|
|
$(node).children("td").eq(colIndex).html(changeToInputField("sample", "name", data["sample"]["name"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample type column
|
|
|
|
var colIndex = getColumnIndex("#sample-type");
|
|
|
|
if (colIndex) {
|
|
|
|
var selectType = createSampleTypeSelect(data["sample_types"], data["sample"]["sample_type"]);
|
|
|
|
$(node).children("td").eq(colIndex).html(selectType);
|
|
|
|
$("select[name=sample_type_id]").selectpicker();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample group column
|
|
|
|
var colIndex = getColumnIndex("#sample-group");
|
|
|
|
if (colIndex) {
|
|
|
|
var selectGroup = createSampleGroupSelect(data["sample_groups"], data["sample"]["sample_group"]);
|
|
|
|
$(node).children("td").eq(colIndex).html(selectGroup);
|
|
|
|
$("select[name=sample_group_id]").selectpicker();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of custom fields
|
|
|
|
var cfields = data["sample"]["custom_fields"];
|
|
|
|
$(node).children("td").each(function(i) {
|
|
|
|
var td = $(this);
|
|
|
|
var rawIndex = table.column.index("fromVisible", i);
|
|
|
|
var colHeader = table.column(rawIndex).header();
|
|
|
|
if ($(colHeader).hasClass("custom-field")) {
|
|
|
|
// Check if custom field on this sample exists
|
|
|
|
var cf = cfields[$(colHeader).attr("id")];
|
|
|
|
if (cf)
|
|
|
|
td.html(changeToInputField("sample_custom_fields", cf["sample_custom_field_id"], cf["value"]));
|
|
|
|
else
|
|
|
|
td.html(changeToInputField("custom_fields", $(colHeader).attr("id"), ""));
|
|
|
|
}
|
|
|
|
});
|
2016-12-20 20:41:19 +08:00
|
|
|
|
2017-01-10 21:13:37 +08:00
|
|
|
// initialize smart annotation
|
2017-02-03 23:42:38 +08:00
|
|
|
SmartAnnotation.init($('[data-object="sample_custom_fields"]'));
|
2017-01-10 21:13:37 +08:00
|
|
|
_.each($('[data-object="custom_fields"]'), function(el) {
|
2017-01-12 16:52:29 +08:00
|
|
|
if(_.isUndefined($(el).data('atwho'))) {
|
|
|
|
SmartAnnotation.init(el);
|
|
|
|
}
|
2017-01-10 21:13:37 +08:00
|
|
|
});
|
2016-12-20 20:41:19 +08:00
|
|
|
// Adjust columns width in table header
|
2017-01-20 16:53:44 +08:00
|
|
|
adjustTableHeader();
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
|
|
|
error: function (e, data, status, xhr) {
|
|
|
|
if (e.status == 403) {
|
2017-03-18 00:56:58 +08:00
|
|
|
HelperModule.flashAlertMsg(
|
2017-03-28 21:14:05 +08:00
|
|
|
I18n.t('samples.js.permission_error'), e.responseJSON.style
|
2017-03-18 00:56:58 +08:00
|
|
|
);
|
2016-02-12 23:52:43 +08:00
|
|
|
changeToViewMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save sample
|
|
|
|
function onClickSave() {
|
|
|
|
if (saveAction == "update") {
|
|
|
|
var row = table.row(selectedSample);
|
|
|
|
var node = row.node();
|
|
|
|
var rowData = row.data();
|
|
|
|
} else if (saveAction == "create")
|
|
|
|
var node = selectedSample;
|
|
|
|
|
|
|
|
// First fetch all the data in input fields
|
|
|
|
data = {
|
2017-04-06 19:03:13 +08:00
|
|
|
request_url: $('#samples').data('current-uri'),
|
2016-02-12 23:52:43 +08:00
|
|
|
sample_id: $(selectedSample).attr("id"),
|
|
|
|
sample: {},
|
|
|
|
custom_fields: {}, // These fields are not currently bound to this sample
|
|
|
|
sample_custom_fields: {} // These fields are already in database (linked to this sample)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Direct sample attributes
|
|
|
|
// Sample name
|
|
|
|
$(node).find("td input[data-object = sample]").each(function() {
|
|
|
|
data["sample"][$(this).attr("name")] = $(this).val();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sample type
|
|
|
|
$(node).find("td select[name = sample_type_id]").each(function() {
|
|
|
|
data["sample"]["sample_type_id"] = $(this).val();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sample group
|
|
|
|
$(node).find("td select[name = sample_group_id]").each(function() {
|
|
|
|
data["sample"]["sample_group_id"] = $(this).val();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Custom fields (new fields)
|
|
|
|
$(node).find("td input[data-object = custom_fields]").each(function () {
|
|
|
|
// Send data only and only if string is not empty
|
|
|
|
if ($(this).val().trim()) {
|
|
|
|
data["custom_fields"][$(this).attr("name")] = $(this).val();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sample custom fields (existent fields)
|
|
|
|
$(node).find("td input[data-object = sample_custom_fields]").each(function () {
|
|
|
|
data["sample_custom_fields"][$(this).attr("name")] = $(this).val();
|
|
|
|
});
|
|
|
|
|
|
|
|
var url = (saveAction == "update" ? rowData["sampleUpdateUrl"] : $("table#samples").data("create-sample"))
|
|
|
|
var type = (saveAction == "update" ? "PUT" : "POST")
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: type,
|
|
|
|
dataType: "json",
|
|
|
|
data: data,
|
|
|
|
success: function (data) {
|
2017-03-18 00:56:58 +08:00
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
2016-02-12 23:52:43 +08:00
|
|
|
onClickCancel();
|
|
|
|
},
|
|
|
|
error: function (e, eData, status, xhr) {
|
|
|
|
var data = e.responseJSON;
|
|
|
|
clearAllErrors();
|
|
|
|
|
|
|
|
if (e.status == 404) {
|
2017-03-18 00:56:58 +08:00
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
I18n.t('samples.js.not_found_error'), 'danger'
|
|
|
|
);
|
2016-02-12 23:52:43 +08:00
|
|
|
changeToViewMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
else if (e.status == 403) {
|
2017-03-18 00:56:58 +08:00
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
I18n.t('samples.js.permission_error'), 'danger'
|
|
|
|
);
|
2016-02-12 23:52:43 +08:00
|
|
|
changeToViewMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
else if (e.status == 400) {
|
|
|
|
if (data["init_fields"]) {
|
|
|
|
var init_fields = data["init_fields"];
|
|
|
|
|
|
|
|
// Validate sample name
|
|
|
|
if (init_fields["name"]) {
|
|
|
|
var input = $(selectedSample).find("input[name=name]");
|
|
|
|
|
|
|
|
if (input) {
|
|
|
|
input.closest(".form-group").addClass("has-error");
|
|
|
|
input.parent().append("<span class='help-block'>" + init_fields["name"] + "<br /></span>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Validate custom fields
|
|
|
|
$.each(data["custom_fields"] || [], function(key, val) {
|
|
|
|
$.each(val, function(key, val) {
|
|
|
|
var input = $(selectedSample).find("input[name=" + key + "]");
|
|
|
|
|
|
|
|
if (input) {
|
|
|
|
input.closest(".form-group").addClass("has-error");
|
|
|
|
input.parent().append("<span class='help-block'>" + val["value"][0] + "<br /></span>");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Validate sample custom fields
|
|
|
|
$.each(data["sample_custom_fields"] || [], function(key, val) {
|
|
|
|
$.each(val, function(key, val) {
|
|
|
|
var input = $(selectedSample).find("input[name=" + key + "]");
|
|
|
|
|
|
|
|
if (input) {
|
|
|
|
input.closest(".form-group").addClass("has-error");
|
|
|
|
input.parent().append("<span class='help-block'>" + val["value"][0] + "<br /></span>");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable/disable edit button
|
|
|
|
function updateButtons() {
|
2016-07-21 19:11:15 +08:00
|
|
|
if (currentMode=="viewMode") {
|
|
|
|
$("#importSamplesButton").removeClass("disabled");
|
|
|
|
$("#importSamplesButton").prop("disabled",false);
|
|
|
|
$("#addSample").removeClass("disabled");
|
|
|
|
$("#addSample").prop("disabled",false);
|
|
|
|
$("#addNewColumn").removeClass("disabled");
|
|
|
|
$("#addNewColumn").prop("disabled",false);
|
2016-12-16 16:50:45 +08:00
|
|
|
$('#samples-columns-dropdown')
|
|
|
|
.find('.dropdown-toggle')
|
|
|
|
.prop("disabled",false);
|
|
|
|
$("th").removeClass('disable-click');
|
2016-07-21 19:11:15 +08:00
|
|
|
if (rowsSelected.length == 1) {
|
|
|
|
$("#editSample").prop("disabled", false);
|
|
|
|
$("#editSample").removeClass("disabled");
|
|
|
|
$("#deleteSamplesButton").prop("disabled", false);
|
|
|
|
$("#deleteSamplesButton").removeClass("disabled");
|
|
|
|
$("#exportSamplesButton").removeClass("disabled");
|
|
|
|
$("#exportSamplesButton").prop("disabled",false);
|
2017-05-29 20:42:55 +08:00
|
|
|
$("#exportSamplesButton").on("click", function() {
|
|
|
|
$('#modal-export-samples-success')
|
|
|
|
.modal('show')
|
|
|
|
.on('hidden.bs.modal', function() {
|
|
|
|
animateSpinner(null, true);
|
|
|
|
$('#form-export').submit();
|
|
|
|
});
|
|
|
|
});
|
2016-07-21 19:11:15 +08:00
|
|
|
$("#assignSamples").removeClass("disabled");
|
|
|
|
$("#assignSamples").prop("disabled", false);
|
|
|
|
$("#unassignSamples").removeClass("disabled");
|
|
|
|
$("#unassignSamples").prop("disabled", false);
|
|
|
|
}
|
|
|
|
else if (rowsSelected.length == 0) {
|
|
|
|
$("#editSample").prop("disabled", true);
|
|
|
|
$("#editSample").addClass("disabled");
|
|
|
|
$("#deleteSamplesButton").prop("disabled", true);
|
|
|
|
$("#deleteSamplesButton").addClass("disabled");
|
|
|
|
$("#exportSamplesButton").addClass("disabled");
|
|
|
|
$("#exportSamplesButton").prop("disabled",true);
|
|
|
|
$("#exportSamplesButton").off("click");
|
|
|
|
$("#assignSamples").addClass("disabled");
|
|
|
|
$("#assignSamples").prop("disabled", true);
|
|
|
|
$("#unassignSamples").addClass("disabled");
|
|
|
|
$("#unassignSamples").prop("disabled", true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("#editSample").prop("disabled", true);
|
|
|
|
$("#editSample").addClass("disabled");
|
|
|
|
$("#deleteSamplesButton").prop("disabled", false);
|
|
|
|
$("#deleteSamplesButton").removeClass("disabled");
|
|
|
|
$("#exportSamplesButton").removeClass("disabled");
|
|
|
|
$("#exportSamplesButton").prop("disabled",false);
|
2017-05-29 20:42:55 +08:00
|
|
|
$("#exportSamplesButton").on("click", function() {
|
|
|
|
$('#modal-export-samples-success')
|
|
|
|
.modal('show')
|
|
|
|
.on('hidden.bs.modal', function() {
|
|
|
|
animateSpinner(null, true);
|
|
|
|
$('#form-export').submit();
|
|
|
|
});
|
|
|
|
});
|
2016-07-21 19:11:15 +08:00
|
|
|
$("#assignSamples").removeClass("disabled");
|
|
|
|
$("#assignSamples").prop("disabled", false);
|
|
|
|
$("#unassignSamples").removeClass("disabled");
|
|
|
|
$("#unassignSamples").prop("disabled", false);
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
else if (currentMode=="editMode") {
|
|
|
|
$("#importSamplesButton").addClass("disabled");
|
|
|
|
$("#importSamplesButton").prop("disabled",true);
|
|
|
|
$("#addSample").addClass("disabled");
|
|
|
|
$("#addSample").prop("disabled",true);
|
|
|
|
$("#editSample").addClass("disabled");
|
|
|
|
$("#editSample").prop("disabled",true);
|
|
|
|
$("#addNewColumn").addClass("disabled");
|
|
|
|
$("#addNewColumn").prop("disabled", true);
|
|
|
|
$("#exportSamplesButton").addClass("disabled");
|
|
|
|
$("#exportSamplesButton").off("click");
|
|
|
|
$("#deleteSamplesButton").addClass("disabled");
|
|
|
|
$("#deleteSamplesButton").prop("disabled",true);
|
|
|
|
$("#assignSamples").addClass("disabled");
|
|
|
|
$("#assignSamples").prop("disabled", true);
|
|
|
|
$("#unassignSamples").addClass("disabled");
|
|
|
|
$("#unassignSamples").prop("disabled", true);
|
2016-12-16 16:50:45 +08:00
|
|
|
$('#samples-columns-dropdown')
|
|
|
|
.find('.dropdown-toggle')
|
|
|
|
.prop("disabled",true);
|
|
|
|
$("th").addClass('disable-click');
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear all has-error tags
|
|
|
|
function clearAllErrors() {
|
|
|
|
// Remove any validation errors
|
|
|
|
$(selectedSample).find(".has-error").each(function() {
|
|
|
|
$(this).removeClass("has-error");
|
|
|
|
$(this).find("span").remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove any alerts
|
|
|
|
$("#alert-container").find("div").remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore previous table
|
|
|
|
function onClickCancel() {
|
2016-12-20 22:19:31 +08:00
|
|
|
table.draw('page');
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
changeToViewMode();
|
|
|
|
updateButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClickAddSample() {
|
|
|
|
changeToEditMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
saveAction = "create";
|
|
|
|
$.ajax({
|
|
|
|
url: $("table#samples").data("new-sample"),
|
|
|
|
type: "GET",
|
|
|
|
dataType: "json",
|
|
|
|
success: function (data) {
|
|
|
|
var tr = document.createElement("tr")
|
|
|
|
$("table#samples thead tr").children("th").each(function(i) {
|
|
|
|
var th = $(this);
|
|
|
|
if ($(th).attr("id") == "checkbox") {
|
|
|
|
var td = createTdElement("");
|
|
|
|
$(td).html($("#saveSample").clone());
|
|
|
|
tr.appendChild(td);
|
|
|
|
}
|
|
|
|
else if ($(th).attr("id") == "assigned") {
|
|
|
|
var td = createTdElement("");
|
|
|
|
$(td).html($("#cancelSave").clone());
|
|
|
|
tr.appendChild(td);
|
|
|
|
}
|
|
|
|
else if ($(th).attr("id") == "sample-name") {
|
|
|
|
var input = changeToInputField("sample", "name", "");
|
|
|
|
tr.appendChild(createTdElement(input));
|
|
|
|
}
|
|
|
|
else if ($(th).attr("id") == "sample-type") {
|
|
|
|
var colIndex = getColumnIndex("#sample-type")
|
|
|
|
if (colIndex) {
|
2016-12-08 22:02:49 +08:00
|
|
|
var selectType = createSampleTypeSelect(data["sample_types"]);
|
2016-02-12 23:52:43 +08:00
|
|
|
var td = createTdElement("");
|
|
|
|
td.appendChild(selectType[0]);
|
|
|
|
tr.appendChild(td);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($(th).attr("id") == "sample-group") {
|
|
|
|
var colIndex = getColumnIndex("#sample-group")
|
|
|
|
if (colIndex) {
|
2016-12-08 22:02:49 +08:00
|
|
|
var selectGroup = createSampleGroupSelect(data["sample_groups"]);
|
2016-02-12 23:52:43 +08:00
|
|
|
var td = createTdElement("");
|
|
|
|
td.appendChild(selectGroup[0]);
|
|
|
|
tr.appendChild(td);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ($(th).hasClass("custom-field")) {
|
|
|
|
var input = changeToInputField("custom_fields", th.attr("id"), "");
|
|
|
|
tr.appendChild(createTdElement(input));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Column we don't care for, just add empty td
|
|
|
|
tr.appendChild(createTdElement(""));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("table#samples").prepend(tr);
|
|
|
|
selectedSample = tr;
|
|
|
|
|
|
|
|
// Init dropdown with icons
|
|
|
|
$("select[name=sample_group_id]").selectpicker();
|
|
|
|
$("select[name=sample_type_id]").selectpicker();
|
2016-12-20 20:41:19 +08:00
|
|
|
|
2017-01-23 17:53:47 +08:00
|
|
|
// initialize smart annotation
|
|
|
|
_.each($('[data-object="custom_fields"]'), function(el) {
|
|
|
|
if(_.isUndefined($(el).data('atwho'))) {
|
|
|
|
SmartAnnotation.init(el);
|
|
|
|
}
|
|
|
|
});
|
2016-12-20 20:41:19 +08:00
|
|
|
// Adjust columns width in table header
|
2017-01-20 16:53:44 +08:00
|
|
|
adjustTableHeader();
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
|
|
|
error: function (e, eData, status, xhr) {
|
|
|
|
if (e.status == 403)
|
2017-03-18 00:56:58 +08:00
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
I18n.t('samples.js.permission_error'), 'danger'
|
|
|
|
);
|
2016-02-12 23:52:43 +08:00
|
|
|
changeToViewMode();
|
2016-07-21 19:11:15 +08:00
|
|
|
updateButtons();
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:46:56 +08:00
|
|
|
$('#assignedSamples').on('click', function () {
|
|
|
|
view_assigned = 'assigned';
|
|
|
|
table.draw();
|
|
|
|
});
|
|
|
|
$('#allSamples').on('click', function () {
|
|
|
|
view_assigned = 'all';
|
|
|
|
table.draw();
|
|
|
|
});
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
// Handle enter key
|
2016-12-02 23:18:11 +08:00
|
|
|
$(document).off('keypress').keypress(function(event) {
|
|
|
|
var keycode = (event.keyCode ? event.keyCode : event.which);
|
|
|
|
if (currentMode === 'editMode' && keycode === '13') {
|
|
|
|
$('#saveSample').click();
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
function getColumnIndex(id) {
|
2016-12-02 23:18:11 +08:00
|
|
|
if (id < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return table.column(id).index('visible');
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes object and surrounds it with input
|
|
|
|
function changeToInputField(object, name, value) {
|
2016-12-02 23:18:11 +08:00
|
|
|
return "<div class='form-group'><input class='form-control' data-object='" +
|
|
|
|
object + "' name='" + name + "' value='" + value + "'></input></div>";
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return td element with content
|
|
|
|
function createTdElement(content) {
|
2016-12-02 23:18:11 +08:00
|
|
|
var td = document.createElement('td');
|
|
|
|
td.innerHTML = content;
|
|
|
|
return td;
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 16:53:44 +08:00
|
|
|
// Adjust columns width in table header
|
|
|
|
function adjustTableHeader() {
|
|
|
|
table.columns.adjust();
|
|
|
|
$('.dropdown-menu').parent().on('shown.bs.dropdown hidden.bs.dropdown', function () {
|
|
|
|
table.columns.adjust();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
/**
|
|
|
|
* Creates select dropdown for sample type
|
2016-12-09 18:28:46 +08:00
|
|
|
* @param {Object[]} data List of sample types
|
|
|
|
* @param {number} selected Selected sample type id
|
|
|
|
* @return {Object} select dropdown
|
2016-02-12 23:52:43 +08:00
|
|
|
*/
|
|
|
|
function createSampleTypeSelect(data, selected) {
|
2016-12-08 22:02:49 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
var $selectType = $('<select></select>')
|
|
|
|
.attr('name', 'sample_type_id').addClass('show-tick');
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-09 18:28:46 +08:00
|
|
|
$option = $('<option></option>')
|
2016-12-02 23:18:11 +08:00
|
|
|
.attr('value', -1).text(I18n.t('samples.table.no_type'))
|
|
|
|
$selectType.append($option);
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
$.each(data, function(i, val) {
|
|
|
|
var $option = $('<option></option>')
|
|
|
|
.attr('value', val.id).text(val.name);
|
2017-01-10 23:51:51 +08:00
|
|
|
if(val.id === selected) {
|
2016-12-22 19:57:49 +08:00
|
|
|
$option.attr('selected', true);
|
|
|
|
}
|
2016-12-02 23:18:11 +08:00
|
|
|
$selectType.append($option);
|
|
|
|
});
|
|
|
|
return $selectType;
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates select dropdown for sample group
|
|
|
|
* @param data List of sample groups
|
|
|
|
* @param selected Selected sample group id
|
|
|
|
*/
|
|
|
|
function createSampleGroupSelect(data, selected) {
|
2016-12-08 22:02:49 +08:00
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
var $selectGroup = $('<select></select>')
|
|
|
|
.attr('name', 'sample_group_id').addClass('show-tick');
|
|
|
|
|
2016-12-09 18:28:46 +08:00
|
|
|
$option = $('<option></option>')
|
2016-12-02 23:18:11 +08:00
|
|
|
.attr('value', -1).text(I18n.t('samples.table.no_group'))
|
2018-07-09 19:13:44 +08:00
|
|
|
.attr('data-icon', 'fas fa-asterisk');
|
2016-12-02 23:18:11 +08:00
|
|
|
$selectGroup.append($option);
|
|
|
|
|
|
|
|
$.each(data, function(i, val) {
|
2018-07-09 19:13:44 +08:00
|
|
|
var $span = $('<span></span>').addClass('fas fa-asterisk')
|
2016-12-02 23:18:11 +08:00
|
|
|
.css('color', val.color);
|
|
|
|
var $option = $('<option></option>')
|
|
|
|
.attr('value', val.id).text(val.name)
|
|
|
|
.attr('data-content', $span.prop('outerHTML') + ' ' + val.name);
|
2017-01-10 23:51:51 +08:00
|
|
|
if(val.id === selected) {
|
2016-12-22 19:57:49 +08:00
|
|
|
$option.attr('selected', true);
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
$selectGroup.append($option);
|
2016-12-02 23:18:11 +08:00
|
|
|
});
|
|
|
|
return $selectGroup;
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function changeToViewMode() {
|
2016-12-02 23:18:11 +08:00
|
|
|
currentMode = 'viewMode';
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// $("#saveCancel").hide();
|
|
|
|
|
|
|
|
// Table specific stuff
|
2016-12-02 23:18:11 +08:00
|
|
|
table.button(0).enable(true);
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function changeToEditMode() {
|
2016-12-02 23:18:11 +08:00
|
|
|
currentMode = 'editMode';
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// $("#saveCancel").show();
|
|
|
|
|
|
|
|
// Table specific stuff
|
2016-12-02 23:18:11 +08:00
|
|
|
table.button(0).enable(false);
|
2016-11-26 00:29:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 18:43:18 +08:00
|
|
|
/*
|
|
|
|
* Sample columns dropdown
|
|
|
|
*/
|
2016-11-26 00:29:30 +08:00
|
|
|
(function(table) {
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-29 22:05:18 +08:00
|
|
|
var dropdown = $('#samples-columns-dropdown');
|
2016-11-26 00:29:30 +08:00
|
|
|
var dropdownList = $('#samples-columns-list');
|
2016-12-08 18:38:06 +08:00
|
|
|
var columnEditMode = false;
|
2016-11-26 00:29:30 +08:00
|
|
|
|
2016-12-06 18:54:56 +08:00
|
|
|
function createNewColumn() {
|
|
|
|
// Make an Ajax request to custom_fields_controller
|
|
|
|
var url = $('#new-column-form').data('action');
|
|
|
|
var columnName = $('#new-column-name').val();
|
|
|
|
if (columnName.length > 0) {
|
|
|
|
$.ajax({
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {custom_field: {name: columnName}},
|
|
|
|
error: function(data) {
|
|
|
|
var form = $('#new-column-form');
|
|
|
|
form.addClass('has-error');
|
|
|
|
form.find('.help-block').remove();
|
|
|
|
form.append('<span class="help-block">' +
|
|
|
|
data.responseJSON.name +
|
|
|
|
'</span>');
|
|
|
|
},
|
|
|
|
success: function(data) {
|
|
|
|
var form = $('#new-column-form');
|
|
|
|
form.find('.help-block').remove();
|
|
|
|
if (form.hasClass('has-error')) {
|
|
|
|
form.removeClass('has-error');
|
|
|
|
}
|
|
|
|
$('#new-column-name').val('');
|
|
|
|
form.append('<span class="help-block">' +
|
|
|
|
I18n.t('samples.js.column_added') +
|
|
|
|
'</span>');
|
2016-12-19 23:59:09 +08:00
|
|
|
|
2016-12-20 22:19:31 +08:00
|
|
|
// Preserve save/delete buttons as we need them after new table
|
|
|
|
// will be created
|
|
|
|
$('div.toolbarButtons').appendTo('div.samples-table');
|
|
|
|
$('div.toolbarButtons').hide();
|
|
|
|
|
2016-12-19 23:59:09 +08:00
|
|
|
// Destroy datatable
|
|
|
|
table.destroy();
|
|
|
|
|
|
|
|
// Add number of columns
|
2016-12-06 18:54:56 +08:00
|
|
|
$('#samples').data('num-columns',
|
|
|
|
$('#samples').data('num-columns') + 1);
|
2016-12-19 23:59:09 +08:00
|
|
|
// Add column to table (=table header)
|
2016-12-06 18:54:56 +08:00
|
|
|
originalHeader.append(
|
2016-12-09 18:58:10 +08:00
|
|
|
'<th class="custom-field" id="' + data.id + '" ' +
|
|
|
|
'data-editable data-deletable ' +
|
|
|
|
'data-edit-url="' + data.edit_url + '" ' +
|
2017-03-18 00:52:15 +08:00
|
|
|
'data-update-url="' + data.update_url + '" ' +
|
2016-12-09 18:58:10 +08:00
|
|
|
'data-destroy-html-url="' + data.destroy_html_url + '"' +
|
2016-12-14 20:36:10 +08:00
|
|
|
'>' + generateColumnNameTooltip(data.name) + '</th>');
|
2016-12-19 23:59:09 +08:00
|
|
|
|
2016-12-06 18:54:56 +08:00
|
|
|
// Remove all event handlers as we re-initialize them later with
|
|
|
|
// new table
|
|
|
|
$('#samples').off();
|
|
|
|
$('#samples thead').empty();
|
|
|
|
$('#samples thead').append(originalHeader);
|
2016-12-19 23:59:09 +08:00
|
|
|
|
|
|
|
// Re-initialize datatable
|
2016-12-06 18:54:56 +08:00
|
|
|
table = dataTableInit();
|
2016-12-09 23:07:41 +08:00
|
|
|
table.on('init.dt', function() {
|
|
|
|
loadColumnsNames();
|
2016-12-14 20:36:10 +08:00
|
|
|
dropdownOverflow();
|
2016-12-09 23:07:41 +08:00
|
|
|
});
|
2016-12-06 18:54:56 +08:00
|
|
|
},
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var form = $('#new-column-form');
|
|
|
|
form.addClass('has-error');
|
|
|
|
form.find('.help-block').remove();
|
|
|
|
form.append('<span class="help-block">' +
|
|
|
|
I18n.t('samples.js.empty_column_name') +
|
|
|
|
'</span>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:55:13 +08:00
|
|
|
function initNewColumnForm() {
|
|
|
|
$('#samples-columns-dropdown').on('show.bs.dropdown', function() {
|
|
|
|
// Clear input and errors when dropdown is opened
|
|
|
|
var input = $(this).find('input#new-column-name');
|
|
|
|
input.val('');
|
|
|
|
var form = $('#new-column-form');
|
|
|
|
if (form.hasClass('has-error')) {
|
|
|
|
form.removeClass('has-error');
|
|
|
|
}
|
|
|
|
form.find('.help-block').remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#add-new-column-button').click(function(e) {
|
|
|
|
e.stopPropagation();
|
2016-12-06 18:54:56 +08:00
|
|
|
createNewColumn();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#new-column-name').keydown(function(e) {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
createNewColumn();
|
2016-11-30 20:55:13 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:29:30 +08:00
|
|
|
// loads the columns names in the dropdown list
|
|
|
|
function loadColumnsNames() {
|
2016-12-13 18:25:35 +08:00
|
|
|
// Save scroll position
|
|
|
|
var scrollPosition = dropdownList.scrollTop();
|
|
|
|
// Clear the list
|
2016-11-30 18:42:05 +08:00
|
|
|
dropdownList.find('li[data-position]').remove();
|
2016-11-28 23:28:22 +08:00
|
|
|
_.each(table.columns().header(), function(el, index) {
|
2016-11-30 18:42:05 +08:00
|
|
|
if (index > 1) {
|
2016-11-26 00:29:30 +08:00
|
|
|
var colIndex = $(el).attr('data-column-index');
|
|
|
|
var visible = table.column(colIndex).visible();
|
2016-11-29 18:43:18 +08:00
|
|
|
var editable = $(el).is('[data-editable]');
|
|
|
|
var deletable = $(el).is('[data-deletable]');
|
|
|
|
|
2018-07-09 19:13:44 +08:00
|
|
|
var visClass = (visible) ? 'fa-eye' : 'fa-eye-slash';
|
2016-11-26 00:29:30 +08:00
|
|
|
var visLi = (visible) ? '' : 'col-invisible';
|
2016-11-29 18:43:18 +08:00
|
|
|
var editClass = (editable) ? '' : 'disabled';
|
|
|
|
var delClass = (deletable) ? '' : 'disabled';
|
2016-12-15 22:26:51 +08:00
|
|
|
|
|
|
|
var thederName;
|
|
|
|
if ($(el).find('.modal-tooltiptext').length > 0) {
|
|
|
|
thederName = $(el).find('.modal-tooltiptext').text();
|
|
|
|
} else {
|
|
|
|
thederName = el.innerText;
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:43:18 +08:00
|
|
|
var html =
|
2016-11-29 22:05:18 +08:00
|
|
|
'<li ' +
|
|
|
|
'data-position="' + colIndex + '" ' +
|
|
|
|
'data-id="' + $(el).attr('id') + '" ' +
|
2016-12-09 18:58:10 +08:00
|
|
|
'data-edit-url=' + $(el).attr('data-edit-url') + ' ' +
|
2017-03-18 00:52:15 +08:00
|
|
|
'data-update-url=' + $(el).attr('data-update-url') + ' ' +
|
2016-12-09 18:58:10 +08:00
|
|
|
'data-destroy-html-url=' + $(el).attr('data-destroy-html-url') + ' ' +
|
2016-11-29 22:05:18 +08:00
|
|
|
'class="' + visLi + '"' +
|
|
|
|
'>' +
|
2016-11-29 18:43:18 +08:00
|
|
|
'<i class="grippy"></i> ' +
|
2016-12-15 22:26:51 +08:00
|
|
|
'<span class="text">' + generateColumnNameTooltip(thederName) + '</span> ' +
|
2016-12-14 00:08:19 +08:00
|
|
|
'<span class="form-group"><input type="text" class="text-edit form-control" style="display: none;" />' +
|
2016-11-29 18:43:18 +08:00
|
|
|
'<span class="pull-right controls">' +
|
2018-07-09 19:13:44 +08:00
|
|
|
'<span class="ok fas fa-check" style="display: none;" title="<%= I18n.t("general.save") %>"></span>' +
|
|
|
|
'<span class="cancel fas fa-times" style="display: none;" title="<%= I18n.t("general.cancel") %>"></span>' +
|
|
|
|
'<span class="vis fas' + visClass + '" title="<%= I18n.t("samples.columns_visibility") %>"></span> ' +
|
2018-05-22 22:55:58 +08:00
|
|
|
'<span class="edit fas fa-pencil-alt ' + editClass + '" title="<%= I18n.t("general.edit") %>"></span>' +
|
2018-07-09 19:13:44 +08:00
|
|
|
'<span class="del fas fa-trash-alt ' + delClass + '" title="<%= I18n.t("samples.columns_delete") %>"></span>' +
|
2016-12-15 22:50:07 +08:00
|
|
|
'</span><br></span></li>';
|
2016-11-28 23:28:22 +08:00
|
|
|
dropdownList.append(html);
|
2016-11-26 00:29:30 +08:00
|
|
|
}
|
|
|
|
});
|
2016-12-13 18:25:35 +08:00
|
|
|
// Restore scroll position
|
|
|
|
dropdownList.scrollTop(scrollPosition);
|
2016-11-30 18:42:05 +08:00
|
|
|
toggleColumnVisibility();
|
2016-11-26 00:29:30 +08:00
|
|
|
// toggles grip img
|
|
|
|
customLiHoverEffect();
|
|
|
|
}
|
|
|
|
|
|
|
|
function customLiHoverEffect() {
|
|
|
|
var liEl = dropdownList.find('li');
|
|
|
|
liEl.mouseover(function() {
|
|
|
|
$(this)
|
|
|
|
.find('.grippy')
|
|
|
|
.addClass('grippy-img');
|
|
|
|
}).mouseout(function() {
|
|
|
|
$(this)
|
|
|
|
.find('.grippy')
|
|
|
|
.removeClass('grippy-img');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleColumnVisibility() {
|
|
|
|
var lis = dropdownList.find('.vis');
|
|
|
|
lis.on('click', function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
var self = $(this);
|
|
|
|
var li = self.closest('li');
|
|
|
|
var column = table.column(li.attr('data-position'));
|
|
|
|
|
2016-12-02 23:18:11 +08:00
|
|
|
if (column.visible()) {
|
2018-07-09 19:13:44 +08:00
|
|
|
self.addClass('fa-eye-slash');
|
2018-05-22 22:55:58 +08:00
|
|
|
self.removeClass('fas fa-eye');
|
2016-11-26 00:29:30 +08:00
|
|
|
li.addClass('col-invisible');
|
|
|
|
column.visible(false);
|
2016-12-24 02:56:54 +08:00
|
|
|
table.setColumnSearchable(column.index(), false);
|
2016-11-26 00:29:30 +08:00
|
|
|
} else {
|
2018-05-22 22:55:58 +08:00
|
|
|
self.addClass('fas fa-eye');
|
2018-07-09 19:13:44 +08:00
|
|
|
self.removeClass('fa-eye-slash');
|
2016-11-26 00:29:30 +08:00
|
|
|
li.removeClass('col-invisible');
|
|
|
|
column.visible(true);
|
2016-12-24 02:56:54 +08:00
|
|
|
table.setColumnSearchable(column.index(), true);
|
2017-01-24 21:30:32 +08:00
|
|
|
initHeaderTooltip();
|
2016-11-26 00:29:30 +08:00
|
|
|
}
|
2017-01-10 18:26:12 +08:00
|
|
|
|
|
|
|
// Re-filter/search if neccesary
|
|
|
|
var searchText = $('div.dataTables_filter input').val();
|
|
|
|
if (!_.isEmpty(searchText)) {
|
|
|
|
table.search(searchText).draw();
|
2016-11-26 00:29:30 +08:00
|
|
|
}
|
2017-01-07 00:31:17 +08:00
|
|
|
sampleInfoListener();
|
2016-11-26 00:29:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:42:05 +08:00
|
|
|
function initSorting() {
|
|
|
|
dropdownList.sortable({
|
2016-12-02 23:18:11 +08:00
|
|
|
items: 'li:not(.add-new-column-form)',
|
2016-11-30 18:42:05 +08:00
|
|
|
cancel: '.new-samples-column',
|
|
|
|
axis: 'y',
|
|
|
|
update: function() {
|
|
|
|
var reorderer = table.colReorder;
|
|
|
|
var listIds = [];
|
|
|
|
// We skip first two columns
|
|
|
|
listIds.push(0, 1);
|
|
|
|
dropdownList.find('li[data-position]').each(function() {
|
|
|
|
listIds.push($(this).first().data('position'));
|
|
|
|
});
|
|
|
|
reorderer.order(listIds, false);
|
|
|
|
loadColumnsNames();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:38:06 +08:00
|
|
|
function initEditColumns() {
|
2016-11-29 21:01:15 +08:00
|
|
|
function cancelEditMode() {
|
|
|
|
dropdownList.find('.text-edit').hide();
|
|
|
|
dropdownList.find('.controls .ok,.cancel').hide();
|
|
|
|
dropdownList.find('.text').css('display', ''); // show() doesn't work
|
|
|
|
dropdownList.find('.controls .vis,.edit,.del').css('display', ''); // show() doesn't work
|
2016-12-08 18:38:06 +08:00
|
|
|
columnEditMode = false;
|
2016-11-29 21:01:15 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:58:10 +08:00
|
|
|
function editColumn(li) {
|
|
|
|
var id = li.attr('data-id');
|
|
|
|
var text = li.find('.text');
|
|
|
|
var textEdit = li.find('.text-edit');
|
|
|
|
var newName = textEdit.val().trim();
|
2017-03-18 00:52:15 +08:00
|
|
|
var url = li.attr('data-update-url');
|
2016-12-09 18:58:10 +08:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: 'PUT',
|
|
|
|
data: {custom_field: {name: newName}},
|
|
|
|
dataType: 'json',
|
|
|
|
success: function() {
|
2016-12-15 17:44:23 +08:00
|
|
|
dropdownList.sortable('enable');
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).clearFormErrors();
|
2016-12-14 23:37:44 +08:00
|
|
|
text.html(generateColumnNameTooltip(newName));
|
2016-12-14 23:47:39 +08:00
|
|
|
$(table.columns().header()).filter('#' + id)
|
|
|
|
.html(generateColumnNameTooltip(newName));
|
2017-05-11 17:16:12 +08:00
|
|
|
originalHeader.find('#' + id).html(newName);
|
2016-12-09 18:58:10 +08:00
|
|
|
cancelEditMode();
|
2017-01-24 21:30:32 +08:00
|
|
|
initHeaderTooltip();
|
2016-12-09 18:58:10 +08:00
|
|
|
},
|
2016-12-15 18:57:17 +08:00
|
|
|
error: function(xhr) {
|
2016-12-15 17:44:23 +08:00
|
|
|
dropdownList.sortable('disable');
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).clearFormErrors();
|
|
|
|
var msg = $.parseJSON(xhr.responseText);
|
2016-12-15 18:57:17 +08:00
|
|
|
|
|
|
|
renderFormError(xhr,
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).find('.text-edit'),
|
2016-12-15 18:57:17 +08:00
|
|
|
Object.keys(msg)[0] + ' ' + msg.name.toString());
|
2016-12-15 17:51:17 +08:00
|
|
|
var verticalHeight = $(li).offset().top;
|
|
|
|
dropdownList.scrollTo(verticalHeight,0);
|
2016-12-09 18:58:10 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:38:06 +08:00
|
|
|
// On edit buttons click (bind onto master dropdown list)
|
|
|
|
dropdownList.on('click', '.edit:not(.disabled)', function(event) {
|
2016-11-29 21:01:15 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
2016-12-15 16:54:38 +08:00
|
|
|
// Clear all input errors
|
|
|
|
_.each(dropdownList, function(el) {
|
|
|
|
$(el).clearFormErrors();
|
|
|
|
});
|
|
|
|
|
2016-11-29 21:01:15 +08:00
|
|
|
cancelEditMode();
|
|
|
|
|
2017-03-15 21:09:44 +08:00
|
|
|
var li = $(this).closest('li');
|
2017-03-18 00:52:15 +08:00
|
|
|
var url = li.attr('data-edit-url');
|
2017-03-15 21:09:44 +08:00
|
|
|
ajaxCallEvent();
|
|
|
|
|
|
|
|
function ajaxCallEvent(){
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
success: function() {
|
2017-03-18 00:52:15 +08:00
|
|
|
var text, textEdit, controls, controlsEdit;
|
|
|
|
text = li.find('.text');
|
2017-03-15 21:09:44 +08:00
|
|
|
if ($(text).find('.modal-tooltiptext').length > 0) {
|
|
|
|
text = $(text).find('.modal-tooltiptext');
|
|
|
|
}
|
2017-03-18 00:52:15 +08:00
|
|
|
textEdit = li.find('.text-edit');
|
|
|
|
controls = li.find('.controls .vis,.edit,.del');
|
|
|
|
controlsEdit = li.find('.controls .ok,.cancel');
|
2017-03-15 21:09:44 +08:00
|
|
|
|
|
|
|
// Toggle edit mode
|
|
|
|
columnEditMode = true;
|
|
|
|
li.addClass('editing');
|
|
|
|
|
|
|
|
// Set the text-edit's value
|
|
|
|
textEdit.val(text.text().trim());
|
|
|
|
|
|
|
|
// Toggle elements
|
|
|
|
text.hide();
|
|
|
|
controls.hide();
|
|
|
|
textEdit.css('display', ''); // show() doesn't work
|
|
|
|
controlsEdit.css('display', ''); // show() doesn't work
|
|
|
|
dropdownList.sortable('disable');
|
|
|
|
dropdownList.on('click', function(ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
});
|
|
|
|
// Focus input
|
|
|
|
textEdit.focus();
|
|
|
|
},
|
|
|
|
error: function(e) {
|
|
|
|
$(li).clearFormErrors();
|
|
|
|
var msg = $.parseJSON(e.responseText);
|
|
|
|
|
|
|
|
renderFormError(undefined,
|
|
|
|
$(li).find('.text-edit'),
|
2017-03-28 21:14:05 +08:00
|
|
|
msg.name.toString());
|
2017-03-15 21:09:44 +08:00
|
|
|
var verticalHeight = $(li).offset().top;
|
|
|
|
dropdownList.scrollTo(verticalHeight,0);
|
|
|
|
setTimeout(function() {
|
|
|
|
$(li).clearFormErrors();
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
});
|
2016-12-15 22:26:51 +08:00
|
|
|
}
|
2016-11-29 21:01:15 +08:00
|
|
|
});
|
|
|
|
|
2016-11-29 22:05:18 +08:00
|
|
|
// On hiding dropdown, cancel edit mode throughout dropdown
|
|
|
|
dropdown.on('hidden.bs.dropdown', function() {
|
|
|
|
cancelEditMode();
|
|
|
|
});
|
|
|
|
|
|
|
|
// On ok buttons click
|
2016-12-08 18:38:06 +08:00
|
|
|
dropdownList.on('click', '.ok', function(event) {
|
2016-11-29 22:05:18 +08:00
|
|
|
event.stopPropagation();
|
2016-12-14 00:08:19 +08:00
|
|
|
dropdownList.sortable('enable');
|
2016-11-29 22:05:18 +08:00
|
|
|
var self = $(this);
|
|
|
|
var li = self.closest('li');
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).clearFormErrors();
|
2016-12-09 18:58:10 +08:00
|
|
|
editColumn(li);
|
|
|
|
});
|
2016-11-29 22:05:18 +08:00
|
|
|
|
2016-12-09 18:58:10 +08:00
|
|
|
// On enter click while editing column text
|
|
|
|
dropdownList.on('keydown', 'input.text-edit', function(event) {
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
event.preventDefault();
|
2016-12-14 00:08:19 +08:00
|
|
|
dropdownList.sortable('enable');
|
2016-12-09 18:58:10 +08:00
|
|
|
var self = $(this);
|
|
|
|
var li = self.closest('li');
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).clearFormErrors();
|
2016-12-09 18:58:10 +08:00
|
|
|
editColumn(li);
|
|
|
|
}
|
2016-11-29 22:05:18 +08:00
|
|
|
});
|
|
|
|
|
2016-11-29 21:01:15 +08:00
|
|
|
// On cancel buttons click
|
2016-12-08 18:38:06 +08:00
|
|
|
dropdownList.on('click', '.cancel', function(event) {
|
2016-11-29 21:01:15 +08:00
|
|
|
event.stopPropagation();
|
2016-12-14 00:08:19 +08:00
|
|
|
dropdownList.sortable('enable');
|
2016-11-29 21:01:15 +08:00
|
|
|
var self = $(this);
|
|
|
|
var li = self.closest('li');
|
2016-12-14 00:08:19 +08:00
|
|
|
$(li).clearFormErrors();
|
2016-12-08 18:38:06 +08:00
|
|
|
columnEditMode = false;
|
2016-11-29 21:01:15 +08:00
|
|
|
li.removeClass('editing');
|
|
|
|
|
|
|
|
li.find('.text-edit').hide();
|
|
|
|
li.find('.controls .ok,.cancel').hide();
|
|
|
|
li.find('.text').css('display', ''); // show() doesn't work
|
|
|
|
li.find('.controls .vis,.edit,.del').css('display', ''); // show() doesn't work
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-09 18:58:10 +08:00
|
|
|
function initDeleteColumns() {
|
|
|
|
var modal = $('#deleteCustomField');
|
|
|
|
|
|
|
|
dropdownList.on('click', '.del', function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
var self = $(this);
|
|
|
|
var li = self.closest('li');
|
|
|
|
var url = li.attr('data-destroy-html-url');
|
2016-12-14 22:23:00 +08:00
|
|
|
var colIndex = originalHeader.find('#' + li.attr('data-id')).index();
|
2016-12-09 18:58:10 +08:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
2016-12-14 22:23:00 +08:00
|
|
|
data: {column_index: colIndex},
|
2016-12-09 18:58:10 +08:00
|
|
|
success: function(data) {
|
|
|
|
var modalBody = modal.find('.modal-body');
|
|
|
|
|
|
|
|
// Inject the body's HTML into modal
|
|
|
|
modalBody.html(data.html);
|
|
|
|
|
|
|
|
// Show the modal
|
|
|
|
modal.modal('show');
|
|
|
|
},
|
|
|
|
error: function(xhr) {
|
2017-02-21 23:25:50 +08:00
|
|
|
dropdownList.sortable('disable');
|
|
|
|
$(li).clearFormErrors();
|
|
|
|
var msg = $.parseJSON(xhr.responseText);
|
|
|
|
|
|
|
|
renderFormError(undefined,
|
|
|
|
$(li).find('.text-edit'),
|
2017-03-28 21:14:05 +08:00
|
|
|
msg.name.toString());
|
2017-02-21 23:25:50 +08:00
|
|
|
var verticalHeight = $(li).offset().top;
|
|
|
|
dropdownList.scrollTo(verticalHeight,0);
|
|
|
|
setTimeout(function() {
|
|
|
|
$(li).clearFormErrors();
|
|
|
|
}, 5000);
|
2016-12-09 18:58:10 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
modal.find('.modal-footer [data-action=delete]').on('click', function() {
|
|
|
|
var modalBody = modal.find('.modal-body');
|
|
|
|
var form = modalBody.find('[data-role=destroy-custom-field-form]');
|
|
|
|
var id = form.attr('data-id');
|
|
|
|
|
|
|
|
form
|
|
|
|
.on('ajax:success', function() {
|
2016-12-20 16:49:21 +08:00
|
|
|
// Preserve save/delete buttons as we need them after new table
|
|
|
|
// will be created
|
|
|
|
$('div.toolbarButtons').appendTo('div.samples-table');
|
|
|
|
$('div.toolbarButtons').hide();
|
|
|
|
|
2016-12-09 18:58:10 +08:00
|
|
|
// Destroy datatable
|
|
|
|
table.destroy();
|
|
|
|
|
|
|
|
// Subtract number of columns
|
|
|
|
$('#samples').data(
|
|
|
|
'num-columns',
|
|
|
|
$('#samples').data('num-columns') - 1
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove column from table (=table header) & rows
|
|
|
|
var th = originalHeader.find('#' + id);
|
|
|
|
var index = th.index();
|
|
|
|
th.remove();
|
|
|
|
$('#samples tbody td:nth-child(' + (index + 1) + ')').remove();
|
|
|
|
|
|
|
|
// Remove all event handlers as we re-initialize them later with
|
|
|
|
// new table
|
|
|
|
$('#samples').off();
|
|
|
|
$('#samples thead').empty();
|
|
|
|
$('#samples thead').append(originalHeader);
|
|
|
|
|
|
|
|
// Re-initialize datatable
|
|
|
|
table = dataTableInit();
|
|
|
|
loadColumnsNames();
|
|
|
|
|
|
|
|
// Hide modal
|
|
|
|
modal.modal('hide');
|
|
|
|
})
|
|
|
|
.on('ajax:error', function() {
|
|
|
|
// TODO
|
|
|
|
});
|
|
|
|
|
|
|
|
form.submit();
|
|
|
|
});
|
|
|
|
|
|
|
|
modal.on('hidden.bs.modal', function() {
|
|
|
|
// Remove event handlers, clear contents
|
|
|
|
var modalBody = modal.find('.modal-body');
|
|
|
|
modalBody.off();
|
|
|
|
modalBody.html('');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:35:50 +08:00
|
|
|
// calculate the max height of window and adjust dropdown max-height
|
|
|
|
function dropdownOverflow() {
|
|
|
|
var windowHeight = $( window ).height();
|
|
|
|
var offset = windowHeight - dropdownList.offset().top;
|
|
|
|
|
|
|
|
if(dropdownList.height() >= offset) {
|
|
|
|
dropdownList.css('maxHeight', offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 20:36:10 +08:00
|
|
|
function generateColumnNameTooltip(name) {
|
|
|
|
if( $.trim(name).length >
|
|
|
|
<%= Constants::NAME_TRUNCATION_LENGTH_DROPDOWN %>) {
|
|
|
|
return '<div class="modal-tooltip">' +
|
|
|
|
truncateLongString(name,
|
|
|
|
<%= Constants::NAME_TRUNCATION_LENGTH_DROPDOWN %>) +
|
|
|
|
'<span class="modal-tooltiptext">' + name + '</span></div>';
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:29:30 +08:00
|
|
|
// initialze dropdown after the table is loaded
|
|
|
|
function initDropdown() {
|
2016-12-02 23:18:11 +08:00
|
|
|
table.on('init.dt', function() {
|
2016-11-30 20:55:13 +08:00
|
|
|
initNewColumnForm();
|
2016-11-30 18:42:05 +08:00
|
|
|
initSorting();
|
2016-11-29 21:01:15 +08:00
|
|
|
toggleColumnVisibility();
|
2016-12-08 18:38:06 +08:00
|
|
|
initEditColumns();
|
2016-12-09 18:58:10 +08:00
|
|
|
initDeleteColumns();
|
2016-11-26 00:29:30 +08:00
|
|
|
});
|
2016-12-10 00:37:22 +08:00
|
|
|
$('#samples-columns-dropdown').on('show.bs.dropdown', function() {
|
|
|
|
loadColumnsNames();
|
2016-12-14 00:08:19 +08:00
|
|
|
dropdownList.sortable('enable');
|
2016-12-10 00:37:22 +08:00
|
|
|
});
|
2016-12-13 18:35:50 +08:00
|
|
|
|
|
|
|
$('#samples-columns-dropdown').on('shown.bs.dropdown', function() {
|
|
|
|
dropdownOverflow();
|
|
|
|
})
|
2016-11-26 00:29:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
initDropdown();
|
|
|
|
})(table);
|