calendar_date_select.js
来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 452 行 · 第 1/2 页
JS
452 行
buttons_div.build("span", {innerHTML:":", className: "seperator"});
var that = this;
this.minute_select = new SelectBox(buttons_div,
blank_time.concat($R(0,59).select(function(x){return (x % that.options.get('minute_interval')==0)}).map(function(x){ return $A([ Date.padded2(x), x]); } ) ),
{
calendar_date_select: this,
onchange: function() { this.calendar_date_select.updateSelectedDate( {minute: this.value }) },
className: "minute"
}
);
} else if (! this.options.get("buttons")) buttons_div.remove();
if (this.options.get("buttons")) {
buttons_div.build("span", {innerHTML: " "});
if (this.options.get("time")=="mixed" || !this.options.get("time")) b = buttons_div.build("a", {
innerHTML: _translations["Today"],
href: "#",
onclick: function() {this.today(false); return false;}.bindAsEventListener(this)
});
if (this.options.get("time")=="mixed") buttons_div.build("span", {innerHTML: " | ", className:"button_seperator"})
if (this.options.get("time")) b = buttons_div.build("a", {
innerHTML: _translations["Now"],
href: "#",
onclick: function() {this.today(true); return false}.bindAsEventListener(this)
});
if (!this.options.get("embedded"))
{
buttons_div.build("span", {innerHTML: " "});
buttons_div.build("a", { innerHTML: _translations["OK"], href: "#", onclick: function() {this.close(); return false;}.bindAsEventListener(this) });
}
}
},
refresh: function ()
{
this.refreshMonthYear();
this.refreshCalendarGrid();
this.setSelectedClass();
this.updateFooter();
},
refreshCalendarGrid: function () {
this.beginning_date = new Date(this.date).stripTime();
this.beginning_date.setDate(1);
this.beginning_date.setHours(12); // Prevent daylight savings time boundaries from showing a duplicate day
var pre_days = this.beginning_date.getDay() // draw some days before the fact
if (pre_days < 3) pre_days += 7;
this.beginning_date.setDate(1 - pre_days + Date.first_day_of_week);
var iterator = new Date(this.beginning_date);
var today = new Date().stripTime();
var this_month = this.date.getMonth();
vdc = this.options.get("valid_date_check");
for (var cell_index = 0;cell_index<42; cell_index++)
{
day = iterator.getDate(); month = iterator.getMonth();
cell = this.calendar_day_grid[cell_index];
Element.remove(cell.childNodes[0]); div = cell.build("div", {innerHTML:day});
if (month!=this_month) div.className = "other";
cell.day = day; cell.month = month; cell.year = iterator.getFullYear();
if (vdc) { if (vdc(iterator.stripTime())) cell.removeClassName("disabled"); else cell.addClassName("disabled") };
iterator.setDate( day + 1);
}
if (this.today_cell) this.today_cell.removeClassName("today");
if ( $R( 0, 41 ).include(days_until = this.beginning_date.stripTime().daysDistance(today)) ) {
this.today_cell = this.calendar_day_grid[days_until];
this.today_cell.addClassName("today");
}
},
refreshMonthYear: function() {
var m = this.date.getMonth();
var y = this.date.getFullYear();
// set the month
if (this.options.get("month_year") == "dropdowns")
{
this.month_select.setValue(m, false);
var e = this.year_select.element;
if (this.flexibleYearRange() && (!(this.year_select.setValue(y, false)) || e.selectedIndex <= 1 || e.selectedIndex >= e.options.length - 2 )) this.populateYearRange();
this.year_select.setValue(y);
} else {
this.month_year_label.update( Date.months[m] + " " + y.toString() );
}
},
populateYearRange: function() {
this.year_select.populate(this.yearRange().toArray());
},
yearRange: function() {
if (!this.flexibleYearRange())
return $R(this.options.get("year_range")[0], this.options.get("year_range")[1]);
var y = this.date.getFullYear();
return $R(y - this.options.get("year_range"), y + this.options.get("year_range"));
},
flexibleYearRange: function() { return (typeof(this.options.get("year_range")) == "number"); },
validYear: function(year) { if (this.flexibleYearRange()) { return true;} else { return this.yearRange().include(year);} },
dayHover: function(element) {
var hover_date = new Date(this.selected_date);
hover_date.setYear(element.year); hover_date.setMonth(element.month); hover_date.setDate(element.day);
this.updateFooter(hover_date.toFormattedString(this.use_time));
},
dayHoverOut: function(element) { this.updateFooter(); },
clearSelectedClass: function() {if (this.selected_cell) this.selected_cell.removeClassName("selected");},
setSelectedClass: function() {
if (!this.selection_made) return;
this.clearSelectedClass()
if ($R(0,42).include( days_until = this.beginning_date.stripTime().daysDistance(this.selected_date.stripTime()) )) {
this.selected_cell = this.calendar_day_grid[days_until];
this.selected_cell.addClassName("selected");
}
},
reparse: function() { this.parseDate(); this.refresh(); },
dateString: function() {
return (this.selection_made) ? this.selected_date.toFormattedString(this.use_time) : " ";
},
parseDate: function()
{
var value = $F(this.target_element).strip()
this.selection_made = (value != "");
this.date = value=="" ? NaN : Date.parseFormattedString(this.options.get("date") || value);
if (isNaN(this.date)) this.date = new Date();
if (!this.validYear(this.date.getFullYear())) this.date.setYear( (this.date.getFullYear() < this.yearRange().start) ? this.yearRange().start : this.yearRange().end);
this.selected_date = new Date(this.date);
this.use_time = /[0-9]:[0-9]{2}/.exec(value) ? true : false;
this.date.setDate(1);
},
updateFooter:function(text) { if (!text) text = this.dateString(); this.footer_div.purgeChildren(); this.footer_div.build("span", {innerHTML: text }); },
updateSelectedDate:function(partsOrElement, via_click) {
var parts = $H(partsOrElement);
if ((this.target_element.disabled || this.target_element.readOnly) && this.options.get("popup") != "force") return false;
if (parts.get("day")) {
var t_selected_date = this.selected_date, vdc = this.options.get("valid_date_check");
for (var x = 0; x<=3; x++) t_selected_date.setDate(parts.get("day"));
t_selected_date.setYear(parts.get("year"));
t_selected_date.setMonth(parts.get("month"));
if (vdc && ! vdc(t_selected_date.stripTime())) { return false; }
this.selected_date = t_selected_date;
this.selection_made = true;
}
if (!isNaN(parts.get("hour"))) this.selected_date.setHours(parts.get("hour"));
if (!isNaN(parts.get("minute"))) this.selected_date.setMinutes( Math.floor_to_interval(parts.get("minute"), this.options.get("minute_interval")) );
if (parts.get("hour") === "" || parts.get("minute") === "")
this.setUseTime(false);
else if (!isNaN(parts.get("hour")) || !isNaN(parts.get("minute")))
this.setUseTime(true);
this.updateFooter();
this.setSelectedClass();
if (this.selection_made) this.updateValue();
if (this.closeOnClick()) { this.close(); }
if (via_click && !this.options.get("embedded")) {
if ((new Date() - this.last_click_at) < 333) this.close();
this.last_click_at = new Date();
}
},
closeOnClick: function() {
if (this.options.get("embedded")) return false;
if (this.options.get("close_on_click")===nil )
return (this.options.get("time")) ? false : true
else
return (this.options.get("close_on_click"))
},
navMonth: function(month) { (target_date = new Date(this.date)).setMonth(month); return (this.navTo(target_date)); },
navYear: function(year) { (target_date = new Date(this.date)).setYear(year); return (this.navTo(target_date)); },
navTo: function(date) {
if (!this.validYear(date.getFullYear())) return false;
this.date = date;
this.date.setDate(1);
this.refresh();
this.callback("after_navigate", this.date);
return true;
},
setUseTime: function(turn_on) {
this.use_time = this.options.get("time") && (this.options.get("time")=="mixed" ? turn_on : true) // force use_time to true if time==true && time!="mixed"
if (this.use_time && this.selected_date) { // only set hour/minute if a date is already selected
var minute = Math.floor_to_interval(this.selected_date.getMinutes(), this.options.get("minute_interval"));
var hour = this.selected_date.getHours();
this.hour_select.setValue(hour);
this.minute_select.setValue(minute)
} else if (this.options.get("time")=="mixed") {
this.hour_select.setValue(""); this.minute_select.setValue("");
}
},
updateValue: function() {
var last_value = this.target_element.value;
this.target_element.value = this.dateString();
if (last_value!=this.target_element.value) this.callback("onchange");
},
today: function(now) {
var d = new Date(); this.date = new Date();
var o = $H({ day: d.getDate(), month: d.getMonth(), year: d.getFullYear(), hour: d.getHours(), minute: d.getMinutes()});
if ( ! now ) o = o.merge({hour: "", minute:""});
this.updateSelectedDate(o, true);
this.refresh();
},
close: function() {
if (this.closed) return false;
this.callback("before_close");
this.target_element.calendar_date_select = nil;
Event.stopObserving(document, "mousedown", this.closeIfClickedOut_handler);
Event.stopObserving(document, "keypress", this.keyPress_handler);
this.calendar_div.remove(); this.closed = true;
if (this.iframe) this.iframe.remove();
if (this.target_element.type!="hidden") this.target_element.focus();
this.callback("after_close");
},
closeIfClickedOut: function(e) {
if (! $(Event.element(e)).descendantOf(this.calendar_div) ) this.close();
},
keyPress: function(e) {
if (e.keyCode==Event.KEY_ESC) this.close();
},
callback: function(name, param) { if (this.options.get(name)) { this.options.get(name).bind(this.target_element)(param); } }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?