-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfirebase-manager.js
More file actions
213 lines (176 loc) · 7.32 KB
/
firebase-manager.js
File metadata and controls
213 lines (176 loc) · 7.32 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* global Firebase */
/* global Firepad */
import {AppConfiguration} from "../app-configuration";
export class FirebaseManager {
baseURL = new AppConfiguration().getConfiguration().firebaseURL;
pastebinId = undefined;
SERVER_TIMESTAMP = Firebase.ServerValue.TIMESTAMP;
isCopy = false;
activate(pastebinId) {
if (pastebinId) {
this.pastebinId = pastebinId;
} else {
this.pastebinId = this.makeNewPastebinFirebaseReferenceId();
}
}
makeNewPastebinFirebaseReferenceId(baseURL = this.baseURL) {
let defaultPastebinScheme = {
creationTimestamp: this.SERVER_TIMESTAMP,
content: {
//js, css and html handled by Firepad
search: 0,
chat: 0,
share: {
currentEvent: 0,
parentPastebinId: 0,
children: 0
}
},
users: 0
};
return new Firebase(baseURL).push(defaultPastebinScheme).key();
}
makePastebinFirebaseReference(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/`);
}
makeTraceSearchHistoryFirebase(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/content/search`);
}
makeChatFirebase(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/content/chat`);
}
makeShareEventsFirebase(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/content/share/events`);
}
makeShareChildrenFirebase(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/content/share/children`);
}
makeMetagsURLFirebaseVote(metagURLKey, pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/metags/urls/${metagURLKey}`);
}
makePastebinMetagsURLsFirebase(pastebinId = this.pastebinId) {
return new Firebase(`${this.baseURL}/${pastebinId}/metags/urls`);
}
makeGlobalMetagsURLsFirebase() {
return new Firebase(`${this.baseURL}/metags/urls`);
}
makeGlobalMetagsURLsFirebaseByKey(metagGlobalURLKey) {
return new Firebase(`${this.baseURL}/metags/urls/${metagGlobalURLKey}`);
}
/**
* Copies a pastebin content to a new one. It associates them as parent and child, once the copy is created. In Firebase, reference "parentPastebinId/content/share/children" will get childPastebinId pushed and "childPastebinId/share/parent" will be set to parentPastebinId.
* @param {String} parentPastebinId - the pastebin id to be copied.
* @param {Boolean} copyChat - the pastebin's should be copied or not. It will not copy the chat content by default(false).
* @return {String} childPastebinId, the pastebin id of the newly created copy.
*/
copyPastebinById(parentPastebinId, copyChat = false) {
this.isCopy = true;
let firebaseManager = this;
let childPastebinId = firebaseManager.makeNewPastebinFirebaseReferenceId();
let parentShareReferenceChildren = firebaseManager.makeShareChildrenFirebase(parentPastebinId);
parentShareReferenceChildren.push({childPastebinId: childPastebinId, timestamp: firebaseManager.SERVER_TIMESTAMP});
let sourceReference = firebaseManager.makePastebinFirebaseReference(parentPastebinId);
let destinationReference = firebaseManager.makePastebinFirebaseReference(childPastebinId);
let dataChanger = {
changeData: (data) => {
if (data.content) {
data.creationTimestamp = this.SERVER_TIMESTAMP;
data.content.chat = {};
data.content.share = {
currentEvent: 0,
parentPastebinId: parentPastebinId,
children: 0
};
}
return data;
}
};
firebaseManager.makeFirebaseReferenceCopy(sourceReference, destinationReference, dataChanger);
return childPastebinId;
}
makeJsEditorFirepad(jsEditor) {
let defaultText = '\nhelloWorld();\n\nfunction helloWorld() {\n\tvar message = "world!";\n\tvar $helloMessage = $("#hello_message");\n\t$helloMessage.append(message);\n\t$helloMessage.addClass("shiny-red");\n}';
return this.makeFirepad("js", jsEditor, defaultText);
}
makeHtmlEditorFirepad(htmlEditor) {
let defaultText = '<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset="utf-8">\n\t<title>SeeCodeRun Pastebin</title>\n\t<script src="https://code.jquery.com/jquery-3.1.1.js">\n\t</script>\n</head>\n<body>\n\t<div id="hello_message">\n\t\tHello,\n\t</div>\n</body>\n</html>';
return this.makeFirepad("html", htmlEditor, defaultText);
}
makeCssEditorFirepad(cssEditor) {
let defaultText = 'body > div:first-child {\n\t font-weight: bold;\n}\n\n.shiny-red {\n\t color: red;\n}';
return this.makeFirepad("css", cssEditor, defaultText);
}
makeFirepad(subject, editor, defaultText) {
let subjectURL = `${this.baseURL}/${this.pastebinId}/content/${subject}`;
let firebase = new Firebase(subjectURL);
if (this.isCopy) {
defaultText = null;
}
return Firepad.fromACE(
firebase,
editor, {
defaultText: defaultText
});
}
makeHistoryViewerFirepad(subject, editor, context) {
let subjectURL = `${this.baseURL}/${this.pastebinId}/content/${subject}`;
let subjectFirebase = new Firebase(subjectURL);
let subjectHistoryURL = `${this.baseURL}/${this.pastebinId}/historyViewer/${subject}`;
let historyFirebase = new Firebase(subjectHistoryURL);
// Copy the entire firebase to the history firebase
subjectFirebase.once("value", function (snapshot) {
historyFirebase.set(snapshot.val());
});
subjectFirebase.child('history').once("value", function (snapshot) {
let sliderMaxValue = snapshot.numChildren();
context.updateSliderMaxValue(sliderMaxValue);
});
let headless = Firepad.Headless(historyFirebase);
headless.getText(function (text) {
editor.setValue(text);
});
return {
subjectFirebase: subjectFirebase,
historyFirebase: historyFirebase,
historyFirepadHeadless: headless
};
}
slideHistoryViewerFirepad(subjectFirebase, historyFirebase, sliderValue, activeHistoryEditor, context) {
context.historyFirepadHeadless.dispose();
// Copy history from the firebase to the history firebase to display values till a specific point in history.
subjectFirebase.child('history').limitToFirst(sliderValue).once("value", function (snapshot) {
historyFirebase.child('history').set(snapshot.val());
context.historyFirepadHeadless = Firepad.Headless(historyFirebase);
context.historyFirepadHeadless.getText(function (text) {
activeHistoryEditor.setValue(text);
});
});
}
stopReceivingHistoryUpdates(firebaseRef) {
if (!firebaseRef) {
return;
}
firebaseRef.child('history').off("child_added");
}
makeFirebaseReferenceCopy(source, destination, dataChanger = this, thenCallback) {
source.once("value", function (snapshot) {
let data = snapshot.val();
if (data) {
data = dataChanger.changeData(data);
}
destination.set(data, function (error) {
if (error && typeof(console) !== 'undefined' && console.error) {
console.error(error);
}
});
});
}
/**
* Default method for DataChanger Interface. In this case, it does not change anything.
* @param {Object} data - the JSON object to be modified.
* @return {Object}, the JSON object result of the modification.
*/
changeData(data) {
return data;
}
}