
<script>
    $(document).ready(function() {

        $(".collapse").on('show.bs.collapse', function(){
            // event: collapsible content is starting to expand
            var id = $(this).prop('id');
            var loading_gif = document.getElementById(id + '_gif');

            if (isEmpty($(this))) {
                loading_gif.style.display = "block";
                $(this).load('/compare/ajax_common_files/{{ result["_id"] }}/' + id + '/', function() {
                    loading_gif.style.display = "none";
                });
            }
        });

        function isEmpty(element) {
            return !$.trim(element.html())
        }

    });
</script>

{% for feature in result['plugins'][plugin] | sort %}

    {# --- similar files exception ---#}
    {% if feature == 'similar_files' %}
        <tr><th colspan="{{ uid_list | length + 1 }}" style="text-align:center;" class="table-head-light">SSDEEP Similarity</th></tr>
        {% if not result['plugins'][plugin][feature] %}
            <tr>
                <td colspan="{{ uid_list | length + 1 }}" style="text-align:center;">No similar files found</td>
            </tr>
        {% endif %}
        {% for match in result['plugins'][plugin][feature] %}
            <tr>
                <td style="text-align:right;" class="table-head-light">{{ result['plugins'][plugin][feature][match]['similarity'] }}</td>
            {% for uid in uid_list %}
                <td>{{ result['plugins'][plugin][feature][match][uid] | replace_uid_with_hid_link | safe }}</td>
            {% endfor %}
            </tr>
        {% endfor %}

    {% elif feature == 'exclusive_files' %}
        <td> {{ feature | replace_underscore }} </td>
        {% for firmware_uid in uid_list %}
            {% if firmware_uid != "collapse" %}
            <td style="font-size: 14px;">
                <div id="fileTreeAjax_{{ loop.index }}"></div>
                {# --- file tree ---#}
                <script>
                    $('#fileTreeAjax_{{ loop.index }}')
                        // generate links to the analysis page
                        .on("activate_node.jstree", function(e,data){window.location.href = data.node.a_attr.href;})
                        // generate file tree
                        .jstree({
                          "core" : {
                            'data' : {
                              'url' : function (node) {
                                return node.id === '#' ?
                                    "/ajax_root/{{ firmware_uid|safe }}/{{ firmware_uid|safe }}" : "/compare/ajax_tree/{{ result["_id"] }}/{{ firmware_uid }}/" + node["data"]["uid"];
                              }
                            }
                          },
                          "plugins" : [ "sort" ]
                        });
                </script>
            </td>
            {% endif %}
        {% endfor %}

    {% else %}
        <tr>
            <td>{{ feature | replace_underscore }}</td>

            {# --- all case --- #}
            {% if 'all' in result['plugins'][plugin][feature] %}
                <td colspan="{{ uid_list | length }}">
                    <div class="list-group">
                        <div role="button" data-toggle="collapse" data-target="#{{ feature + '___all' }}" class="list-group-item list-group-item-primary d-flex justify-content-between align-items-center">
                            show files<span class="badge badge-pill badge-primary">{{ result['plugins'][plugin][feature]['all'] | length }}</span>
                        </div>
                        <div id="{{ feature + '___all' }}" class="collapse"></div>
                        <div id="{{ feature + '___all' + '_gif' }}" style="display: none;">
                            <img src="{{ url_for('static', filename = 'Pacman.gif') }}" width="50px">
                        </div>
                    </div>
                </td>

            {# --- individual results --- #}
            {% else %}
                {% for uid in uid_list %}
                    <td>
                        <div class="list-group">
                            <div role="button" data-toggle="collapse" data-target="#{{ feature + '___' + uid }}" class="list-group-item list-group-item-primary d-flex justify-content-between align-items-center">
                                show files<span class="badge badge-pill badge-primary">{{ result['plugins'][plugin][feature][uid] | length }}</span>
                            </div>
                            <div id="{{ feature + '___' + uid }}" class="collapse"></div>
                            <div id="{{ feature + '___' + uid + '_gif' }}" style="display: none;">
                                <img src="{{ url_for('static', filename = 'Pacman.gif') }}" width="50px">
                            </div>
                        </div>
                    </td>
                {% endfor %}
            {% endif %}
        </tr>
    {% endif %}
{% endfor %}
