scinote-web/app/assets/javascripts/samples/samples.js

264 lines
8.9 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
//= require datatables
// Create custom field ajax
$("#modal-create-custom-field").on("show.bs.modal", function(event) {
// Clear input when modal is opened
input = $(this).find("input#name-input");
input.val("");
input.closest(".form-group").removeClass("has-error");
input.closest(".form-group").find(".help-block").remove();
});
$("#modal-create-custom-field").on("shown.bs.modal", function(event) {
$(this).find("input#name-input").focus();
});
$("form#new_custom_field").on("ajax:success", function(ev, data, status) {
$("#modal-create-custom-field").modal("hide");
// Reload page with URL parameter of newly created field
window.location.href = addParam(window.location.href, "new_col");
});
$("form#new_custom_field").on("ajax:error", function(e, data, status, xhr) {
$('form').renderFormErrors('custom_field', data.responseJSON, true, e);
2016-02-12 23:52:43 +08:00
});
// Create sample type ajax
$("#modal-create-sample-type").on("show.bs.modal", function(event) {
// Clear input when modal is opened
input = $(this).find("input#name-input");
input.val("");
input.closest(".form-group").removeClass("has-error");
input.closest(".form-group").find(".help-block").remove();
});
$("#modal-create-sample-type").on("shown.bs.modal", function(event) {
$(this).find("input#name-input").focus();
});
$("form#new_sample_type").on("ajax:success", function(ev, data, status) {
$("#modal-create-sample-type").modal("hide");
2016-07-21 19:11:15 +08:00
updateSamplesTypesandGroups();
sampleAlertMsg(data.flash, "success");
2016-08-18 22:47:16 +08:00
currentMode = "viewMode";
updateButtons();
2016-02-12 23:52:43 +08:00
});
$("form#new_sample_type").on("ajax:error", function(e, data, status, xhr) {
$('form').renderFormErrors('sample_type', data.responseJSON, true, e);
2016-02-12 23:52:43 +08:00
});
// Create sample group ajax
$("#modal-create-sample-group").on("show.bs.modal", function(event) {
// Clear input when modal is opened
input = $(this).find("input#name-input");
input.val("");
input.closest(".form-group").removeClass("has-error");
input.closest(".form-group").find(".help-block").remove();
});
$("#modal-create-sample-group").on("shown.bs.modal", function(event) {
$(this).find("input#name-input").focus();
});
$("form#new_sample_group").on("ajax:success", function(ev, data, status) {
$("#modal-create-sample-group").modal("hide");
2016-07-21 19:11:15 +08:00
updateSamplesTypesandGroups();
sampleAlertMsg(data.flash, "success");
2016-08-18 22:47:16 +08:00
currentMode = "viewMode";
updateButtons();
2016-02-12 23:52:43 +08:00
});
$("form#new_sample_group").on("ajax:error", function(e, data, status, xhr) {
$('form').renderFormErrors('sample_group', data.responseJSON, true, e);
2016-02-12 23:52:43 +08:00
});
// Create import samples ajax
$("#modal-import-samples").on("show.bs.modal", function(event) {
formGroup = $(this).find(".form-group");
formGroup.removeClass("has-error");
formGroup.find(".help-block").remove();
});
$("form#form-samples-file")
.on("ajax:success", function(ev, data, status) {
$("#modal-parse-samples").html(data.html);
$("#modal-import-samples").modal("hide");
$("#modal-parse-samples").modal("show");
})
.on("ajax:error", function(ev, data, status) {
$(this).find(".form-group").addClass("has-error");
$(this).find(".form-group").find(".help-block").remove();
$(this).find(".form-group").append("<span class='help-block'>" + data.responseJSON.message + "</span>");
});
2016-07-21 19:11:15 +08:00
// Fetch samples data and updates the select options fields for
// sample group and sample type column
function updateSamplesTypesandGroups() {
changeToEditMode();
updateButtons();
$.ajax({
url: $("table#samples").data("new-sample"),
type: "GET",
dataType: "json",
success: function (data) {
$("select[name=sample_group_id]").each(function(){
var sample_group = $(this).val();
var selectGroup = createSampleGroupSelect(data.sample_groups, sample_group);
2016-07-21 19:11:15 +08:00
var gtd = $(this).parent("td");
gtd.html(selectGroup[0]);
});
$("select[name=sample_type_id]").each(function(){
var sample_type = $(this).val();
var selectType = createSampleTypeSelect(data.sample_types, sample_type);
2016-07-21 19:11:15 +08:00
var ttd = $(this).parent("td");
ttd.html(selectType[0]);
});
$("select[name=sample_group_id]").selectpicker();
$("select[name=sample_type_id]").selectpicker();
},
error: function (e, eData, status, xhr) {
if (e.status == 403)
showAlertMessage(I18n.t("samples.js.permission_error"));
changeToViewMode();
updateButtons();
}
});
}
2016-02-12 23:52:43 +08:00
function sampleAlertMsg(message, type) {
2016-08-30 23:12:26 +08:00
var alertType, glyphSign;
if (type == 'success') {
alertType = ' alert-success ';
glyphSign = ' glyphicon-ok-sign ';
} else if (type == 'danger') {
alertType = ' alert-danger ';
glyphSign = ' glyphicon-exclamation-sign ';
}
2016-08-30 23:12:26 +08:00
var htmlSnippet = '<div class="alert' + alertType + 'alert-dismissable alert-floating">' +
'<div class="container">' +
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>' +
2016-08-30 23:12:26 +08:00
'<span class="glyphicon' + glyphSign + '"></span>' +
'<span>'+ message +'</span>' +
'</div>' +
'</div>';
2016-08-30 23:12:26 +08:00
$('#notifications').html(htmlSnippet);
$('#content-wrapper').addClass('alert-shown');
}
function sampleAlertMsgHide() {
$('#notifications').html('<div></div>');
$('#content-wrapper').removeClass('alert-shown');
}
2016-02-12 23:52:43 +08:00
function initTutorial() {
2016-07-21 19:11:15 +08:00
var currentStep = parseInt(Cookies.get('current_tutorial_step'), 10);
if (showTutorial() && (currentStep > 15 && currentStep < 19)) {
2016-02-12 23:52:43 +08:00
var samplesTutorial =$("#samples-toolbar").attr("data-samples-step-text");
var breadcrumbsTutorial = $("#samples-toolbar").attr("data-breadcrumbs-step-text");
introJs()
.setOptions({
steps: [
{
element: document.getElementById("importSamplesButton"),
2016-07-21 19:11:15 +08:00
intro: samplesTutorial
2016-02-12 23:52:43 +08:00
},
{
element: document.getElementById("secondary-menu"),
intro: breadcrumbsTutorial,
tooltipClass: 'custom next-page-link',
2016-02-12 23:52:43 +08:00
}
],
overlayOpacity: '0.1',
nextLabel: 'Next',
doneLabel: 'End tutorial',
skipLabel: 'End tutorial',
showBullets: false,
showStepNumbers: false,
2016-07-21 19:11:15 +08:00
exitOnOverlayClick: false,
exitOnEsc: false,
disableInteraction: true,
tooltipClass: "custom"
2016-02-12 23:52:43 +08:00
})
.onafterchange(function (tarEl) {
Cookies.set('current_tutorial_step', this._currentStep + 17);
2016-07-21 19:11:15 +08:00
if (this._currentStep == 1) {
setTimeout(function() {
$('.next-page-link a.introjs-nextbutton')
.removeClass('introjs-disabled')
.attr('href', $("#reports-nav-tab a").attr('href'));
$('.introjs-disableInteraction').remove();
positionTutorialTooltip();
}, 500);
} else {
positionTutorialTooltip();
}
2016-02-12 23:52:43 +08:00
})
.goToStep(currentStep == 18 ? 2 : 1)
2016-02-12 23:52:43 +08:00
.start();
// Destroy first-time tutorial cookies when skip tutorial
// or end tutorial is clicked
$(".introjs-skipbutton").each(function (){
$(this).click(function (){
Cookies.remove('tutorial_data');
Cookies.remove('current_tutorial_step');
restore_after_tutorial();
2016-02-12 23:52:43 +08:00
});
});
}
}
2016-07-21 19:11:15 +08:00
function positionTutorialTooltip() {
if (Cookies.get('current_tutorial_step') == 13) {
if ($("#reports-nav-tab").offset().left == 0) {
$(".introjs-tooltip").css("left", (window.innerWidth / 2 - 50) + "px");
$(".introjs-tooltip").addClass("repositioned");
} else if ($(".introjs-tooltip").hasClass("repositioned")) {
$(".introjs-tooltip").css("left", "");
$(".introjs-tooltip").removeClass("repositioned");
}
} else {
if ($(".introjs-tooltip").offset().left > window.innerWidth) {
$(".introjs-tooltip").css("left", (window.innerWidth / 2 - 50) + "px");
$(".introjs-tooltip").addClass("repositioned");
} else if ($(".introjs-tooltip").hasClass("repositioned")) {
$(".introjs-tooltip").css("left", "");
$(".introjs-tooltip").removeClass("repositioned");
}
}
};
2016-02-12 23:52:43 +08:00
function showTutorial() {
var tutorialData;
if (Cookies.get('tutorial_data'))
tutorialData = JSON.parse(Cookies.get('tutorial_data'));
else
return false;
var tutorialModuleId = tutorialData[0].qpcr_module;
var currentModuleId = $("#samples-toolbar").attr("data-module-id");
return tutorialModuleId == currentModuleId;
}
2016-08-11 22:06:51 +08:00
function samples_tutorial_helper(){
if( $('div').hasClass('introjs-overlay') ){
$.each( $('#secondary-menu').find('a'), function(){
$(this).css({ 'pointer-events': 'none' });
});
2016-08-11 22:06:51 +08:00
}
}
function restore_after_tutorial(){
$.each( $('#secondary-menu').find('a'), function(){
$(this).css({ 'pointer-events': 'auto' });
});
}
2016-02-12 23:52:43 +08:00
// Initialize first-time tutorial
2016-07-21 19:11:15 +08:00
initTutorial();
2016-08-11 22:06:51 +08:00
samples_tutorial_helper();