From c60db5b4507f4d47cd97985cb2fe4c8a76ef7f9d Mon Sep 17 00:00:00 2001 From: Seal Date: Mon, 19 Mar 2018 17:49:57 +0300 Subject: [PATCH 1/5] add jsfunc --- task4/js/script.js | 259 ++++++++++++++++++++++++++++++++++++++++++++ task4/mainPage.html | 251 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 510 insertions(+) create mode 100644 task4/js/script.js create mode 100644 task4/mainPage.html diff --git a/task4/js/script.js b/task4/js/script.js new file mode 100644 index 0000000..c40c495 --- /dev/null +++ b/task4/js/script.js @@ -0,0 +1,259 @@ + (function() { +var photoPosts = [ + { + id: '1', + description: 'NARUTO best anime', + createdAt: new Date('2012-12-12T12:12:12'), + author: 'Misha2018', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Alex"] + }, + + { + id: '2', + description: 'AVATAR best anime', + createdAt: new Date('2013-12-12T12:12:12'), + author: 'Misha2019', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Egor"] + }, { + id: '3', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2014-12-12T12:12:12'), + author: 'Misha2019', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Pomeedorka"] + }, + { + id: '4', + description: 'NARURO best anime', + createdAt: new Date('2015-12-12T12:12:12'), + author: 'Misha20', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Jora"] + }, + { + id: '5', + description: 'AVATAR best anime', + createdAt: new Date('2016-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Jaga"] + }, + { + id: '6', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2017-12-12T12:12:12'), + author: 'Misha22', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Jaguar"] + }, + { + id: '7', + description: 'NARUTO best anime', + createdAt: new Date('2018-12-12T12:12:12'), + author: 'Misha23', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Jagorka"] + }, + { + id: '8', + description: 'AVATAR best anime', + createdAt: new Date('2019-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Igor"] + }, + { + id: '9', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2020-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Egr"] + }, + +]; + + + + var sortedArrOfPosts = getPhotoPosts(0, 9, "Misha"); + var postOfId = getPhotoPost(5); + console.log("Пост по id: "+postOfId.createdAt); + console.log("Проверка этого поста на валидность: "+validatePhotoPost(postOfId)); + var newPost = { + id: '10', + description: 'NARUTO best anime (one more post)', + createdAt: new Date('2012-12-12T12:12:12'), + author: 'Misha2018', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#JUSTNARUTO"], + likes: ["Maladec"] + }; + console.log("Добавление нового поста с предварительной проверкой: "+addPhotoPost(newPost)); + var postForEditing = { + description: 'NARUTO. Just Naruto', + createdAt: new Date('2007-12-12T12:12:12') + } + console.log("Изменение поста по id: " + editPhotoPost("1",postForEditing)); + + for(var i=0;i photoPosts.length || postStart<0){ + console.log("Нельзя вывести столько постов. Промежуток вывода не совпадает с количеством постов"); + } + else{ + var sortedArrOfPosts = []; + var count=0; + for(var i=postStart; i sortedArrOfPosts[j].createdAt){ + cont = sortedArrOfPosts[i]; + sortedArrOfPosts[i]=sortedArrOfPosts[j]; + sortedArrOfPosts[j]=cont; + } + } + } + } + return sortedArrOfPosts; + } + } + + function getPhotoPost(idOfPost){ + for(var i=0;i200) { + console.log("Неверный description"); + return false; + } + if(postForCheck.author == null || postForCheck.author == "") { + console.log("Поле author пустое"); + return false; + } + if(postForCheck.createdAt == null || (new Date(postForCheck.createdAt) == 'Invalid Date')) { + console.log("Поле даты создания не введено, либо имеет неправильную структуру"); + return false; + } + if(postForCheck.photoLink == null || postForCheck == "") { + console.log("Поле photoLink пустое"); + return false; + } + return true; + } + + function addPhotoPost(newPost){ + if(validatePhotoPost(newPost) == false) return false; + else{ + photoPosts[photoPosts.length] = newPost; + return true; + } + } + + function editPhotoPost(idForEd, postForEd){ + for(var i=0; i + + + + + Title + +
+
+
+
+
Seal98
+
+
+
+ +
+ + + +
+
+ А какое аниме смотрел сегодня ты? +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ + +
+ +
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2012
+
Misha2018
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2013
+
Misha2019
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2014
+
Misha20
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2015
+
Misha21
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2016
+
Misha22
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2017
+
Misha23
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2018
+
Misha24
+
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2019
+
Misha25
+
+
+ +
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
+ +
+
+ +
+ +
12.12.2020
+
Misha26
+
+ + +
+ + + +
+ + +
+ +
+ From 400580daf195354207c7abec367814cc55b4ab92 Mon Sep 17 00:00:00 2001 From: Seal Date: Tue, 20 Mar 2018 17:04:10 +0300 Subject: [PATCH 2/5] js changes --- task4/js/script.js | 197 ++++++++++++++++++++++++++------------------- 1 file changed, 116 insertions(+), 81 deletions(-) diff --git a/task4/js/script.js b/task4/js/script.js index c40c495..7b2036a 100644 --- a/task4/js/script.js +++ b/task4/js/script.js @@ -3,7 +3,7 @@ var photoPosts = [ { id: '1', description: 'NARUTO best anime', - createdAt: new Date('2012-12-12T12:12:12'), + createdAt: new Date('2014-12-12T12:12:12'), author: 'Misha2018', photoLink: 'img/naruto.png', postVisibility: true, @@ -14,7 +14,7 @@ var photoPosts = [ { id: '2', description: 'AVATAR best anime', - createdAt: new Date('2013-12-12T12:12:12'), + createdAt: new Date('2015-12-12T12:12:12'), author: 'Misha2019', photoLink: 'img/avatar.png', postVisibility: true, @@ -23,7 +23,7 @@ var photoPosts = [ }, { id: '3', description: 'SHAMAN-KING best anime', - createdAt: new Date('2014-12-12T12:12:12'), + createdAt: new Date('2012-12-12T12:12:12'), author: 'Misha2019', photoLink: 'img/sham.png', postVisibility: true, @@ -33,7 +33,7 @@ var photoPosts = [ { id: '4', description: 'NARURO best anime', - createdAt: new Date('2015-12-12T12:12:12'), + createdAt: new Date('2013-12-12T12:12:12'), author: 'Misha20', photoLink: 'img/naruto.png', postVisibility: true, @@ -43,7 +43,7 @@ var photoPosts = [ { id: '5', description: 'AVATAR best anime', - createdAt: new Date('2016-12-12T12:12:12'), + createdAt: new Date('2018-12-12T12:12:12'), author: 'Misha', photoLink: 'img/avatar.png', postVisibility: true, @@ -63,7 +63,7 @@ var photoPosts = [ { id: '7', description: 'NARUTO best anime', - createdAt: new Date('2018-12-12T12:12:12'), + createdAt: new Date('2016-12-12T12:12:12'), author: 'Misha23', photoLink: 'img/naruto.png', postVisibility: true, @@ -73,7 +73,7 @@ var photoPosts = [ { id: '8', description: 'AVATAR best anime', - createdAt: new Date('2019-12-12T12:12:12'), + createdAt: new Date('2091-12-12T12:12:12'), author: 'Misha', photoLink: 'img/avatar.png', postVisibility: true, @@ -92,13 +92,27 @@ var photoPosts = [ }, ]; - - - - var sortedArrOfPosts = getPhotoPosts(0, 9, "Misha"); - var postOfId = getPhotoPost(5); + var postForFilter = { + author: 'Misha' + }; + console.log("\n\nСортировка и фильтрация постов. id найденных: \n"); + var sortedArrOfPosts = getPhotoPosts(2, 7, postForFilter); + for(var i=0;i photoPosts.length || postStart<0){ console.log("Нельзя вывести столько постов. Промежуток вывода не совпадает с количеством постов"); + return null; } else{ - var sortedArrOfPosts = []; - var count=0; - for(var i=postStart; i sortedArrOfPosts[j].createdAt){ - cont = sortedArrOfPosts[i]; - sortedArrOfPosts[i]=sortedArrOfPosts[j]; - sortedArrOfPosts[j]=cont; - } - } - } + if(filterConfig.author){ + sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { + return post.author === filterConfig.author; + }); } return sortedArrOfPosts; } } + function dateComparator(date1, date2){ + return date1.createdAt - date2.createdAt; + } + function getPhotoPost(idOfPost){ - for(var i=0;i200) { + + if(typeof postForCheck.description !== "string" || postForCheck.description.length>200 || postForCheck.description==="") { console.log("Неверный description"); return false; } - if(postForCheck.author == null || postForCheck.author == "") { + if(typeof postForCheck.author !== "string" || postForCheck.author === "") { console.log("Поле author пустое"); return false; } - if(postForCheck.createdAt == null || (new Date(postForCheck.createdAt) == 'Invalid Date')) { + if(!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { console.log("Поле даты создания не введено, либо имеет неправильную структуру"); return false; } - if(postForCheck.photoLink == null || postForCheck == "") { + if(typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { console.log("Поле photoLink пустое"); return false; } + if(!(postForCheck.hashTags instanceof Array)){ + console.log("Структура поля hashTags не верна"); + return false; + } return true; } function addPhotoPost(newPost){ - if(validatePhotoPost(newPost) == false) return false; + if(!validatePhotoPost(newPost)) return false; else{ - photoPosts[photoPosts.length] = newPost; + photoPosts.push(newPost); return true; } } function editPhotoPost(idForEd, postForEd){ - for(var i=0; i200 || postForEd.description===""){ + return false; } + postOfId.description = postForEd.description; } - console.log("Пост с таким id не найден"); - return false; + if(postForEd.author){ + if(typeof postForEd.author !== "string" || postForEd.author === "") { + return false; + } + postOfId.author = postForEd.author; + } + if(postForEd.createdAt){ + if(!(postForEd.createdAt instanceof Date) || ((postForEd.createdAt).toString() === 'Invalid Date')) { + return false; + } + postOfId.createdAt = postForEd.createdAt; + } + if(postForEd.photoLink){ + if(typeof postForEd.photoLink !== "string" || postForEd.photoLink === "") { + return false; + } + postOfId.photoLink = postForEd.photoLink; + } + if(postForEd.hashTags){ + if(!(postForEd.hashTags instanceof Array)){ + return false; + } + postOfId.hashTags = postForEd.hashTags; + } + return true; } function removePhotoPost(idForRem){ for(var i=0;i Date: Tue, 20 Mar 2018 17:08:26 +0300 Subject: [PATCH 3/5] changes for remove function --- task4/js/script.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/task4/js/script.js b/task4/js/script.js index 7b2036a..c3f4082 100644 --- a/task4/js/script.js +++ b/task4/js/script.js @@ -282,12 +282,8 @@ var photoPosts = [ } function removePhotoPost(idForRem){ - for(var i=0;i Date: Sun, 25 Mar 2018 12:06:04 +0300 Subject: [PATCH 4/5] functional changes2 --- task4/js/script.js | 428 ++++++++++++++++++++++++-------------------- task4/mainPage.html | 251 -------------------------- 2 files changed, 229 insertions(+), 450 deletions(-) delete mode 100644 task4/mainPage.html diff --git a/task4/js/script.js b/task4/js/script.js index c3f4082..334d29b 100644 --- a/task4/js/script.js +++ b/task4/js/script.js @@ -1,118 +1,118 @@ - (function() { -var photoPosts = [ - { - id: '1', - description: 'NARUTO best anime', - createdAt: new Date('2014-12-12T12:12:12'), - author: 'Misha2018', - photoLink: 'img/naruto.png', - postVisibility: true, - hashTags: ["#NARUTOBESTANIME"], - likes: ["Alex"] - }, +(function () { + var photoPosts = [ + { + id: '1', + description: 'NARUTO best anime', + createdAt: new Date('2014-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Alex"] + }, - { - id: '2', - description: 'AVATAR best anime', - createdAt: new Date('2015-12-12T12:12:12'), - author: 'Misha2019', - photoLink: 'img/avatar.png', - postVisibility: true, - hashTags: ["#AVATARBESTANIME"], - likes: ["Egor"] - }, { - id: '3', - description: 'SHAMAN-KING best anime', - createdAt: new Date('2012-12-12T12:12:12'), - author: 'Misha2019', - photoLink: 'img/sham.png', - postVisibility: true, - hashTags: ["#SHAMANKINGBESTANIME"], - likes: ["Pomeedorka"] - }, - { - id: '4', - description: 'NARURO best anime', - createdAt: new Date('2013-12-12T12:12:12'), - author: 'Misha20', - photoLink: 'img/naruto.png', - postVisibility: true, - hashTags: ["#NARUTOBESTANIME"], - likes: ["Jora"] - }, - { - id: '5', - description: 'AVATAR best anime', - createdAt: new Date('2018-12-12T12:12:12'), - author: 'Misha', - photoLink: 'img/avatar.png', - postVisibility: true, - hashTags: ["#AVATARBESTANIME"], - likes: ["Jaga"] - }, - { - id: '6', - description: 'SHAMAN-KING best anime', - createdAt: new Date('2017-12-12T12:12:12'), - author: 'Misha22', - photoLink: 'img/sham.png', - postVisibility: true, - hashTags: ["#SHAMANKINGBESTANIME"], - likes: ["Jaguar"] - }, - { - id: '7', - description: 'NARUTO best anime', - createdAt: new Date('2016-12-12T12:12:12'), - author: 'Misha23', - photoLink: 'img/naruto.png', - postVisibility: true, - hashTags: ["#NARUTOBESTANIME"], - likes: ["Jagorka"] - }, - { - id: '8', - description: 'AVATAR best anime', - createdAt: new Date('2091-12-12T12:12:12'), - author: 'Misha', - photoLink: 'img/avatar.png', - postVisibility: true, - hashTags: ["#AVATARBESTANIME"], - likes: ["Igor"] - }, - { - id: '9', - description: 'SHAMAN-KING best anime', - createdAt: new Date('2020-12-12T12:12:12'), - author: 'Misha', - photoLink: 'img/sham.png', - postVisibility: true, - hashTags: ["#SHAMANKINGBESTANIME"], - likes: ["Egr"] - }, + { + id: '2', + description: 'AVATAR best anime', + createdAt: new Date('2015-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Egor"] + }, { + id: '3', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2012-12-12T12:12:12'), + author: 'Misha2019', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Pomeedorka"] + }, + { + id: '4', + description: 'NARURO best anime', + createdAt: new Date('2013-12-12T12:12:12'), + author: 'Misha20', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Jora"] + }, + { + id: '5', + description: 'AVATAR best anime', + createdAt: new Date('2018-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Jaga"] + }, + { + id: '6', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2017-12-12T12:12:12'), + author: 'Misha22', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Jaguar"] + }, + { + id: '7', + description: 'NARUTO best anime', + createdAt: new Date('2016-12-12T12:12:12'), + author: 'Misha23', + photoLink: 'img/naruto.png', + postVisibility: true, + hashTags: ["#NARUTOBESTANIME"], + likes: ["Jagorka"] + }, + { + id: '8', + description: 'AVATAR best anime', + createdAt: new Date('2091-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/avatar.png', + postVisibility: true, + hashTags: ["#AVATARBESTANIME"], + likes: ["Igor"] + }, + { + id: '9', + description: 'SHAMAN-KING best anime', + createdAt: new Date('2020-12-12T12:12:12'), + author: 'Misha', + photoLink: 'img/sham.png', + postVisibility: true, + hashTags: ["#SHAMANKINGBESTANIME"], + likes: ["Egr"] + }, -]; - var postForFilter = { - author: 'Misha' - }; - console.log("\n\nСортировка и фильтрация постов. id найденных: \n"); - var sortedArrOfPosts = getPhotoPosts(2, 7, postForFilter); - for(var i=0;i photoPosts.length || postStart<0){ - console.log("Нельзя вывести столько постов. Промежуток вывода не совпадает с количеством постов"); - return null; } - else{ - var sortedArrOfPosts = photoPosts; - sortedArrOfPosts = sortedArrOfPosts.slice(postStart, number+postStart).sort(dateComparator); - if(filterConfig.createdAt){ - sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { - return post.createdAt === filterConfig.createdAt; - }); + + function getPhotoPosts(postStart, number, filterConfig) { + if (postStart + number > photoPosts.length || postStart < 0) { + console.log("Нельзя вывести столько постов. Промежуток вывода не совпадает с количеством постов"); + return null; } - if(filterConfig.author){ - sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { - return post.author === filterConfig.author; + else { + var sortedArrOfPosts = photoPosts; + if (filterConfig.author) { + sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { + return post.author === filterConfig.author; }); + } + if (filterConfig.createdAt) { + sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { + return post.createdAt === filterConfig.createdAt; + }); + } + sortedArrOfPosts = sortedArrOfPosts.slice(postStart, number + postStart).sort(dateComparator); + return sortedArrOfPosts; } - return sortedArrOfPosts; - } } - function dateComparator(date1, date2){ - return date1.createdAt - date2.createdAt; + function dateComparator(date1, date2) { + return date1.createdAt - date2.createdAt; } - function getPhotoPost(idOfPost){ - return photoPosts.find(function findPost(post){ + function getPhotoPost(idOfPost) { + return photoPosts.find(function findPost(post) { return post.id === idOfPost; }); } - function validatePhotoPost(postForCheck){ - if(typeof postForCheck.id !== "string" || postForCheck.id === "") { + function validatePhotoPost(postForCheck) { + if (typeof postForCheck.id !== "string" || postForCheck.id === "") { console.log("Пустое поле id"); - return false;} + return false; + } - if(photoPosts.some(function checkUniqueness(post){ + if (photoPosts.some(function checkUniqueness(post) { return postForCheck.id === post.id; - })){ + })) { console.log("id не уникальный"); } - if(typeof postForCheck.description !== "string" || postForCheck.description.length>200 || postForCheck.description==="") { + if (typeof postForCheck.description !== "string" || postForCheck.description.length > 200 || postForCheck.description === "") { console.log("Неверный description"); return false; } - if(typeof postForCheck.author !== "string" || postForCheck.author === "") { + if (typeof postForCheck.author !== "string" || postForCheck.author === "") { console.log("Поле author пустое"); return false; } - if(!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { + if (!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { console.log("Поле даты создания не введено, либо имеет неправильную структуру"); return false; } - if(typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { + if (typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { console.log("Поле photoLink пустое"); return false; } - if(!(postForCheck.hashTags instanceof Array)){ + if (!(postForCheck.hashTags instanceof Array)) { console.log("Структура поля hashTags не верна"); return false; } return true; } - function addPhotoPost(newPost){ - if(!validatePhotoPost(newPost)) return false; - else{ + function addPhotoPost(newPost) { + if (!validatePhotoPost(newPost)) { + return false; + } + else { photoPosts.push(newPost); return true; } } - function editPhotoPost(idForEd, postForEd){ + function editPhotoPost(idForEd, postForEd) { var postOfId = getPhotoPost(idForEd); - if(!postOfId) { + if (!validatePhotoPost(postOfId)) { return false; } - if(postForEd.description){ - if(typeof postForEd.description !== "string" || postForEd.description.length>200 || postForEd.description===""){ + var postCopy = JSON.parse(JSON.stringify(postOfId)); + if (postForEd.description) { + if (typeof postForEd.description !== "string" || postForEd.description.length > 200 || postForEd.description === "") { return false; } - postOfId.description = postForEd.description; + postCopy.description = postForEd.description; } - if(postForEd.author){ - if(typeof postForEd.author !== "string" || postForEd.author === "") { + if (postForEd.author) { + if (typeof postForEd.author !== "string" || postForEd.author === "") { return false; } - postOfId.author = postForEd.author; + postCopy.author = postForEd.author; } - if(postForEd.createdAt){ - if(!(postForEd.createdAt instanceof Date) || ((postForEd.createdAt).toString() === 'Invalid Date')) { + if (postForEd.createdAt) { + if (!(postForEd.createdAt instanceof Date) || ((postForEd.createdAt).toString() === 'Invalid Date')) { return false; } - postOfId.createdAt = postForEd.createdAt; + postCopy.createdAt = postForEd.createdAt; } - if(postForEd.photoLink){ - if(typeof postForEd.photoLink !== "string" || postForEd.photoLink === "") { + if (postForEd.photoLink) { + if (typeof postForEd.photoLink !== "string" || postForEd.photoLink === "") { return false; } - postOfId.photoLink = postForEd.photoLink; + postCopy.photoLink = postForEd.photoLink; } - if(postForEd.hashTags){ - if(!(postForEd.hashTags instanceof Array)){ + if (postForEd.hashTags) { + if (!(postForEd.hashTags instanceof Array)) { return false; } - postOfId.hashTags = postForEd.hashTags; + postCopy.hashTags = postForEd.hashTags; } + clonePost(postOfId, postCopy); + return true; } - function removePhotoPost(idForRem){ + function clonePost(post1, post2){ + if(post2.description){ + post1.description = post2.description; + } + if(post2.author){ + post1.author = post2.author; + } + if(post2.createdAt){ + post1.createdAt = post2.createdAt; + } + if(post2.photoLink){ + post1.photoLink = post2.photoLink; + } + if(post2.hashTags){ + post1.hashTags = post2.hashTags; + } + } + + function removePhotoPost(idForRem) { var postOfId = getPhotoPost(idForRem); - postOfId.postVisibility = false; - console.log("Пост для удаления не найден"); - return false; + if (postOfId == null) { + console.log("Пост для удаления не найден"); + return false; + } + else { + postOfId.postVisibility = false; + return true; + } } - }()); +}()); diff --git a/task4/mainPage.html b/task4/mainPage.html deleted file mode 100644 index fa4cb87..0000000 --- a/task4/mainPage.html +++ /dev/null @@ -1,251 +0,0 @@ - - - - - - Title - -
-
-
-
-
Seal98
-
-
-
- -
- - - -
-
- А какое аниме смотрел сегодня ты? -
-
-
- -
-
- -
-
- -
-
- -
-
-
-
- -
-
- -
-
- -
-
-
-
- -
-
- -
-
- -
-
-
-
- -
-
- - -
- -
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2012
-
Misha2018
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2013
-
Misha2019
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2014
-
Misha20
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2015
-
Misha21
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2016
-
Misha22
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2017
-
Misha23
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2018
-
Misha24
-
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2019
-
Misha25
-
-
- -
#hashtag #hashtag #hashtag #hashtag #hashtag #hashtag
- -
-
- -
- -
12.12.2020
-
Misha26
-
- - -
- - - -
- - -
- -
- From a5953b49e009c340008ad2a3c96cdd8c006e4162 Mon Sep 17 00:00:00 2001 From: Seal Date: Mon, 26 Mar 2018 19:43:40 +0300 Subject: [PATCH 5/5] changes for filter and etc --- task4/js/script.js | 109 +++++++++++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/task4/js/script.js b/task4/js/script.js index 334d29b..66da76f 100644 --- a/task4/js/script.js +++ b/task4/js/script.js @@ -93,7 +93,8 @@ ]; var postForFilter = { - author: 'Misha' + author: 'Misha', + hashTags: ["#AVATARBESTANIME"] }; console.log("\n\nСортировка и фильтрация постов. id найденных: \n"); var sortedArrOfPosts = getPhotoPosts(1, 2, postForFilter); @@ -125,7 +126,7 @@ }; console.log("Добавление нового поста с предварительной проверкой: " + addPhotoPost(newPost)); var postForEditing = { - description: 'NARUTO. Just Naruto(one more Naruto)', + description: 'NARUTO best anime (one more Naruto)', createdAt: new Date('2007-12-12T12:12:12') } console.log("Изменение поста по id: " + editPhotoPost("1", postForEditing)); @@ -187,13 +188,25 @@ return post.createdAt === filterConfig.createdAt; }); } - sortedArrOfPosts = sortedArrOfPosts.slice(postStart, number + postStart).sort(dateComparator); + if (filterConfig.hashTags) { + sortedArrOfPosts = sortedArrOfPosts.filter(function (post) { + for (let hashTagsOfPost of post.hashTags) { + for (let hashTagsOfFilter of [].concat(filterConfig.hashTags)) { + if (hashTagsOfPost === hashTagsOfFilter) { + return true; + } + } + } + }); + } + sortedArrOfPosts = sortedArrOfPosts.sort(dateComparator).slice(postStart, number + postStart); + return sortedArrOfPosts; } } - function dateComparator(date1, date2) { - return date1.createdAt - date2.createdAt; + function dateComparator(post1, post2) { + return post1.createdAt - post2.createdAt; } function getPhotoPost(idOfPost) { @@ -212,10 +225,36 @@ return postForCheck.id === post.id; })) { console.log("id не уникальный"); + return false; + } + + if (typeof postForCheck.description !== "string" || postForCheck.description.length > 200 || postForCheck.description === "") { + console.log("Неверный description"); + return false; + } + if (typeof postForCheck.author !== "string" || postForCheck.author === "") { + console.log("Поле author пустое"); + return false; + } + if (!(postForCheck.createdAt instanceof Date) || ((postForCheck.createdAt).toString() === 'Invalid Date')) { + console.log("Поле даты создания не введено, либо имеет неправильную структуру"); + return false; + } + if (typeof postForCheck.photoLink !== "string" || postForCheck.photoLink === "") { + console.log("Поле photoLink пустое"); + return false; + } + if (!(postForCheck.hashTags instanceof Array)) { + console.log("Структура поля hashTags не верна"); + return false; } + return true; + } + function validateForEditing(postForCheck) { if (typeof postForCheck.description !== "string" || postForCheck.description.length > 200 || postForCheck.description === "") { console.log("Неверный description"); + return false; } if (typeof postForCheck.author !== "string" || postForCheck.author === "") { @@ -249,61 +288,43 @@ function editPhotoPost(idForEd, postForEd) { var postOfId = getPhotoPost(idForEd); - if (!validatePhotoPost(postOfId)) { + if (!postOfId) { return false; } var postCopy = JSON.parse(JSON.stringify(postOfId)); - if (postForEd.description) { - if (typeof postForEd.description !== "string" || postForEd.description.length > 200 || postForEd.description === "") { - return false; - } + if (postForEd.description != null) { postCopy.description = postForEd.description; } - if (postForEd.author) { - if (typeof postForEd.author !== "string" || postForEd.author === "") { - return false; - } + if (postForEd.author != null) { postCopy.author = postForEd.author; } - if (postForEd.createdAt) { - if (!(postForEd.createdAt instanceof Date) || ((postForEd.createdAt).toString() === 'Invalid Date')) { - return false; - } + if (postForEd.createdAt != null) { postCopy.createdAt = postForEd.createdAt; } - if (postForEd.photoLink) { - if (typeof postForEd.photoLink !== "string" || postForEd.photoLink === "") { - return false; - } + if (postForEd.photoLink != null) { postCopy.photoLink = postForEd.photoLink; } - if (postForEd.hashTags) { - if (!(postForEd.hashTags instanceof Array)) { - return false; - } + if (postForEd.hashTags != null) { postCopy.hashTags = postForEd.hashTags; } - clonePost(postOfId, postCopy); - return true; + if (validateForEditing(postCopy)) { + changePostData(postOfId, postCopy); + return true; + } + return false; } - function clonePost(post1, post2){ - if(post2.description){ - post1.description = post2.description; - } - if(post2.author){ - post1.author = post2.author; - } - if(post2.createdAt){ - post1.createdAt = post2.createdAt; - } - if(post2.photoLink){ - post1.photoLink = post2.photoLink; - } - if(post2.hashTags){ - post1.hashTags = post2.hashTags; - } + function changePostData(post1, post2) { + post1.description = post2.description; + + post1.author = post2.author; + + post1.createdAt = post2.createdAt; + + post1.photoLink = post2.photoLink; + + post1.hashTags = post2.hashTags; } function removePhotoPost(idForRem) {