Projet

Général

Profil

0003-wcs-add-pagination-for-list-of-cards-68037.patch

Lauréline Guérin, 09 août 2022 15:33

Télécharger (7,85 ko)

Voir les différences:

Subject: [PATCH 3/5] wcs: add pagination for list of cards (#68037)

 combo/apps/wcs/forms.py                       |  1 +
 combo/apps/wcs/models.py                      |  2 +-
 combo/public/static/js/combo.public.js        | 68 ++++++++++++-------
 combo/public/templates/combo/pagination.html  |  3 +-
 combo/public/templates/combo/placeholder.html |  3 +
 combo/public/templatetags/combo.py            |  1 +
 tests/test_wcs.py                             |  9 +++
 7 files changed, 60 insertions(+), 27 deletions(-)
combo/apps/wcs/forms.py
86 86
            'carddef_reference',
87 87
            'related_card_path',
88 88
            'card_ids',
89
            'limit',
89 90
            'only_for_user',
90 91
            'custom_schema',
91 92
        )
combo/apps/wcs/models.py
1379 1379

  
1380 1380
    @property
1381 1381
    def css_class_names(self):
1382
        return '%s card' % super().css_class_names
1382
        return '%s card %s' % (super().css_class_names, self.get_reference())
1383 1383

  
1384 1384
    def get_cell_extra_context(self, context):
1385 1385
        extra_context = super().get_cell_extra_context(context)
combo/public/static/js/combo.public.js
355 355
    return false;
356 356
  });
357 357

  
358
  // pagination in cells with list of items
359
  $(document).on('combo:cell-loaded', function(ev, cell) {
360
    var $pagination = $(cell).find('.cell-items-pagination');
361
    if ($pagination.length == 0) return;
362
    var page_index = 0;
358
  function pagination_update_page(items, max_page_index, step, paginate_by, $pagination) {
359
    var page_index = $pagination.data('page_index') + step;
360
    if (page_index == 0) {
361
      $pagination.find('.cell-items-pagination-prev').prop('disabled', true);
362
    } else {
363
      $pagination.find('.cell-items-pagination-prev').prop('disabled', null);
364
    }
365
    if (page_index == max_page_index - 1) {
366
      $pagination.find('.cell-items-pagination-next').prop('disabled', true);
367
    } else {
368
      $pagination.find('.cell-items-pagination-next').prop('disabled', null);
369
    }
370
    start_item = paginate_by * page_index;
371
    items.hide();
372
    items.slice(start_item, start_item + paginate_by).show();
373
    $pagination.data('page_index', page_index);
374
  };
375

  
376
  function paginate($pagination, items) {
377
    $pagination.data('page_index', 0);
363 378
    var paginate_by = parseInt($pagination.data('paginate-by'));
364
    // Get all <li> inside the same div as us, ignoring whether they are part of
365
    // different <ul>
366
    var items = $pagination.parent().find('li');
367 379
    var max_page_index = Math.ceil(items.length / paginate_by);
368 380
    if (items.length <= paginate_by) {
369 381
      return;
370 382
    }
371 383

  
372 384
    function update_page(step) {
373
      page_index = page_index + step;
374
      if (page_index == 0) {
375
        $pagination.find('.cell-items-pagination-prev').prop('disabled', true);
376
      } else {
377
        $pagination.find('.cell-items-pagination-prev').prop('disabled', null);
378
      }
379
      if (page_index == max_page_index - 1) {
380
        $pagination.find('.cell-items-pagination-next').prop('disabled', true);
381
      } else {
382
        $pagination.find('.cell-items-pagination-next').prop('disabled', null);
383
      }
384
      start_item = paginate_by * page_index;
385
      items.hide();
386
      items.slice(start_item, start_item + paginate_by).show();
387
    };
385
      pagination_update_page(items, max_page_index, step, paginate_by, $pagination);
386
    }
388 387

  
389 388
    $pagination.find('.cell-items-pagination-prev').click(function() { update_page(-1); });
390 389
    $pagination.find('.cell-items-pagination-next').click(function() { update_page(1); });
391 390
    update_page(0);
392 391
    $pagination.prop('hidden', null);
392
  };
393

  
394
  function paginate_cards($pagination) {
395
    var items = $pagination.parent().find('.card.' + $pagination.data('cell-reference'));
396
    paginate($pagination, items);
397
  };
398

  
399
  // pagination for cells with list of items or for card cells
400
  $(document).on('combo:cell-loaded', function(ev, cell) {
401
    var $pagination = $(cell).find('.cell-items-pagination');
402
    if ($pagination.length == 0) return;
403
    // Get all <li> inside the same div as us, ignoring whether they are part of
404
    // different <ul>
405
    var items = $pagination.parent().find('li');
406
    paginate($pagination, items);
393 407
  });
408

  
394 409
  $('.cell-items-pagination').each(function(idx, elem) {
395
    $(document).trigger('combo:cell-loaded', $(elem).parents('.cell').first());
410
    var $cells = $(elem).parents('.cell');
411
    if ($cells.length) {
412
      $(document).trigger('combo:cell-loaded', $cells.first());
413
    } else if ($(elem).data('cell-reference')) {
414
      paginate_cards($(elem));
415
    }
396 416
  });
397 417
});
combo/public/templates/combo/pagination.html
1
{% load static %}
2
<div class="cell-items-pagination" data-paginate-by="{{ paginate_by|default:10 }}" hidden>
1
<div class="cell-items-pagination"{% if data_cell_reference %} data-cell-reference="{{ data_cell_reference }}"{% endif %} data-paginate-by="{{ paginate_by|default:10 }}" hidden>
3 2
  <button class="cell-items-pagination-prev" disabled>←</a>
4 3
  <button class="cell-items-pagination-next" disabled>→</a>
5 4
</div>
combo/public/templates/combo/placeholder.html
16 16
     {% if extra_context %}data-extra-context="{{ extra_context|signed|urlencode }}"{% endif %}
17 17
     {% endwith %}
18 18
     ><div>{% render_cell cell %}</div></div>
19
{% if cell.include_pagination %}
20
{% include "combo/pagination.html" with paginate_by=cell.limit data_cell_reference=cell.get_reference %}
21
{% endif %}
19 22
{% endfor %}
20 23
{% if placeholder_options.fx_grid_layout %}</div>{% endif %}
21 24
{% if outer_tag %}</{{outer_tag|default:"div"}}>{% endif %}
combo/public/templatetags/combo.py
127 127
            for i in range(repeat):
128 128
                new_cell = copy.copy(cell)
129 129
                new_cell.repeat_index = i
130
                new_cell.include_pagination = bool(i + 1 == repeat)
130 131
                repeated_cells.append(new_cell)
131 132
            context['cells'][cell_idx:cell_idx] = repeated_cells
132 133

  
tests/test_wcs.py
2659 2659
    assert len(resp.context['cells']) == 3
2660 2660
    for i in range(0, 3):
2661 2661
        assert '<h2>Foo bar X%sY</h2>' % i in resp
2662
    assert 'data-paginate-by="10"' in resp
2663

  
2664
    cell.limit = 42
2665
    cell.save()
2666
    resp = app.get(page.get_online_url())
2667
    assert len(resp.context['cells']) == 3
2668
    for i in range(0, 3):
2669
        assert '<h2>Foo bar X%sY</h2>' % i in resp
2670
    assert 'data-paginate-by="42"' in resp
2662 2671

  
2663 2672
    # using custom view
2664 2673
    cell.carddef_reference = 'default:card_model_1:foo'
2665
-