
import "./form.scss"

$(document).ready(function () {
    initFileManager();
});

$('.field-collection').bind('collection-add-item', function (){
    initFileManager();
});

function initFileManager() {
    $('.lc-filemanager-delete').off('click');
    $('.lc-filemanager-delete').on('click', function (e) {
        let $field = $(this);
        $('#' + $field.data('id')).val("");
        $('#' + $field.data('id') + '_preview').prop('src',"");
        $('#' + $field.data('id') + '_preview_text').html('Aucun fichier');
    });
    $('.lc-filemanager-open').off('click');
    $('.lc-filemanager-open').on('click', function (e) {
        let $field = $(this);
        $('#lc-filemanager-frame').off('load');
        $("#lc-filemanager-frame").on('load', function () {
            $('#lc-filemanager-frame').contents().on('click', '.select', function () {
                var path = $(this).attr('data-path')
                $('#' + $field.data('id')).val(path);
                $('#' + $field.data('id') + '_preview').prop('src',path);
                $('#' + $field.data('id') + '_preview_text').html(path);
                $('#lc-filemanager-modal').modal('hide');
            });
        });
        $("#lc-filemanager-frame").prop('src', $field.data('target'));
        $('#lc-filemanager-modal').modal('show');
    });

}
