Skip to content

Commit

Permalink
NGSTACK-878 add js for image upload and modify css
Browse files Browse the repository at this point in the history
  • Loading branch information
TTrakostanec authored and emodric committed May 24, 2024
1 parent c8f66cb commit aeec4c8
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 52 deletions.
13 changes: 13 additions & 0 deletions assets/js/backoffice/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Sidebar from '../components/backoffice/Sidebar.component';
import EnableFilterButtons from '../components/backoffice/EnableFilterButtons.component';
import HideOverflowingFilters from '../components/backoffice/HideOverflowingFilters.component';
import DateRangeInput from '../components/backoffice/DateRangeInput.component';
import ImageUpload from '../components/backoffice/ImageUpload.component';

const componentConfiguration = [
{
Expand Down Expand Up @@ -53,6 +54,18 @@ const componentConfiguration = [
Component: DateRangeInput,
selector: '.js-date-range-input',
},
{
Component: ImageUpload,
selector: '.js-image-upload',
options: {
parentFormSelector: '.js-custom-form',
removeButtonSelector: '.js-remove-image',
filenameWrapper: '.js-filename-wrapper',
imageFilenameSelector: '.js-image-filename',
replaceImageBtnSelector: '.js-replace-image-btn',
imagePreviewSelector: '.js-image-preview',
},
},
];

addDocumentEventListeners(componentConfiguration);
1 change: 1 addition & 0 deletions assets/js/backoffice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import '../../sass/backoffice/style.scss';
import './components';
import 'bootstrap';
import './date';
import './preview-uploaded-image';
29 changes: 29 additions & 0 deletions assets/js/backoffice/preview-uploaded-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const inputElement = document.getElementById(
'ezplatform_content_forms_content_edit_fieldsData_image_value_file'
);
const imageElement = document.getElementById('img-preview');
const removeImageButton = document.getElementById('btn-remove-img');
const removeImageCheckbox = document.getElementsByClassName('js-remove-image')[0];

function updateSource() {
const file = this.files[0];
if (file !== undefined) {
imageElement.src = URL.createObjectURL(file);
} else {
imageElement.src = '';
}
}

function removeImage() {
removeImageCheckbox.checked = true;
inputElement.value = '';
imageElement.src = '';
}

if (inputElement) {
inputElement.addEventListener('change', updateSource, false);
}

if (removeImageButton) {
removeImageButton.addEventListener('click', removeImage, false);
}
68 changes: 68 additions & 0 deletions assets/js/components/backoffice/ImageUpload.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export default class ImageUpload {
constructor(element, options) {
this.inputElement = element;
this.removeElement = document.querySelector(options.removeButtonSelector);
this.filenameElement = document.querySelector(options.imageFilenameSelector);
this.filenameWrapper = document.querySelector(options.filenameWrapper);
this.replaceImageBtn = document.querySelector(options.replaceImageBtnSelector);
this.imagePreview = document.querySelector(options.imagePreviewSelector);
this.onInit();
}

onInit() {
this.initialFilename = this.filenameElement.innerHTML.trim();
this.initialImageSrc = this.imagePreview.src;

this.inputElement.addEventListener('input', this.handleFileChange.bind(this));

this.replaceImageBtn.addEventListener('click', () => this.inputElement.click());

this.removeElement.addEventListener('change', this.handleRemoveImageChange.bind(this));
}

handleFileChange(e) {
const that = this;

function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (event) {
that.imagePreview.setAttribute('src', event.target.result);
};

reader.readAsDataURL(input.files[0]);
}
}

if (e.target.value) {
readURL(e.target);
this.replaceImageBtn.classList.remove('hidden');
this.inputElement.classList.add('hidden');
this.filenameWrapper.classList.remove('hidden');
if (e.target.value) {
this.filenameElement.innerHTML = this.inputElement.files[0].name;
}
if (this.removeElement.checked) {
this.removeElement.click();
}
}
}

