Slightly more refactoring (allow options), add select styles to material css
parent
e6497a0712
commit
3f3525efd3
|
@ -9,6 +9,34 @@
|
|||
font-family: Roboto, sans-serif;
|
||||
box-shadow: 0 19px 60px rgba(0, 0, 0, .3), 0 15px 20px rgba(0, 0, 0, .22)
|
||||
}
|
||||
.calendar-box select {
|
||||
height: 36px;
|
||||
line-height: 36px;
|
||||
font-family: Roboto, sans-serif;
|
||||
font-size: 15px;
|
||||
text-transform: uppercase;
|
||||
font-weight: 500;
|
||||
-webkit-appearance: none;
|
||||
-moz-appearance: none;
|
||||
appearance: none;
|
||||
-webkit-outline: 0;
|
||||
-moz-outline: 0;
|
||||
outline: 0;
|
||||
padding: 0;
|
||||
color: white;
|
||||
background: transparent;
|
||||
border: 0;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
transition: background 0.35s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
||||
}
|
||||
.calendar-box select:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
.calendar-box select:-moz-focusring {
|
||||
color: transparent;
|
||||
text-shadow: 0 0 0 white;
|
||||
}
|
||||
.calendar-box select.calendar-month {
|
||||
width: 90px;
|
||||
margin-right: 3px;
|
||||
|
@ -113,7 +141,6 @@
|
|||
color: white;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a {
|
||||
display: inline-block;
|
||||
text-decoration: none;
|
||||
|
@ -123,33 +150,27 @@
|
|||
padding: 0 12px;
|
||||
transition: background 0.35s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a:hover {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a.prev {
|
||||
position: absolute;
|
||||
left: 10px;
|
||||
padding: 0;
|
||||
width: 36px;
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a.next {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
padding: 0;
|
||||
width: 36px;
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a.prev:after {
|
||||
content: "chevron_left";
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a.next:after {
|
||||
content: "chevron_right";
|
||||
}
|
||||
|
||||
.calendar-box .calendar-title a.prev:after,
|
||||
.calendar-box .calendar-title a.next:after {
|
||||
font-family: 'Material Icons';
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* Default CSS for calendar.js, version: 2015-05-03 */
|
||||
/* Default CSS for calendar.js, version: 2018-03-14 */
|
||||
.calendar-box {
|
||||
display: none;
|
||||
background: #fff;
|
||||
|
|
215
calendar.js
215
calendar.js
|
@ -3,9 +3,8 @@
|
|||
* Creates a calendar widget which can be used to select the date more easily than using just a text box
|
||||
*
|
||||
* Original: http://www.openjs.com/scripts/ui/calendar/
|
||||
* Modified: http://svn.yourcmc.ru/viewvc.py/vitaphoto/js/{util.js,calendar.js,calendar.css}
|
||||
* (uses addListener() and getOffset() from util.js)
|
||||
* Version: 2017-05-03
|
||||
* Modified: http://yourcmc.ru/git/vitalif-js/calendar
|
||||
* Version: 2018-03-14
|
||||
* License: MIT-like, http://www.openjs.com/license.php
|
||||
*
|
||||
* Example:
|
||||
|
@ -14,9 +13,12 @@
|
|||
* Calendar.set("date");
|
||||
* </script>
|
||||
*/
|
||||
if (!window.Calendar) {
|
||||
window.Calendar = {
|
||||
// Configuration:
|
||||
function Calendar()
|
||||
{
|
||||
}
|
||||
|
||||
// Configuration
|
||||
Calendar.defaultOptions = {
|
||||
month_names: ["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"],
|
||||
close_label: 'Закрыть',
|
||||
weekdays: ["Пн","Вт","Ср","Чт","Пт","Сб","Вс"],
|
||||
|
@ -24,85 +26,85 @@ window.Calendar = {
|
|||
selectboxes: false, // true: use selectboxes for year and month, false: show months and years in table
|
||||
years: {min: -70, max: 10}, // range of displayed years if selectboxes==true
|
||||
format: 'd.m.Y', // either d.m.Y or Y-m-d, other formats are not supported
|
||||
|
||||
// State variables:
|
||||
month_days: [31,28,31,30,31,30,31,31,30,31,30,31],
|
||||
// Today's date
|
||||
today: new Date(),
|
||||
// Selected date
|
||||
selected: new Date(),
|
||||
opt: {},
|
||||
data: [],
|
||||
addedListener: false,
|
||||
};
|
||||
|
||||
//Functions
|
||||
/// Used to create HTML in a optimized way.
|
||||
wrt:function(txt) {
|
||||
this.data.push(txt);
|
||||
},
|
||||
fin:function() {
|
||||
this.wrt("<a class='calendar-cancel' onclick='Calendar.hideCalendar();'>"+this.close_label+"</a>");
|
||||
document.getElementById(this.opt['calendar']).innerHTML = this.data.join("");
|
||||
this.data = [];
|
||||
},
|
||||
// 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.
|
||||
selectDate:function(year,month,day) {
|
||||
var i = this.opt["input"];
|
||||
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;
|
||||
this.hideCalendar();
|
||||
},
|
||||
Calendar.hideCalendar();
|
||||
};
|
||||
|
||||
showMonths:function(year, month) {
|
||||
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;
|
||||
this.wrt("<table>");
|
||||
this.wrt("<tr><th colspan='4' class='calendar-title'><a href='javascript:Calendar.showMonths("+(year-1)+")' title='"+(year-1)+"' class='prev'></a>");
|
||||
this.wrt(" <a href='javascript:Calendar.showYears("+year+")'>"+year+"</a>");
|
||||
this.wrt(" <a href='javascript:Calendar.showMonths("+(year+1)+")' title='"+(year+1)+"' class='next'></a></th></tr>");
|
||||
this.wrt("<tr>");
|
||||
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))
|
||||
this.wrt("</tr><tr>");
|
||||
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' : '');
|
||||
this.wrt("<td class='months "+class_name+"'><a href='javascript:Calendar.makeCalendar("+year+","+i+")'>"+this.month_names[i]+"</a></td>");
|
||||
html += "<td class='months "+class_name+"'><a href='javascript:Calendar.instance.makeCalendar("+year+","+i+")'>"+this.month_names[i]+"</a></td>";
|
||||
}
|
||||
this.wrt("</tr>");
|
||||
this.wrt("</table>");
|
||||
this.fin();
|
||||
},
|
||||
html += "</tr>";
|
||||
html += "</table>";
|
||||
this.setHTML(html);
|
||||
};
|
||||
|
||||
showYears:function(year) {
|
||||
Calendar.prototype.showYears = function(year)
|
||||
{
|
||||
var beg = year & ~15;
|
||||
var cur_y = this.today.getFullYear();
|
||||
var sel_y = this.selected.getFullYear();
|
||||
this.wrt("<table>");
|
||||
this.wrt("<tr><th colspan='4' class='calendar-title'>");
|
||||
this.wrt(" <a href='javascript:Calendar.showYears("+(year-16)+");' title='"+(beg-16)+" - "+(beg-1)+"' class='prev'></a>");
|
||||
this.wrt(" <b>"+beg+" - "+(beg+15)+"</b>");
|
||||
this.wrt(" <a href='javascript:Calendar.showYears("+(year+16)+");' title='"+(beg+16)+" - "+(beg+31)+"' class='next'></a>");
|
||||
this.wrt("</th></tr>");
|
||||
this.wrt("<tr>");
|
||||
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))
|
||||
this.wrt("</tr><tr>");
|
||||
html += "</tr><tr>";
|
||||
var class_name = (beg+i < cur_y ? 'past' : (beg+i > cur_y ? 'future' : 'today'))
|
||||
+ (beg+i == sel_y ? ' selected' : '');
|
||||
this.wrt("<td class='years "+class_name+"'><a href='javascript:Calendar.showMonths("+(beg+i)+")'>"+(beg+i)+"</a></td>");
|
||||
html += "<td class='years "+class_name+"'><a href='javascript:Calendar.instance.showMonths("+(beg+i)+")'>"+(beg+i)+"</a></td>";
|
||||
}
|
||||
this.wrt("</tr>");
|
||||
this.wrt("</table>");
|
||||
this.fin();
|
||||
},
|
||||
html += "</tr>";
|
||||
html += "</table>";
|
||||
this.setHTML(html);
|
||||
};
|
||||
|
||||
/// Creates a calendar with the date given in the argument as the selected date.
|
||||
makeCalendar:function(year, month) {
|
||||
Calendar.prototype.makeCalendar = function(year, month)
|
||||
{
|
||||
// Display the table
|
||||
var next_month = month+1;
|
||||
var next_month_year = year;
|
||||
|
@ -120,36 +122,37 @@ window.Calendar = {
|
|||
|
||||
var current_year = this.today.getFullYear();
|
||||
|
||||
this.wrt("<table>");
|
||||
this.wrt("<tr><th colspan='7' class='calendar-title'><a href='javascript:Calendar.makeCalendar("+previous_month_year+","+previous_month+");' title='"+this.month_names[previous_month]+" "+(previous_month_year)+"' class='prev'></a>");
|
||||
var html = '';
|
||||
html += "<table>";
|
||||
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) {
|
||||
this.wrt(" <a href='javascript:Calendar.showMonths("+year+", "+month+")'>"+this.month_names[month]+"</a>");
|
||||
this.wrt(" <a href='javascript:Calendar.showYears("+year+")'>"+year+"</a>");
|
||||
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 {
|
||||
this.wrt(" <select name='calendar-month' class='calendar-month' onChange='Calendar.makeCalendar("+year+",this.value);'>");
|
||||
html += " <select name='calendar-month' class='calendar-month' onChange='Calendar.instance.makeCalendar("+year+",this.value);'>";
|
||||
for (var i in this.month_names) {
|
||||
this.wrt("<option value='"+i+"'");
|
||||
if (i == month) this.wrt(" selected='selected'");
|
||||
this.wrt(">"+this.month_names[i]+"</option>");
|
||||
html += "<option value='"+i+"'";
|
||||
if (i == month) html += " selected='selected'";
|
||||
html += ">"+this.month_names[i]+"</option>";
|
||||
}
|
||||
this.wrt("</select>");
|
||||
this.wrt("<select name='calendar-year' class='calendar-year' onChange='Calendar.makeCalendar(this.value, "+month+");'>");
|
||||
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++) {
|
||||
this.wrt("<option value='"+i+"'")
|
||||
if (i == year) this.wrt(" selected='selected'");
|
||||
this.wrt(">"+i+"</option>");
|
||||
html += "<option value='"+i+"'"
|
||||
if (i == year) html += " selected='selected'";
|
||||
html += ">"+i+"</option>";
|
||||
}
|
||||
this.wrt("</select>");
|
||||
html += "</select>";
|
||||
}
|
||||
this.wrt(" <a href='javascript:Calendar.makeCalendar("+next_month_year+","+next_month+");' title='"+this.month_names[next_month]+" "+next_month_year+"' class='next'></a></th></tr>");
|
||||
this.wrt("<tr class='header'>");
|
||||
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++)
|
||||
this.wrt("<td>"+this.weekdays[weekday]+"</td>");
|
||||
this.wrt("</tr>");
|
||||
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()+Calendar.sunday)%7;
|
||||
var start_day = (first_day.getDay()+this.sunday)%7;
|
||||
|
||||
var d = 1;
|
||||
var flag = 0;
|
||||
|
@ -169,7 +172,7 @@ window.Calendar = {
|
|||
for (var i = 0; i <= 5; i++) {
|
||||
if (w >= days_in_this_month)
|
||||
break;
|
||||
this.wrt("<tr>");
|
||||
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
|
||||
|
@ -199,24 +202,26 @@ window.Calendar = {
|
|||
|
||||
class_name += " " + this.weekdays[j].toLowerCase();
|
||||
|
||||
this.wrt("<td class='days"+class_name+"'><a href='javascript:Calendar.selectDate(\""+year+"\",\""+mon+"\",\""+w+"\")'>"+d+"</a></td>");
|
||||
html += "<td class='days"+class_name+"'><a href='javascript:Calendar.instance.selectDate(\""+year+"\",\""+mon+"\",\""+w+"\")'>"+d+"</a></td>";
|
||||
d++;
|
||||
}
|
||||
else
|
||||
this.wrt("<td class='days'> </td>");
|
||||
html += "<td class='days'> </td>";
|
||||
}
|
||||
this.wrt("</tr>");
|
||||
html += "</tr>";
|
||||
}
|
||||
this.wrt("</table>");
|
||||
this.fin();
|
||||
},
|
||||
html += "</table>";
|
||||
this.setHTML(html);
|
||||
};
|
||||
|
||||
/// Display the calendar - if a date exists in the input box, that will be selected in the calendar.
|
||||
showCalendar: function() {
|
||||
var input = this.opt['input'];
|
||||
Calendar.prototype.showCalendar = function()
|
||||
{
|
||||
Calendar.instance = this;
|
||||
var input = this.input;
|
||||
|
||||
//Position the div in the correct location...
|
||||
var div = document.getElementById(this.opt['calendar']);
|
||||
var div = Calendar.box;
|
||||
var xy = getOffset(input);
|
||||
var width = input.clientWidth||input.offsetWidth;
|
||||
var height = input.clientHeight||input.offsetHeight;
|
||||
|
@ -248,38 +253,41 @@ window.Calendar = {
|
|||
}
|
||||
|
||||
this.makeCalendar(this.selected.getFullYear(), this.selected.getMonth());
|
||||
document.getElementById(this.opt['calendar']).style.display = "block";
|
||||
},
|
||||
Calendar.box.style.display = "block";
|
||||
};
|
||||
|
||||
/// Hides the currently show calendar.
|
||||
hideCalendar: function() {
|
||||
document.getElementById(this.opt['calendar']).style.display = "none";
|
||||
},
|
||||
Calendar.hideCalendar = function()
|
||||
{
|
||||
Calendar.box.style.display = "none";
|
||||
};
|
||||
|
||||
/// Setup a text input box to be a calendar box.
|
||||
set: function(input_or_id) {
|
||||
Calendar.set = function(input_or_id, options)
|
||||
{
|
||||
if (typeof input_or_id == 'string')
|
||||
input_or_id = document.getElementById(input_or_id);
|
||||
if (!input_or_id)
|
||||
return; // If the input field is not there, exit.
|
||||
|
||||
if (!Calendar.opt['calendar'] || !document.getElementById(Calendar.opt['calendar']))
|
||||
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) {
|
||||
Calendar.opt['input'] = this;
|
||||
Calendar.showCalendar();
|
||||
instance.showCalendar();
|
||||
});
|
||||
},
|
||||
return instance;
|
||||
};
|
||||
|
||||
/// Will be called once when the first input is set.
|
||||
init: function() {
|
||||
if(!this.opt['calendar'] || !document.getElementById(this.opt['calendar'])) {
|
||||
Calendar.init = function()
|
||||
{
|
||||
if (!Calendar.box || !Calendar.box.parentNode) {
|
||||
var div = document.createElement('div');
|
||||
if(!this.opt['calendar'])
|
||||
this.opt['calendar'] = 'calender_div_'+ Math.round(Math.random() * 100);
|
||||
|
||||
div.setAttribute('id',this.opt['calendar']);
|
||||
if (!Calendar.box)
|
||||
Calendar.box = div;
|
||||
div.className = "calendar-box";
|
||||
addListener(div, "mousedown", function(ev) {
|
||||
ev = ev || window.event;
|
||||
|
@ -290,12 +298,9 @@ window.Calendar = {
|
|||
return true;
|
||||
});
|
||||
document.getElementsByTagName("body")[0].insertBefore(div,document.getElementsByTagName("body")[0].firstChild);
|
||||
|
||||
if (!Calendar.addedListener) {
|
||||
addListener(document, "mousedown", function() { Calendar.hideCalendar(); });
|
||||
Calendar.addedListener = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue