-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign3P2.js
More file actions
182 lines (146 loc) · 5.1 KB
/
Assign3P2.js
File metadata and controls
182 lines (146 loc) · 5.1 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
var gistLog = [];
var gistList = [];
var faveGists = [];
var fetchData = function () {
var req = new XMLHttpRequest();
if (!req) {
throw 'Unable to create HttpRequest.';
}
req.onreadystatechange = function () {
if (this.readyState === 4) {
var gistLog = JSON.parse(this.responseText);
for (var i = 0; i < gistLog.length; i++) {
generateGistHtml(gistLog[i]);
gistList[i] = gistLog[i];
}
}
};
req.open('GET', 'https://api.github.com/gists');
req.send();
}
var saveInput = function () {
var input = document.getElementsByName('input')[0].value;
}
var generateGistHtml = function (gist) {
var id = gist.id;
var description = gist.description;
var url = gist.url;
//Create list
var ul = document.getElementById('gistlist');
var ulChild = document.createElement('ul');
var liDesc = document.createElement('li');
liDesc.innerText = description;
var liURL = document.createElement('li');
liURL.innerText = url;
var liButton = document.createElement('li');
var fbutton = document.createElement('input');
fbutton.type = "button";
fbutton.value = "+";
fbutton.setAttribute("gistId", id);
fbutton.onclick = function () {
var gistId = this.getAttribute("gistId");
var faveGist = findById(gistId);
//Add gist to favorite list in local storage
localStorage.setItem("faveGist", faveGists);
//Add to favorite list
var description = faveGist.description;
var url = faveGist.url;
//Create favorite list
var ulf = document.getElementById('favegistlist');
var ulfChild = document.createElement('ul');
var lifDesc = document.createElement('li');
lifDesc.innerText = description;
var lifURL = document.createElement('li');
lifURL.innerText = url;
var lifButton = document.createElement('li');
var rbutton = document.createElement('input');
rbutton.type = "button";
rbutton.value = "-";
rbutton.setAttribute("gistId", gistId);
rbutton.onclick = function () {
//Remove from favorite list
var ulfRemove = this.parentNode.parentNode;
var ulfParent = this.parentNode.parentNode.parentNode;
ulfParent.removeChild(ulfRemove);
//Remove from local storage
localStorage.removeItem("faveGist");
//Creates html to add gist to gist list
generateGistHtml(faveGist);
}
//Append
lifButton.appendChild(rbutton);
ulfChild.appendChild(lifButton);
ulfChild.appendChild(lifDesc);
ulfChild.appendChild(lifURL);
ulf.appendChild(ulfChild);
var ulRemove = this.parentNode.parentNode;
var ulParent = this.parentNode.parentNode.parentNode;
ulParent.removeChild(ulRemove);
}
//Append
liButton.appendChild(fbutton);
ulChild.appendChild(liButton);
ulChild.appendChild(liDesc);
ulChild.appendChild(liURL);
ul.appendChild(ulChild);
}
var findById = function (id) {
//iterate over list of gists to find the gist with id equals to input id
//return that gist
for (var i = 0; i < gistList.length; i++) {
if (id === gistList[i].id) {
return gistList[i];
}
}
}
var printFaves = function () {
temp = localStorage.getItem("faveGist");
faveGists = JSON.parse(temp);
console.log(faveGists);
}
window.onload = function () {
//fetch data from gitHub
fetchData();
//Favorite List
var favString = localStorage.getItem("faveGist"), favObj;
if (favString == null) {
favObj = { "faveGist": [] };
localStorage.setItem("faveGists", JSON.stringify(favObj));
}
else {
favObj = JSON.parse(localStorage.getItem("gistFave"));
printFaves();
}
//Add to favorite list
// var id = faveGist.id;
// var description = faveGist.description;
// var url = faveGist.url;
//Create favorite list
// var ulf = document.getElementById('favegistlist');
//var ulfChild = document.createElement('ul');
//var lifDesc = document.createElement('li');
// lifDesc.innerText = description;
// var lifURL = document.createElement('li');
// lifURL.innerText = url;
// var lifButton = document.createElement('li');
// var rbutton = document.createElement('input');
// rbutton.type = "button";
// rbutton.value = "-";
// rbutton.setAttribute("gistId", id);
// rbutton.onclick = function () {
//Remove from favorite list
// var ulfRemove = this.parentNode.parentNode;
// var ulfParent = this.parentNode.parentNode.parentNode;
// ulfParent.removeChild(ulfRemove);
//Remove from local storage
//localStorage.removeItem("faveGist");
//Creates html to add gist to gist list
// generateGistHtml(faveGist);
// }
//Append
// lifButton.appendChild(rbutton);
//ulfChild.appendChild(lifButton);
//ulfChild.appendChild(lifDesc);
//ulfChild.appendChild(lifURL);
//ulf.appendChild(ulfChild);
}