-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dennyhappysomuch/module12-task2
Module12 task2
- Loading branch information
Showing
20 changed files
with
796 additions
and
301 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*.psd | ||
*.sublime* | ||
js1-kekstagram-e2e/ | ||
build/ | ||
node_modules/ | ||
npm-debug.log* | ||
|
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,19 @@ | ||
import { BASE_URL, ROUTE, METHOD, ERROR_TEXT } from './init.js'; | ||
|
||
const load = (route, errorText, method = METHOD.GET, body = null) => | ||
fetch(`${BASE_URL}${route}`, {method, body}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error(); | ||
} | ||
return response.json(); | ||
}) | ||
.catch(() => { | ||
throw new Error(errorText); | ||
}); | ||
|
||
const getData = () => load(ROUTE.GET_DATA, ERROR_TEXT.GET_DATA); | ||
|
||
const sendData = (body) => load(ROUTE.SEND_DATA, ERROR_TEXT.SEND_DATA, METHOD.POST, body); | ||
|
||
export {getData, sendData}; |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
import { FILE_TYPES } from './init.js'; | ||
|
||
const uploadFormElement = document.querySelector('.img-upload__form'); | ||
const uploadFileElement = uploadFormElement.querySelector('#upload-file'); | ||
|
||
const isValidType = (file) => { | ||
const fileName = file.name.toLowerCase(); | ||
return FILE_TYPES.some((item) => fileName.endsWith(item)); | ||
}; | ||
|
||
const isFileValid = () => { | ||
const file = uploadFileElement.files[0]; | ||
return file && isValidType(file); | ||
}; | ||
|
||
export { isFileValid }; |
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,56 @@ | ||
import { closeUploadForm } from './form.js'; | ||
import { isEscapeKey } from './utils.js'; | ||
import { removePopup } from './popup.js'; | ||
|
||
const uploadFormElement = document.querySelector('.img-upload__form'); | ||
const formOverlayElement = uploadFormElement.querySelector('.img-upload__overlay'); | ||
const closeButtonElement = uploadFormElement.querySelector('#upload-cancel'); | ||
|
||
const onFormOverlayClick = (evt) => { | ||
if (evt.target.classList.contains('img-upload__overlay')) { | ||
closeUploadForm(); | ||
} | ||
}; | ||
|
||
const onFormCloseClick = (evt) => { | ||
if (evt.target.id === 'upload-cancel') { | ||
closeUploadForm(); | ||
} | ||
}; | ||
|
||
const onFormEscapeDown = (evt) => { | ||
if ((isEscapeKey(evt) | ||
&& !evt.target.classList.contains('text__hashtags') | ||
&& !evt.target.classList.contains('text__description') | ||
&& !document.querySelector('.error'))) { | ||
closeUploadForm(); | ||
} | ||
}; | ||
|
||
const setFormCloseFormEvent = () => { | ||
formOverlayElement.addEventListener('click', onFormOverlayClick); | ||
closeButtonElement.addEventListener('click', onFormCloseClick); | ||
document.addEventListener('keydown', onFormEscapeDown); | ||
}; | ||
|
||
const removeFormCloseEvent = () => { | ||
formOverlayElement.removeEventListener('click', onFormOverlayClick); | ||
closeButtonElement.removeEventListener('click', onFormCloseClick); | ||
document.removeEventListener('keydown', onFormEscapeDown); | ||
}; | ||
|
||
const onPopupEscapeDown = (evt) => { | ||
if (isEscapeKey(evt)) { | ||
removePopup(); | ||
} | ||
}; | ||
|
||
const setPopupEscapeControl = () => { | ||
document.addEventListener('keydown', onPopupEscapeDown); | ||
}; | ||
|
||
const removePopupEscapeControl = () => { | ||
document.removeEventListener('keydown', onPopupEscapeDown); | ||
}; | ||
|
||
export { setFormCloseFormEvent, removeFormCloseEvent, setPopupEscapeControl, removePopupEscapeControl }; |
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,88 @@ | ||
import { EFFECT_DEFAULT, EFFECTS } from './init.js'; | ||
|
||
const sliderContainerElement = document.querySelector('.img-upload__effect-level'); | ||
const sliderElement = sliderContainerElement.querySelector('.effect-level__slider'); | ||
const sliderInputElement = sliderContainerElement.querySelector('.effect-level__value'); | ||
const effectListElement = document.querySelector('.effects__list'); | ||
const effectItemElements = document.querySelectorAll('.effects__item'); | ||
const previewImageElement = document.querySelector('.img-upload__preview img'); | ||
|
||
let filterActive = EFFECT_DEFAULT; | ||
|
||
const isDefault = () => filterActive === EFFECT_DEFAULT; | ||
|
||
const showSlider = () => { | ||
sliderContainerElement.classList.remove('hidden'); | ||
}; | ||
|
||
const hideSlider = () => { | ||
sliderContainerElement.classList.add('hidden'); | ||
}; | ||
|
||
const setImageStyles = () => { | ||
if (isDefault()) { | ||
previewImageElement.style.filter = null; | ||
return; | ||
} | ||
const { value } = sliderInputElement; | ||
const { name, units } = EFFECTS[filterActive]; | ||
previewImageElement.style.filter = `${name}(${value}${units})`; | ||
}; | ||
|
||
const sliderUpdateHandler = () => { | ||
sliderInputElement.value = sliderElement.noUiSlider.get(); | ||
setImageStyles(); | ||
}; | ||
|
||
const createSlider = ({ min, max, step }) => { | ||
noUiSlider.create(sliderElement, { | ||
range: { | ||
min: min, | ||
max: max, | ||
}, | ||
start: max, | ||
step: step, | ||
connect: 'lower', | ||
format: { | ||
to: (value) => Number(value), | ||
from: (value) => Number(value), | ||
} | ||
}); | ||
sliderElement.noUiSlider.on('update', sliderUpdateHandler); | ||
hideSlider(); | ||
}; | ||
|
||
const updateSlider = ({ min, max, step }) => { | ||
sliderElement.noUiSlider.updateOptions({ | ||
step: step, | ||
range: { min: min, max: max }, | ||
start: max, | ||
}); | ||
}; | ||
|
||
const setSlider = () => { | ||
updateSlider(EFFECTS[filterActive].config); | ||
showSlider(); | ||
if (isDefault()) { | ||
hideSlider(); | ||
} | ||
}; | ||
|
||
const startFilterHandler = (evt) => { | ||
filterActive = evt.target.value; | ||
setSlider(); | ||
setImageStyles(); | ||
}; | ||
|
||
const setFilters = () => { | ||
createSlider(EFFECTS[EFFECT_DEFAULT].config); | ||
effectListElement.addEventListener('change', startFilterHandler); | ||
}; | ||
|
||
const removeFilters = () => { | ||
effectItemElements[0].children[0].checked = true; | ||
effectListElement.removeEventListener('change', startFilterHandler); | ||
sliderElement.noUiSlider?.destroy(); | ||
}; | ||
|
||
export { setFilters, removeFilters }; |
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,52 @@ | ||
import { SCALE_DEFAULT, SCALE_STEP, SCALE_MAX, SCALE_MIN, SCALE_RATIO } from './init.js'; | ||
|
||
const state = { | ||
scale: SCALE_DEFAULT, | ||
}; | ||
|
||
const buttonUpElement = document.querySelector('.scale__control--bigger'); | ||
const buttonDownElement = document.querySelector('.scale__control--smaller'); | ||
const currentScaleElement = document.querySelector('.scale__control--value'); | ||
const previewImageElement = document.querySelector('.img-upload__preview img'); | ||
|
||
const renderImage = () => { | ||
previewImageElement.style.transform = `scale(${state.scale * SCALE_RATIO})`; | ||
currentScaleElement.attributes.value.textContent = `${state.scale}%`; | ||
currentScaleElement.value = `${state.scale}%`; | ||
}; | ||
|
||
const onButtonUpClick = () => { | ||
if (state.scale + SCALE_STEP <= SCALE_MAX) { | ||
state.scale += SCALE_STEP; | ||
} | ||
renderImage(); | ||
}; | ||
|
||
const onButtonDownClick = () => { | ||
if (state.scale - SCALE_STEP >= SCALE_MIN) { | ||
state.scale -= SCALE_STEP; | ||
} | ||
renderImage(); | ||
}; | ||
|
||
const setScaleDefault = () => { | ||
state.scale = SCALE_DEFAULT; | ||
previewImageElement.style.transform = `scale(${SCALE_DEFAULT * SCALE_RATIO})`; | ||
currentScaleElement.value = `${SCALE_DEFAULT}%`; | ||
}; | ||
|
||
const setScale = () => { | ||
setScaleDefault(); | ||
buttonUpElement.addEventListener('click', onButtonUpClick); | ||
buttonDownElement.addEventListener('click', onButtonDownClick); | ||
}; | ||
|
||
const removeScale = () => { | ||
state.scale = SCALE_DEFAULT; | ||
previewImageElement.style.transform = `scale(${SCALE_DEFAULT * SCALE_RATIO})`; | ||
currentScaleElement.value = `${SCALE_DEFAULT}%`; | ||
buttonUpElement.removeEventListener('click', onButtonUpClick); | ||
buttonDownElement.removeEventListener('click', onButtonDownClick); | ||
}; | ||
|
||
export { setScale, removeScale }; |
Oops, something went wrong.