///
///
///
DateBox = function () {
this.ControlID = "Window_DateBox";
this.ParentElement = null; //要加入的父容器
this.Isbgiframe = false;
var My = this;
var cmdPrevMonth = My.ControlID + "_" + "cmdPrevMonth";
var txtYear = My.ControlID + "_" + "txtYear";
var listYearName = My.ControlID + "_" + "listYearName";
var txtMonth = My.ControlID + "_" + "txtMonth";
var listMonthName = My.ControlID + "_" + "listMonthName";
var cmdNextMonth = My.ControlID + "_" + "cmdNextMonth";
var ListDateDay = My.ControlID + "_" + "ListDateDay";
var cmdPrevYear = My.ControlID + "_" + "cmdPrevYear";
var cmdPrevMonth2 = My.ControlID + "_" + "cmdPrevMonth2";
var cmdDay = My.ControlID + "_" + "cmdDay";
var cmdNextMonth2 = My.ControlID + "_" + "cmdNextMonth2";
var cmdNextYear = My.ControlID + "_" + "cmdNextYear";
var cmdClose = My.ControlID + "_" + "cmdClose";
var cmdMenu = My.ControlID + "_" + "cmdMenu";
var SelectYear = 0;
var SelectMonth = 0;
var SelectDate = null;
var TextBox = null;
//#region 加载
this.Load = function () {
Init();
$(document.body).click(function (e) {
if (e.target.id == My.ControlID || e.target == TextBox) return;
if ($(e.target).parents("#" + My.ControlID).length == 0) {
$("#" + My.ControlID, My.ParentElement).hide();
}
});
$("#" + txtYear + ",#" + txtMonth, My.ParentElement).hover(function () {
$(this).css("background-color", "#ffd700");
}, function () {
$(this).css("background-color", "");
});
$("#" + cmdMenu, My.ParentElement).hover(function () {
$("#" + My.ControlID, My.ParentElement).draggable("enable");
}, function () {
$("#" + My.ControlID, My.ParentElement).draggable("disable");
});
$("#" + cmdPrevYear, My.ParentElement).live("click", function () {
SelectYear -= 1;
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + listYearName, My.ParentElement).hide();
$("#" + listMonthName, My.ParentElement).hide();
BindDateTime();
});
$("#" + cmdNextYear, My.ParentElement).live("click", function () {
SelectYear += 1;
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + listYearName, My.ParentElement).hide();
$("#" + listMonthName, My.ParentElement).hide();
BindDateTime();
});
$("#" + cmdPrevMonth + ",#" + cmdPrevMonth2, My.ParentElement).live("click", function () {
SelectMonth -= 1;
if (SelectMonth == 0) { SelectMonth = 12; SelectYear--; }
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + listYearName, My.ParentElement).hide();
$("#" + listMonthName, My.ParentElement).hide();
BindDateTime();
});
$("#" + cmdNextMonth + ",#" + cmdNextMonth2, My.ParentElement).live("click", function () {
SelectMonth += 1;
if (SelectMonth == 13) { SelectMonth = 1; SelectYear++; }
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + listYearName, My.ParentElement).hide();
$("#" + listMonthName, My.ParentElement).hide();
BindDateTime();
});
$('#' + listYearName, My.ParentElement).live("change", function () {
SelectYear = parseInt($(this).val());
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + listYearName, My.ParentElement).hide();
BindDateTime();
});
$('#' + listMonthName, My.ParentElement).live("change", function () {
SelectMonth = parseInt($(this).val());
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + listMonthName, My.ParentElement).hide();
BindDateTime();
});
$("#" + txtYear, My.ParentElement).click(function () {
BindListYear();
$("#" + txtYear, My.ParentElement).hide();
$("#" + listYearName, My.ParentElement).show();
$("#" + listMonthName, My.ParentElement).hide();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
});
$("#" + txtMonth, My.ParentElement).click(function () {
$("#" + txtMonth, My.ParentElement).hide();
$("#" + listMonthName, My.ParentElement).show();
$("#" + listYearName, My.ParentElement).hide();
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + listMonthName, My.ParentElement).val(SelectMonth);
});
$("#" + cmdDay, My.ParentElement).click(function () {
SelectDate = new Date();
SelectYear = SelectDate.getFullYear();
SelectMonth = SelectDate.getMonth() + 1;
BindDateTime();
$("#" + listMonthName, My.ParentElement).hide()
$("#" + listYearName, My.ParentElement).hide();
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
$("#" + My.ControlID, My.ParentElement).hide();
$(TextBox).val(SelectDate.localeFormat("yyyy-MM-dd"));
});
$("#" + cmdClose, My.ParentElement).live("click", function () {
$("#" + My.ControlID, My.ParentElement).hide();
});
$("#" + ListDateDay + " tr[name='DataGridRow'] td", My.ParentElement).live("click", function () {
if ($(this).find(":button").length > 0) return;
if (SelectDate == null) SelectDate = new Date();
SelectDate.setFullYear(SelectYear);
var Month = $(this).attr("month");
SelectDate.setMonth(Month - 1);
SelectDate.setDate($(this).text());
$("#" + My.ControlID, My.ParentElement).hide();
$(TextBox).val(SelectDate.localeFormat("yyyy-MM-dd"));
});
}
//#endregion
//#region 初始化
function Init() {
if (My.ParentElement == null) My.ParentElement = $(document.body);
if ($("#" + My.ControlID, My.ParentElement).length > 0) return;
var htmlString = new Sys.StringBuilder();
htmlString.append("
");
htmlString.append('
');
htmlString.append('');
htmlString.append('');
htmlString.append('');
htmlString.append('');
htmlString.append('< | ');
htmlString.append('');
htmlString.append('2012年');
htmlString.append('');
htmlString.append(' | ');
htmlString.append('');
htmlString.append('2月');
htmlString.append('');
htmlString.append(' | ');
htmlString.append('> | ');
htmlString.append(' ');
htmlString.append(' ');
htmlString.append(' | ');
htmlString.append('
');
htmlString.append('');
htmlString.append('');
htmlString.append('');
htmlString.append(' ');
htmlString.append(' |
');
htmlString.append('');
htmlString.append('');
htmlString.append(' |
');
htmlString.append('
');
htmlString.append("
");
My.ParentElement.append(htmlString.toString());
$("#" + My.ControlID, My.ParentElement).draggable();
if (My.Isbgiframe == true) $("#" + My.ControlID, My.ParentElement).bgiframe();
}
//#endregion
//#region 显示
this.Show = function (obj) {
TextBox = obj;
var position = $(TextBox).offset();
var top = position.top + $(TextBox).height() + 7;
var left = position.left;
$("#" + My.ControlID, My.ParentElement).css("top", top).css("left", left);
var value = obj.value;
var a = Date.parseInvariant(value, "yyyy-M-d");
if (a == null) {
SelectDate = null;
var CurrentDate = new Date();
SelectYear = CurrentDate.getFullYear();
SelectMonth = CurrentDate.getMonth() + 1;
}
else {
SelectDate = a;
SelectYear = SelectDate.getFullYear();
SelectMonth = SelectDate.getMonth() + 1;
}
$("#" + listMonthName, My.ParentElement).hide()
$("#" + listYearName, My.ParentElement).hide();
BindDateTime();
$("#" + My.ControlID, My.ParentElement).show();
}
//#endregion
//#region 绑定年份下拉框
function BindListYear() {
var html = new Sys.StringBuilder();
for (var i = SelectYear - 20; i < SelectYear; i++) {
html.append("");
}
for (var i = SelectYear; i < SelectYear + 20; i++) {
if (i == SelectYear) { html.append(""); }
else { html.append(""); }
}
$('#' + listYearName, My.ParentElement).html(html.toString());
}
//#endregion
//#region 绑定日期
function BindDateTime() {
var SelectDateTime = Date.parseInvariant(SelectYear + "-" + SelectMonth + "-1", "yyyy-M-d");
$("#" + txtYear, My.ParentElement).html(SelectYear + "年").show();
$("#" + txtMonth, My.ParentElement).html(SelectMonth + "月").show();
var week = SelectDateTime.getDay();
var day = SelectDateTime.getDate() - week;
var StartDate = CloneDate(SelectDateTime);
StartDate.setDate(day);
var html = new Sys.StringBuilder();
var index = 0;
var CurrentYear = 0;
var CurrentMonth = 0;
var CurrentDay = 0;
if (SelectDate != null) {
CurrentYear = SelectDate.getFullYear();
CurrentMonth = SelectDate.getMonth() + 1;
CurrentDay = SelectDate.getDate();
}
var ToDate = new Date();
var ToYear = ToDate.getFullYear();
var ToMonth = ToDate.getMonth() + 1;
var ToDay = ToDate.getDate();
for (var i = 1; i < 40; i++) {
if (index == 0) html.append('');
var month = StartDate.getMonth() + 1;
var year = StartDate.getFullYear();
day = StartDate.getDate();
var FontColor = "color: gray;";
if (month == SelectMonth) FontColor = "";
var TdColor = "background-color: #e0e0e0;";
if (year == ToYear && month == ToMonth && day == ToDay) TdColor = "background-color: #ffd700;";
if (year == CurrentYear && month == CurrentMonth && day == CurrentDay) TdColor = "background-color: #00ffff;";
html.append('' + day + ' | ');
index++;
if (index == 7) { html.append('
'); index = 0; }
StartDate.setDate(day + 1);
}
html.append(' | ');
$("#" + ListDateDay + " tr[name='DataGridRow']", My.ParentElement).remove();
$("#" + ListDateDay, My.ParentElement).append(html.toString());
}
//#endregion
//#region 复制日期
function CloneDate(datetime, s) {
var dt = new Date();
dt.setFullYear(datetime.getFullYear());
dt.setMonth(datetime.getMonth());
dt.setDate(datetime.getDate());
dt.setHours(datetime.getHours());
dt.setMinutes(datetime.getMinutes());
dt.setSeconds(datetime.getSeconds());
dt.setMilliseconds(datetime.getMilliseconds());
return dt;
}
//#endregion
}