-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.js
126 lines (102 loc) · 3.18 KB
/
simple.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
const ANIMATION_DELAY = 200; // in ms
// Initialize Firebase
var config = {
apiKey: "AIzaSyDwy5Kbmd7GHTdXbRewd8OZaArBWPKDurs",
authDomain: "erika-stickies.firebaseapp.com",
databaseURL: "https://erika-stickies.firebaseio.com",
projectId: "erika-stickies",
storageBucket: "erika-stickies.appspot.com",
messagingSenderId: "282840178411"
};
firebase.initializeApp(config);
firebase.auth().signInAnonymously().catch(function(error) {
console.log("error", error);
});
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
// var isAnonymous = user.isAnonymous;
// var uid = user.uid;
// console.log("user");
renderAllStickies(function() {
setTimeout(function() {
$("#wall").append(createHTML({},"new-sticky-template"));
}, ANIMATION_DELAY);
});
} else {
// User is signed out.
// console.log("out");
}
});
Handlebars.registerHelper("localTime", function(timestamp) {
return moment.unix(timestamp/1000).format("YYYY-MM-DD hh:mm a");
});
function renderAllStickies(done) {
firebase.database().ref('/stickies').once('value').then(function(snapshot) {
var stickies = snapshot.val();
var numStickies = Object.keys(stickies).length;
var currIndex = 0;
function renderStickiesWithDelay() {
var key = Object.keys(stickies)[currIndex];
var currentSticky = stickies[key];
currentSticky.id = key;
renderSticky(currentSticky);
currIndex += 1;
if (currIndex <= numStickies-1) {
setTimeout(renderStickiesWithDelay, ANIMATION_DELAY);
} else {
done();
}
}
renderStickiesWithDelay();
});
}
function createHTML(data, templateId) {
var source = document.getElementById(templateId).innerHTML;
var template = Handlebars.compile(source);
return template(data);
}
function isSrcVideo(filename = "") {
var ext = filename.split("?")[0].split(".").pop();
switch (ext.toLowerCase()) {
case "mp4":
case "avi":
case "mpg":
return true;
}
return false;
}
function renderSticky(sticky) {
// check if imgSrc is actually a video...
if (isSrcVideo(sticky.imgSrc)) {
sticky.imgSrcIsVideo = true;
}
$("#wall").append(createHTML(sticky,"sticky-template"));
}
// send new sticky
$("body").on("submit", "#new-sticky-form", function(event) {
event.preventDefault();
var ref = firebase.database().ref("stickies").push();
var task = ref.set({
timestamp: Date.now().toString(),
message: $(this).find("[name=message]").val().replace(/\n/g,"<br>"),
submitter: $(this).find("[name=submitter]").val(),
imgSrc: $(this).find("[name=imgSrc]").val(),
bgColor: $(this).find("[name=bgColor]:checked").val(),
textColor: null
}, function complete() {
// reload page so new sticky shows... a cheat...
window.location.reload(true);
done();
}
);
});
$("body").on("change", "#new-sticky-form input[type=radio][name=bgColor]", function(event) {
$.each($("input[type=radio][name=bgColor]"), function(index, option) {
$("#new-sticky-note").removeClass(option.value);
});
$("#new-sticky-note").addClass(this.value);
});
setInterval(function() {
$("#shreddy").toggleClass("jump");
}, 3000)