98 lines
2.6 KiB
JavaScript
98 lines
2.6 KiB
JavaScript
define(function (require, exports, module) {
|
|
var ev = require("../platform/EventObject.js");
|
|
var tpl = $("#datebar-template").doT();
|
|
var container = $("ul.date-bar-list");
|
|
var parser = require("../platform/parser.js");
|
|
var utility = require("../utility.js");
|
|
|
|
var getFirstDayOfWeek = function (date) {
|
|
date = utility.toDate(date);
|
|
var day = date.getDay();
|
|
|
|
if (day === 1)
|
|
return date;
|
|
var offset = day === 0 ? -6 : 1 - day;
|
|
return utility.addDays(date, offset);
|
|
};
|
|
var formatDate = function (date) {
|
|
var m = date.getMonth() + 1;
|
|
var d = date.getDate();
|
|
|
|
return (m < 10 ? "0" : "") + m + "-" + (d < 10 ? "0" : "") + d;
|
|
};
|
|
var getDateString = function (date) {
|
|
return utility.formatDate(date, 'yyyy-MM-dd');
|
|
};
|
|
|
|
var WidgetDateBarUi = function () {
|
|
var that = this;
|
|
var today = utility.addDays(new Date(), 0);
|
|
var date = null;
|
|
var maxdays = utility.addDays(today, 30);
|
|
var currentHightDate = today;
|
|
|
|
ev.apply(this);
|
|
|
|
this.init = function () {
|
|
$("div.date-bar-prev").click(that.goPrev);
|
|
$("div.date-bar-next").click(that.goNext);
|
|
$(document).on("click", "ul.date-bar-list li:not(.disabled):not(.selected)", function () {
|
|
that.fireEvent("requireChangeDate", this.dataset.date);
|
|
});
|
|
|
|
that.go(today);
|
|
};
|
|
this.go = function (goDate, selectedDate) {
|
|
goDate = utility.toDate(goDate);
|
|
if (selectedDate)
|
|
currentHightDate = utility.toDate(selectedDate);
|
|
|
|
var d = getFirstDayOfWeek(goDate);
|
|
if (!date || d.getTime() !== date.getTime()) {
|
|
date = d;
|
|
that.render();
|
|
}
|
|
container.find("li:not(.disabled)").removeClass("selected").filter("[data-date='" + getDateString(currentHightDate) + "']").addClass("selected");
|
|
};
|
|
this.goNext = function () {
|
|
that.go(utility.addDays(date, 7));
|
|
};
|
|
this.goPrev = function () {
|
|
that.go(utility.addDays(date, -7));
|
|
};
|
|
this.render = function () {
|
|
var data = [];
|
|
var namestr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
|
for (var i = 0; i < 7; i++) {
|
|
var d = utility.addDays(date, i);
|
|
|
|
data.push({
|
|
_date: d,
|
|
name: namestr[d.getDay()],
|
|
date: formatDate(d),
|
|
disabled: d < today || d > maxdays,
|
|
selected: d.getTime() == today.getTime(),
|
|
datecode: getDateString(d)
|
|
});
|
|
}
|
|
|
|
container.empty().html(tpl(data));
|
|
};
|
|
|
|
Object.defineProperty(this, "current", {
|
|
get: function () {
|
|
return date;
|
|
},
|
|
set: function (value) {
|
|
that.go(getFirstDayOfWeek(value));
|
|
}
|
|
});
|
|
|
|
return this;
|
|
};
|
|
WidgetDateBarUi.prototype = Object.create(ev);
|
|
WidgetDateBarUi.constructor = ev;
|
|
|
|
return new WidgetDateBarUi();
|
|
});
|