From a50ccedcaa774b6ad9a4a7d657329b03bd6b3f7f Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sat, 21 Jan 2023 09:08:22 -0500 Subject: start backend --- public/js/header.js | 25 +++++++ public/js/home.js | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++ public/js/main.js | 15 ++++ public/js/people.js | 1 + 4 files changed, 248 insertions(+) create mode 100644 public/js/header.js create mode 100644 public/js/home.js create mode 100644 public/js/people.js (limited to 'public/js') diff --git a/public/js/header.js b/public/js/header.js new file mode 100644 index 0000000..24643d6 --- /dev/null +++ b/public/js/header.js @@ -0,0 +1,25 @@ +function header(home, people) { + const html = ` + +
+ ` + + add(html, 'header') +} \ No newline at end of file diff --git a/public/js/home.js b/public/js/home.js new file mode 100644 index 0000000..289ce9b --- /dev/null +++ b/public/js/home.js @@ -0,0 +1,207 @@ +function parseDate(date) { + var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; + + return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear() + ' ' + date.toLocaleTimeString(); + } + +function parseComment(data) { + const html = ` +
+ + + + + ${data.firstname + ' ' + data.lastname} +

${data.content}

+
+
+ ` + return html +} + +function parsePost(post, likes) { + const html = ` +
+
+ + + +
+ ${post.author.firstname + ' ' + post.author.lastname} + ${parseDate(new Date(post.time))} +
+
+

+ ${post.content} +

+ + ${post.likes} Likes + +
+
+ + + Like + + + + Comment + +
+
+
+ ${post.comments.map(parseComment).join('')} +
+ + + +
+ +
+
+
+
+ ` + + return html +} + +function getPost(id) { + for (let i = 0; i < data.posts.length; i++) { + if (data.posts[i].id === id) { + return i + } + } + return -1 +} + +function like(span) { + const id = parseInt(span.parentElement.parentElement.getAttribute('postid')) + const index = data.user.likes.indexOf(id) + if (index === -1) { + data.user.likes.push(id) + data.posts[getPost(id)].likes++ + } else { + data.user.likes.splice(index, 1) + data.posts[getPost(id)].likes-- + } + load() +} + +function comment(event) { + event.preventDefault(); + const text = event.target.elements.text.value.trim(); + if (text.length < 1) return; + const id = parseInt(event.target.parentElement.parentElement.parentElement.getAttribute('postid')) + var index = getPost(id); + console.log(index) + if (index === -1) return; + data.posts[index].comments.push({ + firstname: data.user.firstname, + lastname: data.user.lastname, + content: text + }) + load() +} + +function post(event) { + const text = document.getElementById("text").value.trim() + if (text.length < 1) return; + data.posts.unshift({ + id: data.posts[0].id + 1, + author: { + firstname: data.user.firstname, + lastname: data.user.lastname + }, + time: Date.now(), + content: text, + likes: 0, + comments: [] + }) + load() +} + +function load() { + const html = ` +
+
+ + + + +
+ ${data.posts.map(p => parsePost(p, data.user.likes)).join('')} +
+ ` + + add(html, 'posts') + + const popup = ` + + ` + + add(popup, 'popup') +} + +var data = { + user: { + firstname: 'John', + lastname: 'Doe', + likes: [1] + }, + posts: [ + { + id: 1, + author: { + firstname: 'Joe', + lastname: 'Biden', + }, + time: 1674269687905, + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + likes: 2, + comments: [], + }, + { + id: 0, + author: { + firstname: 'Amazon', + lastname: 'Employee', + }, + time: 0, + content: 'I dont like working at amazon >:(', + likes: 69, + comments: [ + { + firstname: 'Jeff', + lastname: 'Bezos', + content: 'You\'re fired.' + }, + ], + } + ] +} + +header(true, false) +load(); \ No newline at end of file diff --git a/public/js/main.js b/public/js/main.js index e69de29..42cc6fe 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -0,0 +1,15 @@ +var range; + +function add(html, id) { + const old = document.getElementById(id) + if (old !== null) { + old.remove() + } + if (range === undefined) { + var range = document.createRange() + range.setStart(document.body, 0) + } + document.body.appendChild( + range.createContextualFragment(html) + ) +} \ No newline at end of file diff --git a/public/js/people.js b/public/js/people.js new file mode 100644 index 0000000..992fd45 --- /dev/null +++ b/public/js/people.js @@ -0,0 +1 @@ +header(false, true) \ No newline at end of file -- cgit v1.2.3-freya