-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain.js
More file actions
176 lines (163 loc) · 5.31 KB
/
main.js
File metadata and controls
176 lines (163 loc) · 5.31 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
const btnMonthPrev = document.querySelector(".ctrl .btnMonthPrev");
const btnMonthNext = document.querySelector(".ctrl .btnMonthNext");
let dateNow = new Date(new Date().getFullYear(), new Date().getMonth(),1);
let maxDays = 8;
let startDate = "";
let endDate = "";
let canSelect = true;
let bookingList = [
"2023-9-13",
["2023-9-15", "2023-9-22"],
["2023-10-2", "2023-10-6"],
["2023-10-22", "2023-10-26"],
["2023-11-15", "2023-11-17"]
];
bookingList = bookingList.reduce((acc, item) => {
if (Array.isArray(item)) {
acc.push(...getDatesArray(...item));
} else {
acc.push(item);
}
return acc;
}, []);
console.log(bookingList)
if(startDate !== ""){
dateNow = new Date(new Date(startDate).getFullYear(), new Date(startDate).getMonth(),1)
}
setCalendar();
btnMonthNext.addEventListener("click", (evt) => {
let newMonth = dateNow.getMonth()+1;
dateNow = new Date(dateNow.getFullYear(),newMonth,1);
setCalendar();
});
btnMonthPrev.addEventListener("click", (evt) => {
let newMonth = dateNow.getMonth()-1;
dateNow = new Date(dateNow.getFullYear(),newMonth,1);
setCalendar();
});
function setCalendar(){
let totalDays = new Date(dateNow.getFullYear(),dateNow.getMonth()+1,0).getDate();
let firstDay = dateNow.getDay();
const mainLine1 = document.querySelector(".mLeft .mainLine");
const info1 = document.querySelector(".mLeft .info");
info1.innerHTML = dateNow.getFullYear()+"-"+(dateNow.getMonth()+1).toString().padStart(2,"0");
let dayHtml1 = "";
for(let i=1;i<=totalDays;i++){
let date = new Date(dateNow.getFullYear(),dateNow.getMonth(),i);
let day = date.getDay();
let template = `<div class="day date date${i} day${day}" date="${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}">${i}</div>`;
dayHtml1+=template;
}
mainLine1.innerHTML = dayHtml1;
// 每個月第一天的縮排,寫法會和週日是第一天或最後一天有關
// 目前這寫法是週日為最後一天
const date1 = document.querySelector(".date1");
if(firstDay === 0){
firstDay = 7;
}
date1.style.marginLeft = ((firstDay-1)*40)+"px";
let dates = document.querySelectorAll(".calendar .date");
dates.forEach((dateBtn)=>{
let thisDate = dateBtn.getAttribute("date");
if(bookingList.includes(thisDate)){
dateBtn.classList.add("disabled");
}
if(startDate !== "" && endDate === ""){
if(startDate === thisDate){
dateBtn.classList.add("started");
}
}
if(startDate !== "" && endDate !== ""){
colorSelected();
}
if(canSelect === true){
dateBtn.addEventListener("click",function(event){
let date = event.currentTarget.getAttribute("date");
if(startDate !== "" && endDate !== ""){
// 如果已經有註記了起始與結束日期,就清掉重置
startDate = "";
endDate = "";
resetSelected();
}
if(startDate === ""){
// 如果 startDate 是空字串,就把按下的那個變成 startDate
startDate = date;
this.classList.add("started");
}else if(endDate === ""){
endDateCheck(date, this);
}
});
}
})
}
function endDateCheck(date, target){
// 檢查是不是能被選,預設是可以被點選變成 endDate
let canSet = true;
let alertInfo = "";
// 判斷結束日期是不是小於起始日期
if(new Date(date) < new Date(startDate)){
canSet = false;
alertInfo = "不能夠往起始日期的前面日期選";
}
// 判斷選擇區間是否包含 disabled 的日期
bookingList.forEach((date1)=>{
if(new Date(date1) > new Date(startDate) && new Date(date1) < new Date(date)){
canSet = false;
alertInfo = "選取的區間有無法預定的日期";
}
})
// 判斷是否大於最大可選天數
let passDays = parseInt((new Date(date) - new Date(startDate))/1000/60/60/24)+1
if(passDays>maxDays){
canSet = false;
alertInfo = "目前選取天數多於可支援天數";
}
// 總結判斷結果
if(canSet === true){
endDate = date;
target.classList.add("started");
colorSelected();
}else{
alert(alertInfo);
startDate = "";
endDate = "";
resetSelected();
}
}
function colorSelected(){
let dates = document.querySelectorAll(".calendar .date");
dates.forEach((dateBtn)=>{
dateBtn.classList.remove("started");
let date = dateBtn.getAttribute("date");
if(date === startDate){
dateBtn.classList.add("selectedStart");
}
if(new Date(date) > new Date(startDate) && new Date(date) < new Date(endDate)){
dateBtn.classList.add("selectedInclude");
}
if(date === endDate){
dateBtn.classList.add("selectedEnd");
}
});
}
function resetSelected(){
let dates = document.querySelectorAll(".calendar .date");
dates.forEach((dateBtn)=>{
dateBtn.classList.remove("started");
dateBtn.classList.remove("selectedStart");
dateBtn.classList.remove("selectedEnd");
dateBtn.classList.remove("selectedInclude");
});
}
function getDatesArray(start, end) {
const startDate = new Date(start);
const endDate = new Date(end);
const datesArray = [];
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
const year = d.getFullYear();
const month = d.getMonth() + 1;
const date = d.getDate();
datesArray.push(`${year}-${month}-${date}`);
}
return datesArray;
}