luci-base: form.js: rework section add/remove event handling

- Redraw map even when it could not be saved due to validation errors
 - Use createHandlerFn() for attaching event handlers
 - Convert buttons to <button> element to allow use of spinning class

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-08-20 15:04:13 +02:00
parent b0dde567ca
commit 745878d6cd

View file

@ -132,18 +132,19 @@ var CBIMap = CBINode.extend({
return Promise.all(tasks); return Promise.all(tasks);
}, },
save: function(cb) { save: function(cb, silent) {
this.checkDepends(); this.checkDepends();
return this.parse() return this.parse()
.then(cb) .then(cb)
.then(uci.save.bind(uci)) .then(uci.save.bind(uci))
.then(this.load.bind(this)) .then(this.load.bind(this))
.then(this.renderContents.bind(this))
.catch(function(e) { .catch(function(e) {
alert('Cannot save due to invalid values') if (!silent)
alert('Cannot save due to invalid values');
return Promise.reject(); return Promise.reject();
}); }).finally(this.renderContents.bind(this));
}, },
reset: function() { reset: function() {
@ -658,14 +659,14 @@ var CBITypedSection = CBIAbstractSection.extend({
var config_name = this.uciconfig || this.map.config; var config_name = this.uciconfig || this.map.config;
uci.add(config_name, this.sectiontype, name); uci.add(config_name, this.sectiontype, name);
this.map.save(); return this.map.save(null, true);
}, },
handleRemove: function(section_id, ev) { handleRemove: function(section_id, ev) {
var config_name = this.uciconfig || this.map.config; var config_name = this.uciconfig || this.map.config;
uci.remove(config_name, section_id); uci.remove(config_name, section_id);
this.map.save(); return this.map.save(null, true);
}, },
renderSectionAdd: function(extra_class) { renderSectionAdd: function(extra_class) {
@ -680,13 +681,11 @@ var CBITypedSection = CBIAbstractSection.extend({
createEl.classList.add(extra_class); createEl.classList.add(extra_class);
if (this.anonymous) { if (this.anonymous) {
createEl.appendChild(E('input', { createEl.appendChild(E('button', {
'type': 'submit',
'class': 'cbi-button cbi-button-add', 'class': 'cbi-button cbi-button-add',
'value': btn_title || _('Add'),
'title': btn_title || _('Add'), 'title': btn_title || _('Add'),
'click': L.bind(this.handleAdd, this) 'click': L.ui.createHandlerFn(this, 'handleAdd')
})); }, btn_title || _('Add')));
} }
else { else {
var nameEl = E('input', { var nameEl = E('input', {
@ -701,12 +700,12 @@ var CBITypedSection = CBIAbstractSection.extend({
'type': 'submit', 'type': 'submit',
'value': btn_title || _('Add'), 'value': btn_title || _('Add'),
'title': btn_title || _('Add'), 'title': btn_title || _('Add'),
'click': L.bind(function(ev) { 'click': L.ui.createHandlerFn(this, function(ev) {
if (nameEl.classList.contains('cbi-input-invalid')) if (nameEl.classList.contains('cbi-input-invalid'))
return; return;
this.handleAdd(ev, nameEl.value); return this.handleAdd(ev, nameEl.value);
}, this) })
}) })
]); ]);
@ -743,14 +742,12 @@ var CBITypedSection = CBIAbstractSection.extend({
if (this.addremove) { if (this.addremove) {
sectionEl.appendChild( sectionEl.appendChild(
E('div', { 'class': 'cbi-section-remove right' }, E('div', { 'class': 'cbi-section-remove right' },
E('input', { E('button', {
'type': 'submit',
'class': 'cbi-button', 'class': 'cbi-button',
'name': 'cbi.rts.%s.%s'.format(config_name, cfgsections[i]), 'name': 'cbi.rts.%s.%s'.format(config_name, cfgsections[i]),
'value': _('Delete'),
'data-section-id': cfgsections[i], 'data-section-id': cfgsections[i],
'click': L.bind(this.handleRemove, this, cfgsections[i]) 'click': L.ui.createHandlerFn(this, 'handleRemove', cfgsections[i])
}))); }, _('Delete'))));
} }
if (!this.anonymous) if (!this.anonymous)
@ -1004,7 +1001,7 @@ var CBITableSection = CBITypedSection.extend({
'class': 'cbi-button cbi-button-remove', 'class': 'cbi-button cbi-button-remove',
'click': L.bind(function(sid, ev) { 'click': L.bind(function(sid, ev) {
uci.remove(config_name, sid); uci.remove(config_name, sid);
this.map.save(); this.map.save(null, true);
}, this, section_id) }, this, section_id)
}) })
); );
@ -1283,7 +1280,7 @@ var CBINamedSection = CBIAbstractSection.extend({
config_name = this.uciconfig || this.map.config; config_name = this.uciconfig || this.map.config;
uci.add(config_name, this.sectiontype, section_id); uci.add(config_name, this.sectiontype, section_id);
this.map.save(); return this.map.save(null, true);
}, },
handleRemove: function(ev) { handleRemove: function(ev) {
@ -1291,7 +1288,7 @@ var CBINamedSection = CBIAbstractSection.extend({
config_name = this.uciconfig || this.map.config; config_name = this.uciconfig || this.map.config;
uci.remove(config_name, section_id); uci.remove(config_name, section_id);
this.map.save(); return this.map.save(null, true);
}, },
renderContents: function(data) { renderContents: function(data) {
@ -1315,12 +1312,10 @@ var CBINamedSection = CBIAbstractSection.extend({
if (this.addremove) { if (this.addremove) {
sectionEl.appendChild( sectionEl.appendChild(
E('div', { 'class': 'cbi-section-remove right' }, E('div', { 'class': 'cbi-section-remove right' },
E('input', { E('button', {
'type': 'submit',
'class': 'cbi-button', 'class': 'cbi-button',
'value': _('Delete'), 'click': L.ui.createHandlerFn(this, 'handleRemove')
'click': L.bind(this.handleRemove, this) }, _('Delete'))));
})));
} }
sectionEl.appendChild(E('div', { sectionEl.appendChild(E('div', {
@ -1332,12 +1327,10 @@ var CBINamedSection = CBIAbstractSection.extend({
} }
else if (this.addremove) { else if (this.addremove) {
sectionEl.appendChild( sectionEl.appendChild(
E('input', { E('button', {
'type': 'submit',
'class': 'cbi-button cbi-button-add', 'class': 'cbi-button cbi-button-add',
'value': _('Add'), 'click': L.ui.createHandlerFn(this, 'handleAdd')
'click': L.bind(this.handleAdd, this) }, _('Add')));
}));
} }
L.dom.bindClassInstance(sectionEl, this); L.dom.bindClassInstance(sectionEl, this);