STARK-web/static/js/init.js

126 lines
4.1 KiB
JavaScript

// Global array to store input names
var globalInputList = ['tree_size_min', 'tree_size_max', 'file', 'association_measures', 'labeled_trees', 'node_type_upos', 'fixed_order', 'input_url', 'node_type_lemma', 'root_restriction', 'node_type_form'];
// Function to store values to local storage
function storeValuesToLocalstorage() {
globalInputList.forEach(function(inputName) {
var inputElement = $('[name="' + inputName + '"]');
if (inputElement.length > 0) {
var inputType = inputElement.attr('type');
if (inputType === 'text' | inputType === 'hidden') {
localStorage.setItem(inputName, inputElement.val());
} else if (inputType === 'checkbox') {
localStorage.setItem(inputName, inputElement.prop('checked'));
}
}
});
}
// Function to read values from local storage
function readValuesFromLocalstorage() {
globalInputList.forEach(function(inputName) {
var inputElement = $('[name="' + inputName + '"]');
if (inputElement.length > 0) {
var inputType = inputElement.attr('type');
if (inputType === 'text') {
var text_val = localStorage.getItem(inputName);
if (text_val !== '' & text_val !== null) {
// set label to active
$("label[for='" + inputElement.attr('id') + "']").addClass('active');
}
inputElement.val(text_val);
} else if (inputType === 'checkbox') {
var check_value = localStorage.getItem(inputName);
if (check_value !== null) {
inputElement.prop('checked', check_value === 'true');
}
}
}
});
var tree_size_min = localStorage.getItem('tree_size_min') !== null ? localStorage.getItem('tree_size_min') : 2;
var tree_size_max = localStorage.getItem('tree_size_max') !== null ? localStorage.getItem('tree_size_max') : 3;
return [tree_size_min, tree_size_max]
}
document.addEventListener("DOMContentLoaded", function(event) {
tree_size = readValuesFromLocalstorage()
var valuesForSlider = [2,3,4,5];
var format = {
to: function(value) {
return valuesForSlider[Math.round(value)];
},
from: function (value) {
return valuesForSlider.indexOf(Number(value));
}
};
var slider = document.getElementById('slider');
noUiSlider.create(slider, {
start: [tree_size[0], tree_size[1]],
connect: true,
tooltips: true,
step: 1,
range: {
'min': 0,
'max': valuesForSlider.length - 1
},
format: format
});
});
(function($){
$(function(){
$('.sidenav').sidenav();
}); // end of document ready
$(document).ready(function(){
$('.input-field input[type="checkbox"]').on('change', function() {
var isChecked = $('.input-field input[type="checkbox"]:checked').length > 0;
$('#node-type-error').hide();
});
$('#submit-form input').on('change', function() {
$('#unknown-error').hide();
});
$('#advanced-tree').hide();
var advancedTreeExpanded = false;
$('#advanced-tree-expand').bind('click', function(e) {
if (!advancedTreeExpanded){
advancedTreeExpanded = true;
$('#advanced-tree').show('fast');
$('#advanced-tree-expand i').text('remove');
} else {
advancedTreeExpanded = false;
$('#advanced-tree').hide('fast');
$('#advanced-tree-expand i').text('add');
}
});
});
$("#submit-form").submit( function(eventObj) {
var spans = $(".noUi-tooltip").find('span');
var tree_size_min = spans[0].innerText;
var tree_size_max = spans[1].innerText;
console.log('amm');
$("<input />").attr("type", "hidden")
.attr("name", "tree_size_min")
.attr("value", tree_size_min)
.appendTo("#submit-form");
$("<input />").attr("type", "hidden")
.attr("name", "tree_size_max")
.attr("value", tree_size_max)
.appendTo("#submit-form");
storeValuesToLocalstorage();
return true;
});
})(jQuery); // end of jQuery name space