-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Правда или действие #8
Changes from all commits
1951f01
199e614
8ff9b20
a0e6a4b
601014a
587f320
e7a07bb
461ceff
95f03c9
1dbd8c8
a33bdda
d9fe107
511f2c2
5ff0151
486d4e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import {isEscapeKey} from"./util.js"; | ||
const imgInput = document.querySelector(".img-upload__input"); | ||
const body = document.body; | ||
const imgOverlay = document.querySelector(".img-upload__overlay"); | ||
const closeImg = document.querySelector(".img-upload__cancel"); | ||
const postForm = document.querySelector(".img-upload__form"); | ||
const formButton = document.querySelector(".img-upload__submit"); | ||
const pristine = new Pristine(postForm, { | ||
classTo: "img-upload__text", | ||
errorTextParent: "img-upload__text", | ||
}); | ||
const regexp = /^#[^\s#]+$/i; | ||
|
||
imgInput.addEventListener("change", (evt) => { | ||
evt.preventDefault(); | ||
imgOverlay.classList.remove("hidden"); | ||
body.classList.add("modal-open"); | ||
pristine.validate(); | ||
closeFormClick(); | ||
// eslint-disable-next-line no-shadow | ||
postForm.addEventListener("submit",(evt) => { | ||
evt.preventDefault(); | ||
formButton.disabled = true; | ||
if(pristine.validate()){ | ||
openSuccess(); | ||
}else { | ||
openError(); | ||
} | ||
formButton.disabled = false; | ||
}); | ||
}); | ||
|
||
function validateHashtags (value) { | ||
const valueTrim = value.trim(); | ||
const hashtags = valueTrim.split(" "); | ||
if (valueTrim === "") { | ||
return true; | ||
} | ||
pristine.errorText = "введён невалидный хэш-тег"; | ||
return hashtags.every((hashtag) => regexp.test(hashtag)); | ||
} | ||
function validateComment (value) { | ||
if (value.trim() === "") { | ||
return true; | ||
} | ||
return value.length <= 140; | ||
} | ||
const hastags = document.querySelector(".text__hashtags"); | ||
const postText = document.querySelector(".text__description"); | ||
|
||
function dublicateHashtags(value) { | ||
const hashtags = value.trim().split(" "); | ||
for (let i = 0; i < hashtags.length; i++){ | ||
const hashtag = hashtags[i].toLowerCase(); | ||
if (hashtags.slice(0, i).includes(hashtag)) { | ||
pristine.errorText = "хэш-теги повторяются"; | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
pristine.addValidator( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Исправила There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
hastags, | ||
(value) => { | ||
if (!validateHashtags(value)) { | ||
return false; | ||
} | ||
if (!dublicateHashtags(value)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
() => pristine.errorText | ||
); | ||
|
||
pristine.addValidator( | ||
postText, | ||
validateComment, | ||
"комментарий не может содрежать больше 140 символов" | ||
); | ||
|
||
|
||
function openSuccess (){ | ||
const successTemplate = document.querySelector("#success").content; | ||
const successElement = successTemplate.cloneNode(true).querySelector(".success"); | ||
const successButton = successElement.querySelector(".success__button"); | ||
const successInner = successElement.querySelector(".success__inner"); | ||
|
||
document.body.append(successElement); | ||
hastags.value = ""; | ||
postText.value = ""; | ||
|
||
successButton.addEventListener("click", closeSuccess); | ||
document.addEventListener("keydown", closeSuccessEscape); | ||
document.addEventListener("click", closeOutside); | ||
|
||
function closeSuccess(){ | ||
successElement.remove(); | ||
document.removeEventListener("keydown",closeSuccessEscape); | ||
document.removeEventListener("click",closeOutside); | ||
imgOverlay.classList.add("hidden"); | ||
body.classList.remove("modal-open"); | ||
} | ||
|
||
function closeSuccessEscape (evt){ | ||
if(isEscapeKey(evt)) { | ||
evt.preventDefault(); | ||
closeSuccess(); | ||
} | ||
|
||
} | ||
function closeOutside(evt){ | ||
if (!successInner.contains(evt.target)){ | ||
closeSuccess(); | ||
} | ||
} | ||
} | ||
|
||
|
||
function openError (){ | ||
const errorTemplate = document.querySelector("#error").content; | ||
const errorElement = errorTemplate.cloneNode(true).querySelector(".error"); | ||
const errorButton = errorElement.querySelector(".error__button"); | ||
const errorInner = errorElement.querySelector(".error__inner"); | ||
|
||
document.body.append(errorElement); | ||
|
||
|
||
errorButton.addEventListener("click", closeError); | ||
document.addEventListener("keydown", closeErrorEscape); | ||
document.addEventListener("click", closeOutside); | ||
document.removeEventListener("click",closeForm); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Может без слушателя лучше, а внутрь других слушателей сложить? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
|
||
function closeError(){ | ||
errorElement.remove(); | ||
document.removeEventListener("keydown",closeErrorEscape); | ||
document.removeEventListener("click",closeOutside); | ||
} | ||
|
||
function closeErrorEscape (evt){ | ||
if(isEscapeKey(evt)) { | ||
evt.preventDefault(); | ||
closeError(); | ||
imgOverlay.classList.remove("hidden"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В других обработчиках нет этого( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В других обработчиках нет этого, так как при нажатии на esc без этой строчки с убиранием класса hidden, у меня закрывалось и модальное окно с фотографией, а в других случаях такого не было There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
body.classList.add("modal-open"); | ||
} | ||
|
||
} | ||
function closeOutside(evt){ | ||
if (!errorInner.contains(evt.target)){ | ||
closeError(); | ||
} | ||
} | ||
} | ||
|
||
function closeForm(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На закрытие нужно обнулять состояния и скидывать загруженное фото. Сейчас странно работает. Если закрыть через кнопку, то можно загрузить тоже фото, если закрыть через esc, тоже фото нельзя загрузить - оно не сбрасывается из инпута скорее всего. Плюс слушатели закрытия на закрытие нужно удалять There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Теперь удаляю форму и слушатель на закрытие There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
imgOverlay.classList.add("hidden"); | ||
body.classList.remove("modal-open"); | ||
postForm.reset(); | ||
document.removeEventListener(closeFormClick); | ||
} | ||
|
||
function closeFormClick (){ | ||
closeImg.addEventListener("click",closeForm); | ||
document.addEventListener("keydown", (evt) => { | ||
if (isEscapeKey(evt)) { | ||
evt.preventDefault(); | ||
closeForm(); | ||
} | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
import {renderPosts} from"./posts-rendering.js"; | ||
import "./form-filling.js"; | ||
import {loadPhoto} from "./photo-load.js"; | ||
renderPosts(); | ||
loadPhoto(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {FILE_TYPES} from "./constants.js"; | ||
function loadPhoto(){ | ||
const imgInput = document.querySelector(".img-upload__input"); | ||
const preview = document.querySelector(".img-upload__preview img"); | ||
|
||
imgInput.addEventListener("change", () => { | ||
const file = imgInput.files[0]; | ||
const fileName = file.name.toLowerCase(); | ||
const matches = FILE_TYPES.some((it) => fileName.endsWith(it)); | ||
if(matches){ | ||
preview.src = URL.createObjectURL(file); | ||
} | ||
}); | ||
} | ||
|
||
export{loadPhoto}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему нельзя все это сделать в одном событии? Сейчас неочевидна логика работы(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надеюсь правильно поняла, теперь у меня в слушателе для input вызываются слушатели на другие кнопки