-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
137 lines (125 loc) · 4.25 KB
/
script.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
//initialize the firebase app
var config = {
apiKey: "AIzaSyCKNcULQZxFMYioXei32XNWQVoeutz4XDA",
authDomain: "contact-book-new.firebaseapp.com",
databaseURL: "https://contact-book-new.firebaseio.com",
projectId: "contact-book-new",
storageBucket: "contact-book-new.appspot.com",
messagingSenderId: "473268388365"
};
firebase.initializeApp(config);
//create firebase references
const Storage = firebase.storage();
const allImagesRef = Storage.ref().child('all-images')
const dbRef = firebase.database();
const userImagesRef = dbRef.ref('user-images');
const Auth = firebase.auth();
let user = null;
let userData = null;
const body = document.body;
Auth.onAuthStateChanged(updateUserStatus);
document.addEventListener("DOMContentLoaded", function(event) {
const logoutButton = document.querySelector('#logout');
const fbLoginButton = document.querySelector('#fbLogin');
const twLoginButton = document.querySelector('#twLogin');
const createPostButton = document.querySelector('#create-post');
const preview = document.querySelector('#form-image-preview')
const statusDropzone = document.querySelector('[file-drop]');
const statusDropzonePreview = document.querySelector('[file-drop-preview]');
const fileCollection = [];
const fileDropzoneCollection = [];
const events = [
'dragenter',
'dragleave',
'dragover', // to allow drop
'drop'
];
events.forEach(e => {
statusDropzone.addEventListener(e, (ev) => {
ev.preventDefault();
if (ev.type === 'dragenter') {
statusDropzone.classList.add('solid-border');
}
if (ev.type === 'dragleave') {
statusDropzone.classList.remove('solid-border');
}
if(ev.type === 'drop') {
statusDropzone.classList.remove('solid-border');
[].slice.call(ev.dataTransfer.files).map(f => fileDropzoneCollection.push(f));
renderCollection(fileDropzoneCollection, statusDropzonePreview);
}
})
})
document.querySelector('#pictures').addEventListener('change', (e) => {
const formData = extractFormData('#statusForm');
while (fileCollection.length) {
fileCollection.pop();
}
[].slice.call(formData.pictures).map(f => fileCollection.push(f));
renderCollection(fileCollection, preview);
});
document.forms.statusForm.addEventListener('submit', (e) => {
e.preventDefault();
e.stopPropagation();
const formData = extractFormData('#statusForm');
const text = formData.status;
formData.status = '';
sendData(text, fileCollection)
setTimeout(() => {
while (fileCollection.length) {
fileCollection.pop();
}
document.forms.statusForm.reset()
}, 100);
});
createPostButton.addEventListener('click', (e) => {
const status = document.getElementById('status');
const text = status.value;
status.value = '';
sendData(text, fileDropzoneCollection)
setTimeout(() => {
status.value = '';
while (fileDropzoneCollection.length) {
fileDropzoneCollection.pop();
}
}, 100);
});
logoutButton.addEventListener('click', (e) => {
e.preventDefault();
Auth.signOut();
});
fbLoginButton.addEventListener('click', (e) => {
const provider = new firebase.auth.FacebookAuthProvider();
provider.addScope('public_profile');
request = Auth.signInWithPopup(provider)
e.preventDefault();
Auth.signOut();
});
twLoginButton.addEventListener('click', (e) => {
const provider = new firebase.auth.TwitterAuthProvider();
request = Auth.signInWithPopup(provider)
e.preventDefault();
Auth.signOut();
});
const renderCollection = (collection, container) => {
removeAllChildren(container);
Promise
.all(collection.map(generatePreviewData))
.then(imgs => imgs.map((img, i) => {
img.setAttribute('index', i);
img.addEventListener('click', e => {
collection.splice(i, 1);
renderCollection(collection, container);
})
container.appendChild(img);
}))
}
const sendData = (text, files) => Promise
.all(files.map(file =>
saveImage(file, +(new Date) + '_' + Math.random(), allImagesRef, progress)
)).then((values) => userImagesRef.child(user.uid).push({
status: text,
pictures: values,
timestamp: +(new Date()),
}));
});