-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
166 lines (153 loc) · 6.94 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function createNewStory() {
var buttonToLoad = document.getElementById("save-story-btn");
buttonToLoad.innerHTML = "<i class='fas fa-circle-notch'></i>";
var ContentText = document.getElementById("story-text-area").innerHTML;
db.collection("stories").add({
title: document.getElementById("story-title-input").value,
author: localStorage.name,
content: document.getElementById("story-text-area").innerHTML,
uid: firebase.auth().currentUser.uid,
tags: document.getElementById("tags-input").value.split(","),
published: true,
likes: [""]
}).then(function(docRef) {
window.location.href = "?" + docRef.id + "?edit";
buttonToLoad.innerHTML = "<i class='fas fa-save'></i>";
}).catch(function(error) {
alert(error);
throw error;
buttonToLoad.innerHTML = "<i class='fas fa-exclamation'></i>";
});
}
function saveStory() {
if (window.location.href.split("?")[1]) {
var buttonToLoad = document.getElementById("save-story-btn");
buttonToLoad.innerHTML = "<i class='fas fa-circle-notch'></i>";
db.collection("stories").doc(window.location.href.split("?")[1]).update({
title: document.getElementById("story-title-input").value,
author: localStorage.name,
content: document.getElementById("story-text-area").innerHTML,
uid: firebase.auth().currentUser.uid,
tags: document.getElementById("tags-input").value.split(",")
}).then(function() {
buttonToLoad.innerHTML = "<i class='fas fa-save'></i>";
}).catch(function(error) {
alert(error);
throw error;
buttonToLoad.innerHTML = "<i class='fas fa-exclamation'></i>";
});
}
}
function likeStory(storyId) {
db.collection("stories").doc(storyId).get().then(function(snapshot) {
let likes = snapshot.data().likes;
likes.push(firebase.auth().currentUser.uid);
db.collection("stories").doc(storyId).update({
likes: likes
});
});
}
function rateStory(storyId, rating) {
db.collection("stories").doc(storyId).collection("ratings").add(rating);
}
function getStoryInfo(storyId) {
var toReturn;
db.collection("stories").doc(storyId).get().then(function(snapshot) {
toReturn = snapshot.data();
});
return toReturn;
}
function deleteStory(storyId) {
if (confirm("Are you sure that you would like to delete this story? All data will be permanently lost.")) {
db.collection("stories").doc(storyId).delete().then(function() {
alert("The story has been successfully deleted!");
}).catch(function(error) {
alert(error);
});
}
}
function loadStories(ref) {
hideAllPages();
document.getElementById("main-page").style.display = "block";
ref.get().then(function(snapshot) {
document.getElementById('story-cards').innerHTML = '';
snapshot.forEach(function(storySnapshot) {
document.getElementById('story-cards').innerHTML += '<span class="card" onclick="window.location.href = \'https://synco.tk/?' + storySnapshot.id + '\'"><font class="card-title">' + storySnapshot.data().title + '</font><p><i class="fas fa-address-card"></i> ' + storySnapshot.data().author + ' </p><p><i class="fas fa-thumbs-up"></i> ' + storySnapshot.data().likes.length + '</p></span>';
});
});
}
function hideAllPages() {
for (var i = 0; i < document.getElementsByClassName("page").length; i++) {
document.getElementsByClassName("page")[i].style.display = "none";
}
}
hideAllPages();
if (!window.location.href.split("?")[1]) {
//Load main page
loadStories(db.collection("stories").orderBy("likes", "desc").where("published", "==", true));
} else {
if (window.location.href.split("?")[1] == "mystories") {
//Load my stories page
loadStories(db.collection("stories").where("author", "==", localStorage.name));
} else if (document.getElementById(window.location.href.split("?")[1] + "-page")) {
//Display Other Page
document.getElementById(window.location.href.split("?")[1] + "-page").style.display = "block"
} else if(window.location.href.split("?")[2]) {
//Story sub-pages
if(window.location.href.split("?")[2] == "edit") {
//Display Edit Page
hideAllPages();
db.collection("stories").doc(window.location.href.split("?")[1]).get().then(function(snapshot) {
if (snapshot.data().author == localStorage.name || snapshot.data.access[firebase.auth().currentUser.uid] == true) {
document.getElementById("edit-page").style.display = "block";
document.getElementById("story-title-input").value = snapshot.data().title;
document.getElementById("story-text-area").innerHTML = snapshot.data().content;
document.getElementById("tags-input").value = snapshot.data().tags;
document.getElementById("save-story-btn").onclick = saveStory;
} else {
alert("You do not have permission to edit this story.");
}
});
//Insert Tab when the key is pressed
document.getElementById("story-text-area").onkeydown = function(e) {
if (e.keyCode == 9) {
document.execCommand("insertHTML", false, " ");
return false;
}
}
//Alert the user that changes may not be saved
window.onbeforeunload = function() {
return false;
}
//Save Story when Ctrl+S is pressed
document.onkeyup = function(e) {
if (e.ctrlKey && e.which == 83) {
saveStory();
}
};
}
} else {
//Story page
db.collection("stories").doc(window.location.href.split("?")[1]).get().then(function(snapshot) {
if (snapshot.data()) {
var ValContent = snapshot.data().content;
var ValTitle = snapshot.data().title;
document.getElementById("story-page").style.display = "block";
document.getElementById("story-page").innerHTML = "<center><h1>" + snapshot.data().title + "</h1><h5> By " + snapshot.data().author + "</h5></center> <div>" + snapshot.data().content + "</div>";
if (localStorage.name == snapshot.data().author) {
document.getElementById("story-page").innerHTML += "<button class='btn-primary' id='editBtn' onclick='window.location.href += \"?edit\"' style='right: 5px;'><i class='fas fa-edit'></i></button>";
}
if (localStorage.name !== "null" && snapshot.data().title !== "Synco - A Project") {
document.getElementById("story-page").innerHTML += "<br><br><button class='btn-primary' onclick='likeStory(\"" + window.location.href.split("?")[1] + "\")'><i class='fas fa-thumbs-up'></i> </button> <a class='btn-primary' id='Download' style='right: 5px;' download='" + ValTitle + ".html' href='data:text/plain;charset=utf-8," + ValContent + "'> <i class='fas fa-upload'></i></a>";
}
} else {
document.getElementById("story-404-page").style.display = "block";
}
if (snapshot.data().tags !== [""]) {
for (var i = 0; i < snapshot.data().tags.length; i++) {
document.getElementById("story-page").innerHTML += "<tag>" + snapshot.data().tags[i] + "</tag>";
}
}
});
}
}