Verified Commit 9566838a authored by Štěpán Henek's avatar Štěpán Henek 🌩

subordinates: UI improvements

parent e06fd492
......@@ -544,57 +544,6 @@ class SubordinatesConfigPage(ConfigPageMixin, subordinates.SubordinatesConfigHan
template_adapter=bottle.Jinja2Template,
)
def _ajax_update_sub(self):
controller_id = self._check_and_get_controller_id()
form = self.get_subordinate_update_form(bottle.request.POST.decode())
form.save()
if form.callback_results["result"]:
message = {
"classes": ['success'],
"text": _("Subordinate '%(controller_id)s' was sucessfully updated.")
% dict(controller_id=controller_id)
}
else:
message = {
"classes": ['error'],
"text": _("Failed to update subordinate '%(controller_id)s'.")
% dict(controller_id=controller_id)
}
res = template(
"config/_subordinates_message.html.j2",
message=message,
dom_id="edit-form-message",
template_adapter=bottle.Jinja2Template,
)
return res
def _ajax_update_subsub(self):
controller_id = self._check_and_get_controller_id()
form = self.get_subsubordinate_update_form(bottle.request.POST.decode())
form.save()
if form.callback_results["result"]:
message = {
"classes": ['success'],
"text": _("Subsubordinate '%(controller_id)s' was sucessfully updated.")
% dict(controller_id=controller_id)
}
else:
message = {
"classes": ['error'],
"text": _("Failed to update subsubordinate '%(controller_id)s'.")
% dict(controller_id=controller_id)
}
return template(
"config/_subordinates_message.html.j2",
message=message,
dom_id="edit-form-message",
template_adapter=bottle.Jinja2Template,
)
def call_ajax_action(self, action):
if action == "list":
return self._ajax_list_subordinates()
......@@ -604,10 +553,6 @@ class SubordinatesConfigPage(ConfigPageMixin, subordinates.SubordinatesConfigHan
return self._ajax_set_enabled(True)
elif action == "delete":
return self._ajax_delete()
elif action == "update_sub":
return self._ajax_update_sub()
elif action == "update_subsub":
return self._ajax_update_subsub()
raise ValueError("Unknown AJAX action.")
@classmethod
......
......@@ -2,7 +2,7 @@
<form action="{{ url("config_ajax_form", page_name="subordinates", form_name=form_name) }}" method="post" class="subordinate-buttons">
<input type="hidden" name="controller_id" value="{{ controller_id }}">
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
<button name="action" value="edit" type="submit"><i class="fas fa-edit"></i></button>
<button name="action" value="edit" type="submit" style="display: none"><i class="fas fa-edit"></i></button>
</form>
{% endmacro -%}
{% macro sub_buttons(controller_id, enabled, form_class) -%}
......
<div id="subordinates-edit" {% if hide %}style="display: none"{% endif %}>
<div id="subordinates-edit" {% if hide %}style="display: none"{% endif %} data-controller-id="{{ ajax_form.controller_id }}">
<h3>{{ ajax_form.title }}</h3>
{% include 'config/_message.html.j2' %}
<form id="form-{{ form.name }}" class="config-form" action="{{ ajax_form.url }}" method="post" autocomplete="off" novalidate>
<p class="config-description">{{ form.sections[0].description|safe }}</p>
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
......
......@@ -64,6 +64,10 @@
Foris.setSubordinatesTimeouts();
}
Foris.setSubordinatesTimeouts = () => {
for (timeout in Foris.subordinateKeepAliveTimeouts) {
clearTimeout(Foris.subordinateKeepAliveTimeouts[timeout]);
}
Foris.subordinateKeepAliveTimeouts = {};
$(".sub-connection").each((idx, val) => {
let id = $(val).attr("id");
if (id) {
......@@ -75,15 +79,22 @@
$("#subordinates-edit form").submit(async (e) => {
e.preventDefault();
let form = $(e.currentTarget);
resp = await $.ajax({
resp = $.ajax({
type: "POST",
url: form.attr('action'),
data: form.serialize()
});
$("#subordinates-edit").replaceWith(resp);
await Foris.suboridnatesOverrideEditClose();
await Foris.loadSubordinatesList();
$(".subordinate-buttons button").hide();
form.find("input, select, button").attr("disabled", "disabled");
try {
let new_form = await resp;
$("#subordinates-edit").replaceWith(new_form);
await Foris.suboridnatesOverrideEditClose();
await Foris.overrideSubordinatesEditButton();
await Foris.loadSubordinatesList();
$(".subordinate-buttons button").hide();
} catch(err) {
alert(err);
}
});
};
Foris.overrideSubordinatesButtons = async () => {
......@@ -151,7 +162,7 @@
data: `&csrf_token=${form_data["csrf_token"]}&_controller_id=${form_data["controller_id"]}&_update=1`,
});
}
Foris.subordinateInEditMode = true;
await $("#subordinates-edit").promise();
$("#subordinates-edit").replaceWith(resp);
$(".subordinate-buttons button").hide('slow');
......@@ -167,14 +178,18 @@
};
Foris.suboridnatesOverrideEditClose = async () => {
$("#subordinates-edit").find("a.button").click(async (e) => {
Foris.subordinateInEditMode = false;
e.preventDefault();
$("#subordinates-edit").hide('slow');
await $("#subordinates-edit").promise();
$("#subordinates-edit").replaceWith("<div id='subordinates-edit'></div>");
$(".subordinate-buttons button").show('slow');
await $(".subordinate-buttons button").promise();
if (Foris.subordinatesGetView() == "subordinates") {
$(".subordinate-buttons button").show('slow');
await $(".subordinate-buttons button").promise();
}
});
};
Foris.subordinateInEditMode = false;
Foris.subordinatesAlive = {};
Foris.subordinateKeepAliveTimeouts = {};
Foris.subordinateSetKeepAliveTimeout = (controller_id) => {
......@@ -185,6 +200,7 @@
state_element.removeClass();
state_element.addClass("state fas fa-exclamation-circle");
delete Foris.subordinateKeepAliveTimeouts[controller_id];
$(`div[data-controller-id=${controller_id}]`).replaceWith("<div id='subordinates-edit'>Chyba</div>");
}, 3000);
};
Foris.updateSubordinateState = async (data) => {
......@@ -208,6 +224,9 @@
Foris.queryBackendWifi(data.id);
}
Foris.subordinatesAlive[data.id] = true;
if (!Foris.subordinateInEditMode) {
$(`#sub-${data.id}`).find("button[value=edit]").show("slow");
}
break;
case 'exitted':
state_element.addClass("state fas fa-exclamation-circle");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment