admin: make data source origin widget dynamic (#22247)

This commit is contained in:
Frédéric Péters 2018-03-03 18:42:42 +01:00
parent 1ce2a0082b
commit 952655c044
3 changed files with 12 additions and 7 deletions

View File

@ -1080,15 +1080,18 @@ def test_form_edit_field_advanced(pub):
assert resp.forms[0]['label'].value == '1st field'
assert '<legend>Additional parameters</legend>' in resp.body
assert '<label for="form_data_source">Data Source</label>' in resp.body
# hceck the "data source" field is under additional parameters
# check the "data source" field is under additional parameters
assert resp.body.index('<legend>Additional parameters</legend>') < \
resp.body.index('<label for="form_data_source">Data Source</label>')
# start filling the "data source" field
resp.forms[0]['data_source$type'] = 'JSON URL'
resp = resp.forms[0].submit('data_source$apply')
resp.forms[0]['data_source$value'] = 'http://example.net'
resp = resp.forms[0].submit('submit')
resp = resp.follow()
# it should now appear before the additional parameters section
resp = resp.click('Edit', href='1/')
assert resp.body.index('<legend>Additional parameters</legend>') > \
resp.body.index('<label for="form_data_source">Data Source</label>')

View File

@ -61,16 +61,16 @@ class DataSourceSelectionWidget(CompositeWidget):
options.extend([(x.slug, x.name) for x in
NamedDataSource.select(order_by='name')])
self.add(SingleSelectWidget, 'type', options = options, value = value.get('type'))
self.add(SingleSelectWidget, 'type', options=options, value=value.get('type'),
attrs={'data-dynamic-display-parent': 'true'})
self.parse()
if not self.value:
self.value = {}
if self.value.get('type') in ('formula', 'json', 'jsonp'):
self.add(StringWidget, 'value', value = value.get('value'), size=80)
self.add(SubmitWidget, 'apply', value = _('Apply'))
self.add(StringWidget, 'value', value=value.get('value'), size=80,
attrs={'data-dynamic-display-child-of': 'data_source$type',
'data-dynamic-display-invert-value': options[0][1]})
self._parsed = False

View File

@ -5,8 +5,10 @@ $(function() {
$('[data-dynamic-display-parent]').off('change keyup').on('change keyup', function() {
var sel1 = '[data-dynamic-display-child-of="' + $(this).attr('name') + '"]';
var sel2 = '[data-dynamic-display-value="' + $(this).val() + '"]';
var sel3 = '[data-dynamic-display-invert-value][data-dynamic-display-invert-value!="' + $(this).val() + '"]';
$(sel1).hide();
$(sel1 + sel2).show();
$(sel1 + sel3).show();
});
$('[data-dynamic-display-child-of]').hide();
$('select[data-dynamic-display-parent]').trigger('change');