Rewrite to es6 class
parent
3f3525efd3
commit
964930c39f
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"presets": [ [ "env", { modules: "umd" } ], "stage-1", "react" ],
|
||||||
|
"retainLines": true
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
width: 301px;
|
width: 301px;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
z-index: 100;
|
z-index: 10000;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
font-family: Roboto, sans-serif;
|
font-family: Roboto, sans-serif;
|
||||||
box-shadow: 0 19px 60px rgba(0, 0, 0, .3), 0 15px 20px rgba(0, 0, 0, .22)
|
box-shadow: 0 19px 60px rgba(0, 0, 0, .3), 0 15px 20px rgba(0, 0, 0, .22)
|
||||||
|
|
607
calendar.js
607
calendar.js
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* Original: http://www.openjs.com/scripts/ui/calendar/
|
* Original: http://www.openjs.com/scripts/ui/calendar/
|
||||||
* Modified: http://yourcmc.ru/git/vitalif-js/calendar
|
* Modified: http://yourcmc.ru/git/vitalif-js/calendar
|
||||||
* Version: 2018-03-14
|
* Version: 2018-03-17
|
||||||
* License: MIT-like, http://www.openjs.com/license.php
|
* License: MIT-like, http://www.openjs.com/license.php
|
||||||
*
|
*
|
||||||
* Example:
|
* Example:
|
||||||
|
@ -13,294 +13,365 @@
|
||||||
* Calendar.set("date");
|
* Calendar.set("date");
|
||||||
* </script>
|
* </script>
|
||||||
*/
|
*/
|
||||||
function Calendar()
|
export class Calendar
|
||||||
{
|
{
|
||||||
}
|
// Configuration
|
||||||
|
static defaultOptions = {
|
||||||
// Configuration
|
month_names: ["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"],
|
||||||
Calendar.defaultOptions = {
|
close_label: 'Закрыть',
|
||||||
month_names: ["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"],
|
weekdays: ["Пн","Вт","Ср","Чт","Пт","Сб","Вс"],
|
||||||
close_label: 'Закрыть',
|
sunday: 6,
|
||||||
weekdays: ["Пн","Вт","Ср","Чт","Пт","Сб","Вс"],
|
selectboxes: false, // true: use selectboxes for year and month, false: show months and years in table
|
||||||
sunday: 6,
|
years: {min: -70, max: 10}, // range of displayed years if selectboxes==true
|
||||||
selectboxes: false, // true: use selectboxes for year and month, false: show months and years in table
|
format: 'd.m.Y', // either d.m.Y or Y-m-d, other formats are not supported
|
||||||
years: {min: -70, max: 10}, // range of displayed years if selectboxes==true
|
month_days: [31,28,31,30,31,30,31,31,30,31,30,31],
|
||||||
format: 'd.m.Y', // either d.m.Y or Y-m-d, other formats are not supported
|
// Today's date
|
||||||
month_days: [31,28,31,30,31,30,31,31,30,31,30,31],
|
today: new Date(),
|
||||||
// Today's date
|
callback: null,
|
||||||
today: new Date(),
|
start: 'days',
|
||||||
};
|
|
||||||
|
|
||||||
// State variables
|
|
||||||
Calendar.instance = null;
|
|
||||||
Calendar.box = null;
|
|
||||||
Calendar.addedListener = false;
|
|
||||||
|
|
||||||
Calendar.prototype.setHTML = function(html)
|
|
||||||
{
|
|
||||||
html += "<a class='calendar-cancel' onclick='Calendar.hideCalendar();'>"+this.close_label+"</a>";
|
|
||||||
Calendar.box.innerHTML = html;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Called when the user clicks on a date in the calendar.
|
|
||||||
Calendar.prototype.selectDate = function(year, month, day)
|
|
||||||
{
|
|
||||||
var i = this.input;
|
|
||||||
var t = i.value.split(/\s+/, 2)[1]||'';
|
|
||||||
if (t)
|
|
||||||
t = ' '+t;
|
|
||||||
i.value = (this.format == 'Y-m-d' ? year + '-' + month + '-' + day : day + '.' + month + '.' + year) + t;
|
|
||||||
Calendar.hideCalendar();
|
|
||||||
};
|
|
||||||
|
|
||||||
Calendar.prototype.showMonths = function(year, month)
|
|
||||||
{
|
|
||||||
var cur_y = this.today.getFullYear();
|
|
||||||
var cur_m = this.today.getMonth();
|
|
||||||
var sel_m = this.selected.getFullYear() == year ? this.selected.getMonth() : -1;
|
|
||||||
var html = '';
|
|
||||||
html += "<table>";
|
|
||||||
html += "<tr><th colspan='4' class='calendar-title'><a href='javascript:Calendar.instance.showMonths("+(year-1)+")' title='"+(year-1)+"' class='prev'></a>";
|
|
||||||
html += " <a href='javascript:Calendar.instance.showYears("+year+")'>"+year+"</a>";
|
|
||||||
html += " <a href='javascript:Calendar.instance.showMonths("+(year+1)+")' title='"+(year+1)+"' class='next'></a></th></tr>";
|
|
||||||
html += "<tr>";
|
|
||||||
for (var i in this.month_names) {
|
|
||||||
if (i && !(i % 3))
|
|
||||||
html += "</tr><tr>";
|
|
||||||
var class_name = (year < cur_y || year == cur_y && i < cur_m ? 'past' :
|
|
||||||
(year > cur_y || year == cur_y && i > cur_m ? 'future' : 'today'))
|
|
||||||
+ (i == sel_m ? ' selected' : '');
|
|
||||||
html += "<td class='months "+class_name+"'><a href='javascript:Calendar.instance.makeCalendar("+year+","+i+")'>"+this.month_names[i]+"</a></td>";
|
|
||||||
}
|
|
||||||
html += "</tr>";
|
|
||||||
html += "</table>";
|
|
||||||
this.setHTML(html);
|
|
||||||
};
|
|
||||||
|
|
||||||
Calendar.prototype.showYears = function(year)
|
|
||||||
{
|
|
||||||
var beg = year & ~15;
|
|
||||||
var cur_y = this.today.getFullYear();
|
|
||||||
var sel_y = this.selected.getFullYear();
|
|
||||||
var html = '';
|
|
||||||
html += "<table>";
|
|
||||||
html += "<tr><th colspan='4' class='calendar-title'>";
|
|
||||||
html += " <a href='javascript:Calendar.instance.showYears("+(year-16)+");' title='"+(beg-16)+" - "+(beg-1)+"' class='prev'></a>";
|
|
||||||
html += " <b>"+beg+" - "+(beg+15)+"</b>";
|
|
||||||
html += " <a href='javascript:Calendar.instance.showYears("+(year+16)+");' title='"+(beg+16)+" - "+(beg+31)+"' class='next'></a>";
|
|
||||||
html += "</th></tr>";
|
|
||||||
html += "<tr>";
|
|
||||||
for (var i = 0; i < 16; i++) {
|
|
||||||
if (i && !(i % 4))
|
|
||||||
html += "</tr><tr>";
|
|
||||||
var class_name = (beg+i < cur_y ? 'past' : (beg+i > cur_y ? 'future' : 'today'))
|
|
||||||
+ (beg+i == sel_y ? ' selected' : '');
|
|
||||||
html += "<td class='years "+class_name+"'><a href='javascript:Calendar.instance.showMonths("+(beg+i)+")'>"+(beg+i)+"</a></td>";
|
|
||||||
}
|
|
||||||
html += "</tr>";
|
|
||||||
html += "</table>";
|
|
||||||
this.setHTML(html);
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Creates a calendar with the date given in the argument as the selected date.
|
|
||||||
Calendar.prototype.makeCalendar = function(year, month)
|
|
||||||
{
|
|
||||||
// Display the table
|
|
||||||
var next_month = month+1;
|
|
||||||
var next_month_year = year;
|
|
||||||
if (next_month >= 12) {
|
|
||||||
next_month = 0;
|
|
||||||
next_month_year++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var previous_month = month-1;
|
// State variables
|
||||||
var previous_month_year = year;
|
static instance = null;
|
||||||
if (previous_month < 0) {
|
static box = null;
|
||||||
previous_month = 11;
|
static addedListener = false;
|
||||||
previous_month_year--;
|
|
||||||
|
setHTML(html)
|
||||||
|
{
|
||||||
|
html += "<a class='calendar-cancel' onclick='Calendar.instance.hideCalendar()'>"+this.close_label+"</a>";
|
||||||
|
Calendar.box.innerHTML = html;
|
||||||
}
|
}
|
||||||
|
|
||||||
var current_year = this.today.getFullYear();
|
isVisible()
|
||||||
|
{
|
||||||
|
return Calendar.instance == this && Calendar.box.style.display == 'block';
|
||||||
|
}
|
||||||
|
|
||||||
var html = '';
|
/// Called when the user clicks on a date in the calendar.
|
||||||
html += "<table>";
|
selectDate(year, month, day)
|
||||||
html += "<tr><th colspan='7' class='calendar-title'><a href='javascript:Calendar.instance.makeCalendar("+previous_month_year+","+previous_month+");' title='"+this.month_names[previous_month]+" "+(previous_month_year)+"' class='prev'></a>";
|
{
|
||||||
if (!this.selectboxes) {
|
var i = this.input;
|
||||||
html += " <a href='javascript:Calendar.instance.showMonths("+year+", "+month+")'>"+this.month_names[month]+"</a>";
|
var t = i.value.split(/\s+/, 2)[1]||'';
|
||||||
html += " <a href='javascript:Calendar.instance.showYears("+year+")'>"+year+"</a>";
|
if (t)
|
||||||
} else {
|
t = ' '+t;
|
||||||
html += " <select name='calendar-month' class='calendar-month' onChange='Calendar.instance.makeCalendar("+year+",this.value);'>";
|
if (this.callback)
|
||||||
for (var i in this.month_names) {
|
{
|
||||||
html += "<option value='"+i+"'";
|
var c = this.callback;
|
||||||
if (i == month) html += " selected='selected'";
|
t = year + '-' + month + '-' + day + t;
|
||||||
html += ">"+this.month_names[i]+"</option>";
|
c(new Date(t));
|
||||||
}
|
}
|
||||||
html += "</select>";
|
else
|
||||||
html += "<select name='calendar-year' class='calendar-year' onChange='Calendar.instance.makeCalendar(this.value, "+month+");'>";
|
{
|
||||||
for (var i = current_year+this.years.min; i < current_year+this.years.max; i++) {
|
t = (this.format == 'Y-m-d' ? year + '-' + month + '-' + day : day + '.' + month + '.' + year) + t;
|
||||||
html += "<option value='"+i+"'"
|
i.value = t;
|
||||||
if (i == year) html += " selected='selected'";
|
if ("Event" in window)
|
||||||
html += ">"+i+"</option>";
|
{
|
||||||
}
|
var evt = document.createEvent('Event');
|
||||||
html += "</select>";
|
evt.initEvent('change', true, true);
|
||||||
}
|
i.dispatchEvent(evt);
|
||||||
html += " <a href='javascript:Calendar.instance.makeCalendar("+next_month_year+","+next_month+");' title='"+this.month_names[next_month]+" "+next_month_year+"' class='next'></a></th></tr>";
|
|
||||||
html += "<tr class='header'>";
|
|
||||||
for (var weekday = 0; weekday < 7; weekday++)
|
|
||||||
html += "<td>"+this.weekdays[weekday]+"</td>";
|
|
||||||
html += "</tr>";
|
|
||||||
|
|
||||||
// Get the first day of this month
|
|
||||||
var first_day = new Date(year,month,1);
|
|
||||||
var start_day = (first_day.getDay()+this.sunday)%7;
|
|
||||||
|
|
||||||
var d = 1;
|
|
||||||
var flag = 0;
|
|
||||||
|
|
||||||
// Leap year support
|
|
||||||
if (!(year % 4) && ((year % 100) || !(year % 400)))
|
|
||||||
this.month_days[1] = 29;
|
|
||||||
else
|
|
||||||
this.month_days[1] = 28;
|
|
||||||
|
|
||||||
var days_in_this_month = this.month_days[month];
|
|
||||||
|
|
||||||
// Create the calendar
|
|
||||||
var yea = this.today.getFullYear();
|
|
||||||
var all_diff = (year - yea) || (month - this.today.getMonth());
|
|
||||||
var sel_day = year == this.selected.getFullYear() && month == this.selected.getMonth() ? this.selected.getDate() : -1;
|
|
||||||
for (var i = 0; i <= 5; i++) {
|
|
||||||
if (w >= days_in_this_month)
|
|
||||||
break;
|
|
||||||
html += "<tr>";
|
|
||||||
for (var j = 0; j < 7; j++) {
|
|
||||||
if (d > days_in_this_month)
|
|
||||||
flag = 0; // If the days has overshooted the number of days in this month, stop writing
|
|
||||||
else if (j >= start_day && !flag)
|
|
||||||
flag = 1; // If the first day of this month has come, start the date writing
|
|
||||||
|
|
||||||
if (flag) {
|
|
||||||
var w = d, mon = month+1;
|
|
||||||
if (w < 10)
|
|
||||||
w = "0" + w;
|
|
||||||
if (mon < 10)
|
|
||||||
mon = "0" + mon;
|
|
||||||
|
|
||||||
// Is it today?
|
|
||||||
var class_name = '';
|
|
||||||
|
|
||||||
var diff = all_diff || (d - this.today.getDate());
|
|
||||||
if (diff < 0)
|
|
||||||
class_name = ' past';
|
|
||||||
else if (!diff)
|
|
||||||
class_name = ' today';
|
|
||||||
else
|
|
||||||
class_name = ' future';
|
|
||||||
|
|
||||||
if (d == sel_day)
|
|
||||||
class_name += ' selected';
|
|
||||||
|
|
||||||
class_name += " " + this.weekdays[j].toLowerCase();
|
|
||||||
|
|
||||||
html += "<td class='days"+class_name+"'><a href='javascript:Calendar.instance.selectDate(\""+year+"\",\""+mon+"\",\""+w+"\")'>"+d+"</a></td>";
|
|
||||||
d++;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
html += "<td class='days'> </td>";
|
i.fireEvent("onchange");
|
||||||
|
}
|
||||||
|
Calendar.instance.hideCalendar();
|
||||||
|
}
|
||||||
|
|
||||||
|
showMonths(year)
|
||||||
|
{
|
||||||
|
var cur_y = this.today.getFullYear();
|
||||||
|
var cur_m = this.today.getMonth();
|
||||||
|
var sel_m = this.selected.getFullYear() == year ? this.selected.getMonth() : -1;
|
||||||
|
var html = '';
|
||||||
|
html += "<table>";
|
||||||
|
html += "<tr><th colspan='4' class='calendar-title'><a href='javascript:Calendar.instance.showMonths("+(year-1)+")' title='"+(year-1)+"' class='prev'></a>";
|
||||||
|
html += " <a href='javascript:Calendar.instance.showYears("+year+")'>"+year+"</a>";
|
||||||
|
html += " <a href='javascript:Calendar.instance.showMonths("+(year+1)+")' title='"+(year+1)+"' class='next'></a></th></tr>";
|
||||||
|
html += "<tr>";
|
||||||
|
for (var i in this.month_names) {
|
||||||
|
if (i && !(i % 3))
|
||||||
|
html += "</tr><tr>";
|
||||||
|
var class_name = (year < cur_y || year == cur_y && i < cur_m ? 'past' :
|
||||||
|
(year > cur_y || year == cur_y && i > cur_m ? 'future' : 'today'))
|
||||||
|
+ (i == sel_m ? ' selected' : '');
|
||||||
|
html += "<td class='months "+class_name+"'><a href='javascript:Calendar.instance.makeCalendar("+year+","+i+")'>"+this.month_names[i]+"</a></td>";
|
||||||
}
|
}
|
||||||
html += "</tr>";
|
html += "</tr>";
|
||||||
|
html += "</table>";
|
||||||
|
this.setHTML(html);
|
||||||
}
|
}
|
||||||
html += "</table>";
|
|
||||||
this.setHTML(html);
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Display the calendar - if a date exists in the input box, that will be selected in the calendar.
|
showYears(year)
|
||||||
Calendar.prototype.showCalendar = function()
|
{
|
||||||
{
|
var beg = year & ~15;
|
||||||
Calendar.instance = this;
|
var cur_y = this.today.getFullYear();
|
||||||
var input = this.input;
|
var sel_y = this.selected.getFullYear();
|
||||||
|
var html = '';
|
||||||
|
html += "<table>";
|
||||||
|
html += "<tr><th colspan='4' class='calendar-title'>";
|
||||||
|
html += " <a href='javascript:Calendar.instance.showYears("+(year-16)+");' title='"+(beg-16)+" - "+(beg-1)+"' class='prev'></a>";
|
||||||
|
html += " <b>"+beg+" - "+(beg+15)+"</b>";
|
||||||
|
html += " <a href='javascript:Calendar.instance.showYears("+(year+16)+");' title='"+(beg+16)+" - "+(beg+31)+"' class='next'></a>";
|
||||||
|
html += "</th></tr>";
|
||||||
|
html += "<tr>";
|
||||||
|
for (var i = 0; i < 16; i++) {
|
||||||
|
if (i && !(i % 4))
|
||||||
|
html += "</tr><tr>";
|
||||||
|
var class_name = (beg+i < cur_y ? 'past' : (beg+i > cur_y ? 'future' : 'today'))
|
||||||
|
+ (beg+i == sel_y ? ' selected' : '');
|
||||||
|
html += "<td class='years "+class_name+"'><a href='javascript:Calendar.instance.showMonths("+(beg+i)+")'>"+(beg+i)+"</a></td>";
|
||||||
|
}
|
||||||
|
html += "</tr>";
|
||||||
|
html += "</table>";
|
||||||
|
this.setHTML(html);
|
||||||
|
}
|
||||||
|
|
||||||
//Position the div in the correct location...
|
/// Creates a calendar with the date given in the argument as the selected date.
|
||||||
var div = Calendar.box;
|
makeCalendar(year, month)
|
||||||
var xy = getOffset(input);
|
{
|
||||||
var width = input.clientWidth||input.offsetWidth;
|
// Display the table
|
||||||
var height = input.clientHeight||input.offsetHeight;
|
var next_month = month+1;
|
||||||
div.style.left = (xy.left-1)+"px";
|
var next_month_year = year;
|
||||||
div.style.top = (xy.top+height-1)+"px";
|
if (next_month >= 12) {
|
||||||
|
next_month = 0;
|
||||||
|
next_month_year++;
|
||||||
|
}
|
||||||
|
|
||||||
// Show the calendar with the date in the input as the selected date
|
var previous_month = month-1;
|
||||||
this.selected = new Date();
|
var previous_month_year = year;
|
||||||
var date_in_input = input.value.replace(/\s+.*$/, ''); //Remove time
|
if (previous_month < 0) {
|
||||||
if (date_in_input) {
|
previous_month = 11;
|
||||||
// date format is HARDCODE
|
previous_month_year--;
|
||||||
var selected_date = false;
|
}
|
||||||
var date_parts = date_in_input.split("-");
|
|
||||||
if (date_parts.length == 3) {
|
var current_year = this.today.getFullYear();
|
||||||
// Y-m-d
|
|
||||||
date_parts[1]--; //Month starts with 0
|
var html = '';
|
||||||
selected_date = new Date(date_parts[0], date_parts[1], date_parts[2]);
|
html += "<table>";
|
||||||
} else if (date_parts.length == 1) {
|
html += "<tr><th colspan='7' class='calendar-title'><a href='javascript:Calendar.instance.makeCalendar("+previous_month_year+","+previous_month+");' title='"+this.month_names[previous_month]+" "+(previous_month_year)+"' class='prev'></a>";
|
||||||
date_parts = date_in_input.split('.');
|
if (!this.selectboxes) {
|
||||||
|
html += " <a href='javascript:Calendar.instance.showMonths("+year+", "+month+")'>"+this.month_names[month]+"</a>";
|
||||||
|
html += " <a href='javascript:Calendar.instance.showYears("+year+")'>"+year+"</a>";
|
||||||
|
} else {
|
||||||
|
html += " <select name='calendar-month' class='calendar-month' onChange='Calendar.instance.makeCalendar("+year+",this.value);'>";
|
||||||
|
for (var i in this.month_names) {
|
||||||
|
html += "<option value='"+i+"'";
|
||||||
|
if (i == month) html += " selected='selected'";
|
||||||
|
html += ">"+this.month_names[i]+"</option>";
|
||||||
|
}
|
||||||
|
html += "</select>";
|
||||||
|
html += "<select name='calendar-year' class='calendar-year' onChange='Calendar.instance.makeCalendar(this.value, "+month+");'>";
|
||||||
|
for (var i = current_year+this.years.min; i < current_year+this.years.max; i++) {
|
||||||
|
html += "<option value='"+i+"'"
|
||||||
|
if (i == year) html += " selected='selected'";
|
||||||
|
html += ">"+i+"</option>";
|
||||||
|
}
|
||||||
|
html += "</select>";
|
||||||
|
}
|
||||||
|
html += " <a href='javascript:Calendar.instance.makeCalendar("+next_month_year+","+next_month+");' title='"+this.month_names[next_month]+" "+next_month_year+"' class='next'></a></th></tr>";
|
||||||
|
html += "<tr class='header'>";
|
||||||
|
for (var weekday = 0; weekday < 7; weekday++)
|
||||||
|
html += "<td>"+this.weekdays[weekday]+"</td>";
|
||||||
|
html += "</tr>";
|
||||||
|
|
||||||
|
// Get the first day of this month
|
||||||
|
var first_day = new Date(year,month,1);
|
||||||
|
var start_day = (first_day.getDay()+this.sunday)%7;
|
||||||
|
|
||||||
|
var d = 1;
|
||||||
|
var flag = 0;
|
||||||
|
|
||||||
|
// Leap year support
|
||||||
|
if (!(year % 4) && ((year % 100) || !(year % 400)))
|
||||||
|
this.month_days[1] = 29;
|
||||||
|
else
|
||||||
|
this.month_days[1] = 28;
|
||||||
|
|
||||||
|
var days_in_this_month = this.month_days[month];
|
||||||
|
|
||||||
|
// Create the calendar
|
||||||
|
var yea = this.today.getFullYear();
|
||||||
|
var all_diff = (year - yea) || (month - this.today.getMonth());
|
||||||
|
var sel_day = year == this.selected.getFullYear() && month == this.selected.getMonth() ? this.selected.getDate() : -1;
|
||||||
|
for (var i = 0; i <= 5; i++) {
|
||||||
|
if (w >= days_in_this_month)
|
||||||
|
break;
|
||||||
|
html += "<tr>";
|
||||||
|
for (var j = 0; j < 7; j++) {
|
||||||
|
if (d > days_in_this_month)
|
||||||
|
flag = 0; // If the days has overshooted the number of days in this month, stop writing
|
||||||
|
else if (j >= start_day && !flag)
|
||||||
|
flag = 1; // If the first day of this month has come, start the date writing
|
||||||
|
|
||||||
|
if (flag) {
|
||||||
|
var w = d, mon = month+1;
|
||||||
|
if (w < 10)
|
||||||
|
w = "0" + w;
|
||||||
|
if (mon < 10)
|
||||||
|
mon = "0" + mon;
|
||||||
|
|
||||||
|
// Is it today?
|
||||||
|
var class_name = '';
|
||||||
|
|
||||||
|
var diff = all_diff || (d - this.today.getDate());
|
||||||
|
if (diff < 0)
|
||||||
|
class_name = ' past';
|
||||||
|
else if (!diff)
|
||||||
|
class_name = ' today';
|
||||||
|
else
|
||||||
|
class_name = ' future';
|
||||||
|
|
||||||
|
if (d == sel_day)
|
||||||
|
class_name += ' selected';
|
||||||
|
|
||||||
|
class_name += " " + this.weekdays[j].toLowerCase();
|
||||||
|
|
||||||
|
html += "<td class='days"+class_name+"'><a href='javascript:Calendar.instance.selectDate(\""+year+"\",\""+mon+"\",\""+w+"\")'>"+d+"</a></td>";
|
||||||
|
d++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
html += "<td class='days'> </td>";
|
||||||
|
}
|
||||||
|
html += "</tr>";
|
||||||
|
}
|
||||||
|
html += "</table>";
|
||||||
|
this.setHTML(html);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Display the calendar - if a date exists in the input box, that will be selected in the calendar.
|
||||||
|
showCalendar()
|
||||||
|
{
|
||||||
|
Calendar.instance = this;
|
||||||
|
var input = this.input;
|
||||||
|
|
||||||
|
// Show the calendar with the date in the input as the selected date
|
||||||
|
this.selected = new Date();
|
||||||
|
var date_in_input = input.value.replace(/\s+.*$/, ''); //Remove time
|
||||||
|
if (date_in_input) {
|
||||||
|
// date format is HARDCODE
|
||||||
|
var selected_date = false;
|
||||||
|
var date_parts = date_in_input.split("-");
|
||||||
if (date_parts.length == 3) {
|
if (date_parts.length == 3) {
|
||||||
// d.m.Y
|
// Y-m-d
|
||||||
date_parts[1]--; //Month starts with 0
|
date_parts[1]--; //Month starts with 0
|
||||||
selected_date = new Date(date_parts[2], date_parts[1], date_parts[0]);
|
selected_date = new Date(date_parts[0], date_parts[1], date_parts[2]);
|
||||||
|
} else if (date_parts.length == 1) {
|
||||||
|
date_parts = date_in_input.split('.');
|
||||||
|
if (date_parts.length == 3) {
|
||||||
|
// d.m.Y
|
||||||
|
date_parts[1]--; //Month starts with 0
|
||||||
|
selected_date = new Date(date_parts[2], date_parts[1], date_parts[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (selected_date && !isNaN(selected_date.getFullYear())) { //Valid date.
|
||||||
|
this.selected = selected_date;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (selected_date && !isNaN(selected_date.getFullYear())) { //Valid date.
|
|
||||||
this.selected = selected_date;
|
if (this.start == 'years')
|
||||||
}
|
this.showYears(this.selected.getFullYear());
|
||||||
|
else if (this.start == 'months')
|
||||||
|
this.showMonths(this.selected.getFullYear());
|
||||||
|
else
|
||||||
|
this.makeCalendar(this.selected.getFullYear(), this.selected.getMonth());
|
||||||
|
|
||||||
|
// Position the div in the correct location...
|
||||||
|
var div = Calendar.box;
|
||||||
|
var xy = getOffset(input);
|
||||||
|
var height = input.clientHeight||input.offsetHeight;
|
||||||
|
div.style.display = "block";
|
||||||
|
div.style.left = (xy.left-1)+"px";
|
||||||
|
if (div.offsetHeight + xy.top+height-1 >= (document.documentElement.clientHeight||document.body.clientHeight) &&
|
||||||
|
xy.top-div.offsetHeight >= 0)
|
||||||
|
div.style.top = (xy.top-div.offsetHeight)+'px';
|
||||||
|
else
|
||||||
|
div.style.top = (xy.top+height-1)+"px";
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Hides the currently show calendar.
|
||||||
|
hideCalendar()
|
||||||
|
{
|
||||||
|
Calendar.box.style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
this.makeCalendar(this.selected.getFullYear(), this.selected.getMonth());
|
/// Setup a text input box to be a calendar box.
|
||||||
Calendar.box.style.display = "block";
|
static set(input_or_id, options)
|
||||||
};
|
{
|
||||||
|
if (typeof input_or_id == 'string')
|
||||||
/// Hides the currently show calendar.
|
input_or_id = document.getElementById(input_or_id);
|
||||||
Calendar.hideCalendar = function()
|
if (!input_or_id)
|
||||||
{
|
return; // If the input field is not there, exit.
|
||||||
Calendar.box.style.display = "none";
|
options = options||{};
|
||||||
};
|
var instance = new Calendar();
|
||||||
|
for (var i in Calendar.defaultOptions)
|
||||||
/// Setup a text input box to be a calendar box.
|
instance[i] = options[i] || Calendar.defaultOptions[i];
|
||||||
Calendar.set = function(input_or_id, options)
|
instance.input = input_or_id;
|
||||||
{
|
Calendar.init();
|
||||||
if (typeof input_or_id == 'string')
|
input_or_id.addEventListener('focus', function(ev) {
|
||||||
input_or_id = document.getElementById(input_or_id);
|
instance.showCalendar();
|
||||||
if (!input_or_id)
|
|
||||||
return; // If the input field is not there, exit.
|
|
||||||
options = options||{};
|
|
||||||
var instance = new Calendar();
|
|
||||||
for (var i in Calendar.defaultOptions)
|
|
||||||
instance[i] = options[i] || Calendar.defaultOptions[i];
|
|
||||||
instance.input = input_or_id;
|
|
||||||
Calendar.init();
|
|
||||||
addListener(input_or_id, 'focus', function(ev) {
|
|
||||||
instance.showCalendar();
|
|
||||||
});
|
|
||||||
return instance;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Will be called once when the first input is set.
|
|
||||||
Calendar.init = function()
|
|
||||||
{
|
|
||||||
if (!Calendar.box || !Calendar.box.parentNode) {
|
|
||||||
var div = document.createElement('div');
|
|
||||||
if (!Calendar.box)
|
|
||||||
Calendar.box = div;
|
|
||||||
div.className = "calendar-box";
|
|
||||||
addListener(div, "mousedown", function(ev) {
|
|
||||||
ev = ev || window.event;
|
|
||||||
if (ev.stopPropagation)
|
|
||||||
ev.stopPropagation();
|
|
||||||
else
|
|
||||||
ev.cancelBubble = true;
|
|
||||||
return true;
|
|
||||||
});
|
});
|
||||||
document.getElementsByTagName("body")[0].insertBefore(div,document.getElementsByTagName("body")[0].firstChild);
|
return instance;
|
||||||
if (!Calendar.addedListener) {
|
}
|
||||||
addListener(document, "mousedown", function() { Calendar.hideCalendar(); });
|
|
||||||
Calendar.addedListener = true;
|
/// Will be called once when the first input is set.
|
||||||
|
static init()
|
||||||
|
{
|
||||||
|
if (!Calendar.box || !Calendar.box.parentNode) {
|
||||||
|
var div = document.createElement('div');
|
||||||
|
if (!Calendar.box)
|
||||||
|
Calendar.box = div;
|
||||||
|
div.className = "calendar-box";
|
||||||
|
div.addEventListener("mousedown", function(ev) {
|
||||||
|
ev = ev || window.event;
|
||||||
|
if (ev.stopPropagation)
|
||||||
|
ev.stopPropagation();
|
||||||
|
else
|
||||||
|
ev.cancelBubble = true;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
document.getElementsByTagName("body")[0].insertBefore(div,document.getElementsByTagName("body")[0].firstChild);
|
||||||
|
if (!Calendar.addedListener) {
|
||||||
|
document.addEventListener("mousedown", function() { Calendar.instance && Calendar.instance.hideCalendar(); });
|
||||||
|
Calendar.addedListener = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
window.Calendar = Calendar;
|
||||||
|
|
||||||
|
function getOffsetRect(elem)
|
||||||
|
{
|
||||||
|
var box = elem.getBoundingClientRect();
|
||||||
|
|
||||||
|
var body = document.body;
|
||||||
|
var docElem = document.documentElement;
|
||||||
|
|
||||||
|
var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
|
||||||
|
var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;
|
||||||
|
var clientTop = docElem.clientTop || body.clientTop || 0;
|
||||||
|
var clientLeft = docElem.clientLeft || body.clientLeft || 0;
|
||||||
|
var top = box.top + scrollTop - clientTop;
|
||||||
|
var left = box.left + scrollLeft - clientLeft;
|
||||||
|
|
||||||
|
return { top: Math.round(top), left: Math.round(left) };
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOffsetSum(elem)
|
||||||
|
{
|
||||||
|
var top = 0, left = 0;
|
||||||
|
while(elem)
|
||||||
|
{
|
||||||
|
top = top + parseInt(elem.offsetTop);
|
||||||
|
left = left + parseInt(elem.offsetLeft);
|
||||||
|
elem = elem.offsetParent;
|
||||||
|
}
|
||||||
|
return { top: top, left: left };
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOffset(elem)
|
||||||
|
{
|
||||||
|
if (elem.getBoundingClientRect)
|
||||||
|
return getOffsetRect(elem);
|
||||||
|
else
|
||||||
|
return getOffsetSum(elem);
|
||||||
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,47 +0,0 @@
|
||||||
(function() {
|
|
||||||
|
|
||||||
// UTILITY FUNCTIONS
|
|
||||||
window.addListener = (function() {
|
|
||||||
return window.addEventListener
|
|
||||||
? function(el, type, fn) { el.addEventListener(type, fn, false); }
|
|
||||||
: function(el, type, fn) { el.attachEvent('on'+type, fn); };
|
|
||||||
})();
|
|
||||||
|
|
||||||
var getOffsetRect = function(elem)
|
|
||||||
{
|
|
||||||
var box = elem.getBoundingClientRect();
|
|
||||||
|
|
||||||
var body = document.body;
|
|
||||||
var docElem = document.documentElement;
|
|
||||||
|
|
||||||
var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
|
|
||||||
var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;
|
|
||||||
var clientTop = docElem.clientTop || body.clientTop || 0;
|
|
||||||
var clientLeft = docElem.clientLeft || body.clientLeft || 0;
|
|
||||||
var top = box.top + scrollTop - clientTop;
|
|
||||||
var left = box.left + scrollLeft - clientLeft;
|
|
||||||
|
|
||||||
return { top: Math.round(top), left: Math.round(left) };
|
|
||||||
};
|
|
||||||
|
|
||||||
var getOffsetSum = function(elem)
|
|
||||||
{
|
|
||||||
var top = 0, left = 0;
|
|
||||||
while(elem)
|
|
||||||
{
|
|
||||||
top = top + parseInt(elem.offsetTop);
|
|
||||||
left = left + parseInt(elem.offsetLeft);
|
|
||||||
elem = elem.offsetParent;
|
|
||||||
}
|
|
||||||
return { top: top, left: left };
|
|
||||||
};
|
|
||||||
|
|
||||||
window.getOffset = function(elem)
|
|
||||||
{
|
|
||||||
if (elem.getBoundingClientRect)
|
|
||||||
return getOffsetRect(elem);
|
|
||||||
else
|
|
||||||
return getOffsetSum(elem);
|
|
||||||
};
|
|
||||||
|
|
||||||
})();
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
{
|
||||||
|
"name": "calendarjs",
|
||||||
|
"author": {
|
||||||
|
"name": "Vitaliy Filippov",
|
||||||
|
"email": "vitalif@yourcmc.ru",
|
||||||
|
"url": "http://yourcmc.ru/wiki/"
|
||||||
|
},
|
||||||
|
"description": "Yet another date picker",
|
||||||
|
"dependencies": {},
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-cli": "^6.26.0",
|
||||||
|
"babel-core": "^6.26.0",
|
||||||
|
"babel-preset-env": "^1.6.1",
|
||||||
|
"babel-preset-react": "^6.24.1",
|
||||||
|
"babel-preset-stage-1": "^6.24.1",
|
||||||
|
"uglify-js": "^3.3.15"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"compile": "node_modules/.bin/babel calendar.js | node_modules/.bin/uglifyjs > calendar.min.js"
|
||||||
|
}
|
||||||
|
}
|
3
test.htm
3
test.htm
|
@ -1,8 +1,7 @@
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||||
<link rel="stylesheet" href="calendar-material.css">
|
<link rel="stylesheet" href="calendar-material.css">
|
||||||
<script src="calendar.util.js"></script>
|
<script src="calendar.min.js"></script>
|
||||||
<script src="calendar.js"></script>
|
|
||||||
|
|
||||||
<input id="x" style="margin: 50px">
|
<input id="x" style="margin: 50px">
|
||||||
<script>
|
<script>
|
||||||
|
|
Loading…
Reference in New Issue