Initial release
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
{% extends "@Contao/frontend_module/_base.html.twig" %}
|
||||
|
||||
{% block content %}
|
||||
{% if error is defined and error %}
|
||||
<p role="alert">{{ error }}</p>
|
||||
<p><a href="{{ backUrl }}">Zurück</a></p>
|
||||
{% elseif form is defined and form %}
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css">
|
||||
<link rel="stylesheet" href="https://unpkg.com/filepond/dist/filepond.min.css">
|
||||
<link rel="stylesheet" href="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
|
||||
<script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>
|
||||
<script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>
|
||||
|
||||
{{ form_start(form, { attr: { 'aria-live': 'polite' } }) }}
|
||||
<input type="hidden" name="REQUEST_TOKEN" value="{{ requestToken }}">
|
||||
<input type="hidden" id="remove_logo" name="remove_logo" value="0">
|
||||
{{ form_widget(form) }}
|
||||
<div class="actions" aria-label="Formularaktionen">
|
||||
<button type="submit" id="save-btn" disabled>Speichern</button>
|
||||
<button type="submit" id="save-back-btn" name="save_back" value="1" disabled>Speichern und Zurück</button>
|
||||
<a href="{{ backUrl }}" aria-label="Zurück zur vorherigen Seite">Zurück</a>
|
||||
</div>
|
||||
{{ form_end(form) }}
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
const typeSelect = document.querySelector('select.js-organization-type-choice');
|
||||
|
||||
if (typeSelect && typeof window.Choices === 'function') {
|
||||
new window.Choices(typeSelect, {
|
||||
searchEnabled: false,
|
||||
shouldSort: false,
|
||||
removeItemButton: true,
|
||||
itemSelectText: '',
|
||||
placeholder: true,
|
||||
placeholderValue: typeSelect.dataset.placeholder || 'Typ auswählen …',
|
||||
noResultsText: 'Keine Treffer',
|
||||
noChoicesText: 'Keine Optionen verfügbar'
|
||||
});
|
||||
}
|
||||
|
||||
const currentLogoPath = {{ (currentLogoPath is defined and currentLogoPath) ? ('/' ~ currentLogoPath)|json_encode|raw : 'null' }};
|
||||
const logoInput = document.querySelector('input.js-logo-upload');
|
||||
const removeLogoInput = document.getElementById('remove_logo');
|
||||
const hadInitialLogo = !!currentLogoPath;
|
||||
let pondDirty = false;
|
||||
|
||||
const form = document.querySelector('form');
|
||||
const saveButton = document.getElementById('save-btn');
|
||||
const saveBackButton = document.getElementById('save-back-btn');
|
||||
if (!form || !saveButton || !saveBackButton) {
|
||||
return;
|
||||
}
|
||||
|
||||
const initial = new FormData(form);
|
||||
const hasChanges = () => {
|
||||
const current = new FormData(form);
|
||||
for (const [key, value] of current.entries()) {
|
||||
if (initial.getAll(key).join(',') !== current.getAll(key).join(',')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const updateButtons = () => {
|
||||
const changed = hasChanges() || pondDirty;
|
||||
saveButton.disabled = !changed;
|
||||
saveBackButton.disabled = !changed;
|
||||
};
|
||||
|
||||
if (logoInput && typeof window.FilePond !== 'undefined') {
|
||||
if (typeof window.FilePondPluginImagePreview !== 'undefined') {
|
||||
window.FilePond.registerPlugin(window.FilePondPluginImagePreview);
|
||||
}
|
||||
|
||||
const filePondOptions = {
|
||||
instantUpload: false,
|
||||
storeAsFile: true,
|
||||
allowMultiple: false,
|
||||
allowReplace: true,
|
||||
credits: false,
|
||||
acceptedFileTypes: ['image/*'],
|
||||
labelIdle: 'Logo hierher ziehen oder <span class="filepond--label-action">durchsuchen</span>'
|
||||
};
|
||||
|
||||
if (currentLogoPath) {
|
||||
filePondOptions.files = [
|
||||
{
|
||||
source: currentLogoPath,
|
||||
options: {
|
||||
type: 'local'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
filePondOptions.server = {
|
||||
load: (source, load, error, progress, abort) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', source);
|
||||
xhr.responseType = 'blob';
|
||||
xhr.onload = function () {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
load(xhr.response);
|
||||
} else {
|
||||
error('Laden fehlgeschlagen');
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
error('Laden fehlgeschlagen');
|
||||
};
|
||||
xhr.send();
|
||||
|
||||
return {
|
||||
abort: () => {
|
||||
xhr.abort();
|
||||
abort();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const pond = window.FilePond.create(logoInput, filePondOptions);
|
||||
|
||||
const syncPondState = () => {
|
||||
const files = pond.getFiles();
|
||||
const hasLocalFile = files.some((file) => window.FilePond.FileOrigin && file.origin === window.FilePond.FileOrigin.LOCAL);
|
||||
const hasNewFile = files.some((file) => window.FilePond.FileOrigin && file.origin !== window.FilePond.FileOrigin.LOCAL);
|
||||
|
||||
pondDirty = hasNewFile || (hadInitialLogo && !hasLocalFile);
|
||||
|
||||
if (removeLogoInput) {
|
||||
removeLogoInput.value = hadInitialLogo && !hasLocalFile && !hasNewFile ? '1' : '0';
|
||||
}
|
||||
|
||||
updateButtons();
|
||||
};
|
||||
|
||||
syncPondState();
|
||||
|
||||
pond.on('removefile', function (error, file) {
|
||||
if (error) {
|
||||
return;
|
||||
}
|
||||
|
||||
syncPondState();
|
||||
});
|
||||
|
||||
pond.on('addfile', function () {
|
||||
syncPondState();
|
||||
});
|
||||
|
||||
pond.on('updatefiles', function () {
|
||||
syncPondState();
|
||||
});
|
||||
}
|
||||
form.addEventListener('input', function () {
|
||||
updateButtons();
|
||||
});
|
||||
form.addEventListener('change', function () {
|
||||
updateButtons();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{% else %}
|
||||
<p>Kein Formular verfügbar.</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user