Add generic functions for confirm dialogs
This commit is contained in:
parent
6afbcb44a1
commit
6be2f086df
1 changed files with 106 additions and 28 deletions
|
@ -89,8 +89,60 @@ function isValidTrigramme(trigramme) {
|
||||||
return trigramme.match(pattern);
|
return trigramme.match(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dialogs with user via jconfirm
|
||||||
|
*/
|
||||||
|
|
||||||
|
class UserDialog {
|
||||||
|
|
||||||
|
static get default_data() {
|
||||||
|
return {'title': '', 'content': '', 'callback_as_dict': false};
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
$.extend(this, this.constructor.default_data, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
open(callback, errors, next_focus) {
|
||||||
|
var that = this;
|
||||||
|
$.confirm({
|
||||||
|
title: this.title,
|
||||||
|
content: errors + this.content,
|
||||||
|
backgroundDismiss: true,
|
||||||
|
animation:'top',
|
||||||
|
closeAnimation:'bottom',
|
||||||
|
keyboardEnabled: true,
|
||||||
|
confirm: function() {
|
||||||
|
if (that.callback_as_dict) {
|
||||||
|
var inputs = {};
|
||||||
|
this.$content.find('input').each(function () {
|
||||||
|
inputs[$(this).attr('name')] = $(this).val();
|
||||||
|
});
|
||||||
|
return callback(inputs);
|
||||||
|
} else {
|
||||||
|
var input_values = this.$content.find('input').map(function () {
|
||||||
|
return $(this).val();
|
||||||
|
}).get();
|
||||||
|
return callback.apply(null, input_values);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onOpen: function() {
|
||||||
|
var that = this
|
||||||
|
this.$content.find('input').on('keydown', function(e) {
|
||||||
|
if (e.keyCode == 13) {
|
||||||
|
e.preventDefault();
|
||||||
|
that.$confirmButton.click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onClose: function() { if (next_focus) { this._lastFocused = next_focus; } }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getErrorsHtml(data) {
|
function getErrorsHtml(data) {
|
||||||
var content = '';
|
var content = '';
|
||||||
|
console.log(data);
|
||||||
if ('operation_group' in data['errors']) {
|
if ('operation_group' in data['errors']) {
|
||||||
content += 'Général';
|
content += 'Général';
|
||||||
content += '<ul>';
|
content += '<ul>';
|
||||||
|
@ -128,27 +180,60 @@ function getErrorsHtml(data) {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
function requestAuth(data, callback, focus_next = null) {
|
var authDialog = new UserDialog({
|
||||||
var content = getErrorsHtml(data);
|
'title': 'Authentification requise',
|
||||||
content += '<div class="capslock"><span class="glyphicon glyphicon-lock"></span><input type="password" name="password" autofocus><div>',
|
'content': '<div class="capslock"><span class="glyphicon glyphicon-lock"></span><input type="password" name="password" autofocus><div>',
|
||||||
$.confirm({
|
});
|
||||||
title: 'Authentification requise',
|
|
||||||
content: content,
|
|
||||||
backgroundDismiss: true,
|
|
||||||
animation:'top',
|
|
||||||
closeAnimation:'bottom',
|
|
||||||
keyboardEnabled: true,
|
|
||||||
confirm: function() {
|
|
||||||
var password = this.$content.find('input').val();
|
|
||||||
callback(password);
|
|
||||||
},
|
|
||||||
onOpen: function() {
|
|
||||||
var that = this;
|
|
||||||
var capslock = -1 ; // 1 -> caps on ; 0 -> caps off ; -1 or 2 -> unknown
|
|
||||||
this.$content.find('input').on('keypress', function(e) {
|
|
||||||
if (e.keyCode == 13)
|
|
||||||
that.$confirmButton.click();
|
|
||||||
|
|
||||||
|
function api_with_auth(settings, password) {
|
||||||
|
if (window.lock == 1)
|
||||||
|
return false;
|
||||||
|
window.lock = 1;
|
||||||
|
|
||||||
|
var url = settings.url;
|
||||||
|
if (!url)
|
||||||
|
return false;
|
||||||
|
var data = settings.data || {} ;
|
||||||
|
var on_success = settings.on_success || $.noop ;
|
||||||
|
var on_400 = settings.on_400 || $.noop ;
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
dataType: "json",
|
||||||
|
url: url,
|
||||||
|
method: "POST",
|
||||||
|
data: data,
|
||||||
|
beforeSend: function ($xhr) {
|
||||||
|
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
|
||||||
|
if (password)
|
||||||
|
$xhr.setRequestHeader("KFetPassword", password);
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.done(function(data) {
|
||||||
|
on_success(data);
|
||||||
|
window.lock = 0 ;
|
||||||
|
})
|
||||||
|
.fail(function($xhr) {
|
||||||
|
var response = $xhr.responseJSON;
|
||||||
|
switch ($xhr.status) {
|
||||||
|
case 403:
|
||||||
|
authDialog.open(
|
||||||
|
function(password) {
|
||||||
|
api_with_auth(settings, password)
|
||||||
|
},
|
||||||
|
getErrorsHtml(response),
|
||||||
|
settings.next_focus
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case 400:
|
||||||
|
on_400(response);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
window.lock = 0 ;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: add capslock to all needed password inputs
|
||||||
|
*
|
||||||
var s = String.fromCharCode(e.which);
|
var s = String.fromCharCode(e.which);
|
||||||
if ((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey)|| //caps on, shift off
|
if ((s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey)|| //caps on, shift off
|
||||||
(s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)) { //caps on, shift on
|
(s.toUpperCase() !== s && s.toLowerCase() === s && e.shiftKey)) { //caps on, shift on
|
||||||
|
@ -172,11 +257,4 @@ function requestAuth(data, callback, focus_next = null) {
|
||||||
else if (capslock == 0)
|
else if (capslock == 0)
|
||||||
$('.capslock .glyphicon').hide() ;
|
$('.capslock .glyphicon').hide() ;
|
||||||
});
|
});
|
||||||
},
|
*/
|
||||||
onClose: function() {
|
|
||||||
if (focus_next)
|
|
||||||
this._lastFocused = focus_next;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue