86 lines
2.3 KiB
JavaScript
86 lines
2.3 KiB
JavaScript
var util = require('../../utils/util')
|
|
|
|
Component({
|
|
properties: {
|
|
year: { type: Number, value: new Date().getFullYear() },
|
|
month: { type: Number, value: new Date().getMonth() + 1 },
|
|
records: { type: Array, value: [] }
|
|
},
|
|
|
|
data: {
|
|
weeks: []
|
|
},
|
|
|
|
observers: {
|
|
'year, month, records': function (year, month, records) {
|
|
this._compute(year, month, records)
|
|
}
|
|
},
|
|
|
|
lifetimes: {
|
|
ready: function () {
|
|
this._compute(this.properties.year, this.properties.month, this.properties.records)
|
|
}
|
|
},
|
|
|
|
methods: {
|
|
_compute: function (year, month, records) {
|
|
var weeks = util.getMonthCalendar(year, month)
|
|
var recordMap = {}
|
|
if (records && records.length) {
|
|
records.forEach(function (r) {
|
|
var d = parseInt(r.date.split('-')[2])
|
|
recordMap[d] = (recordMap[d] || 0) + r.duration
|
|
})
|
|
}
|
|
|
|
var data = weeks.map(function (week) {
|
|
return week.map(function (day) {
|
|
if (!day) return null
|
|
var duration = recordMap[day] || 0
|
|
var color = 'transparent'
|
|
if (duration > 0) {
|
|
if (duration < 30) color = '#FFE0D0'
|
|
else if (duration < 60) color = '#FFB088'
|
|
else if (duration < 120) color = '#FF6B35'
|
|
else color = '#E05520'
|
|
}
|
|
return { day: day, duration: duration, color: color }
|
|
})
|
|
})
|
|
|
|
this.setData({ weeks: data })
|
|
},
|
|
|
|
onDayTap: function (e) {
|
|
var day = e.currentTarget.dataset.day
|
|
if (!day) return
|
|
var cell = null
|
|
var weeks = this.data.weeks
|
|
for (var i = 0; i < weeks.length; i++) {
|
|
for (var j = 0; j < weeks[i].length; j++) {
|
|
var c = weeks[i][j]
|
|
if (c && c.day === day) { cell = c; break }
|
|
}
|
|
if (cell) break
|
|
}
|
|
if (!cell || cell.duration <= 0) return
|
|
this.triggerEvent('daytap', {
|
|
year: this.properties.year,
|
|
month: this.properties.month,
|
|
day: day,
|
|
duration: cell.duration
|
|
})
|
|
},
|
|
|
|
getHeatColor: function (cell) {
|
|
if (!cell || cell.duration <= 0) return 'transparent'
|
|
var d = cell.duration
|
|
if (d < 30) return '#FFE0D0'
|
|
if (d < 60) return '#FFB088'
|
|
if (d < 120) return '#FF6B35'
|
|
return '#E05520'
|
|
}
|
|
}
|
|
})
|