summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/upload.js
blob: 8924cebed5c9808583b66d5e1565257152ec0a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * Fetch and display preview images for various image upload forms.
 */

import { fetchJson, handleError } from './utils/requests';
import { $, $$, hideEl, showEl, makeEl, clearEl } from './utils/dom';
import { addTag } from './tagsinput';

function scrapeUrl(url) {
    return fetchJson('POST', '/posts/scrape_url', { url })
        .then(handleError)
        .then(response => response.json());
}

function setupImageUpload() {
    const imgPreviews = $('#js-image-upload-previews');
    if (!imgPreviews) return;

    const form = imgPreviews.closest('form');
    const [ fileField, remoteUrl, scraperError ] = $$('.js-scraper', form);
    const [ sourceEl, tagsEl, descrEl ] = $$('.js-image-input', form);
    const fetchButton = $('#js-scraper-preview');

    function showImages(images) {
        clearEl(imgPreviews);

        images.forEach((image, index) => {
            const imgWrap = makeEl('span', { className: 'scraper-preview--image-wrapper' });

            if (image.type === 'image') {
                const img = makeEl('img', { className: 'scraper-preview--image' });
                img.src = image.camo_url;

                imgWrap.appendChild(img);
            } else if (image.type === 'paste') {
                const paste = makeEl('pre', { className: 'scraper-preview--paste' });
                const disclaimer = makeEl('p');

                paste.innerText = image.preview;

                disclaimer.innerText = 'Preview is only the first 250 characters - it will all be there in the real post.'

                imgWrap.appendChild(paste);
                imgWrap.appendChild(disclaimer);
            }

            const label = makeEl('label', { className: 'scraper-preview--label' });
            const radio = makeEl('input', {
                type: 'radio',
                className: 'scraper-preview--input',
                name: 'scraper_cache[url]'
            });

            const otherRadio = makeEl('input', {
                type: 'radio',
                className: 'scraper-preview--input',
                name: 'scraper_cache[type]'
            });

            if (image.url) {
                radio.value = image.url;
                otherRadio.value = image.type;
            }

            if (index === 0) {
                radio.checked = true;
                otherRadio.checked = true;
            }

            label.appendChild(radio);
            label.appendChild(otherRadio);
            label.appendChild(imgWrap);
            imgPreviews.appendChild(label);
        });
    }
    function showError(err) {
        console.error(err);
        clearEl(imgPreviews);
        showEl(scraperError);
        enableFetch();
    }
    function hideError()    { hideEl(scraperError); }
    function disableFetch() { fetchButton.setAttribute('disabled', ''); }
    function enableFetch()  { fetchButton.removeAttribute('disabled'); }

    const reader = new FileReader();

    reader.addEventListener('load', event => {
        const fileReader = event.target;
        // I hate this.
        const isPaste = !fileReader.result.startsWith('data:image/') && !fileReader.result.startsWith('data:video/');
        const data = isPaste ? { type: 'paste', preview: fileReader.result.substring(0, 250) } : { type: 'image', camo_url: fileReader.result };

        showImages([data]);

        // Clear any currently cached data, because the file field
        // has higher priority than the scraper:
        remoteUrl.value = '';
        hideError();
    });

    // Watch for files added to the form
    fileField.addEventListener('change', () => {
        if (fileField.files.length !== 1) return;

        const file = fileField.files[0];

        if (file.type.startsWith('text/')) {
            reader.readAsText(file);
        } else {
            reader.readAsDataURL(file);
        }
    });

    // Watch for [Fetch] clicks
    fetchButton.addEventListener('click', () => {
        if (!remoteUrl.value) return;

        disableFetch();

        scrapeUrl(remoteUrl.value).then(data => {
            if (data.errors && data.errors.length > 0) {
                scraperError.innerText = data.errors.join(' ');
                showError();
                return;
            }

            hideError();

            // Set source
            if (sourceEl) sourceEl.value = sourceEl.value || data.source_url || '';
            // Set description
            if (descrEl) descrEl.value = descrEl.value || data.description || '';
            // Add author
            if (tagsEl && data.author_name) addTag(tagsEl, `artist:${data.author_name.toLowerCase()}`);

            // Add scraped tags
            if (tagsEl && data.tags) {
                for (let tag of data.tags) {
                    addTag(tagsEl, tag);
                }
            }
            // Clear selected file, if any
            fileField.value = '';
            showImages(data.images);

            enableFetch();
        }).catch(showError);
    });
}

export { setupImageUpload };