101 lines
3.4 KiB
JavaScript
101 lines
3.4 KiB
JavaScript
/* Lightweight dd/mm/yyyy datepicker compatible with $(selector).datepicker(opts) */
|
|
(function($) {
|
|
if (!$ || !$.fn) return;
|
|
|
|
function pad(n) { return n < 10 ? "0" + n : "" + n; }
|
|
|
|
$.fn.datepicker = function(opts) {
|
|
opts = opts || {};
|
|
this.els.forEach(function(input) {
|
|
var popup = null;
|
|
|
|
function closePopup() {
|
|
if (popup && popup.parentNode) {
|
|
popup.parentNode.removeChild(popup);
|
|
popup = null;
|
|
}
|
|
}
|
|
|
|
function renderCalendar(year, month) {
|
|
closePopup();
|
|
popup = document.createElement("div");
|
|
popup.className = "dmh-datepicker-popup";
|
|
var rect = input.getBoundingClientRect();
|
|
popup.style.top = (window.scrollY + rect.bottom + 2) + "px";
|
|
popup.style.left = (window.scrollX + rect.left) + "px";
|
|
|
|
var header = document.createElement("div");
|
|
header.className = "dmh-dp-header";
|
|
|
|
var monthSel = document.createElement("select");
|
|
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
|
|
months.forEach(function(m, idx) {
|
|
var o = document.createElement("option");
|
|
o.value = idx;
|
|
o.textContent = m;
|
|
if (idx === month) o.selected = true;
|
|
monthSel.appendChild(o);
|
|
});
|
|
|
|
var yearSel = document.createElement("select");
|
|
var curYear = new Date().getFullYear();
|
|
for (var y = curYear; y >= curYear - 100; y--) {
|
|
var oy = document.createElement("option");
|
|
oy.value = y;
|
|
oy.textContent = y;
|
|
if (y === year) oy.selected = true;
|
|
yearSel.appendChild(oy);
|
|
}
|
|
|
|
monthSel.onchange = function() { renderCalendar(parseInt(yearSel.value, 10), parseInt(monthSel.value, 10)); };
|
|
yearSel.onchange = function() { renderCalendar(parseInt(yearSel.value, 10), parseInt(monthSel.value, 10)); };
|
|
header.appendChild(monthSel);
|
|
header.appendChild(yearSel);
|
|
popup.appendChild(header);
|
|
|
|
var grid = document.createElement("div");
|
|
grid.className = "dmh-dp-grid";
|
|
var daysInMonth = new Date(year, month + 1, 0).getDate();
|
|
var today = new Date();
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
for (var d = 1; d <= daysInMonth; d++) {
|
|
(function(dayNum) {
|
|
var btn = document.createElement("button");
|
|
btn.type = "button";
|
|
btn.textContent = dayNum;
|
|
var candidate = new Date(year, month, dayNum);
|
|
if (opts.maxDate === "0" && candidate > today) {
|
|
btn.disabled = true;
|
|
}
|
|
btn.onclick = function() {
|
|
var formatted = pad(dayNum) + "/" + pad(month + 1) + "/" + year;
|
|
input.value = formatted;
|
|
closePopup();
|
|
if (typeof opts.onSelect === "function") {
|
|
opts.onSelect(formatted);
|
|
}
|
|
};
|
|
grid.appendChild(btn);
|
|
})(d);
|
|
}
|
|
popup.appendChild(grid);
|
|
document.body.appendChild(popup);
|
|
}
|
|
|
|
input.addEventListener("click", function() {
|
|
if (input.readOnly) return;
|
|
var now = new Date();
|
|
renderCalendar(now.getFullYear(), now.getMonth());
|
|
});
|
|
|
|
document.addEventListener("click", function(ev) {
|
|
if (popup && ev.target !== input && !popup.contains(ev.target)) {
|
|
closePopup();
|
|
}
|
|
});
|
|
});
|
|
return this;
|
|
};
|
|
})(window.$);
|