Add support and websocket to transfers.html

This commit is contained in:
Ludovic Stephan 2017-03-18 22:35:47 -03:00
parent fa64a68378
commit 951932a6c8
5 changed files with 32 additions and 144 deletions

View file

@ -165,6 +165,9 @@ class History {
} }
if (opegroup['add']) { if (opegroup['add']) {
if (opegroup.type === 'opegroup' && this.api_options.transfersonly)
return;
this.add_node(opegroup); this.add_node(opegroup);
} }
} }

View file

@ -143,6 +143,17 @@ function getErrorsHtml(data) {
return content; return content;
} }
function displayErrors(html) {
$.alert({
title: 'Erreurs',
content: html,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
});
}
function requestAuth(data, callback, focus_next = null) { function requestAuth(data, callback, focus_next = null) {
var content = getErrorsHtml(data); var content = getErrorsHtml(data);
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>',

View file

@ -53,8 +53,8 @@
<div class="content-right-block"> <div class="content-right-block">
<h2>Opérations</h2> <h2>Opérations</h2>
<div> <div>
<table id="history" class="table"> <div id="history" class="table">
</table> </div>
</div> </div>
</div> </div>
</div> </div>
@ -146,7 +146,7 @@ $(document).ready(function() {
var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host;
var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/"); var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/");
socket.onmessage = function(e) { socket.onmessage = function(e) {
data = $.extend({}, websocket_msg_default, JSON.parse(e.data)); var data = $.extend({}, websocket_msg_default, JSON.parse(e.data));
history.update_data(data); history.update_data(data);
} }

View file

@ -208,21 +208,6 @@ $(document).ready(function() {
$('#id_comment').val(''); $('#id_comment').val('');
} }
// -----
// Errors ajax
// -----
function displayErrors(html) {
$.alert({
title: 'Erreurs',
content: html,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
});
}
// ----- // -----
// Perform operations // Perform operations
// ----- // -----

View file

@ -10,7 +10,9 @@
<script type="text/javascript" src="{% static 'kfet/js/moment.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/moment.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script>
<script type="text/javascript" src="{% url 'js_reverse' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.api.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script> <script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script>
{% endblock %} {% endblock %}
@ -23,6 +25,8 @@
<div class="col-sm-4 col-md-3 col-content-left"> <div class="col-sm-4 col-md-3 col-content-left">
<div class="content-left"> <div class="content-left">
<div class="content-left-top"> <div class="content-left-top">
<div class="line line-big" id="nb_opes"></div>
<div class="line line-bigsub">transferts</div>
</div> </div>
<div class="buttons"> <div class="buttons">
<a class="btn btn-primary btn-lg" href="{% url 'kfet.transfers.create' %}"> <a class="btn btn-primary btn-lg" href="{% url 'kfet.transfers.create' %}">
@ -36,8 +40,8 @@
<div class="content-right"> <div class="content-right">
<div class="content-right-block"> <div class="content-right-block">
<h2>Liste des transferts</h2> <h2>Liste des transferts</h2>
<table id="history" class="table"> <div id="history" class="table">
</table> </div>
</div> </div>
</div> </div>
</div> </div>
@ -46,146 +50,31 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
'use strict';
lock = 0; // Lock to avoid multiple requests
window.lock = 0;
function displayErrors(html) { var history = new History({'transfersonly': true}, {});
$.alert({
title: 'Erreurs',
content: html,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
});
}
khistory = new KHistory({
display_trigramme: false,
});
function getHistory() {
var data = {'transfersonly': true};
$.ajax({
dataType: "json",
url : "{% url 'kfet.history.json' %}",
method : "POST",
data : data,
})
.done(function(data) {
for (var i=0; i<data['opegroups'].length; i++) {
khistory.addOpeGroup(data['opegroups'][i]);
}
});
}
khistory.$container.selectable({
filter: 'div.opegroup, div.ope',
selected: function(e, ui) {
$(ui.selected).each(function() {
if ($(this).hasClass('opegroup')) {
var type = $(this).data('type');
var id = $(this).data('id');
$(this).siblings('.ope').filter(function() {
return $(this).data(type) == id
}).addClass('ui-selected');
}
});
},
});
$(document).on('keydown', function (e) {
if (e.keyCode == 46) {
// DEL (Suppr)
var opes_to_cancel = [];
khistory.$container.find('.ope.ui-selected').each(function () {
opes_to_cancel.push($(this).data('type')+' '+$(this).data('id'));
});
if (opes_to_cancel.length > 0)
confirmCancel(opes_to_cancel);
}
});
function confirmCancel(opes_to_cancel) {
var nb = opes_to_cancel.length;
var content = nb+' opération'.pluralize(nb)
+' va'.pluralize(nb, ' vont')
+ ' être'
+ ' annulée'.pluralize(nb);
$.confirm({
title: 'Confirmation',
content: content,
backgroundDismiss: true,
animation: 'top',
closeAnimation: 'bottom',
keyboardEnabled: true,
confirm: function() {
cancelOperations(opes_to_cancel);
}
});
}
function cancelOperations(opes_array, password = '') {
if (lock == 1)
return false
lock = 1 ;
var data = { 'operations' : opes_array }
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.cancel_operations' %}",
method : "POST",
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
})
.done(function(data) {
khistory.$container.find('.ui-selected').removeClass('ui-selected');
lock = 0;
})
.fail(function($xhr) {
var data = $xhr.responseJSON;
switch ($xhr.status) {
case 403:
requestAuth(data, function(password) {
cancelOperations(opes_array, password);
});
break;
case 400:
displayErrors(getErrorsHtml(data));
break;
}
lock = 0;
});
}
// ----- // -----
// Synchronization // Synchronization
// ----- // -----
websocket_msg_default = {'opes':[]} var websocket_msg_default = {'opes':[]}
var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws';
var location_host = window.location.host; var location_host = window.location.host;
var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host;
socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/"); var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/");
socket.onmessage = function(e) { socket.onmessage = function(e) {
data = $.extend({}, websocket_msg_default, JSON.parse(e.data)); var data = $.extend({}, websocket_msg_default, JSON.parse(e.data));
history.update_data(data);
for (var i=0; i<data['opes'].length; i++) {
if (data['opes'][i]['cancellation']) {
khistory.cancelOpe(data['opes'][i]);
}
}
} }
getHistory(); history.reset();
}); });
</script> </script>