luci-base: rework filebrowser initialization

Do not call cbi_init() from the browser field template but lazily initialize
the field, like it is being done for all other widgets as well.

Fixes: #2398
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2018-12-29 14:19:59 +01:00
parent 8cecff2197
commit ed914df3f5
2 changed files with 22 additions and 25 deletions

View file

@ -808,9 +808,8 @@ function cbi_init() {
node.getAttribute('data-type')); node.getAttribute('data-type'));
} }
document.querySelectorAll('.cbi-dropdown').forEach(function(s) { document.querySelectorAll('.cbi-dropdown').forEach(cbi_dropdown_init);
cbi_dropdown_init(s); 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) {
s.parentNode.classList.add('cbi-tooltip-container'); s.parentNode.classList.add('cbi-tooltip-container');
@ -872,30 +871,26 @@ function cbi_combobox_init(id, values, def, man) {
} }
function cbi_filebrowser(id, defpath) { function cbi_filebrowser(id, defpath) {
var field = document.getElementById(id); var field = L.dom.elem(id) ? id : document.getElementById(id);
var browser = window.open( var browser = window.open(
cbi_strings.path.browser + ( field.value || defpath || '' ) + '?field=' + id, cbi_strings.path.browser + (field.value || defpath || '') + '?field=' + field.id,
"luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes" "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
); );
browser.focus(); browser.focus();
} }
function cbi_browser_init(id, resource, defpath) function cbi_browser_init(field)
{ {
function cbi_browser_btnclick(e) { field.parentNode.insertBefore(
cbi_filebrowser(id, defpath); E('img', {
return false; 'src': L.resource('cbi/folder.gif'),
} 'class': 'cbi-image-button',
'click': function(ev) {
var field = document.getElementById(id); cbi_filebrowser(field, field.getAttribute('data-browser'));
ev.preventDefault();
var btn = document.createElement('img'); }
btn.className = 'cbi-image-button'; }), field.nextSibling);
btn.src = (resource || cbi_strings.path.resource) + '/cbi/folder.gif';
field.parentNode.insertBefore(btn, field.nextSibling);
btn.addEventListener('click', cbi_browser_btnclick);
} }
CBIDynamicList = { CBIDynamicList = {

View file

@ -1,8 +1,10 @@
<% local v = self:cfgvalue(section) or self.default -%>
<%+cbi/valueheader%> <%+cbi/valueheader%>
<input class="cbi-input-text" type="text"<%= attr("value", v) .. attr("name", cbid) .. attr("id", cbid) %> />
<script type="text/javascript"> <input class="cbi-input-text" type="text"<%=
cbi_init() attr("id", cbid) ..
cbi_browser_init('<%=cbid%>', '<%=resource%>', '<%=url('admin/filebrowser')%>'<%=self.default_path and ", '"..self.default_path.."'"%>); attr("name", cbid) ..
</script> attr("value", self:cfgvalue(section) or self.default) ..
attr("data-browser", self.default_path or "")
%> />
<%+cbi/valuefooter%> <%+cbi/valuefooter%>