Projet

Général

Profil

0001-dataviz-factorize-querystring-building-code-62533.patch

Valentin Deniaud, 02 juin 2022 14:44

Télécharger (4,27 ko)

Voir les différences:

Subject: [PATCH 1/2] dataviz: factorize querystring building code (#62533)

 combo/apps/dataviz/models.py                  |  3 ++
 combo/apps/dataviz/static/js/chartngcell.js   | 12 ++++++
 .../dataviz/templates/combo/chartngcell.html  | 37 ++-----------------
 3 files changed, 18 insertions(+), 34 deletions(-)
 create mode 100644 combo/apps/dataviz/static/js/chartngcell.js
combo/apps/dataviz/models.py
276 276
    class Meta:
277 277
        verbose_name = _('Chart')
278 278

  
279
    class Media:
280
        js = ('js/chartngcell.js',)
281

  
279 282
    @classmethod
280 283
    def is_enabled(cls):
281 284
        return settings.KNOWN_SERVICES.get('bijoe') or settings.STATISTICS_PROVIDERS
combo/apps/dataviz/static/js/chartngcell.js
1
function get_graph_querystring(extra_context, width=undefined) {
2
  qs = [];
3
  if($('#chart-filters'))
4
    qs.push($('#chart-filters').serialize());
5
  if(extra_context)
6
      qs.push('ctx=' + extra_context);
7
  if (window.location.search)
8
      qs.push(window.location.search.slice(1));
9
  if(width)
10
    qs.push('width=' + width);
11
  return '?' + qs.join('&');
12
};
combo/apps/dataviz/templates/combo/chartngcell.html
5 5
<script>
6 6
$(function() {
7 7
  var extra_context = $('#chart-{{cell.id}}').parents('.cell').data('extra-context');
8
  var chart_filters_form = $('#chart-filters');
9 8
  $(window).on('combo:refresh-graphs', function() {
10
    qs = [];
11
    if(chart_filters_form)
12
        qs.push(chart_filters_form.serialize());
13
    if(extra_context)
14
        qs.push('ctx=' + extra_context);
15
    if (window.location.search)
16
        qs.push(window.location.search.slice(1));
17 9
    $.ajax({
18
      url : "{% url 'combo-dataviz-graph' cell=cell.id %}?" + qs.join('&'),
10
      url : "{% url 'combo-dataviz-graph' cell=cell.id %}" + get_graph_querystring(extra_context),
19 11
      type: 'GET',
20 12
      success: function(data) {
21 13
          $('#chart-{{cell.id}}').html(data);
......
32 24
$(function() {
33 25
  var last_width = 1;
34 26
  var extra_context = $('#chart-{{cell.id}}').parents('.cell').data('extra-context');
35
  var chart_filters_form = $('#chart-filters');
36 27
  $(window).on('load resize gadjo:sidepage-toggled combo:resize-graphs', function() {
37 28
    var chart_cell = $('#chart-{{cell.id}}').parent();
38 29
    var new_width = Math.floor($(chart_cell).width());
39 30
    var ratio = new_width / last_width;
40
    var qs;
41
    if (window.location.search) {
42
      qs = window.location.search + '&';
43
    } else {
44
      qs = '?';
45
    }
46
    qs += 'width=' + new_width
47
    if(chart_filters_form)
48
        qs += '&' + chart_filters_form.serialize()
49
    if(extra_context)
50
        qs += '&ctx=' + extra_context
51 31
    if (ratio > 1.2 || ratio < 0.8) {
52
      $('#chart-{{cell.id}}').attr('src', "{% url 'combo-dataviz-graph' cell=cell.id %}" + qs);
32
      $('#chart-{{cell.id}}').attr('src', "{% url 'combo-dataviz-graph' cell=cell.id %}" +  get_graph_querystring(extra_context, new_width));
53 33
      last_width = new_width;
54 34
    }
55 35
  }).trigger('combo:resize-graphs');
56 36
  $(window).on('combo:refresh-graphs', function() {
57
    var qs;
58
    if (window.location.search) {
59
      qs = window.location.search + '&';
60
    } else {
61
      qs = '?';
62
    }
63
    qs += 'width=' + last_width
64
    if(chart_filters_form)
65
        qs += '&' + chart_filters_form.serialize()
66
    if(extra_context)
67
        qs += '&ctx=' + extra_context
68
    $('#chart-{{cell.id}}').attr('src', "{% url 'combo-dataviz-graph' cell=cell.id %}" + qs);
37
    $('#chart-{{cell.id}}').attr('src', "{% url 'combo-dataviz-graph' cell=cell.id %}" + get_graph_querystring(extra_context, last_width));
69 38
  });
70 39
});
71 40
</script>
72
-