-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvue-fa-calendar.js
160 lines (150 loc) · 6.54 KB
/
vue-fa-calendar.js
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
156
157
158
159
160
//
//
// customize this Vue 2 Font Awesome calendar component with your own classes
//
// version 0.1
//
// Nathan Frick, 2016
// MIT License
//
Vue.component('fa-day',{
props: { dayOfMonth: {type:String,required:true}
,faClass: {type:String,default:''}
,faText: {type:String,default:''}
,cellClss: {default:''}
,hoverTitle: {type:String}
}
,computed: {
facClass: function() {
var faC = (this.dayOfMonth) ? this.faClass : '';
return faC;
}
,cTitle: function () {
var tiC = (this.hoverTitle) ? this.hoverTitle : this.dayOfMonth;
return tiC;
}
}
,methods: { eclick: function() {
this.$emit('eclick')
}
}
,template:`<div :title="cTitle" @click="eclick" :class="cellClss">{{ faClass.length ? faText:dayOfMonth }}<i :class="facClass"></i><slot></slot></div>`
});
Vue.component('fa-month-table',{
props: { monthDate: {type:String,default:'03/15/2016'} // can be any day in month
,dateFormat: {type:String,default:'MM/DD/YYYY'}
,tableClass: {type:String,default:'table table-bordered'}
,tableCaptionClass: {type:String,default:'text-center lead'}
,trClass: {type:String,default:''}
,thClass: {type:String,default:'text-center'}
,tdClass: {type:String,default:'text-center'}
,selectedCellClass: {type:String,default:'info'}
,specialDays: { type:Array,
default: function () { return [
{ date:'03/15/2016',title:'et 2?',text:'the Ides of March',faIconClass:'fa fa-commenting',cellClass:''}
] ; }
}
}
,data: function() {
return ({
selectedDateStr:moment().format(this.dateFormat)
,baseDate: moment(this.monthDate,this.dateFormat).startOf('month').format(this.dateFormat)
});
}
,computed: {
baseDateMom: function() {
return moment(this.baseDate,this.dateFormat);
}
,dayNames:function () { var dN = [];
for (var i=0;i<7;i++) dN.push(moment().weekday(i).format('ddd'));
return dN;
}
,baseDateTitle: function() {
return this.baseDateMom.format('MMMM YYYY');
}
,baseMonthN: function () {
return this.baseDateMom.month();
}
,baseWeeks: function () {
var weeks = [];
// get the first day and last day in the weeks that are in this month
var iMom1 = moment(this.baseDateMom).startOf('week');
var iMomEnd = moment(this.baseDateMom).endOf('month').endOf('week');
while (iMom1.isBefore(iMomEnd))
{
// begin constructing a week
var days = [];
for (var i=0;i<7;i++) {
var specObj = this.getSpecialDayInfo(iMom1); // cull from user array
var dteStr0 = iMom1.date().toString();
var dteStr = (this.baseMonthN == iMom1.month()) ? dteStr0 : '';
days.push({
dateStr:iMom1.format(this.dateFormat)
,idom: iMom1.date() // integer
,domnth: dteStr
,inCurrentMonth: (this.baseMonthN == iMom1.month())
,faIconClass:specObj.faIconClass
,xtext:specObj.text
,cellClass:specObj.cellClass
,title:specObj.title
});
iMom1.add(1,'days');
}
weeks.push(days);
}
return weeks;
}
}
,methods: { getSpecialDayInfo: function (iMomC) {
// lookup this day (iMomC) in specialDays array and get info
var retObj = {};
for (var i in this.specialDays) {
if (this.specialDays[i].date) { if (moment(this.specialDays[i].date,this.dateFormat).isSame(iMomC)) {
if (this.specialDays[i].faIconClass)
retObj.faIconClass = this.specialDays[i].faIconClass;
if (this.specialDays[i].text)
retObj.text = this.specialDays[i].text;
if (this.specialDays[i].cellClass)
retObj.cellClass = this.specialDays[i].cellClass;
if (this.specialDays[i].title)
retObj.title = this.specialDays[i].title;
// TODO: other attributes
}}
}
return retObj;
}
,handleEClick: function (dayEntry,e) {
this.selectedDateStr = dayEntry.dateStr;
this.$emit('dateclicked',dayEntry.dateStr);
//console.log(this.selectedDateStr);
}
,bumpRight: function () {
this.baseDate = moment(this.baseDate,this.dateFormat).add(1,'months').format(this.dateFormat);
this.$emit('monthclicked',this.baseDate);
}
,bumpLeft: function () {
this.baseDate = moment(this.baseDate,this.dateFormat).add(-1,'months').format(this.dateFormat);
this.$emit('monthclicked',this.baseDate);
}
,tdClassObj: function(dday) {
var retObj = {};
retObj[this.tdClass] = true;
retObj[this.selectedCellClass] = (dday.dateStr == this.selectedDateStr);
return retObj;
}
}
,template:`
<table :class="tableClass">
<caption :class="tableCaptionClass"><i class="fa fa-caret-left" @click="bumpLeft"></i> {{baseDateTitle}} <i class="fa fa-caret-right" @click="bumpRight"></i></caption>
<thead>
<tr><th :class="thClass" v-for="dayName in dayNames">{{dayName}}</th></tr>
</thead>
<tbody>
<tr :class="trClass" v-for="weekAry in baseWeeks">
<td :class="tdClassObj(day)" v-for="day in weekAry" >
<fa-day :day-of-month="day.domnth" :fa-class="day.faIconClass" :cell-clss="day.cellClass" :hover-title="day.title" @eclick="handleEClick(day,$event)">
<span v-show="day.xtext"> {{day.xtext}}</span> <!-- use child component slot -->
</fa-day>
</td></tr></tbody></table>
`
});