{#
/**
 * @file
 * Default theme implementation for an unformatted view display.
 *
 * If the display is configured to use Display Suite's alternating view modes,
 * column classes are added appropriate to the view modes.
 *
 * @see http://bulma.io/documentation/columns/basics
 *
 * Available variables:
 * - title: The title of this group of rows. May be empty.
 * - rows: A list of the view's row items.
 *   - attributes: The row's HTML attributes.
 *   - content: The row's content.
 * - view: The view object.
 * - default_row_class: A flag indicating whether default classes should be
 *   used on rows.
 *
 * @see template_preprocess_views_view_unformatted()
 * @ingroup themeable
 */
#}
{#
  View rows can be configured to have default classes per view mode. A child
  template may define view_modes, which will be merged with the
  default_view_modes below.
#}
{%
  set default_view_modes = {
    '__default': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-12',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
    'box': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-half-tablet',
        'is-one-quarter-desktop',
        'is-one-quarter-widescreen',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
    'card': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-half-tablet',
        'is-half-desktop',
        'is-one-quarter-widescreen',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
    'media': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-half-tablet',
        'is-half-desktop',
        'is-half-widescreen',
      ],
      'is_grouped': true,
      'group_limit': 4,
    },
    'simple_card': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-half-tablet',
        'is-half-desktop',
        'is-one-quarter-widescreen',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
    'small_card': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-half-tablet',
        'is-3-desktop',
        'is-2-widescreen',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
    'teaser': {
      'row_classes': [
        'column',
        'is-flex-column',
        'is-12-tablet',
        'is-12-desktop',
        'is-9-widescreen',
      ],
      'is_grouped': false,
      'group_limit': null,
    },
  }
%}
{% set view_modes = view_modes ? default_view_modes|merge(view_modes) : default_view_modes %}
{% set has_view_mode = view.rowPlugin.options['view_mode'] is defined or view.rowPlugin.options['view_modes'] is defined %}
{% set view_mode = view.rowPlugin.options['view_mode'] ?? null %}
{# Add classes to the title, which is often the grouping field title. #}
{%
  set title_classes = [
    'subtitle',
    'is-size-4',
    'is-uppercase',
    'has-text-centered',
    'is-marginless',
  ]
%}
{% set title_attributes = create_attribute().addClass(title_classes) %}
{# Only apply to page displays with a selected view mode. #}
{% if has_view_mode %}
  {# Make the title a full-width column. #}
  {% set title_attributes = title_attributes.addClass(view_modes.__default.row_classes) %}
  {# Determine if we use the alternating view modes. #}
  {% set ds_alternating = (view.rowPlugin.options['alternating_fieldset']['alternating'] ?? false) %}
  {% set ds_allpages = (view.rowPlugin.options['alternating_fieldset']['allpages'] ?? false) %}
  {% set is_paged = view.getCurrentPage() ? true : false %}
  {% set use_alternating_view_mode = (ds_alternating and (ds_allpages or not is_paged)) %}
  {% set grouped_row = null %}
  {% set consecutive_count = 0 %}
  {% set previous_view_mode = null %}
  {% set grouped_rows = {} %}
  {% for row in rows %}
    {# View mode is set per row when the view_modes option is present. #}
    {% if view.rowPlugin.options['view_modes'] is defined and row.content['#view_mode'] is defined %}
      {% set view_mode = row.content['#view_mode'] %}
    {% endif %}
    {% if use_alternating_view_mode %}
      {% set view_mode = (view.rowPlugin.options['alternating_fieldset']['item_' ~ loop.index0] ?? view_mode) %}
    {% endif %}
    {#
      Add the previously set grouped_row if:
      - we've switched view modes or
      - we've reached the group_limit.
    #}
    {% set group_limit = view_modes[view_mode]['group_limit'] ?? null %}
    {% set row_classes = view_modes[view_mode]['row_classes'] ?? [] %}
    {% set is_grouped = view_modes[view_mode]['is_grouped'] ?? false %}
    {% if is_grouped %}
      {% if consecutive_count == 0 %}
        {%
          set grouped_row = {
            'content': [row.content],
            'attributes': row.attributes.addClass(row_classes),
          }
        %}
        {% set consecutive_count = 1 %}
      {% else %}
        {% set grouped_row = grouped_row|merge({ 'content': grouped_row.content|merge([row.content]) }) %}
        {% if previous_view_mode == view_mode %}
          {% set consecutive_count = (consecutive_count + 1) %}
        {% endif %}
      {% endif %}
      {#
        Add the grouped_row if:
        - we've reached the group_limit or
        - we're at the last item.
      #}
      {% if grouped_row and ((group_limit and (group_limit == consecutive_count)) or loop.last) %}
        {% if consecutive_count > 1 %}
          {% set grouped_row = grouped_row|merge({ 'attributes': grouped_row.attributes.addClass('has-grouped-items') }) %}
        {% endif %}
        {% set grouped_rows = grouped_rows|merge([grouped_row]) %}
        {% set grouped_row = null %}
        {% set consecutive_count = 0 %}
      {% endif %}
    {% else %}
      {% set row = row|merge({ 'attributes': row.attributes.addClass(row_classes) }) %}
      {% set grouped_rows = grouped_rows|merge([row]) %}
    {% endif %}
    {% set previous_view_mode = view_mode %}
  {% endfor %}
  {% set rows = grouped_rows %}
{% endif %}
{% if title %}
  <h3{{ title_attributes }}>{{ title }}</h3>
{% endif %}
{% for row in rows %}
  <div{{ row.attributes }}>
    {{ row.content }}
  </div>
{% endfor %}
