scinote-web/app/assets/javascripts/my_modules/results.js.erb

265 lines
8.2 KiB
Text
Raw Normal View History

//= require comments
2016-02-12 23:52:43 +08:00
function initHandsOnTables(root) {
2016-07-21 19:11:15 +08:00
root.find("div.hot-table").each(function() {
2016-02-12 23:52:43 +08:00
var $container = $(this).find(".step-result-hot-table");
var contents = $(this).find('.hot-contents');
$container.handsontable({
2016-07-21 19:11:15 +08:00
width: '100%',
2016-02-12 23:52:43 +08:00
startRows: 5,
startCols: 5,
rowHeaders: true,
colHeaders: true,
2016-07-21 19:11:15 +08:00
fillHandle: false,
2016-09-05 19:54:20 +08:00
formulas: true,
2016-02-12 23:52:43 +08:00
cells: function (row, col, prop) {
var cellProperties = {};
if (col >= 0)
cellProperties.readOnly = true;
else
cellProperties.readOnly = false;
return cellProperties;
}
});
var hot = $container.handsontable('getInstance');
var data = JSON.parse(contents.attr("value"));
hot.loadData(data.data);
});
}
2016-07-21 19:11:15 +08:00
function applyCollapseLinkCallBack() {
$(".result-panel-collapse-link")
.on("ajax:success", function() {
var collapseIcon = $(this).find(".collapse-result-icon");
var collapsed = $(this).hasClass("collapsed");
// Toggle collapse button
collapseIcon.toggleClass("glyphicon-collapse-up", !collapsed);
collapseIcon.toggleClass("glyphicon-collapse-down", collapsed);
});
}
2016-02-12 23:52:43 +08:00
// Toggle editing buttons
function toggleResultEditButtons(show) {
if (show) {
$("#results-toolbar").show();
$(".edit-result-button").show();
} else {
$(".edit-result-button").hide();
$("#results-toolbar").hide();
}
}
// Expand all results
function expandAllResults() {
$('.result .panel-collapse').collapse('show');
$(document).find("span.collapse-result-icon").each(function() {
$(this).addClass("glyphicon-collapse-up");
$(this).removeClass("glyphicon-collapse-down");
});
2016-07-21 19:11:15 +08:00
$(document).find("div.step-result-hot-table").each(function() {
renderTable(this);
});
2016-02-12 23:52:43 +08:00
}
function expandResult(result) {
$('.panel-collapse', result).collapse('show');
$(result).find("span.collapse-result-icon").each(function() {
$(this).addClass("glyphicon-collapse-up");
$(this).removeClass("glyphicon-collapse-down");
});
2016-07-21 19:11:15 +08:00
renderTable($(result).find("div.step-result-hot-table"));
2016-02-12 23:52:43 +08:00
}
2016-07-21 19:11:15 +08:00
function renderTable(table) {
$(table).handsontable("render");
// Yet another dirty hack to solve HandsOnTable problems
if (parseInt($(table).css("height"), 10) < parseInt($(table).css("max-height"), 10) - 30) {
$(table).find(".ht_master .wtHolder").css({ 'height': '100%',
'width': '100%' });
}
}
2016-02-12 23:52:43 +08:00
// Initialize first-time tutorial
function initTutorial() {
var currentStep = Cookies.get('current_tutorial_step');
if (showTutorial() && (currentStep > 13 && currentStep < 17)) {
2016-02-12 23:52:43 +08:00
var moduleResultsTutorial = $("#results").attr("data-module-protocols-step-text");
2016-07-21 19:11:15 +08:00
var moduleResultsClickSamplesTutorial = $("#results").attr("data-module-protocols-click-samples-step-text");
var samplesTab = $("#module-samples-nav-tab");
2016-02-12 23:52:43 +08:00
introJs()
.setOptions({
2016-07-21 19:11:15 +08:00
steps: [
{
element: document.getElementById("results-toolbar"),
intro: moduleResultsTutorial,
disableInteraction: true
},
{
element: samplesTab[0],
intro: moduleResultsClickSamplesTutorial,
2016-11-16 22:44:06 +08:00
position: 'bottom-right-aligned',
2016-07-21 19:11:15 +08:00
tooltipClass: 'custom next-page-link'
}
],
2016-02-12 23:52:43 +08:00
overlayOpacity: '0.1',
doneLabel: 'End tutorial',
2016-07-21 19:11:15 +08:00
skipLabel: 'End tutorial',
nextLabel: 'Next',
2016-02-12 23:52:43 +08:00
showBullets: false,
showStepNumbers: false,
2016-07-21 19:11:15 +08:00
exitOnOverlayClick: false,
exitOnEsc: false,
tooltipClass: 'custom',
2016-02-12 23:52:43 +08:00
disableInteraction: true
})
2016-07-21 19:11:15 +08:00
.onafterchange(function (tarEl){
Cookies.set('current_tutorial_step', this._currentStep + 15);
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', samplesTab.find("a").attr('href'));
$(".introjs-disableInteraction").remove();
positionTutorialTooltip();
}, 500);
} else {
positionTutorialTooltip();
}
})
.goToStep(currentStep == 16 ? 2 : 1)
2016-02-12 23:52:43 +08:00
.start();
2016-07-21 19:11:15 +08:00
window.onresize = positionTutorialTooltip;
2016-02-12 23:52:43 +08:00
// 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');
});
});
}
}
2016-07-21 19:11:15 +08:00
function positionTutorialTooltip() {
if (Cookies.get('current_tutorial_step') == 13) {
if ($("#module-samples-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 = $("#results").attr("data-module-id");
return tutorialModuleId == currentModuleId;
}
var ResultTypeEnum = Object.freeze({
FILE: 0,
TABLE: 1,
TEXT: 2,
COMMENT: 3
});
function processResult(ev, resultTypeEnum, editMode, forS3) {
forS3 = (typeof forS3 !== 'undefined') ? forS3 : false;
var $form = $(ev.target.form);
$form.clearFormErrors();
switch(resultTypeEnum) {
case ResultTypeEnum.FILE:
var $nameInput = $form.find("#result_name");
var nameValid = textValidator(ev, $nameInput, 0,
<%= Constants::NAME_MAX_LENGTH %>);
var $fileInput = $form.find("#result_asset_attributes_file");
var filesValid = filesValidator(ev, $fileInput, FileTypeEnum.FILE,
editMode);
if(nameValid && filesValid) {
if(forS3) {
// Redirects file uploading to S3
var url = '/asset_signature.json';
directUpload(ev, url);
} else {
// Local file uploading
animateSpinner();
}
}
break;
case ResultTypeEnum.TABLE:
var $nameInput = $form.find("#result_name");
var nameValid = textValidator(ev, $nameInput, 0,
<%= Constants::NAME_MAX_LENGTH %>);
break;
case ResultTypeEnum.TEXT:
var $nameInput = $form.find("#result_name");
var nameValid = textValidator(ev, $nameInput, 0,
<%= Constants::NAME_MAX_LENGTH %>);
var $textInput = $form.find("#result_result_text_attributes_text");
textValidator(ev, $textInput, 1, <%= Constants::TEXT_MAX_LENGTH %>);
break;
case ResultTypeEnum.COMMENT:
var $commentInput = $form.find("#comment_message");
var commentValid = textValidator(ev, $commentInput, 1,
<%= Constants::TEXT_MAX_LENGTH %>);
break;
}
}
2016-07-21 19:11:15 +08:00
// This checks if the ctarget param exist in the
// rendered url and opens the comment tab
$(document).ready(function(){
initHandsOnTables($(document));
expandAllResults();
initTutorial();
applyCollapseLinkCallBack();
Comments.bindNewElement();
Comments.initialize();
Comments.initCommentOptions("ul.content-comments");
Comments.initEditComments("#results");
Comments.initDeleteComments("#results");
$(function () {
$("#results-collapse-btn").click(function () {
$('.result .panel-collapse').collapse('hide');
$(document).find("span.collapse-result-icon").each(function() {
$(this).addClass("glyphicon-collapse-down");
$(this).removeClass("glyphicon-collapse-up");
});
});
$("#results-expand-btn").click(expandAllResults);
});
2016-07-21 19:11:15 +08:00
if( getParam('ctarget') ){
var target = "#"+ getParam('ctarget');
$(target).find('a.comment-tab-link').click();
}
});