ttvtlb commited on
Commit
8050a8c
·
verified ·
1 Parent(s): 63fb39f

thêm lịch

Browse files
Files changed (2) hide show
  1. components/calendar.js +128 -0
  2. index.html +1 -0
components/calendar.js ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class CustomCalendar extends HTMLElement {
2
+ connectedCallback() {
3
+ this.attachShadow({ mode: 'open' });
4
+ this.renderCalendar(new Date());
5
+ }
6
+
7
+ renderCalendar(date) {
8
+ const monthNames = ["January", "February", "March", "April", "May", "June",
9
+ "July", "August", "September", "October", "November", "December"
10
+ ];
11
+
12
+ const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
13
+ const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
14
+ const daysInMonth = lastDay.getDate();
15
+ const startingDay = firstDay.getDay();
16
+
17
+ let calendarHTML = '';
18
+ let day = 1;
19
+
20
+ this.shadowRoot.innerHTML = `
21
+ <style>
22
+ .calendar {
23
+ width: 100%;
24
+ max-width: 400px;
25
+ background: white;
26
+ border-radius: 0.75rem;
27
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
28
+ padding: 1rem;
29
+ }
30
+ .calendar-header {
31
+ display: flex;
32
+ justify-content: space-between;
33
+ align-items: center;
34
+ margin-bottom: 1rem;
35
+ }
36
+ .calendar-title {
37
+ font-size: 1.25rem;
38
+ font-weight: 600;
39
+ color: #1a365d;
40
+ }
41
+ .calendar-nav {
42
+ display: flex;
43
+ gap: 0.5rem;
44
+ }
45
+ .calendar-nav button {
46
+ background: #edf2f7;
47
+ border: none;
48
+ border-radius: 0.375rem;
49
+ padding: 0.25rem 0.5rem;
50
+ cursor: pointer;
51
+ }
52
+ .calendar-weekdays {
53
+ display: grid;
54
+ grid-template-columns: repeat(7, 1fr);
55
+ text-align: center;
56
+ font-weight: 500;
57
+ margin-bottom: 0.5rem;
58
+ }
59
+ .calendar-days {
60
+ display: grid;
61
+ grid-template-columns: repeat(7, 1fr);
62
+ gap: 0.25rem;
63
+ }
64
+ .calendar-day {
65
+ padding: 0.5rem;
66
+ text-align: center;
67
+ border-radius: 0.375rem;
68
+ cursor: pointer;
69
+ }
70
+ .calendar-day:hover {
71
+ background: #ebf8ff;
72
+ }
73
+ .calendar-day.empty {
74
+ visibility: hidden;
75
+ }
76
+ .calendar-day.today {
77
+ background: #4299e1;
78
+ color: white;
79
+ }
80
+ .calendar-day.selected {
81
+ background: #3182ce;
82
+ color: white;
83
+ }
84
+ </style>
85
+ <div class="calendar">
86
+ <div class="calendar-header">
87
+ <div class="calendar-title">${monthNames[date.getMonth()]} ${date.getFullYear()}</div>
88
+ <div class="calendar-nav">
89
+ <button id="prevMonth">&lt;</button>
90
+ <button id="nextMonth">&gt;</button>
91
+ </div>
92
+ </div>
93
+ <div class="calendar-weekdays">
94
+ <div>Sun</div>
95
+ <div>Mon</div>
96
+ <div>Tue</div>
97
+ <div>Wed</div>
98
+ <div>Thu</div>
99
+ <div>Fri</div>
100
+ <div>Sat</div>
101
+ </div>
102
+ <div class="calendar-days">
103
+ ${Array(42).fill().map((_, i) => {
104
+ if (i < startingDay || day > daysInMonth) {
105
+ return `<div class="calendar-day empty"></div>`;
106
+ }
107
+ const currentDay = day;
108
+ const isToday = currentDay === new Date().getDate() &&
109
+ date.getMonth() === new Date().getMonth() &&
110
+ date.getFullYear() === new Date().getFullYear();
111
+ day++;
112
+ return `<div class="calendar-day ${isToday ? 'today' : ''}">${currentDay}</div>`;
113
+ }).join('')}
114
+ </div>
115
+ </div>
116
+ `;
117
+
118
+ this.shadowRoot.getElementById('prevMonth').addEventListener('click', () => {
119
+ this.renderCalendar(new Date(date.getFullYear(), date.getMonth() - 1, 1));
120
+ });
121
+
122
+ this.shadowRoot.getElementById('nextMonth').addEventListener('click', () => {
123
+ this.renderCalendar(new Date(date.getFullYear(), date.getMonth() + 1, 1));
124
+ });
125
+ }
126
+ }
127
+
128
+ customElements.define('custom-calendar', CustomCalendar);
index.html CHANGED
@@ -9,6 +9,7 @@
9
  <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
10
  <script src="https://unpkg.com/feather-icons"></script>
11
  <script src="components/auth-form.js"></script>
 
12
  </head>
13
  <body class="bg-gray-50 min-h-screen flex items-center justify-center">
14
  <auth-form></auth-form>
 
9
  <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
10
  <script src="https://unpkg.com/feather-icons"></script>
11
  <script src="components/auth-form.js"></script>
12
+ <script src="components/calendar.js"></script>
13
  </head>
14
  <body class="bg-gray-50 min-h-screen flex items-center justify-center">
15
  <auth-form></auth-form>