-
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
Правда или действие #10
base: master
Are you sure you want to change the base?
Changes from 5 commits
c212453
f3ddfdc
78d72df
1a15b21
bacb8ef
980652b
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { photoslist } from './modules/photo-template.js'; | ||
import { displaysPictures } from './modules/photo-template.js'; | ||
import './modules/upload-form.js'; | ||
|
||
displaysPictures(photoslist); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { isEscapeKey } from './util'; | ||
|
||
const regexp = /^#[a-zф-яё0-9]{1,19}$/i; | ||
const HASHTAGS_COUNT = 5; | ||
const COMMENT_LENGTH = 140; | ||
|
||
const formUpload = document.querySelector('.img-upload__form'); | ||
const imageUploadInput = formUpload.querySelector('.img-upload__input'); | ||
const imageOverlay = formUpload.querySelector('.img-upload__overlay'); | ||
const imageUploadCancle = formUpload.querySelector('.img-upload__cancel'); | ||
const uploadHashtag = formUpload.querySelector('.text__hashtags'); | ||
const uploadComment = formUpload.querySelector('.text__description'); | ||
|
||
|
||
const pristine = new Pristine(formUpload, { | ||
classTo: 'img-upload__field-wrapper', | ||
errorTextParent: 'img-upload__field-wrapper', | ||
errorTextTag: 'div', | ||
errorTextClass: 'img-upload__field-wrapper--error' | ||
}); | ||
|
||
const onDocumentKeyDown = (evt) => { | ||
if(isEscapeKey(evt)){ | ||
evt.preventDefault(); | ||
closeUploadWindow(); | ||
} | ||
}; | ||
|
||
const onCancleKeyDown = (evt) => { | ||
if(isEscapeKey(evt)){ | ||
evt.stopPropagation(); | ||
} | ||
}; | ||
|
||
const openUploadWindow = () => { | ||
imageOverlay.classList.remove('hidden'); | ||
document.body.classList.add('modal-open'); | ||
uploadHashtag.addEventListener('keydown', onCancleKeyDown); | ||
uploadComment.addEventListener('keydown', onCancleKeyDown); | ||
document.addEventListener('keydown', onDocumentKeyDown); | ||
}; | ||
|
||
function closeUploadWindow() { | ||
imageUploadInput.value = ''; | ||
uploadHashtag.value = ''; | ||
uploadComment.value = ''; | ||
imageOverlay.classList.add('hidden'); | ||
document.body.classList.remove('modal-open'); | ||
document.removeEventListener('keydown', onDocumentKeyDown); | ||
uploadHashtag.removeEventListener('focus', onCancleKeyDown); | ||
uploadComment.removeEventListener('focus', onCancleKeyDown); | ||
} | ||
|
||
imageUploadInput.addEventListener('change', openUploadWindow); | ||
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. именование обработчиков |
||
|
||
imageUploadCancle.addEventListener('click', closeUploadWindow); | ||
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. именование обработчиков |
||
|
||
const getHashtagsArray = (element) => { | ||
const hashtagsArray = element.toLowerCase().split(/\s+/).filter((item) => item !== ''); | ||
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. const hashtagsArray = element.toLowerCase().split(/\s+/).filter(Boolean); |
||
return hashtagsArray; | ||
}; | ||
|
||
const getHashtagsName = (element) => { | ||
const hashtagsName = getHashtagsArray(element); | ||
const newArray = []; | ||
|
||
hashtagsName.forEach((hashtag) => { | ||
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. |
||
if(regexp.test(hashtag) === true) { | ||
newArray.push(hashtag); | ||
} | ||
}); | ||
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. if(regexp.test(hashtag)) { |
||
|
||
return hashtagsName.length === newArray.length; | ||
}; | ||
|
||
const getHashtagsCount = (element) => { | ||
const hashtagsArray = getHashtagsArray(element); | ||
return hashtagsArray.length <= HASHTAGS_COUNT; | ||
}; | ||
|
||
const getHashtagsDuplicate = (element) => { | ||
const hashtagsArray = getHashtagsArray(element); | ||
const newArray = []; | ||
for (let i = 0; i < hashtagsArray.length; i++) { | ||
if(!newArray.includes(hashtagsArray[i])){ | ||
newArray.push(hashtagsArray[i]); | ||
} | ||
} | ||
if(newArray.length === hashtagsArray.length) { | ||
return newArray; | ||
} | ||
}; | ||
|
||
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. new Set(hashtagsArray ).size === hashtagsArray .length |
||
const getCommentLength = (element) => element.length < COMMENT_LENGTH; | ||
|
||
|
||
pristine.addValidator(uploadHashtag, getHashtagsName, 'введён невалидный хэштег'); | ||
pristine.addValidator(uploadHashtag, getHashtagsCount, 'превышено количество хэштегов'); | ||
pristine.addValidator(uploadHashtag, getHashtagsDuplicate, 'хэштеги повторяются'); | ||
pristine.addValidator(uploadComment, getCommentLength, 'длина комментария больше 140 символов'); | ||
|
||
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. ошибки в константы |
||
formUpload.addEventListener('submit', (evt) => { | ||
evt.preventDefault(); | ||
pristine.validate(); | ||
}); |
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.
должны быть сразу после import