-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
284 lines (254 loc) · 8.37 KB
/
index.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var list={};
var res={};
var a;
$(document).ready(function(){
loadLeft();
loadRight();
$("#newQues").click(function(){
loadRight();
});
$("#addList").click(function(evt){
var clicked = evt.target;
//console.log(clicked.className);
var clss=clicked.className;
a=clss;
if(clss!="p-2" && clss!="")
$("#right").empty();
discussion(clss);
});
filterSearch();
});
function addToLeft()
{
var subject=document.getElementById("subject").value;
var question=document.getElementById("question").value;
if(subject==='' || question=='')
{
alert("Cannot Submit Empty Data");
return;
}
localStorage.setItem(question, JSON.stringify([subject, question]));
var data=JSON.parse(localStorage.getItem(question));
//console.log(data);
createLists(data);
$("#subject").val('');
$("#question").val('');
}
function createLists(data)
{
var div=document.createElement("li");
div.setAttribute("id", data[0]);
var h3=document.createElement("h3");
h3.setAttribute("class", data[0]);
var h6=document.createElement("h6");
h6.setAttribute("class", data[0]);
var hr=document.createElement("hr");
div.style.padding="5px";
hr.style.margin="0px";
h3.innerHTML=data[0];
h6.innerHTML=data[1];
div.appendChild(h3);
div.appendChild(h6);
div.appendChild(hr);
document.getElementById("addList").appendChild(div);
//console.log(div);
}
function loadLeft()
{
for(i=localStorage.length-1; i>=0; i--)
{
var key=localStorage.key(i);
//console.log(typeof key);
if(key.endsWith("^key^")==false){
var data=JSON.parse(localStorage.getItem(key));
createLists(data);
}
}
}
function loadRight()
{
var right=document.getElementById("right");
$("#right").empty();
var h1=document.createElement("h1");
h1.innerHTML="Welcome to Discussion Portal !";
var h6=document.createElement("h6");
h6.innerHTML="Enter a subject and question to get started."
var div1=document.createElement("div");
div1.setAttribute("class","form-group");
var ip=document.createElement("input");
ip.setAttribute("type","text");
ip.setAttribute("class", "form-control");
ip.setAttribute("id","subject");
ip.setAttribute("placeholder","JSON and Web storage");
ip.style.width="50%";
div1.appendChild(ip);
var div2=document.createElement("div");
div2.setAttribute("class","form-group");
var textArea=document.createElement("textarea");
textArea.setAttribute("class", "form-control");
textArea.setAttribute("id","question");
textArea.setAttribute("rows","4");
textArea.setAttribute("placeholder","How to use Web storage");
div2.appendChild(textArea);
var btn=document.createElement("button"); //submit ques
btn.setAttribute("class", "btn btn-primary");
btn.innerHTML="Submit";
btn.setAttribute("id","submit");
btn.setAttribute("onclick","addToLeft()");
btn.style.float="right";
right.appendChild(h1);
right.appendChild(h6);
right.appendChild(div1);
right.appendChild(div2);
right.appendChild(btn);
}
function discussion(clss)
{
try{
var d=document.getElementsByClassName(clss);
//console.log(typeof JSON.stringify(d[0].firstChild)[0]);
//console.log(d[1].firstChild);
var div=document.createElement("div");
var h3=document.createElement("h3");
h3.setAttribute("id", "heading");
var h6=document.createElement("h6");
h6.setAttribute("id", "f");
h3.innerHTML=getText(d[0].firstChild);
h6.innerHTML=getText(d[1].firstChild);
div.appendChild(h3);
div.appendChild(h6);
div.style.background="#D3D3D3";
div.style.padding="15px";
$("#right").append("<h5>Question</h5>");
$("#right").append(div);
var btn=document.createElement("button"); //resolve button
btn.setAttribute("class", "btn btn-primary");
btn.setAttribute("id","resolve");
btn.setAttribute("onclick","resolving()");
btn.innerHTML="Resolve";
btn.style.float="right";
btn.style.marginTop="10px";
$("#right").append(btn);
$("#right").append("<h5 id='response'>Response</h5>");
$("#response").css({"margin-top": "50px", "margin-bottom": "10px", "padding":"10px"});
//Response entries-------------------------------------//
var div=document.createElement("div");
div.setAttribute("id","responseDiv");
$("#right").append(div);
//------------------------------------------------------//
$("#right").append("<h4 id='addR'>Add Response</h4>");
$("#addR").css({"margin-top": "10px", "margin-bottom": "2px", "padding":"15px"});
var name=document.createElement("div");
name.setAttribute("class","form-group");
var ip=document.createElement("input");
ip.setAttribute("type","text");
ip.setAttribute("class", "form-control");
ip.setAttribute("id","name");
ip.setAttribute("placeholder","Enter Name");
name.style.paddingLeft="15px";
ip.style.width="40%";
name.appendChild(ip);
$("#right").append(name);
var txtA=document.createElement("div");
txtA.setAttribute("class","form-group");
var area=document.createElement("textarea");
area.setAttribute("id", "comment");
area.setAttribute("class", "form-control");
area.setAttribute("placeholder","Enter Comment");
area.setAttribute("rows","3");
txtA.style.paddingLeft="15px";
txtA.appendChild(area);
$("#right").append(txtA);
var btn=document.createElement("button"); //add response button
btn.setAttribute("class", "btn btn-primary");
btn.setAttribute("onClick", "addResponse()");
btn.innerHTML="Submit";
btn.style.float="right";
$("#right").append(btn);
addResponseList(clss+"^key^");
}
catch(e){}
}
function getText( obj ) {
return obj.textContent ? obj.textContent : obj.innerText;
}
var arr={};
function addResponse()
{
$("#responseDiv").empty();
var nam=document.getElementById("name").value;
var comment=document.getElementById("comment").value;
var key=a+"^key^";
var v;
var localArr=JSON.parse(localStorage.getItem(key));
if(nam==='' || comment=='')
{
alert("Cannot Submit Empty Data");
addResponseList(key);
return;
}
if(localArr!=null){
if(nam in localArr){
var lcom=localArr[nam];
lcom.push(comment);
localArr[nam]=lcom;
}
else{
localArr[nam]=[comment];
}
localStorage.setItem(key, JSON.stringify(localArr));
}
else{
v={[nam]:[comment]};
localStorage.setItem(key, JSON.stringify(v));
}
$("#name").val('');
$("#comment").val('');
addResponseList(key);
}
function addResponseList(key)
{
var val=JSON.parse(localStorage.getItem(key));
var len=Object.keys(val).length;
for(i in val){
for(var j=0; j<val[i].length; j++){
//console.log(i+" -> "+val[i][j]);
var div=document.getElementById("responseDiv");
var h5=document.createElement("h5");
var p=document.createElement("span");
var hr=document.createElement("hr");
hr.style.marginBottom="0px";
h5.innerHTML=i;
p.innerHTML=val[i][j];
div.appendChild(h5);
div.appendChild(p);
div.appendChild(hr);
div.style.background="#D3D3D3";
div.style.paddingLeft="10px";
div.style.paddingRight="10px";
}
}
}
function resolving()
{
var h=document.getElementById("heading").firstChild;
var f=document.getElementById("f").firstChild;
h=getText(h);
f=getText(f);
localStorage.removeItem(f);
localStorage.removeItem(h+"^key^");
$("#addList").empty();
loadLeft();
loadRight();
}
function filterSearch()
{
var $rows = $('#addList li');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
}