export default (ngModule) => {
* @ 日期筛选组件
* @param: {
* data: {
* beginDate: string?, // 开始日期
* endDate: string?, // 结束日期
* chosenDate: string?, // 选中日期
* },
* text: string, // 未选择日期时显示
* chosen: function, // 选中日期后回调(传入选中日期:xxxx-xx-xx)
* }
*
*/
ngModule.directive('selectDate', function () {
return {
restrict: 'AE',
scope: {
data: '=?',
chosen: '=?',
text: '@?'
},
templateUrl: './shared/components/selectDate.template.html',
transclude: true,
replace: false,
link(scope, element, attrs) {
const dateNow = new Date();
const todate = `${dateNow.getFullYear()}-${dateNow.getMonth() + 1}-${dateNow.getDate()}`;
let beginDate;
let endDate;
scope.isShown = false;
scope.toggle = toggle;
scope.init = init;
scope.chooseDay = chooseDay;
scope.lastMonth = lastMonth;
scope.nextMonth = nextMonth;
scope.lastYear = lastYear;
scope.nextYear = nextYear;
scope.isDayChosen = isDayChosen;
scope.isDayActive = isDayActive;
function toggle() {
scope.isShown = !scope.isShown;
}
function init() {
if(!scope.data) {scope.data = {};}
scope.chosenDate = scope.data && scope.data.chosenDate || todate;
beginDate = scope.data && scope.data.beginDate || '1700-01-01';
endDate = scope.data && scope.data.endDate || '2900-12-30';
render();
}
function render(date?) {
scope.readingDate = FormatDate(date || scope.data.chosenDate || todate);
const year = Number(scope.readingDate.split('-')[0]);
const month = Number(scope.readingDate.split('-')[1]);
const firstday = getFirstDay(year, month);
const monthlength = getMonthLength(year, month);
scope.readingDays = [];
for (let i = 0; i < firstday; i++) {
scope.readingDays.push('');
}
for (let i = 1; i <= monthlength; i++) {
scope.readingDays.push(i);
}
}
function chooseDay(day){
const {year, month} = getDateDetail(scope.readingDate);
scope.chosenDate = scope.data.chosenDate = `${year}-${month}-${day}`;
scope.isShown = false;
if (typeof scope.chosen === 'function') {
scope.chosen(scope.chosenDate);
}
}
function lastMonth() {
let {year, month, day} = getDateDetail(scope.readingDate);
if (--month === 0) {
render(`${year - 1}-12-${day}`);
} else {
render(`${year}-${month}-${day}`);
}
}
function nextMonth() {
let {year, month, day} = getDateDetail(scope.readingDate);
if (++month > 12) {
render(`${year + 1}-1-${day}`);
} else {
render(`${year}-${month}-${day}`);
}
}
function lastYear() {
let {year, month, day} = getDateDetail(scope.readingDate);
render(`${year - 1}-${month}-${day}`);
}
function nextYear() {
let {year, month, day} = getDateDetail(scope.readingDate);
render(`${year + 1}-${month}-${day}`);
}
function isDayChosen(day) {
if (!day) { return false; }
const {year, month} = getDateDetail(scope.readingDate);
return FormatDate(`${year}-${month}-${day}`) === FormatDate(scope.chosenDate);
}
function isDayActive(day) {
if (!day) { return false; }
const {year, month} = getDateDetail(scope.readingDate);
const date = FormatDate(`${year}-${month}-${day}`);
return (date >= FormatDate(beginDate) && date <= FormatDate(endDate));
}
function getFirstDay(year, month) {
return new Date(year, month - 1, 1).getDay();
}
function getMonthLength(year, month) {
const nextMonth = new Date(year, month, 1);
nextMonth.setHours(nextMonth.getHours() - 2);
return nextMonth.getDate();
}
function getDateDetail(date) {
let year = Number(scope.readingDate.split('-')[0]);
let month = Number(scope.readingDate.split('-')[1]);
let day = Number(scope.readingDate.split('-')[2]);
return { year, month, day };
}
},
};
});
};