Projet

Général

Profil

0001-forms-move-hint-inside-select2-like-normal-select-32.patch

Frédéric Péters, 12 avril 2019 09:27

Télécharger (4,04 ko)

Voir les différences:

Subject: [PATCH] forms: move hint inside select2 (like normal select) (#32222)

 tests/test_form_pages.py                         | 16 ++++++++++++++++
 wcs/qommon/form.py                               |  2 +-
 wcs/qommon/static/js/qommon.forms.js             |  3 ++-
 .../templates/qommon/forms/widgets/select.html   |  2 +-
 4 files changed, 20 insertions(+), 3 deletions(-)
tests/test_form_pages.py
4809 4809
        urlopen.side_effect = lambda *args: StringIO.StringIO(json.dumps(data))
4810 4810
        resp = get_app(pub).get('/test/')
4811 4811
        assert 'data-autocomplete="true"' in resp.body
4812
        assert resp.form['f0'].value == '1'
4812 4813
        resp.form['f0'] = '2'
4813 4814
        resp = resp.form.submit('submit') # -> validation page
4814 4815
        resp = resp.form.submit('submit') # -> submit
......
4816 4817
        assert formdef.data_class().select()[0].data['0_display'] == 'world'
4817 4818
        assert formdef.data_class().select()[0].data['0_structured'] == data['data'][1]
4818 4819

  
4820
    # check hint is displayed within
4821
    formdef.fields[0].hint = 'help text'
4822
    formdef.store()
4823
    with mock.patch('qommon.misc.urlopen') as urlopen:
4824
        data = {'data': [{'id': '1', 'text': 'hello', 'extra': 'foo'},
4825
                         {'id': '2', 'text': 'world', 'extra': 'bar'}]}
4826
        urlopen.side_effect = lambda *args: StringIO.StringIO(json.dumps(data))
4827
        resp = get_app(pub).get('/test/')
4828
        assert 'data-autocomplete="true"' in resp.body
4829
        assert 'data-hint="help text"' in resp.body
4830
        assert resp.form['f0'].value == ''
4831

  
4832
    formdef.fields[0].hint = ''
4833
    formdef.store()
4834

  
4819 4835
    # check with possibility of remote query
4820 4836
    data_source.query_parameter = 'q'
4821 4837
    data_source.id_parameter = 'id'
wcs/qommon/form.py
1532 1532
            get_response().add_css_include('../js/select2/select2.css')
1533 1533

  
1534 1534
    def separate_hint(self):
1535
        return (self.hint and len(self.hint) > 80) or self.select2
1535
        return (self.hint and len(self.hint) > 80)
1536 1536

  
1537 1537
    def get_options(self):
1538 1538
        if self.options_with_attributes:
wcs/qommon/static/js/qommon.forms.js
141 141
        searching: function () { return WCS_I18N.s2_searching; }
142 142
      }
143 143
    };
144
    options.placeholder = $(elem).find('[data-hint]').data('hint');
144 145
    if (!required) {
145
      options.placeholder = '...';
146
      if (!options.placeholder) options.placeholder = '...';
146 147
      options.allowClear = true;
147 148
    }
148 149
    $(elem).select2(options);
wcs/qommon/templates/qommon/forms/widgets/select.html
5 5
    {% if widget.required %}data-required="true"{% endif %}
6 6
    {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
7 7
  {% if not widget.separate_hint and widget.hint %}
8
  <option value="">{{ widget.hint }}</option>
8
  <option value="" data-hint="{{ widget.hint }}">{% if not widget.select2 %}{{ widget.hint }}{% endif %}</option>
9 9
  {% endif %}
10 10
  {% for option in widget.get_options %}
11 11
  <option{% for attr in option.attrs.items %} {{attr.0}}="{{attr.1}}"{% endfor %}>{{ option.description }}</option>
12
-