1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| export default (ngModule) => {
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 }; } }, }; }); };
|