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

269 lines
5.7 KiB
JavaScript
Raw Normal View History

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'
2015-07-15 13:39:07 +00:00
,Properties:
{
restrictFunc:
{
type: Function
2020-05-04 19:55:18 +00:00
,set: function(x) {
2015-07-15 13:39:07 +00:00
this._restrictFunc = x;
}
2020-05-04 19:55:18 +00:00
,get: function(x) {
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
2020-05-04 19:55:18 +00:00
,render: function() {
var len = Vn.Date.WDays.length;
2020-05-04 19:55:18 +00:00
var node = this.createRoot('div');
2016-10-16 14:16:08 +00:00
node.className = 'htk-calendar';
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);
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();
}
2020-05-04 19:55:18 +00:00
,getMonthDays: function() {
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;
}
2020-05-04 19:55:18 +00:00
,goToMonth: function(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();
}
2020-05-04 19:55:18 +00:00
,goToSelectedMonth: function() {
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();
}
2020-05-04 19:55:18 +00:00
,goToCurrentMonth: function() {
var date = new Date();
this.goToMonth(date.getFullYear(), date.getMonth());
}
2020-05-04 19:55:18 +00:00
,refresh: function() {
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
2020-05-04 19:55:18 +00:00
var today = new Date();
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);
}
2020-05-04 19:55:18 +00:00
,selectCell: function(cellIndex) {
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;
}
2020-05-04 19:55:18 +00:00
,putValue: function() {
this.goToSelectedMonth();
}
2020-05-04 19:55:18 +00:00
,dayClicked: function(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);
}
}
2020-05-04 19:55:18 +00:00
,prevMonthClicked: function() {
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();
}
2020-05-04 19:55:18 +00:00
,nextMonthClicked: function() {
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();
}
});