fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.5 KiB

8 years ago
/**
* Created by GUY on 2015/8/28.
* @class BI.Calendar
* @extends BI.Widget
*/
BI.Calendar = BI.inherit(BI.Widget, {
_defaultConfig: function () {
var conf = BI.Calendar.superclass._defaultConfig.apply(this, arguments);
return BI.extend(conf, {
baseCls: "bi-calendar",
logic: {
dynamic: false
},
7 years ago
min: "1900-01-01", // 最小日期
max: "2099-12-31", // 最大日期
8 years ago
year: 2015,
month: 8,
8 years ago
day: 25
7 years ago
});
8 years ago
},
_dateCreator: function (Y, M, D) {
var self = this, o = this.options, log = {}, De = BI.getDate();
8 years ago
var mins = o.min.match(/\d+/g);
var maxs = o.max.match(/\d+/g);
Y < (mins[0] | 0) && (Y = (mins[0] | 0));
Y > (maxs[0] | 0) && (Y = (maxs[0] | 0));
De.setFullYear(Y, M, D);
log.ymd = [De.getFullYear(), De.getMonth(), De.getDate()];
var MD = Date._MD.slice(0);
MD[1] = BI.isLeapYear(log.ymd[0]) ? 29 : 28;
8 years ago
// 日期所在月第一天
8 years ago
De.setFullYear(log.ymd[0], log.ymd[1], 1);
// 是周几
8 years ago
log.FDay = De.getDay();
// 当前月页第一天是几号
log.PDay = MD[M === 0 ? 11 : M - 1] - log.FDay + 1;
8 years ago
log.NDay = 1;
var items = [];
BI.each(BI.range(42), function (i) {
var td = {}, YY = log.ymd[0], MM = log.ymd[1] + 1, DD;
// 上个月的日期
8 years ago
if (i < log.FDay) {
td.lastMonth = true;
DD = i + log.PDay;
// 上一年
8 years ago
MM === 1 && (YY -= 1);
MM = MM === 1 ? 12 : MM - 1;
} else if (i >= log.FDay && i < log.FDay + MD[log.ymd[1]]) {
8 years ago
DD = i - log.FDay + 1;
if (i - log.FDay + 1 === log.ymd[2]) {
td.currentDay = true;
}
} else {
td.nextMonth = true;
DD = log.NDay++;
MM === 12 && (YY += 1);
MM = MM === 12 ? 1 : MM + 1;
}
if (BI.checkDateVoid(YY, MM, DD, mins, maxs)[0]) {
8 years ago
td.disabled = true;
}
td.text = DD;
items.push(td);
7 years ago
});
8 years ago
return items;
},
_init: function () {
BI.Calendar.superclass._init.apply(this, arguments);
var self = this, o = this.options;
var items = BI.map(Date._SDN.slice(0, 7), function (i, value) {
return {
type: "bi.label",
height: 24,
8 years ago
text: value
7 years ago
};
});
8 years ago
var title = BI.createWidget({
type: "bi.button_group",
height: 44,
items: items,
layouts: [{
type: "bi.center",
hgap: 10,
vgap: 10
}]
7 years ago
});
var days = this._dateCreator(o.year, o.month - 1, o.day);
8 years ago
items = [];
items.push(days.slice(0, 7));
items.push(days.slice(7, 14));
items.push(days.slice(14, 21));
items.push(days.slice(21, 28));
items.push(days.slice(28, 35));
items.push(days.slice(35, 42));
items = BI.map(items, function (i, item) {
return BI.map(item, function (j, td) {
return BI.extend(td, {
type: "bi.text_item",
cls: "bi-list-item-active",
textAlign: "center",
whiteSpace: "normal",
once: false,
forceSelected: true,
height: 24,
8 years ago
value: o.year + "-" + o.month + "-" + td.text,
disabled: td.lastMonth || td.nextMonth || td.disabled
7 years ago
// selected: td.currentDay
8 years ago
});
});
});
this.days = BI.createWidget({
type: "bi.button_group",
items: BI.createItems(items, {}),
layouts: [BI.LogicFactory.createLogic("table", BI.extend({}, o.logic, {
columns: 7,
rows: 6,
columnSize: [1 / 7, 1 / 7, 1 / 7, 1 / 7, 1 / 7, 1 / 7, 1 / 7],
rowSize: 24,
hgap: 10,
vgap: 10
8 years ago
}))]
});
this.days.on(BI.Controller.EVENT_CHANGE, function () {
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments);
7 years ago
});
8 years ago
BI.createWidget(BI.extend({
8 years ago
element: this
8 years ago
}, BI.LogicFactory.createLogic("vertical", BI.extend({}, o.logic, {
items: BI.LogicFactory.createLogicItemsByDirection("top", title, this.days)
}))));
},
isFrontDate: function () {
var o = this.options, c = this._const;
var Y = o.year, M = o.month, De = BI.getDate(), day = De.getDay();
8 years ago
Y = Y | 0;
De.setFullYear(Y, M, 1);
var newDate = De.getOffsetDate(-1 * (day + 1));
return !!BI.checkDateVoid(newDate.getFullYear(), newDate.getMonth(), newDate.getDate(), o.min, o.max)[0];
8 years ago
},
isFinalDate: function () {
var o = this.options, c = this._const;
var Y = o.year, M = o.month, De = BI.getDate(), day = De.getDay();
8 years ago
Y = Y | 0;
De.setFullYear(Y, M, 1);
var newDate = De.getOffsetDate(42 - day);
return !!BI.checkDateVoid(newDate.getFullYear(), newDate.getMonth(), newDate.getDate(), o.min, o.max)[0];
8 years ago
},
setValue: function (ob) {
this.days.setValue([ob.year + "-" + ob.month + "-" + ob.day]);
},
getValue: function () {
var date = this.days.getValue()[0].match(/\d+/g);
return {
year: date[0] | 0,
month: date[1] | 0,
day: date[2] | 0
7 years ago
};
8 years ago
}
});
BI.extend(BI.Calendar, {
getPageByDateJSON: function (json) {
var year = BI.getDate().getFullYear();
var month = BI.getDate().getMonth();
8 years ago
var page = (json.year - year) * 12;
page += json.month - month;
return page;
},
7 years ago
getDateJSONByPage: function (v) {
var months = BI.getDate().getMonth();
var page = v;
7 years ago
// 对当前page做偏移,使到当前年初
page = page + months;
var year = BI.parseInt(page / 12);
7 years ago
if(page < 0 && page % 12 !== 0) {
year--;
}
var month = page >= 0 ? (page % 12) : ((12 + page % 12) % 12);
return {
year: BI.getDate().getFullYear() + year,
month: month
7 years ago
};
8 years ago
}
});
8 years ago
BI.shortcut("bi.calendar", BI.Calendar);