{% load i18n %}
<div class="assetmanager-file">
  <input type="hidden" class="file" id="{{ name }}" name="{{ name }}" value="{{ value_as_json }}"/>
  <a class="related-widget-wrapper-link change-related asset-manager-link" id="change_{{ name }}"
     href="/admin/assetmanager/file/list/?{{ url_params }}"
     title="{% blocktrans %}Change file{% endblocktrans %}">
    {% blocktrans %}Change file{% endblocktrans %}
  </a>
  <div class="preview" {% if not value %}style="display: none;"{% endif %}>
    <a href="{{ value.url }}" target="_blank">
      {% if images_only %}
        <img class="url" src="{{ value.url }}" width="200"/>
        <div class="name"><span>{{ value.fileOriginalName }}</span> <i class="icon-popup"></i></div>
      {% else %}
        <div class="name"><span>{{ value.fileOriginalName }}</span> <i class="icon-popup"></i></div>
      {% endif %}
    </a>
  </div>
  {% if not is_required %}
  <a class="clear"
     {% if not value %}style="display: none;"{% endif %}
     href="#">Clear</a>
  {% endif %}
</div>
<script type="application/javascript">
  (function(globals, $) {
    'use strict';

    function dismissChooseFilePopup(win, newFile) {
      var name = win;
      if (win.name) {
        name = win.name;
      }
      name = windowname_to_id(name);

      var elem = document.getElementById('change_'+name);
      var fileInput = document.getElementById(name);
      var previewElement = $('.preview');
      if (elem && fileInput) {
        $(fileInput).val(JSON.stringify(newFile));

        previewElement.find('> a').attr('href', newFile.url);
        previewElement.find('.url').attr('src', newFile.url);
        previewElement.find('.name > span').text(newFile.fileOriginalName);
        previewElement.show();
      }

      $(elem).siblings('.clear').show();

      if (win.close) {
        win.close();
      }
    }

    var container = $(document);
    container.on('click', '.asset-manager-file .clear', function(event) {
      $(this).hide();
      $(this).siblings('.file').val('');
      previewElement.hide();
      event.preventDefault();
    });

    globals.dismissChooseFilePopup = dismissChooseFilePopup;
  })(window, django.jQuery);
</script>
