covas-bo/web/js/library.js

126 lines
3.4 KiB
JavaScript

function checkToken(){
instance.get("token", { withCredentials: true})
.then(function(response) {
if(location.pathname == "/")
{
location.href="/html/users.html";
}
})
.catch(function(error){
if(location.pathname != "/"){
location.href="/";
}
if(error.response.status == 406){
instance.delete("token" , {withCredentials:true}).then(function(response){
location.reload();
});
}
});
}
function buildParam(e, id){
var search = document.getElementById("searchUser").value;
var param = "";
var listParam = ["page", "nbPages"];
if(id.length > 0){
var select = document.getElementById(id);
var getId = select.options[select.selectedIndex].value;
if(id == "inputRoles"){
var status = e.target.value;
if(status != "-2"){
param = param + "status="+status+"&";
}
if(getId != "All"){
param = param + "roles="+getId;
}
}
if(id == "inputStatus"){
var roles = e.target.value;
if(getId != "-2"){
param = param + "status="+getId+"&";
}
if(roles != "All"){
param = param + "roles="+roles+"&";
}
}
} else {
listParam.push("roles");
listParam.push("status");
}
if(search.length > 0){
if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(search)){
param = param + "email="+search+"&";
} else {
param = param + "id="+search+"&";
}
} else {
listParam.push("email"); listParam.push("id");
}
for (var i=0; i<listParam.length; i++){
var query = getQuery(listParam[i]);
if(query.length > 0){
param = param + listParam[i]+"="+query+"&";
}
}
return param;
}
function signOut(){
document.getElementById("signout").addEventListener("click", function(){
instance.delete("token" , {withCredentials:true}).then(function(response){
if(response.status == 200) {
location.href="/";
}
});
});
}
function closeButton(){
var btnclose = document.getElementsByClassName("btn-close");
for (var i = 0; i<btnclose.length; i++){
btnclose[i].addEventListener("click", function(e){
var parent = e.currentTarget.parentElement;
if(!parent.classList.contains("hidden")){
parent.classList.add("hidden");
}
});
}
}
function getQuery(param){
var result = "";
if(location.search.length > 0){
query = location.search.split("?")[1];
var variables = query.split("&")
for (var i=0; i<variables.length; i++){
if(variables[i].split("=")[0] == param){
result = variables[i].split("=")[1];
}
}
}
return result;
}
function updateParam(param, value){
var result = "";
if(location.search.length > 0){
query = location.search.split("?")[1];
var variables = query.split("&");
for (var i=0; i<variables.length; i++){
var data = variabes[i];
if(variables[i].split("=")[0] == param){
data = param+"="+value;
}
result = result + "&" + data;
}
}
return result;
}