- class DatePicker {
- constructor(id, callback) {
- this.date = undefined;
- this.id = id;
- this.callback = callback;
- }
- render(date) {
- this.date = date;
- this.update();
- }
- createDayInMonth() {
- const td = document.createElement('td');
- td.setAttribute('onmouseover', 'MouseOverCell(event);');
- td.setAttribute('onmouseout', 'MouseOutCell(event);');
- td.addEventListener('click', (event) => {
- const attribute = event.target.getAttribute("class");
- if (!attribute) {
- this.callback(this.id, {
- year: this.date.getFullYear(),
- month: this.date.getMonth() + 1,
- day: event.target.textContent
- });
- }
- var tdList = document.getElementsByClassName('chooseDay')
- for (var i = 0; i < tdList.length; i++) {
- tdList[i].classList.remove('chooseDay');
- td.setAttribute('class', 'ThisMonth')
- }
- td.setAttribute('class','chooseDay')
- });
- return td;
- }
- createDayNotInMonth() {
- const td = document.createElement('td');
- td.setAttribute('onmouseover', 'MouseOverCell(event);');
- td.setAttribute('onmouseout', 'MouseOutCell(event);');
- td.addEventListener('click', (event) => {
- const attribute = event.target.getAttribute("class");
- if (!attribute) {
- this.callback(this.id, {
- year: this.date.getFullYear(),
- month: this.date.getMonth() + 1,
- day: event.target.textContent
- });
- }
- });
- return td;
- }
- update() {
- const root = document.getElementById(this.id);
- const table = document.createElement('table');
- root.appendChild(table);
- const headers = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
- table.style.border = '1';
- //Thêm tiêu đề cho lịch
- const topRow = document.createElement('tr');
- const header = document.createElement('tr');
- topRow.setAttribute('class', 'TopRow');
- table.appendChild(topRow);
- table.appendChild(header);
- const top = document.createElement('th');
- top.setAttribute('class', 'TopTitle');
- top.setAttribute('colspan', '7');
- const topTitle = document.createElement('span');
- topTitle.setAttribute('class', 'TopTitleText');
- topTitle.textContent = `${this.date.getMonth() + 1}/${this.date.getFullYear()}`;
- topTitle.setAttribute(onmouseover, 'MouseOverTitle(event);');
- topTitle.setAttribute(onmouseout, 'MouseOutTitle(event);');
- topRow.appendChild(top);
- const lastButton = document.createElement('button');
- lastButton.setAttribute("id", "left-button")
- const nextButton = document.createElement('button');
- nextButton.setAttribute("id", "right-button")
- lastButton.addEventListener("click", () => {
- this.date.setMonth(this.date.getMonth() - 1);
- this.update();
- table.remove();
- });
- lastButton.textContent = '<';
- nextButton.addEventListener('click', () => {
- this.date.setMonth(this.date.getMonth() + 1);
- this.update();
- table.remove();
- });
- nextButton.textContent = '>';
- top.appendChild(lastButton);
- top.appendChild(topTitle);
- top.appendChild(nextButton);
- for (var day of headers) {
- const e = document.createElement('th');
- e.textContent = day;
- header.appendChild(e);
- }
- const date = new Date(this.date);
- date.setDate(1);
- const first_row = document.createElement('tr');
- let i = 0;
- //Hoàn thành ngày của tháng về trước
- date.setDate(date.getDate() - date.getDay());
- for (; date.getMonth() !== this.date.getMonth(); ++i) {
- const cell = this.createDayNotInMonth();
- cell.setAttribute('class', 'NotThisMonth');
- cell.textContent = date.getDate();
- first_row.appendChild(cell);
- date.setDate(date.getDate() + 1);
- }
- // Các ngày trong tháng
- let row = first_row;
- while (this.date.getMonth() === date.getMonth()) {
- for (; i < 7 && this.date.getMonth() === date.getMonth(); i++) {
- const cell = this.createDayInMonth();
- cell.setAttribute('class', 'ThisMonth')
- cell.textC
- // cell.appendChild(p);
- date.setDate(date.getDate() + 1);
- row.appendChild(cell);
- }
- table.appendChild(row);
- if (i >= 7) {
- row = document.createElement('tr');
- i = 0;
- }
- }
- while (date.getDay() > 0) {
- const cell = this.createDayInMonth();
- cell.setAttribute('class', 'NotThisMonth');
- // cell.appendChild(p);
- row.appendChild(cell);
- cell.textContent = date.getDate();
- date.setDate(date.getDate() + 1);
- }
- }
- }
- function MouseOverCell(event) {
- event.target.setAttribute("id", "MouseOver");
- }
- function MouseOutCell(event) {
- event.target.removeAttribute("id");
- }
- function MouseOutTitle(event) {
- event.target.removeAttribute("Title");
- }
- function MouseOverTitle(event) {
- event.target.setAttribute("id", "Title");
- }