luci-base: cbi.js: untangle dropdown and dynlists from dependency update

In order to prepare the move of the dynlist and dropdown widgets into
the LuCI ui class, remove the direct calls to cbi_d_update() and replace
them with custom events instead.

Extend cbi_init() to handle these custom events and to invoke
cbi_d_update() when receiving them.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-02-05 10:39:17 +01:00
parent 1b931e0262
commit 6b8701c7bf

View file

@ -799,6 +799,7 @@ function cbi_init() {
} }
cbi_dynlist_init(node, choices[2], choices[3], options); cbi_dynlist_init(node, choices[2], choices[3], options);
node.addEventListener('cbi-dynlist-change', cbi_d_update);
} }
nodes = document.querySelectorAll('[data-type]'); nodes = document.querySelectorAll('[data-type]');
@ -808,7 +809,11 @@ function cbi_init() {
node.getAttribute('data-type')); node.getAttribute('data-type'));
} }
document.querySelectorAll('.cbi-dropdown').forEach(cbi_dropdown_init); document.querySelectorAll('.cbi-dropdown').forEach(function(node) {
cbi_dropdown_init(node);
node.addEventListener('cbi-dropdown-change', cbi_d_update);
});
document.querySelectorAll('[data-browser]').forEach(cbi_browser_init); document.querySelectorAll('[data-browser]').forEach(cbi_browser_init);
document.querySelectorAll('.cbi-tooltip:not(:empty)').forEach(function(s) { document.querySelectorAll('.cbi-tooltip:not(:empty)').forEach(function(s) {
@ -915,22 +920,37 @@ CBIDynamicList = {
exists = !!item.parentNode.insertBefore(new_item, item); exists = !!item.parentNode.insertBefore(new_item, item);
}); });
cbi_d_update(); dl.dispatchEvent(new CustomEvent('cbi-dynlist-change', {
bubbles: true,
detail: {
instance: this,
element: dl,
value: value,
add: true
}
}));
}, },
removeItem: function(dl, item) { removeItem: function(dl, item) {
var value = item.querySelector('input[type="hidden"]').value;
var sb = dl.querySelector('.cbi-dropdown'); var sb = dl.querySelector('.cbi-dropdown');
if (sb) { if (sb)
var value = item.querySelector('input[type="hidden"]').value;
sb.querySelectorAll('ul > li').forEach(function(li) { sb.querySelectorAll('ul > li').forEach(function(li) {
if (li.getAttribute('data-value') === value) if (li.getAttribute('data-value') === value)
li.removeAttribute('unselectable'); li.removeAttribute('unselectable');
}); });
}
item.parentNode.removeChild(item); item.parentNode.removeChild(item);
cbi_d_update();
dl.dispatchEvent(new CustomEvent('cbi-dynlist-change', {
bubbles: true,
detail: {
instance: this,
element: dl,
value: value,
remove: true
}
}));
}, },
handleClick: function(ev) { handleClick: function(ev) {
@ -1671,8 +1691,6 @@ CBIDropdown = {
bubbles: true, bubbles: true,
detail: detail detail: detail
})); }));
cbi_d_update();
}, },
setValues: function(sb, values) { setValues: function(sb, values) {