handleRemoveImageChange(e) {
if (!e.target.checked) return;

if (this.inputElement.value && this.initialFilename) {
e.target.checked = false;
this.filenameElement.innerHTML = this.initialFilename;
this.imagePreview.setAttribute('src', this.initialImageSrc);
} else {
this.replaceImageBtn.classList.add('hidden');
this.inputElement.classList.remove('hidden');
this.filenameWrapper.classList.add('hidden');
this.imagePreview.setAttribute('src', '');
}

this.inputElement.value = '';
}
}
94 changes: 50 additions & 44 deletions assets/sass/backoffice/_my_account.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
.my-account {


&__image-container {
display: flex;
gap: 1rem;

@include media-breakpoint-down(sm) {
flex-direction: column;
}
}

&__avatar {
border-radius: 4px;
max-width: 200px;
max-height: 200px;

@include media-breakpoint-down(sm) {
align-self: flex-start;
}

&[src=''] {
display: none;
}
}

.custom-image-upload {
display: flex;
flex-direction: column;
gap: 0.75rem;

.file-wrapper {
width: unset;
}

.btn-replace-image {
align-self: flex-start;
margin-left: 0.5rem;
}
}

form {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -55,44 +95,6 @@
}
}

.my-account-image-container {
display: flex;
gap: 1rem;

@include media-breakpoint-down(sm) {
flex-direction: column;

img {
align-self: flex-start;
}
}

img {
border-radius: 4px;
max-width: 200px;
max-height: 200px;

&[src=''] {
display: none;
}

+ .custom-image-upload {
display: flex;
flex-direction: column;
gap: 0.75rem;

.file-wrapper {
width: unset;
}

.btn-replace-image {
align-self: unset;
margin-left: 0.5rem;
}
}
}
}

> .checkbox-row {

@include media-breakpoint-down(sm) {
Expand Down Expand Up @@ -204,15 +206,15 @@
margin-left: auto;
display: inline-block;

@include media-breakpoint-down(lg) {
width: 100%;
}

p {
font-size: 0.875rem;
color: $gray-54;
margin-bottom: 0;
}

@include media-breakpoint-down(lg) {
width: 100%;
}
}

.form-actions {
Expand All @@ -223,15 +225,19 @@
justify-content: space-between;

@include media-breakpoint-down(lg) {
justify-content: center;
justify-content: flex-start;
}

button {
button {

@include media-breakpoint-down(lg) {
margin-bottom: 0.5rem;
}
}
}

&.my-account-preferences {

div.checkbox-row {
display: flex;
align-items: center;
Expand Down
10 changes: 5 additions & 5 deletions assets/sass/backoffice/_top_navigation.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.top-navigation {
height: 60px;
height: 3.75rem;
border-bottom: 2px solid $secondary_2;

ul {
Expand All @@ -9,9 +9,9 @@
}

&-btn {
height: 60px;
height: 3.75rem;

@media (max-width: 768px) {
@include media-breakpoint-down(md) {
flex: 1;
}

Expand All @@ -32,7 +32,7 @@

cursor: pointer;

@media (max-width: 768px) {
@include media-breakpoint-down(md) {
text-align: center;
padding: 0 0.5rem;
justify-content: center;
Expand All @@ -45,7 +45,7 @@
&:hover {
background-color: $secondary_4;
border-color: $secondary_4;
color: $darkgray;
color: $white;
}
}

Expand Down
6 changes: 3 additions & 3 deletions templates/backoffice/my_account/personal_details.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
{% endif %}
<div>
{{ form_label(form.image) }}
<div class="my-account-image-container">
<div class="my-account__image-container">
{% if not ibexa_field_is_empty(app.user.APIUser, 'image') %}
<img class="avatar js-image-preview" src="{{ asset(ibexa_image_alias(app.user.APIUser.field('image'), app.user.APIUser.versionInfo, 'i160').uri) }}" alt="">
<img class="my-account__avatar js-image-preview" src="{{ asset(ibexa_image_alias(app.user.APIUser.field('image'), app.user.APIUser.versionInfo, 'i160').uri) }}" alt="">
{% else %}
<img class="avatar js-image-preview" src="" alt="">
<img class="my-account__avatar js-image-preview" src="" alt="">
{% endif %}
{{ form_widget(form.image, {remove_image_form: form.remove_image}) }}
</div>
Expand Down

0 comments on commit aeec4c8

Please sign in to comment.