hedera-web/js/htk/calendar/index.js

266 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2022-06-06 08:53:59 +00:00
require('./style.scss');
2016-09-26 09:28:47 +00:00
2020-05-04 19:55:18 +00:00
module.exports = new Class({
Extends: Htk.Field
,Tag: 'htk-calendar'
2022-06-06 12:49:18 +00:00
,Properties: {
restrictFunc: {
2015-07-15 13:39:07 +00:00
type: Function
2022-11-16 01:46:44 +00:00
,set(x) {
2015-07-15 13:39:07 +00:00
this._restrictFunc = x;
}
2022-11-16 01:46:44 +00:00
,get() {
2015-07-15 13:39:07 +00:00
return this._restrictFunc;
}
}
}
2015-07-15 13:39:07 +00:00
,cells: []
,selectedCell: -1
,year: null
,month: null
2022-11-16 01:46:44 +00:00
,render() {
2022-06-07 08:19:29 +00:00
this.createRoot('div');
2020-05-04 19:55:18 +00:00
var table = this.createElement('table');
this.node.appendChild(table);
2020-05-04 19:55:18 +00:00
var colgroup = this.createElement('colgroup');
table.appendChild(colgroup);
2022-06-07 08:19:29 +00:00
var len = Vn.Date.WDays.length;
for (var i = 0; i < len; i++)
2020-05-04 19:55:18 +00:00
colgroup.appendChild(this.createElement('col'));
2020-05-04 19:55:18 +00:00
var thead = this.createElement('thead');
table.appendChild(thead);
2020-05-04 19:55:18 +00:00
var tr = this.createElement('tr');
thead.appendChild(tr);
2020-05-04 19:55:18 +00:00
var th = this.createElement('th');
2022-05-24 10:18:44 +00:00
th.className = 'previous';
2020-05-04 19:55:18 +00:00
tr.appendChild(th);
2022-05-24 10:18:44 +00:00
var previousButton = new Htk.Button({
icon: 'arrow_back_ios'
}).node;
previousButton.addEventListener('click', this.prevMonthClicked.bind(this));
th.appendChild(previousButton);
2020-05-04 19:55:18 +00:00
var th = this.createElement('th');
2022-05-24 10:18:44 +00:00
th.className = 'month-year';
2015-07-15 13:39:07 +00:00
th.colSpan = 5;
2020-05-04 19:55:18 +00:00
tr.appendChild(th);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
var monthNode = this.createElement('span');
th.appendChild(monthNode);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
var space = this.createTextNode(' ');
th.appendChild(space);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
var yearNode = this.createElement('span');
th.appendChild(yearNode);
2020-05-04 19:55:18 +00:00
var th = this.createElement('th');
2022-05-24 10:18:44 +00:00
th.className = 'next';
2020-05-04 19:55:18 +00:00
tr.appendChild(th);
2022-05-24 10:18:44 +00:00
var nextButton = new Htk.Button({
icon: 'arrow_forward_ios'
}).node;
nextButton.addEventListener('click', this.nextMonthClicked.bind(this));
th.appendChild(nextButton);
2020-05-04 19:55:18 +00:00
var tr = this.createElement('tr');
2022-05-24 10:18:44 +00:00
tr.className = 'weekdays';
2020-05-04 19:55:18 +00:00
thead.appendChild(tr);
2020-05-04 19:55:18 +00:00
for (var i = 1; i <= len; i++) {
var th = this.createElement('th');
tr.appendChild(th);
var weekday = _(Vn.Date.AbrWDays [i%len]);
2020-05-04 19:55:18 +00:00
th.appendChild(this.createTextNode(weekday));
}
2020-05-04 19:55:18 +00:00
var tbody = this.createElement('tbody');
table.appendChild(tbody);
2020-05-04 19:55:18 +00:00
for (var i = 0; i < 6; i++) {
var tr = this.createElement('tr');
tbody.appendChild(tr);
for (var j = 0; j < len; j++) {
var td = this.createElement('td');
td.addEventListener('click', this.dayClicked.bind(this, td, i*len+j));
tr.appendChild(td);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
var div = this.createElement('div');
td.appendChild(div);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
this.cells.push({
2015-07-15 13:39:07 +00:00
node: div,
enabled: false,
day: -1
});
}
}
this.monthNode = monthNode;
this.yearNode = yearNode;
2020-05-04 19:55:18 +00:00
this.goToCurrentMonth();
}
2022-11-16 01:46:44 +00:00
,getMonthDays() {
if (this.month > 6)
return (this.month % 2 != 0) ? 31 : 30;
else if (this.month != 1)
return (this.month % 2 != 1) ? 31 : 30;
else
return (this.year % 4 != 0) ? 28 : 29;
}
2022-11-16 01:46:44 +00:00
,goToMonth(year, month) {
if (year)
this.year = year;
2020-05-04 19:55:18 +00:00
if (!isNaN(month))
this.month = month;
2020-05-04 19:55:18 +00:00
this.refresh();
}
2022-11-16 01:46:44 +00:00
,goToSelectedMonth() {
var date = this._value;
if (date instanceof Date)
2020-05-04 19:55:18 +00:00
this.goToMonth(date.getFullYear(), date.getMonth());
else
2020-05-04 19:55:18 +00:00
this.goToCurrentMonth();
}
2022-11-16 01:46:44 +00:00
,goToCurrentMonth() {
var date = Date.vnNew();
2020-05-04 19:55:18 +00:00
this.goToMonth(date.getFullYear(), date.getMonth());
}
2022-11-16 01:46:44 +00:00
,refresh() {
2020-05-04 19:55:18 +00:00
Vn.Node.setText(this.yearNode, this.year);
Vn.Node.setText(this.monthNode, _(Vn.Date.Months[this.month]));
var day = 1;
2020-05-04 19:55:18 +00:00
var cellDate = new Date(this.year, this.month, 1);
2015-07-15 13:39:07 +00:00
2020-05-04 19:55:18 +00:00
var weekDay = cellDate.getDay();
2015-07-15 13:39:07 +00:00
var firstWeekDay = (weekDay != 0) ? weekDay - 1 : 6;
2020-05-04 19:55:18 +00:00
var monthDays = this.getMonthDays();
2020-05-04 19:55:18 +00:00
for (var i = 0; i < this.cells.length; i++) {
2015-07-15 13:39:07 +00:00
var cell = this.cells[i];
2020-05-04 19:55:18 +00:00
if (firstWeekDay <= i && day <= monthDays) {
Vn.Node.setText(cell.node, day);
2015-07-15 13:39:07 +00:00
cell.enabled = true;
cell.day = day++;
2020-05-04 19:55:18 +00:00
if (this._restrictFunc) {
cell.enabled = this._restrictFunc(cellDate);
cellDate.setDate(cellDate.getDate() + 1);
2015-07-15 13:39:07 +00:00
}
2020-05-04 19:55:18 +00:00
} else {
Vn.Node.removeChilds(cell.node);
2015-07-15 13:39:07 +00:00
cell.enabled = false;
cell.day = -1;
}
cell.node.className = cell.enabled ? 'enabled' : 'disabled';
}
2015-07-15 13:39:07 +00:00
// Marks the current day
2015-07-15 13:39:07 +00:00
var today = Date.vnNew();
2020-05-04 19:55:18 +00:00
if (this.year == today.getFullYear()
&& this.month == today.getMonth()) {
var cellIndex = (firstWeekDay + today.getDate()) - 1;
Vn.Node.addClass(this.cells[cellIndex].node, 'today');
}
// Marks the selected day
var date = this._value;
if (date instanceof Date
2020-05-04 19:55:18 +00:00
&& this.year == date.getFullYear()
&& this.month == date.getMonth())
this.selectCell((firstWeekDay + date.getDate()) - 1);
else
2020-05-04 19:55:18 +00:00
this.selectCell(-1);
}
2022-11-16 01:46:44 +00:00
,selectCell(cellIndex) {
2020-05-04 19:55:18 +00:00
if (this.selectedCell != -1) {
2015-07-15 13:39:07 +00:00
var node = this.cells[this.selectedCell].node;
2020-05-04 19:55:18 +00:00
Vn.Node.removeClass(node, 'selected');
2015-07-15 13:39:07 +00:00
}
2020-05-04 19:55:18 +00:00
if (cellIndex != -1) {
2015-07-15 13:39:07 +00:00
var node = this.cells[cellIndex].node;
2020-05-04 19:55:18 +00:00
Vn.Node.addClass(node, 'selected');
2015-07-15 13:39:07 +00:00
}
2015-07-15 13:39:07 +00:00
this.selectedCell = cellIndex;
}
2022-11-16 01:46:44 +00:00
,putValue() {
2020-05-04 19:55:18 +00:00
this.goToSelectedMonth();
}
2022-11-16 01:46:44 +00:00
,dayClicked(td, cellIndex) {
2015-07-15 13:39:07 +00:00
var cell = this.cells[cellIndex];
2020-05-04 19:55:18 +00:00
if (cell.enabled) {
this.selectCell(cellIndex);
var newDate = new Date(this.year, this.month, cell.day);
if (this.value) {
var orgDate = this.value instanceof Date
? this.value
: new Date(this.value);
2021-01-22 19:07:52 +00:00
if (!isNaN(orgDate.getTime()))
newDate.setHours(
orgDate.getHours(),
orgDate.getMinutes(),
orgDate.getSeconds(),
orgDate.getMilliseconds()
);
2020-05-04 19:55:18 +00:00
}
2020-05-04 19:55:18 +00:00
this.valueChanged(newDate);
}
}
2022-11-16 01:46:44 +00:00
,prevMonthClicked() {
if (this.month > 0)
this.month--;
2020-05-04 19:55:18 +00:00
else {
this.month = 11;
this.year--;
}
2020-05-04 19:55:18 +00:00
this.refresh();
}
2022-11-16 01:46:44 +00:00
,nextMonthClicked() {
if (this.month < 11)
this.month++;
2020-05-04 19:55:18 +00:00
else {
this.month = 0;
this.year++;
}
2020-05-04 19:55:18 +00:00
this.refresh();
}
});