vmailadmin/resources/js/lib/handlers.js
2020-05-23 15:20:01 +02:00

105 lines
3.7 KiB
JavaScript
Vendored

define("handlers",['lib/flash','jquery'],function(flash,$){
function elFind(element,types)
{
if(undefined == types)
{
types = ["A","BUTTON"]
}
if(!types.includes('HTML')){ types.push('HTML');}
let el = element;
while(!(types.includes(el.tagName))){
el = el.parentElement;
}
if(el.tagName !== 'HTML'){
return el;
} else {
return element;
}
}
function mkErrorList(errors,indexed)
{
indexed = !!indexed;
let msg = "<ul class='errortooltip'>";
for(let i in errors){
if(indexed){
msg += `<li>${i}: ${errors[i]}</li>`;
} else{
msg += `<li>${errors[i]}</li>`;
}
}
msg += "</ul>";
return msg;
}
let my = {
fielderror: function(eventsrc, duration, eltypes){
if(undefined == duration) { duration = 5000;}
if(undefined == eltypes) { eltypes = ["INPUT","TEXTAREA","SELECT"];}
return function(error){
let el = elFind(eventsrc,eltypes);
let errors = error;
try{ errors = mkErrorList(error.response.data.errors,true); }
catch { console.warn("Unexpected error",error);}
flash.class(el,['border-danger','text-danger'],duration);
if(undefined !== el.getAttribute('n-field') && errors.hasOwnProperty(el.getAttribute('n-field')))
{
flash.tooltip(el,errors[el.getAttribute('n-field')],duration,'top');
}
else
{
flash.tooltip(el,errors,duration,'top');
}
};
},
buttonerror: function(eventsrc, duration){
if(undefined == duration) { duration = 5000;}
return function(error){
let el = elFind(eventsrc,["BUTTON"]);
let errors = error;
try{ errors = mkErrorList(error.response.data.errors); }
catch { console.warn("Unexpected error",error);}
flash.class(el,['btn-danger','tooltip-danger'],duration);
flash.tooltip(el,errors,duration);
};
},
linkerror: function(eventsrc, duration){
if(undefined == duration) { duration = 5000;}
return function(error){
let el = elFind(eventsrc,["A"]);
let errors = error;
try{ errors = mkErrorList(error.response.data.errors); }
catch { console.warn("Unexpected error",error);}
flash.class(el,'text-danger',duration);
flash.tooltip(el,errors,duration);
};
},
fieldsuccess: function(eventsrc, duration, eltypes){
if(undefined == duration) { duration = 3000;}
if(undefined == eltypes) { eltypes = ["INPUT","TEXTAREA","SELECT"];}
return function(response){
let el = elFind(eventsrc,eltypes);
flash.class(el,['border-success','text-success'],duration);
};
},
buttonsuccess: function(eventsrc, duration){
if(undefined == duration) { duration = 3000;}
return function(response){
let el = elFind(eventsrc,["BUTTON"]);
flash.class(el,'btn-success',duration);
};
},
linksuccess: function(eventsrc, duration){
if(undefined == duration) { duration = 3000;}
return function(response){
let el = elFind(eventsrc,["A"]);
flash.class(el,'text-success',duration);
};
},
}
return my;
});