king
2020-08-12 2ed2188bb01fa321177125917102e5f664497d2a
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Select, Radio, Row, Col, Popover, Badge } from 'antd'
import moment from 'moment'
 
// import Utils from '@/utils/utils.js'
import './index.scss'
 
const { Option } = Select
 
class Calendar extends Component {
  static propTpyes = {
    data: PropTypes.any,            // 事件数据
    levels: PropTypes.any
  }
 
  state = {
    level: 'day',
    levels: null,
    yearlist: null,
    selectYear: moment().format('YYYY'),
    selectMonth: moment().format('MM'),
    datelist: null,
    monthlist: null,
  }
 
  UNSAFE_componentWillMount() {
    const { levels } = this.props
 
    let yearlist = []
    let _selectYear = +this.state.selectYear
    yearlist.push(`${_selectYear}`)
    
    for (let i = 1; i <= 50; i++) {
      yearlist.unshift(`${_selectYear - i}`)
      yearlist.push(`${_selectYear + i}`)
    }
 
    let datelist = this.getDateList(this.state.selectYear)
    let _levels = null
 
    if (!levels) {
      _levels = ['day', 'month', 'year']
    } else {
      _levels = levels
    }
 
    let level = _levels[0]
    let monthlist = null
    level = 'month'
 
    if (_levels.includes('month')) {
      monthlist = datelist.filter(item => item.month === moment().format('MM'))[0]
    }
 
    this.setState({
      yearlist,
      datelist,
      monthlist,
      level,
      levels: _levels
    })
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (!is(fromJS(this.props.data), fromJS(nextProps.data))) {
      
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  getDateList = (selectYear) => {
    let datelist = []
    let months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
    let monthName = {
      '01': '一月', '02': '二月', '03': '三月', '04': '四月', '05': '五月', '06': '六月',
      '07': '七月', '08': '八月', '09': '九月', '10': '十月', '11': '十一月', '12': '十二月'
    }
 
    months.forEach(month => {
      let _weeklist = [{week: 1, sublist: []}]
      let _week = moment(`${selectYear}${month}01`, 'YYYYMMDD').weekday()
      let end = +moment(`${selectYear}${month}`, 'YYYYMM').endOf('month').format('DD')
 
      for (let i = 0; i < _week; i++) {
        _weeklist[0].sublist.push(null)
      }
 
      for (let i = 1; i <= end; i++) {
        if (_weeklist[_weeklist.length - 1].sublist.length < 7) {
          _weeklist[_weeklist.length - 1].sublist.push({day: i < 10 ? `0${i}` : `${i}`, label: i})
        } else {
          let _week = {week: _weeklist.length + 1, sublist: [{day: i < 10 ? `0${i}` : `${i}`, label: i}]}
          _weeklist.push(_week)
        }
      }
 
      let re = 7 - _weeklist[_weeklist.length - 1].sublist.length
      for (let i = 0; i < re; i++) {
        _weeklist[_weeklist.length - 1].sublist.push(null)
      }
 
      datelist.push({
        month: month,
        label: monthName[month],
        sublist: _weeklist
      })
    })
 
    return datelist
  }
 
  levelChange = (e) => {
    this.setState({ level: e.target.value })
  }
 
  yearChange = (value) => {
    const { levels, selectMonth } = this.state
    let datelist = this.getDateList(value)
    let monthlist = null
 
    if (levels.includes('month')) {
      monthlist = datelist.filter(item => item.month === selectMonth)[0]
    }
 
    this.setState({ selectYear: value, datelist, monthlist })
  }
 
  monthChange = (value) => {
    const { datelist } = this.state
 
    this.setState({
      selectMonth: value,
      monthlist: datelist.filter(item => item.month === value)[0]
    })
  }
 
  triggerDay = (item) => {
 
  }
 
  render() {
    const { level, selectMonth, selectYear, yearlist, levels, datelist, monthlist } = this.state
    const _levelName = {day: '日', month: '月', year: '年'}
    const listData = [
      { type: 'orange', content: 'This is error event 2.' },
      { type: 'cyan', content: 'This is error event 3.' },
      { type: 'green', content: 'This is error event 4.' },
    ]
    
    return (
      <div className="mk-calendar">
        <div className="mk-calendar-control">
          <Select value={selectYear} onChange={this.yearChange}>
            {yearlist.map(item => (<Option key={item} value={item}>{item}年</Option>))}
          </Select>
          {level === 'month' ? <Select value={selectMonth} onChange={this.monthChange}>
            <Option value="01">1月</Option>
            <Option value="02">2月</Option>
            <Option value="03">3月</Option>
            <Option value="04">4月</Option>
            <Option value="05">5月</Option>
            <Option value="06">6月</Option>
            <Option value="07">7月</Option>
            <Option value="08">8月</Option>
            <Option value="09">9月</Option>
            <Option value="10">10月</Option>
            <Option value="11">11月</Option>
            <Option value="12">12月</Option>
          </Select> : null}
          {levels.length > 1 ? <Radio.Group value={level} onChange={this.levelChange}>
            {levels.map(item => (<Radio.Button key={item} value={item}>{_levelName[item]}</Radio.Button>))}
          </Radio.Group> : null}
        </div>
        <div className="mk-calendar-content">
          {level === 'day' ? <Row className="day-calendar" gutter={16}>
            {datelist.map(item => (
              <Col span={4} key={item.month}>
                <table>
                  <thead>
                    <tr>
                      <th colSpan="7">{item.label}</th>
                    </tr>
                    <tr>
                      <th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th>
                    </tr>
                  </thead>
                  <tbody>
                    {item.sublist.map(cell => (
                      <tr key={cell.week}>
                        {cell.sublist.map((d, i) => (
                          <td key={i}>
                            {d ? <div className={'day-wrap ' + d.class} onClick={() => this.triggerDay(d)}>
                              {d.label ? <Popover mouseEnterDelay={0.3} overlayClassName="calendar-day-pop" content={
                                <div>
                                  {listData.map((data, index) => (
                                    <div key={index} className="message">
                                      <Badge color={data.type} text={data.content} />
                                    </div>
                                  ))}
                                </div>
                              } trigger="hover">
                                {d.label}
                              </Popover> : d.label}
                            </div> : null }
                          </td>
                        ))}
                      </tr>
                    ))}
                  </tbody>
                </table>
              </Col>
            ))}
          </Row> : null}
          {level === 'month' && monthlist ? <div className="month-calendar">
            <table>
              <thead>
                <tr>
                  <th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th>
                </tr>
              </thead>
              <tbody>
                {monthlist.sublist.map(cell => (
                  <tr key={cell.week}>
                    {cell.sublist.map((d, i) => (
                      <td key={i}>
                        {d ? <div className="month-wrap" onClick={() => this.triggerDay(d)}>
                          <div className="header">
                            {d.label}
                          </div>
                          <ul className="content">
                            {[1,3,7,18,22].includes(d.label) && listData.map((data, index) => (
                              <li key={index} className="message" title={data.content}>
                                <Badge color={data.type} text={data.content} />
                              </li>
                            ))}
                          </ul>
                        </div> : null }
                      </td>
                    ))}
                  </tr>
                ))}
              </tbody>
            </table>
          </div>: null}
          {level === 'year' && monthlist ? <Row className="year-calendar">
            {datelist.map(item => (
              <Col span={8} key={item.month}>
                <div className="year-wrap">
                  <div className="header">
                    {item.label}
                  </div>
                  <ul className="content">
                    {['01', '05', '10'].includes(item.month) && listData.map((data, index) => (
                      <li key={index} className="message" title={data.content}>
                        <Badge color={data.type} text={data.content} />
                      </li>
                    ))}
                  </ul>
                </div>
              </Col>
            ))}
          </Row>: null}
        </div>
      </div>
    )
  }
}
 
export default Calendar