-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
203 lines (151 loc) · 4.89 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
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
$(document).ready(function() {
/***
*Weather widget
**/
$.getJSON("https://freegeoip.net/json/", function(json) {
$.getJSON("https://api.openweathermap.org/data/2.5/weather?lat=" + json.latitude + "&lon=" + json.longitude + "&units=imperial&appid=03f010544045f1772f781cfaf70d9cdd", function(x) {
var f = Math.round(x.main.temp) * 100 / 100;
console.log(f);
$("#weather").html(" <div><img src='http://openweathermap.org/img/w/" + x.weather[0].icon + ".png'/><span> | " + f + "° F</span></div>");
});
});
/***
*TODO widget
**/
//login to firebase
var email = '[email protected]';
var password = 'P@ssword1';
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function(user) {
console.log(user)
})
var currentUser = firebase.auth().currentUser;
// LOAD UP todos
//cross out todo
$('.todo_list').on("click", "li", function changeCSS() {
$(this).toggleClass('todo_completed');
// console.log('yo!!: ' + $(this).attr("uid"))
// deleteTodos($(this).attr("uid"));
});
// delete todo
$('.todo_list').on("click", '.todo_X', function(e) {
// prevent "bubble up"
e.preventDefault();
e.stopPropagation();
$(this).parent().fadeOut(500, function removeAfterFadeOut() {
$(this).remove();
deleteTodos($(this).attr("uid"));
});
console.log('yo!!: ' + $(this).attr("uid"))
// deleteTodos($(this).parent().attr("uid"));
});
//add todo
$('.todo_input').keypress(function hitEnter(e) {
if (e.which == 13) {
console.log('thisvale: ', $(this).val())
saveTodos($(this).val());
//I think the bug is here somewhere
$(this).val('');
};
});
var loaded = false;
$('#fetchTodos').click(function() {
console.log('from #fetchtodos')
if (loaded == false) {
fetchTodos();
}
loaded = true;
$('#todo_container').toggle({
'display': 'none'
});
// $(this).toggle({'display': 'none'});
});
//toggle todo input
$('.icon-plus').click(function() {
$('.todo_input').fadeToggle();
});
});
// var myButton = document.getElementById("clickButton");
var myText = document.getElementById("clock");
// myButton.addEventListener('click', doSomething, false)
function doSomething() {
var d = new Date();
var clock = d.getTime();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var day = d.getDate();
myText.textContent = hours + ":" + minutes + ":" + seconds;
}
setInterval(function() {
doSomething();
console.log("do something");
}, 1000);
// ======== CHANGE BACKGROUND FUNCTION =========
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open("GET", aUrl, true);
anHttpRequest.send(null);
}
}
function changeBackground() {
var client = new HttpClient();
client.get('https://api.unsplash.com/photos/random/?client_id=f1a98c4f258ca33b9175aaa05f96adbeb130f4ae831458b358111c06e413bc4c', function(response) {
var responseJSON = JSON.parse(response);
var randomImageUrl = responseJSON.urls.regular;
document.querySelector('body').style.backgroundImage = 'url(' + randomImageUrl + ')';
// fetchTodos();
});
}
function getTodos(id) {
}
function saveTodos(todo) {
var currentUser = firebase.auth().currentUser;
firebase.database().ref('/users/' + currentUser.uid + '/todos')
.push({
todo: {
todo: todo,
id: Date.now()
}
}).then(function() {
console.log('pushed')
console.log(this);
});
}
function deleteTodos(uid) {
var currentUser = firebase.auth().currentUser;
firebase.database().ref('/users/' + currentUser.uid + '/todos/' + uid)
.remove().then(function() {
console.log('ish was deleted');
});
}
function fetchTodos() {
var currentUser = firebase.auth().currentUser;
firebase.database().ref('/users/' + currentUser.uid + '/todos')
.on('child_added', function(snapshot) {
var the_objects = snapshot.val();
console.log(getParent(snapshot));
console.log(snapshot.key);
_.map(the_objects, function(val, uid) {
return $('.todo_list').prepend('<li class="todo_item" uid=' + snapshot.key + ' "><span class="todo_X"><i class="icon-trash"></i></span> ' + the_objects.todo.todo + ' </li>');
console.log('fetching todos from the function')
})
});
}
function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref;
// Now simply find the parent and return the name.
return ref.parent.val;
}
// var the_todos = _.map(todos, function(val, uid){
// return {val, uid}
// })
changeBackground();
console.log(_)