Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated a year picker #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion source/jsCalendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

/* Default Theme */
/* General style */
.jsCalendar {
position: relative;
}
.jsCalendar * {
margin: 0;
padding: 0;
Expand Down Expand Up @@ -89,12 +92,15 @@
float: right;
}
.jsCalendar thead .jsCalendar-title-name {
cursor: default;
cursor: pointer;
float: left;
font-size: 18px;
font-weight: lighter;
padding: 0 20px;
}
.jsCalendar thead .jsCalendar-title-name:hover {
background-color: #F0F0F0;
}
.jsCalendar thead .jsCalendar-nav-left:after {
content: "<";
}
Expand Down Expand Up @@ -397,3 +403,27 @@
.jsCalendar.classic-theme.green thead .jsCalendar-nav-right:hover {
background-color: #639000;
}

.jsCalendar table.year-picker {
display: none;
z-index: -1;
}

.jsCalendar table.year-picker.active {
display: block;
z-index: 1;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #FFFFFF;
}

.jsCalendar table.year-picker.active tbody td {
width: 22%;
}

.jsCalendar table.year-picker tbody tr {
display: block;
}
64 changes: 64 additions & 0 deletions source/jsCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ var jsCalendar = (function(){
date.setDate(1);
that._eventFire('month', date, event);
}, false);

this._target.querySelector('.'+ this._elements.month.className)
.addEventListener('click', function(event) {
that.yearPicker.className += ' active';
});
}

// Days row
Expand All @@ -639,6 +644,8 @@ var jsCalendar = (function(){
]);
}

this._createYearsPicker();

// Body rows
this._elements.bodyRows = [];
this._elements.bodyCols = [];
Expand All @@ -662,6 +669,57 @@ var jsCalendar = (function(){
this.extensionsCall('create', [this._elements]);
};

JsCalendar.prototype._createYearsPicker = function() {
var that = this;

// If it doesn't exist, create it.
if (!this.yearPicker) {
this.yearPicker = document.createElement( 'table' );
this.yearPicker.className = 'year-picker';

this._target.append(this.yearPicker);
}

// Clean up its content.
while(this.yearPicker.firstChild)
this.yearPicker.removeChild(this.yearPicker.firstChild);

var yearPickerBody = document.createElement('tbody');

var selectableYears = [];
for (var offset = 15; offset > 0; --offset)
selectableYears.push(this._date.getFullYear() - offset);
for (var offset = 0; offset < 15; ++offset)
selectableYears.push(this._date.getFullYear() + offset);

for (var row = 0, insertedYears = 0; row < 7; ++row) {
var tr = document.createElement('tr');

for (var column = 0; column < 4; ++column) {
var td = document.createElement('td');
td.innerText = selectableYears[++insertedYears];

if (td.innerText.trim() == this._date.getFullYear())
td.className = 'jsCalendar-current';

td.addEventListener('click', function(event) {
var date = new Date(that._date.getTime());
date.setFullYear(event.currentTarget.innerText);
that.yearPicker.className = that.yearPicker.className.replace('active', '');
that.set(date);

that._eventFire('month', date, event);
});

tr.append(td);
}

yearPickerBody.append(tr);
}

this.yearPicker.append(yearPickerBody);
}

// Select dates on calendar
JsCalendar.prototype._selectDates = function(dates) {
// Copy array instance
Expand Down Expand Up @@ -842,6 +900,9 @@ var jsCalendar = (function(){
}
}
}

this._createYearsPicker();

// Extensions call update
this.extensionsCall('update', [month]);
};
Expand Down Expand Up @@ -887,6 +948,9 @@ var jsCalendar = (function(){
JsCalendar.prototype.onMonthChange = function(callback) {
return this._on('month', callback);
};
JsCalendar.prototype.onYearChange = function(callback) {
return this._on('year', callback);
};
JsCalendar.prototype.onDayRender = function(callback) {
return this._on('day_render', callback);
};
Expand Down