-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.js
642 lines (589 loc) · 16.6 KB
/
list.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//main.js와 겹치는 부분이 많음. 다른 부분만 주석 작성.
var count = 0;
$(document).ready(function () {
$('#calendarIcon').click(function () {
window.location.href = 'main.html';
});
var userID = sessionStorage.getItem('id');
$('#userID').text(userID);
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var date = today.getDate();
$('h1').text(year + '년 ' + (month + 1) + '월');
listContents(year, month);
//todo 초기화
$.post(
'php/ReadTodoList.php',
{
userID: $('#userID').text(),
},
function (data) {
var savedTodo = JSON.parse(data);
for (var i = 0; i < savedTodo.length; i++) {
$('.listBox').append(
'<li>' +
savedTodo[i]['todo'] +
"<button class='todoRemove' type='button'>삭제</button></li>"
);
}
$('.todoRemove').click(function () {
var temp = $(this);
var text = $(this).parent().html();
var afterText = text.split('<');
$.post(
'php/RemoveTodoList.php',
{
userID: $('#userID').text(),
todo: afterText[0],
},
function (data) {
temp.parent().remove();
}
);
});
}
);
//1개월 단위로 날짜 변경. 단, 12월을 넘어가거나 1월에서 감소할때는 예외 처리.
$('#nextMonth').click(function () {
$('tbody').empty();
month = month + 1;
//12월을 넘어갈 경우
if (Number(month + 1) > 12) {
year = year + 1;
month = 0;
}
$('h1').text(year + '년 ' + Number(month + 1) + '월');
listContents(year, month);
});
$('#prevMonth').click(function () {
$('tbody').empty();
month = month - 1;
if (month < 1) {
year = year - 1;
month = 11;
}
$('h1').text(year + '년 ' + Number(month + 1) + '월');
listContents(year, month);
});
//일기 작성 버튼 클릭시 일기 작성 페이지가 나타남
$('#writePlan').click(function () {
var year = today.getFullYear();
var month = today.getMonth();
var date = today.getDate();
if ($('.diaryBox').css('display') == 'none') {
$('#date').text(year + '.' + (month + 1) + '.' + date);
$('.diaryBox').css('display', 'block');
} else {
$('.diaryBox').css('display', 'none');
}
});
$('#Xbutton').click(function () {
removeContents();
$('.diaryBox').css('display', 'none');
});
$('#w_Xbutton').click(function () {
removeContents();
$('.w_diaryBox').css('display', 'none');
window.location.reload();
});
$('#emojiPicker').click(function () {
if ($('.pick').css('display') == 'none') {
$('.pick').css('display', 'block');
} else {
$('.pick').css('display', 'none');
}
});
$('.pick span').click(function () {
$('#feeling').val($(this).text());
$('.pick').css('display', 'none');
});
$('#wheatherPicker').click(function () {
if ($('.wheatherpick').css('display') == 'none') {
$('.wheatherpick').css('display', 'block');
} else {
$('.wheatherpick').css('display', 'none');
}
});
$('.wheatherpick span').click(function () {
$('#wheather').val($(this).text());
$('.wheatherpick').css('display', 'none');
});
$('#w_emojiPicker').click(function () {
if ($('.w_pick').css('display') == 'none') {
$('.w_pick').css('display', 'block');
} else {
$('.w_pick').css('display', 'none');
}
});
$('.w_pick span').click(function () {
$('#w_feeling').val($(this).text());
$('.w_pick').css('display', 'none');
});
$('#w_wheatherPicker').click(function () {
if ($('.w_wheatherpick').css('display') == 'none') {
$('.w_wheatherpick').css('display', 'block');
} else {
$('.w_wheatherpick').css('display', 'none');
}
});
$('.w_wheatherpick span').click(function () {
$('#w_wheather').val($(this).text());
$('.w_wheatherpick').css('display', 'none');
});
$('#searchemojiPicker').click(function () {
if ($('.searchpick').css('display') == 'none') {
$('.searchpick').css('display', 'block');
} else {
$('.searchpick').css('display', 'none');
}
});
$('.searchpick span').click(function () {
$('#searchfeeling').val($(this).text());
$('.searchpick').css('display', 'none');
if ($('#searchwheather').val() == '') {
feelingSearch(year, month, $(this).text());
} else {
//bothSearch(year,month,feeling,wheather)
bothSearch(year, month, $(this).text(), $('#searchwheather').val());
}
});
$('#searchwheatherPicker').click(function () {
if ($('.searchwheatherpick').css('display') == 'none') {
$('.searchwheatherpick').css('display', 'block');
} else {
$('.searchwheatherpick').css('display', 'none');
}
});
$('.searchwheatherpick span').click(function () {
$('#searchwheather').val($(this).text());
$('.searchwheatherpick').css('display', 'none');
if ($('#searchfeeling').val() == '') {
wheatherSearch(year, month, $(this).text());
} else {
//bothSearch(year,month,feeling,wheather)
bothSearch(year, month, $('#searchfeeling').val(), $(this).text());
}
});
//Todo List 입력하고 버튼 클릭했을 경우
//서버에 투두 내용 저장
$('#listClick').click(function () {
var todo = $('#ToDo').val();
$('#ToDo').val('');
$('.listBox').append(
'<li>' +
todo +
"<button class='todoRemove' type='button'>삭제</button></li>"
);
$.post('php/TodoList.php', {
userID: $('#userID').text(),
todo: todo,
});
$('.todoRemove').click(function () {
var temp = $(this);
var text = $(this).parent().html();
var afterText = text.split('<');
$.post(
'php/RemoveTodoList.php',
{
userID: $('#userID').text(),
todo: afterText[0],
},
function (data) {
temp.parent().remove();
console.log(data);
}
);
});
});
$('#completeButton').click(function () {
//작성완료 버튼을 누를시 서버로 정보 전달.
var today = $('#date').text();
var title = $('#title').val();
var wheather = $('#wheather').val();
var feeling = $('#feeling').val();
var textPlan = $('#textPlan').val();
if (title == '' || textPlan == '') {
alert('제목과 내용은 필수 입력입니다.');
} else {
$.post(
'php/SaveDiary.php',
{
userID: $('#userID').text(),
today: today,
title: title,
wheather: wheather,
feeling: feeling,
textPlan: textPlan,
},
function (data) {
removeContents();
alert('작성 완료되었습니다.');
window.location.reload();
}
);
}
});
//챌린지 화면으로 전환
$('#user').click(function () {
window.location.href = 'userPage.html';
});
$('#logout').click(function () {
sessionStorage.clear();
window.location.href = 'index.html';
});
$('#modifyDiary').click(function () {
var today = $('#w_date').text();
var title = $('#w_title').val();
var wheather = $('#w_wheather').val();
var feeling = $('#w_feeling').val();
var textPlan = $('#w_textPlan').val();
$.post(
'php/modifyDiary.php',
{
index: count,
userID: $('#userID').text(),
today: today,
title: title,
wheather: wheather,
feeling: feeling,
textPlan: textPlan,
},
function (data) {
alert('수정되었습니다.');
removeContents();
window.location.reload();
}
);
});
$('#removeDiary').click(function () {
var today = $('#w_date').text();
$.post(
'php/removeDiary.php',
{
index: count,
userID: $('#userID').text(),
today: today,
},
function (data) {
alert('삭제되었습니다.');
removeContents();
window.location.reload();
}
);
});
});
function removeContents() {
$('#title').val('');
$('#wheather').val('');
$('#feeling').val('');
$('#textPlan').val('');
}
//일기 개수로 버튼 배치가 다름.
//1개 이하 -> 버튼 나타나지않고,
//2개 -> 오른쪽으로 넘길 수 있는 버튼 생성
//2개 이상 ~ 해당 날짜 일기 개수 -1 이하 -> 좌우로 이동이 가능한 버튼 생성
//해당 날짜 일기 개수 -> 왼쪽으로 넘길 수 있는 버튼 생성
function buttonFunction(data) {
var diary = JSON.parse(data);
if (diary.length > 1) {
$('#prevDiary').css('display', 'none');
$('#nextDiary').css('display', 'inline');
} else {
$('#prevDiary').css('display', 'none');
$('#nextDiary').css('display', 'none');
}
$('#w_title').val(diary[0]['title']);
$('#w_wheather').val(diary[0]['wheather']);
$('#w_feeling').val(diary[0]['feeling']);
$('#w_textPlan').text(diary[0]['textPlan']);
$('#nextDiary').click(function () {
count++;
$('#w_title').val(diary[count]['title']);
$('#w_wheather').val(diary[count]['wheather']);
$('#w_feeling').val(diary[count]['feeling']);
$('#w_textPlan').text(diary[count]['textPlan']);
$('#prevDiary').css('display', 'inline');
if (count == diary.length - 1) {
$('#nextDiary').css('display', 'none');
}
$('#w_Xbutton').click(function () {
removeContents();
$('.w_diaryBox').css('display', 'none');
count = 0;
});
});
$('#prevDiary').click(function () {
count--;
if (count == 0) {
$('#prevDiary').css('display', 'none');
$('#nextDiary').css('display', 'inline');
}
if (count > 0 && count < diary.length) {
$('#nextDiary').css('display', 'inline');
}
$('#w_title').val(diary[count]['title']);
$('#w_wheather').val(diary[count]['wheather']);
$('#w_feeling').val(diary[count]['feeling']);
$('#w_textPlan').text(diary[count]['textPlan']);
$('#w_Xbutton').click(function () {
removeContents();
$('.w_diaryBox').css('display', 'none');
count = 0;
});
});
}
function tableFunction() {
$('table').click(function () {
var today = $(this).find('th').text();
$('#w_date').text(today);
$('.w_diaryBox').css('display', 'block');
$.post(
'php/ReadDiary.php',
{
userID: $('#userID').text(),
today: today,
},
function (data) {
buttonFunction(data);
}
);
});
}
//기분별로 일기 모아보기
function feelingSearch(year, month, feeling) {
var newYear = year;
var newMonth = month;
var newFeeling = feeling;
$.post(
'php/feelingSearch.php',
{
userID: $('#userID').text(),
year: newYear,
month: newMonth + 1,
feeling: newFeeling,
},
function (data) {
var day_array = JSON.parse(data);
$('.tableBox').empty();
for (var i = 0; i < day_array.length; i++) {
var today = day_array[i]['date'];
var content = day_array[i]['title'];
var newCount = day_array[i]['count'] - 1;
if (newCount > 0) {
$('.tableBox').append(
'<table><tr><th>' +
today +
"</th></tr><tr><td>'" +
content +
"'외 " +
newCount +
'개의 일기</td></tr></table>'
);
} else {
$('.tableBox').append(
'<table><tr><th>' +
today +
'</th></tr><tr><td>' +
content +
'</td></tr></table>'
);
}
}
$('table').click(function () {
var today = $(this).find('th').text();
$('#w_date').text(today);
$('.w_diaryBox').css('display', 'block');
$.post(
'php/feelingReadDiary.php',
{
userID: $('#userID').text(),
today: today,
feeling: newFeeling,
},
function (data) {
buttonFunction(data);
}
);
});
}
);
}
//날짜별로 일기 모아보기
function wheatherSearch(year, month, wheather) {
var newYear = year;
var newMonth = month;
var newWheather = wheather;
$.post(
'php/wheatherSearch.php',
{
userID: $('#userID').text(),
year: newYear,
month: newMonth + 1,
wheather: newWheather,
},
function (data) {
var day_array = JSON.parse(data);
console.log(day_array);
$('.tableBox').empty();
for (var i = 0; i < day_array.length; i++) {
var today = day_array[i]['date'];
var content = day_array[i]['title'];
var newCount = day_array[i]['count'] - 1;
if (newCount > 0) {
$('.tableBox').append(
'<table><tr><th>' +
today +
"</th></tr><tr><td>'" +
content +
"'외 " +
newCount +
'개의 일기</td></tr></table>'
);
} else {
$('.tableBox').append(
'<table><tr><th>' +
today +
'</th></tr><tr><td>' +
content +
'</td></tr></table>'
);
}
}
$('table').click(function () {
var today = $(this).find('th').text();
$('#w_date').text(today);
$('.w_diaryBox').css('display', 'block');
$.post(
'php/wheatherReadDiary.php',
{
userID: $('#userID').text(),
today: today,
wheather: newWheather,
},
function (data) {
buttonFunction(data);
}
);
});
}
);
}
function bothSearch(year, month, feeling, wheather) {
var newYear = year;
var newMonth = month;
var newFeeling = feeling;
var newWheather = wheather;
$.post(
'php/bothSearch.php',
{
userID: $('#userID').text(),
year: newYear,
month: newMonth + 1,
feeling: newFeeling,
wheather: newWheather,
},
function (data) {
var day_array = JSON.parse(data);
$('.tableBox').empty();
for (var i = 0; i < day_array.length; i++) {
var today = day_array[i]['date'];
var content = day_array[i]['title'];
var newCount = day_array[i]['count'] - 1;
if (newCount > 0) {
$('.tableBox').append(
'<table><tr><th>' +
today +
"</th></tr><tr><td>'" +
content +
"'외 " +
newCount +
'개의 일기</td></tr></table>'
);
} else {
$('.tableBox').append(
'<table><tr><th>' +
today +
'</th></tr><tr><td>' +
content +
'</td></tr></table>'
);
}
}
$('table').click(function () {
var today = $(this).find('th').text();
$('#w_date').text(today);
$('.w_diaryBox').css('display', 'block');
$.post(
'php/bothReadDiary.php',
{
userID: $('#userID').text(),
today: today,
wheather: newWheather,
feeling: newFeeling,
},
function (data) {
buttonFunction(data);
}
);
});
}
);
}
function listContents(year, month) {
// 년도와 월에 해당하는 데이터 가져옴
//ex) 2021.12 -> 2021년 12월의 데이터
var newYear = year;
var newMonth = month;
$.post(
'php/ListDiary.php',
{
userID: $('#userID').text(),
year: newYear,
month: newMonth + 1,
},
function (data) {
var day_array = JSON.parse(data);
$('.tableBox').empty();
for (var i = 0; i < day_array.length; i++) {
var today = day_array[i]['date'];
var content = day_array[i]['title'];
var newCount = day_array[i]['count'] - 1;
if (newCount > 0) {
$('.tableBox').append(
'<table><tr><th>' +
today +
"</th></tr><tr><td>'" +
content +
"'외 " +
newCount +
'개의 일기</td></tr></table>'
);
} else {
$('.tableBox').append(
'<table><tr><th>' +
today +
'</th></tr><tr><td>' +
content +
'</td></tr></table>'
);
}
}
$('table').click(function () {
var today = $(this).find('th').text();
$('#w_date').text(today);
$('.w_diaryBox').css('display', 'block');
$.post(
'php/ReadDiary.php',
{
userID: $('#userID').text(),
today: today,
},
function (data) {
buttonFunction(data);
}
);
});
}
);
}