-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NGSTACK-878 add js for image upload and modify css
- Loading branch information
1 parent
c8f66cb
commit aeec4c8
Showing
7 changed files
with
169 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters