74 lines
2.7 KiB
Vue
74 lines
2.7 KiB
Vue
<template>
|
||
<div class="aa-calendar">
|
||
<div class="aa-calendar-head">
|
||
<button class="aa-calendar-nav" @click="prev">‹</button>
|
||
<span class="aa-calendar-title">{{ viewY }} 年 {{ viewM + 1 }} 月</span>
|
||
<button class="aa-calendar-nav" @click="next">›</button>
|
||
</div>
|
||
<div class="aa-calendar-week">
|
||
<span class="aa-calendar-weekday" v-for="w in weeks" :key="w">{{ w }}</span>
|
||
</div>
|
||
<div class="aa-calendar-grid">
|
||
<div v-for="(c, i) in cells" :key="i" class="aa-calendar-cell"
|
||
:class="{ 'is-out': c.out, 'is-today': c.today, 'is-selected': c.key === selected }"
|
||
@click="pick(c)">{{ c.day }}</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script>
|
||
export default {
|
||
name: 'AaCalendar',
|
||
props: { value: { type: String, default: '' } },
|
||
data: function () {
|
||
var n = new Date();
|
||
return {
|
||
weeks: ['日', '一', '二', '三', '四', '五', '六'],
|
||
viewY: n.getFullYear(), viewM: n.getMonth(),
|
||
selected: this.value, today: this.fmt(n)
|
||
};
|
||
},
|
||
computed: {
|
||
cells: function () {
|
||
var first = new Date(this.viewY, this.viewM, 1).getDay();
|
||
var days = new Date(this.viewY, this.viewM + 1, 0).getDate();
|
||
var prevDays = new Date(this.viewY, this.viewM, 0).getDate();
|
||
var arr = [];
|
||
var self = this;
|
||
for (var i = 0; i < first; i++) {
|
||
var pd = prevDays - first + 1 + i;
|
||
var pm = this.viewM - 1, py = this.viewY; if (pm < 0) { pm = 11; py--; }
|
||
arr.push({ day: pd, out: true, key: self.fmt(new Date(py, pm, pd)) });
|
||
}
|
||
for (var d = 1; d <= days; d++) {
|
||
var k = self.fmt(new Date(this.viewY, this.viewM, d));
|
||
arr.push({ day: d, out: false, today: k === this.today, key: k });
|
||
}
|
||
var tail = (7 - (arr.length % 7)) % 7;
|
||
for (var t = 1; t <= tail; t++) {
|
||
var nm = this.viewM + 1, ny = this.viewY; if (nm > 11) { nm = 0; ny++; }
|
||
arr.push({ day: t, out: true, key: self.fmt(new Date(ny, nm, t)) });
|
||
}
|
||
return arr;
|
||
}
|
||
},
|
||
methods: {
|
||
fmt: function (dt) {
|
||
var p = function (n) { return n < 10 ? '0' + n : '' + n; };
|
||
return dt.getFullYear() + '-' + p(dt.getMonth() + 1) + '-' + p(dt.getDate());
|
||
},
|
||
prev: function () { this.viewM--; if (this.viewM < 0) { this.viewM = 11; this.viewY--; } },
|
||
next: function () { this.viewM++; if (this.viewM > 11) { this.viewM = 0; this.viewY++; } },
|
||
pick: function (c) {
|
||
this.selected = c.key;
|
||
this.viewY = parseInt(c.key.slice(0, 4), 10);
|
||
this.viewM = parseInt(c.key.slice(5, 7), 10) - 1;
|
||
this.$emit('input', c.key);
|
||
this.$emit('change', c.key);
|
||
}
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<style src="./Calendar.css"></style>
|