Projet

Général

Profil

0001-js-make-it-possible-to-dynamically-show-hide-widgets.patch

Frédéric Péters, 04 janvier 2016 16:15

Télécharger (2,27 ko)

Voir les différences:

Subject: [PATCH 1/3] js: make it possible to dynamically show/hide widgets
 with radio buttons

Give the parent radio buttons data-dynamic-display-parent="true" and the
widgets that have to be shown/hidden a data-dynamic-display-child-of attribute
with the @name of the parent and a data-dynamic-display-value attribute with
the value that will show this element.

form.add(RadiobuttonsWidget, 'foo', ...,
         options=[('1', 'Foo1), ('2', 'Foo2')],
         attrs={'data-dynamic-display-parent': 'true'})
form.add(StringWidget, 'bar', ...,
         attrs={'data-dynamic-display-child-of': 'foo',
                'data-dynamic-display-value': 'Foo2'})
 wcs/qommon/form.py             | 3 +++
 wcs/qommon/static/js/qommon.js | 8 ++++++++
 2 files changed, 11 insertions(+)
wcs/qommon/form.py
124 124
            attributes['data-' + k] = v
125 125
    if hasattr(self, 'div_id') and self.div_id:
126 126
        attributes['id'] = self.div_id
127
    for attr in ('data-dynamic-display-child-of', 'data-dynamic-display-value'):
128
        if attr in self.attrs:
129
            attributes[attr] = self.attrs.pop(attr)
127 130
    attributes['class'] = classnames
128 131
    r += htmltext('<div %s>' % ' '.join(['%s="%s"' % x for x in attributes.items()]))
129 132
    r += self.render_title(self.get_title())
wcs/qommon/static/js/qommon.js
9 9
            error: function(error) { windows.console && console.log('bouh', error); }
10 10
           });
11 11
  });
12
  $('[data-dynamic-display-parent]').change(function() {
13
    var sel1 = '[data-dynamic-display-child-of="' + $(this).attr('name') + '"]';
14
    var sel2 = '[data-dynamic-display-value="' + $(this).val() + '"]';
15
    $(sel1).hide();
16
    $(sel1 + sel2).show();
17
  });
18
  $('[data-dynamic-display-child-of]').hide();
19
  $('[data-dynamic-display-parent]:checked').trigger('change');
12 20
});
13
-