<commit_before>import {Controller} from "stimulus"

export default class extends Controller {

    static targets = [
        "name",
        "original_name",
        "alt",
        "description",
        "url",
        "active"
    ]

    constructor(props) {
        super(props);
        this.attachments = {}
    }


    connect() {
        console.log(this.dropname);
        this.initDropZone()
        this.initSortable()
    }

    get dropname() {
        return '#dropzone-'+this.data.get('name')+' ';
    }
    
    save() {
        const data = this.activeAttachment;

        $('#modalUploadAttachment').modal('toggle');
        axios
            .put(platform.prefix(`/systems/files/post/${data.id}`), data)
            .then();
    }

    getAttachmentTargetKey(dataKey) {
        return `${dataKey}Target`
    }

    get activeAttachment() {
        const id = this.activeAchivmentId
        return ([
            'name',
            'original_name',
            'alt',
            'url',
            'description'
        ]).reduce((res, key) => {
            const targetKey = this.getAttachmentTargetKey(key)
            const target = this[targetKey]
            if (key === 'url') {
                return {
                    ...res,
                    [key]: target.href
                }
            }
            return {
                ...res,
                [key]: target.value
            }
        }, {id})
    }

    set activeAttachment(data) {
        this.activeAchivmentId = data.id
        Object.keys(data).map((key) => {
            const value = data[key]
            const targetKey = this.getAttachmentTargetKey(key)
            const target = this[targetKey]

            if (!target) {
                return
            }

            if (key === 'url') {
                target.href = value
                return
            }
            target.value = value
        })
    }

    loadInfo(data) {
        const name = data.name + data.id;
        data.url = '/storage/' + data.path + data.name + '.' + data.extension;
        if (!this.attachments.hasOwnProperty(name)) {
            this.attachments[name] = data;
        }
        this.activeAttachment = data
    }

    initSortable() {
        $(this.dropname+'.sortable-dropzone').sortable({
            scroll: false,
            containment: "parent",
            update: function () {
                const items = {};
                $('.file-sort').each(function (index, value) {
                    const id = $(this).attr('data-file-id');
                    items[id] = index;
                });

                axios
                    .post(platform.prefix('/systems/files/sort'), {
                        files: items,
                    })
                    .then();

            },
        });
    }

    initDropZone() {
        const data = this.data.get('data') && JSON.parse(this.data.get('data'))
        const storage = this.data.get('storage')
        const name = this.data.get('name')
        const loadInfo = this.loadInfo.bind(this)
        const dropname = this.dropname
        
        Dropzone.autoDiscover = false;
        //var UploadDropzone = [];
        var UploadDropzone = new Dropzone(dropname, {<commit_msg>Apply fixes<commit_after>import {Controller} from "stimulus"

export default class extends Controller {

    static targets = [
        "name",
        "original_name",
        "alt",
        "description",
        "url",
        "active"
    ]

    constructor(props) {
        super(props);
        this.attachments = {}
    }


    connect() {
        this.initDropZone()
        this.initSortable()
    }

    get dropname() {
        return '#dropzone-'+this.data.get('name')+' ';
    }
    
    save() {
        const data = this.activeAttachment;

        $('#modalUploadAttachment').modal('toggle');
        axios
            .put(platform.prefix(`/systems/files/post/${data.id}`), data)
            .then();
    }

    getAttachmentTargetKey(dataKey) {
        return `${dataKey}Target`
    }

    get activeAttachment() {
        const id = this.activeAchivmentId
        return ([
            'name',
            'original_name',
            'alt',
            'url',
            'description'
        ]).reduce((res, key) => {
            const targetKey = this.getAttachmentTargetKey(key)
            const target = this[targetKey]
            if (key === 'url') {
                return {
                    ...res,
                    [key]: target.href
                }
            }
            return {
                ...res,
                [key]: target.value
            }
        }, {id})
    }

    set activeAttachment(data) {
        this.activeAchivmentId = data.id
        Object.keys(data).map((key) => {
            const value = data[key]
            const targetKey = this.getAttachmentTargetKey(key)
            const target = this[targetKey]

            if (!target) {
                return
            }

            if (key === 'url') {
                target.href = value
                return
            }
            target.value = value
        })
    }

    loadInfo(data) {
        const name = data.name + data.id;
        data.url = '/storage/' + data.path + data.name + '.' + data.extension;
        if (!this.attachments.hasOwnProperty(name)) {
            this.attachments[name] = data;
        }
        this.activeAttachment = data
    }

    initSortable() {
        $(this.dropname+'.sortable-dropzone').sortable({
            scroll: false,
            containment: "parent",
            update: function () {
                const items = {};
                $('.file-sort').each(function (index, value) {
                    const id = $(this).attr('data-file-id');
                    items[id] = index;
                });

                axios
                    .post(platform.prefix('/systems/files/sort'), {
                        files: items,
                    })
                    .then();

            },
        });
    }

    initDropZone() {
        const data = this.data.get('data') && JSON.parse(this.data.get('data'))
        const storage = this.data.get('storage')
        const name = this.data.get('name')
        const loadInfo = this.loadInfo.bind(this)
        const dropname = this.dropname
        
        Dropzone.autoDiscover = false;
        var UploadDropzone = new Dropzone(dropname, {
{'fraction_non_alphanumeric': 0.13056978730010868, 'fraction_numerical': 0.0, 'mean_word_length': 1.3197695354699315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4890582', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1730, 'n_words': 318}