gluon-web-model: add exclusive options for MultiListValue

This commit is contained in:
lemoer 2022-05-06 04:15:41 +02:00
parent 03b9ffdb24
commit 7805ff700e
6 changed files with 47 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -273,6 +273,10 @@ input[type=checkbox] {
text-align: center; text-align: center;
font-size: 1.7em; font-size: 1.7em;
} }
&[disabled] + label {
background-color: #dcdcdc !important;
}
} }
input[type=radio] { input[type=radio] {

View File

@ -13,7 +13,9 @@
attr("id", id.."."..entry.key) .. attr("id", id.."."..entry.key) ..
attr("name", id) .. attr("name", id) ..
attr("value", entry.key) .. attr("value", entry.key) ..
attr("checked", (util.contains(self:cfgvalue(), entry.key)) and "checked") attr("checked", (util.contains(self:cfgvalue(), entry.key)) and "checked") ..
attr("data-exclusive-with", self.exclusions[entry.key]) ..
attr("data-update", "change")
%>> %>>
<label<%= attr("for", id.."."..entry.key)%>></label> <label<%= attr("for", id.."."..entry.key)%>></label>
<span class="gluon-multi-list-option-descr"><%|entry.value%></span> <span class="gluon-multi-list-option-descr"><%|entry.value%></span>

View File

@ -219,6 +219,20 @@
parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : ''; parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
} }
var nodes = document.querySelectorAll('[data-exclusive-with]');
for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
var exclusive_with = JSON.parse(node.getAttribute('data-exclusive-with'));
node.disabled = false;
for (var list_item of exclusive_with) {
var el = document.getElementById(node.name + '.' + list_item);
node.disabled ||= el.checked;
}
if (node.disabled)
node.checked = false;
}
if (state) { if (state) {
update(); update();
} }

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local util = require "gluon.web.util" local util = require "gluon.web.util"
local gluon_util = require "gluon.util"
local datatypes = require "gluon.web.model.datatypes" local datatypes = require "gluon.web.model.datatypes"
local class = util.class local class = util.class
@ -381,6 +382,7 @@ function MultiListValue:value(key, val, ...)
return return
end end
self.keys[key] = true self.keys[key] = true
self.exclusions = {}
val = val or key val = val or key
table.insert(self.entry_list, { table.insert(self.entry_list, {
@ -403,9 +405,31 @@ function MultiListValue:validate()
end end
end end
for key, exclusive_with in pairs(self.exclusions) do
if gluon_util.contains(self.data, key) then
for _, exclusion in ipairs(exclusive_with) do
if gluon_util.contains(self.data, exclusion) then
return false
end
end
end
end
return true return true
end end
function MultiListValue:exclusive(a, b)
if not self.exclusions[a] then
self.exclusions[a] = {}
end
if not self.exclusions[b] then
self.exclusions[b] = {}
end
gluon_util.add_to_set(self.exclusions[a], b)
gluon_util.add_to_set(self.exclusions[b], a)
end
function MultiListValue:defaultvalue() function MultiListValue:defaultvalue()
return self.default or {} return self.default or {}
end end