{% macro chart_timeline(timeline, product, period='month') -%}
    <div class="chart-timeline">
        {% if timeline %}
            {% set month_width = 650 / timeline | length %}
            {% if month_width > 5 %}
                {% set month_width = 5 %}
            {%endif%}

            {% set max_count = timeline.most_common(1)[0][1] %}
            <div class="chart-timeline-bars">
                {% set last = namespace(year=False, bars_this_year=100) %}
                {% set timeline_items = timeline.items() | sort %}
                {% for start_time, count in timeline_items  %}
                    <a href="{{ url_for('pages.product_page',
                            product_name=product.name,
                            **{
                                'year': start_time.year,
                                'month':start_time.month,
                                'day': start_time.day if period=='day' else None}
                            ) }}"
                       class="chart-timeline-bar {% if start_time[period] == 1 %}new-year{% endif %}"
                       style="height: {{ (count or 0)/(max_count if max_count > 0 else 1)*100 }}%">
                        {% set is_always_shown = loop.index == 1 or loop.index == (timeline_items|length) %}
                        {%  if is_always_shown %}
                            <span class="chart-timeline-year">
                                {%- if period == 'day' -%}{{- start_time.day | day_ordinal }} {% endif -%}
                                {{-start_time.strftime('%b %Y')-}}
                            </span>
                        {%endif%}

                        <span class="chart-timeline-year tooltip">
                            <strong>
                                {%- if period == 'day' -%}{{- start_time.day | day_ordinal }} {% endif -%}
                                {{-start_time.strftime('%b %Y')-}}
                            </strong><br/>
                            {{ count }} datasets
                        </span>
                    </a>
                {% endfor %}
            </div>

            {# We're assuming only 1 control per page, sorry. #}
            <style>
                .chart-timeline-bar {
                    width: {{month_width}}px;
                }
            </style>
        {% endif %}
    </div>
{%- endmacro %}

{% macro query_param_list(fields, prefix="", wide=false, show_nulls=true, show_dicts=true, descriptions=None, fallback_dict={}, dataset=None) -%}
    <div class="query-param-list {% if wide %}wide{% endif %}">
        {% for key, value in fields | dictsort if (show_dicts or value is not mapping) recursive %}
            {# "creation_dt" is excluded, as it's a duplicate of the creation_time field (latter is newer and date typed) #}
            {% if (show_nulls or (value is not none) or (key in fallback_dict)) and (key != "creation_dt") %}
                <span class="query-param {{ prefix+key | maybe_to_css_class_name(prefix="key-") }}">
                    {% set search_link = (dataset and (key in (dataset.product | searchable_fields_keys))) %}
                    {%- if search_link -%}
                        <a href="{{ url_for('pages.search_page', product_name=dataset.product.name, **{ key: value }) }}" class="query-param-search">
                    {%- endif -%}
                    <span class="key"
                        {% if descriptions  and descriptions[key] -%}
                        title="{{ descriptions[key].description}}"
                        {%- endif %}>
                        {{- key -}}
                    </span>
                    <span class="value">
                        {%- if value is mapping -%}
                            {{- loop(value.items()) -}}
                        {%- elif value is none -%}
                            {%- if key in fallback_dict -%}
                                {% if fallback_dict[key] is not none %}
                                    <span class="missing-value" title="Inferred from datasets: not present in product definition">
                                        {{- fallback_dict[key] | query_value -}}
                                    </span>
                                {% else %}
                                    <span class="missing-value null-value" title="Always empty in dataset samples">•</span>
                                {% endif %}
                            {%- else -%}
                                <span class="null-value" title="Unspecified">•</span>
                            {%- endif -%}
                        {%- else -%}
                            {{- value | query_value -}}
                        {%- endif -%}
                    </span>
                    {%- if search_link -%}
                        <span class="search-icon"> &#128269; </span>
                        </a>
                    {%- endif -%}
                </span>
            {% endif %}
        {% endfor %}
    </div>
{%- endmacro %}




{% macro _show_raw_keyval(value) -%}
    {%- if value is string or value is number -%}
        <span class="value">{{ value }}</span>
    {%- elif value is mapping -%}
        {%- for key, val in value.items() %}
            <div class="property"  id="{{key}}">
                <span class="key">{{ key }}:</span>
                {{ _show_raw_keyval(val) }}
            </div>
        {%- endfor %}
    {# A string is an iterable, so we check for it above #}
    {%- elif value is iterable -%}
        {# If it's a long list, make is expandable/collapsable #}
        {% if value | length > 20 %}
            <details class="array">[
                <summary><span>[{{ value | length }} items...]</span></summary>
            {%- for item in value %}
                <div class="array-item" id="{{ item.name }}">{{ _show_raw_keyval(item) }},</div>
            {% endfor %}
            ]
            </details>
        {% else %}
            <span class="array">[
            {%- for item in value %}
                <div class="array-item" id="{{ item.name }}">{{ _show_raw_keyval(item) }}</div>
            {% endfor %}
            ]</span>
        {% endif %}
    {%- else -%}
        <span class="value unknown">{{ value | torapidjson}}</span>
    {%- endif -%}
{%- endmacro %}



{% macro show_raw_document(doc) -%}
    <div class="raw-metadata">
        {{ _show_raw_keyval(doc) }}
    </div>
{%- endmacro %}
