Manage default value for client selection

This commit is contained in:
Cédric Krier
2024-07-02 15:57:48 +02:00
parent 6b15a5c680
commit 5b23e66b10
2 changed files with 15 additions and 7 deletions
+7 -3
View File
@@ -127,7 +127,8 @@
return result;
};
Sao.common.selection = function(title, values, alwaysask=false) {
Sao.common.selection = function(
title, values, alwaysask=false, default_=null) {
var prm = jQuery.Deferred();
if (jQuery.isEmptyObject(values)) {
prm.reject();
@@ -152,11 +153,14 @@
.prepend(jQuery('<input/>', {
'type': 'radio',
'name': 'selection-' + dialog.modal.attr('id'),
'value': i
'value': i,
'checked': values[keys[i]] == default_,
})))
.appendTo(dialog.body);
});
dialog.body.find('input').first().prop('checked', true);
if (!default_) {
dialog.body.find('input').first().prop('checked', true);
}
jQuery('<button/>', {
'class': 'btn btn-link',
+8 -4
View File
@@ -400,7 +400,7 @@ def get_sensible_widget(window):
return window
def selection(title, values, alwaysask=False):
def selection(title, values, alwaysask=False, default=None):
if not values or len(values) == 0:
return None
elif len(values) == 1 and (not alwaysask):
@@ -439,14 +439,18 @@ def selection(title, values, alwaysask=False):
model = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_INT)
keys = list(values.keys())
keys.sort()
i = 0
for val in keys:
selected = None
for i, val in enumerate(keys):
model.append([str(val), i])
i += 1
if default:
if values[keys[i]] == default:
selected = i
treeview.set_model(model)
treeview.connect('row-activated',
lambda x, y, z: dialog.response(Gtk.ResponseType.OK) or True)
if selected is not None:
treeview.get_selection().select_path(selected)
dialog.show_all()
response = dialog.run()