108 lines
4.0 KiB
JavaScript
108 lines
4.0 KiB
JavaScript
function setCookie(name, value) {
|
|
var end = new Date();
|
|
end.setTime( end.getTime() + 3600 * 1000 );
|
|
var str=name+"="+escape(value)+"; expires="+end.toGMTString()+"; path=/";
|
|
document.cookie = str;
|
|
}
|
|
function getCookie(name) {
|
|
var debut = document.cookie.indexOf(name);
|
|
if( debut == -1 ) return null;
|
|
var end = document.cookie.indexOf( ";", debut+name.length+1 );
|
|
if( end == -1 ) end = document.cookie.length;
|
|
return unescape( document.cookie.substring( debut+name.length+1, end ) );
|
|
}
|
|
|
|
/*
|
|
Flash messages
|
|
TODO: Find a way to have good flash messages in a KISS & DRY way
|
|
*/
|
|
function flash_add(type, message) {
|
|
template = `<div class="flash {{ category }}" style="top: {{ top }}px;" onclick="flash_close(this)">
|
|
<svg style='width:24px;height:24px' viewBox='0 0 24 24'>
|
|
{{ icon }}
|
|
</svg>
|
|
<span>
|
|
{{ message }}
|
|
</span>
|
|
<input type="button" value="MASQUER"></input>
|
|
</div>`;
|
|
paths = {
|
|
'error': '<path fill="#727272" d="M11,15H13V17H11V15M11,7H13V13H11V7M12,2C6.47,2 2,6.5 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20Z"></path>',
|
|
'warning': '<path fill="#727272" d="M12,2L1,21H23M12,6L19.53,19H4.47M11,10V14H13V10M11,16V18H13V16"></path>',
|
|
'ok': '<path fill="#727272" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M11,16.5L6.5,12L7.91,10.59L11,13.67L16.59,8.09L18,9.5L11,16.5Z"></path>',
|
|
'info': '<path fill="#727272" d="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z"></path>'
|
|
};
|
|
var top = (document.getElementsByClassName('flash').length + 1) * 70 - 45;
|
|
template = template.replace("{{ category }}", type);
|
|
template = template.replace("{{ top }}", top);
|
|
template = template.replace("{{ icon }}", paths[type]);
|
|
template = template.replace("{{ message }}", message);
|
|
document.body.innerHTML += template;
|
|
}
|
|
function flash_close(element) {
|
|
element.style.opacity = 0;
|
|
setTimeout(function(){
|
|
var parent = element.parentNode;
|
|
parent.removeChild(element);
|
|
var childs = parent.getElementsByClassName('flash');
|
|
for(var i = 0; i < childs.length; i++) {
|
|
childs[i].style.top = ((i + 1) * 70 - 45) + 'px';
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
/*
|
|
Send post ajax request to url defined in action.
|
|
Callback the function defined in the callback attribute from the submit type.
|
|
*/
|
|
/* We don't need Ajax at that time. Maybe later
|
|
function ajaxWrapper(evt){
|
|
evt.preventDefault();
|
|
var elems = evt.target;
|
|
var params = "";
|
|
// do not embed submit value (-1)
|
|
for(i = 0; i < elems.length-1; i++){
|
|
if(params) params += "&";
|
|
params += encodeURIComponent(elems[i].name)+"="+encodeURIComponent(elems[i].value);
|
|
}
|
|
const req = new XMLHttpRequest();
|
|
req.open("POST", evt.target.action, true);
|
|
req.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
|
|
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
|
req.onreadystatechange = function(){
|
|
if(req.readyState == 4 && (req.status == 200 || req.status == 0)){
|
|
var fn = window[elems[elems.length-1].getAttribute("callback")];
|
|
if(typeof fn == 'function'){
|
|
fn(req.responseText);
|
|
}
|
|
}
|
|
}
|
|
req.send(params);
|
|
}
|
|
|
|
// Add event listener on submit for all form with class with-ajax.
|
|
|
|
window.onload = function(){
|
|
|
|
var ele;
|
|
var elems = document.getElementsByClassName('with-ajax');
|
|
for(i = 0; i < elems.length; i++){
|
|
ele = elems[i];
|
|
if(ele.addEventListener){ // Normal people
|
|
ele.addEventListener("submit", ajaxWrapper, false);
|
|
}else if(ele.attachEvent){ // Retarded user using IE
|
|
ele.attachEvent("onsubmit", ajaxWrapper);
|
|
}
|
|
}
|
|
|
|
if(getCookie('pc_notif') == 'true')
|
|
document.getElementsByClassName('alert')[0].parentNode.removeChild(document.getElementsByClassName('alert')[0]);
|
|
if(getCookie('pc_notif_2') == 'true')
|
|
document.getElementsByClassName('alert')[0].parentNode.removeChild(document.getElementsByClassName('alert')[0]);
|
|
|
|
}
|
|
|
|
function login(response){
|
|
alert(response);
|
|
}
|
|
//*/ |