refactor: ES6+ modernization + 2 bugfixes
- var→const/let, function→arrow/class across all 13 JS files - Timer: prototype→class with ES6 getters - plan days: IIFE→Array.from declarative generation - storage/plan: unified formatDate via util.js - Bugfix: getPlanDay now filters by planId (plan switch accuracy) - Bugfix: getTodayRecord searches all months (cross-month boundary) - WXML/WXSS unchanged; public API unchanged
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
var util = require('../../utils/util')
|
||||
const util = require('../../utils/util')
|
||||
|
||||
Component({
|
||||
properties: {
|
||||
@@ -12,70 +12,63 @@ Component({
|
||||
},
|
||||
|
||||
observers: {
|
||||
'year, month, records': function (year, month, records) {
|
||||
'year, month, records'(year, month, records) {
|
||||
this._compute(year, month, records)
|
||||
}
|
||||
},
|
||||
|
||||
lifetimes: {
|
||||
ready: function () {
|
||||
this._compute(this.properties.year, this.properties.month, this.properties.records)
|
||||
ready() {
|
||||
const { year, month, records } = this.properties
|
||||
this._compute(year, month, records)
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
_compute: function (year, month, records) {
|
||||
var weeks = util.getMonthCalendar(year, month)
|
||||
var recordMap = {}
|
||||
_compute(year, month, records) {
|
||||
const weeks = util.getMonthCalendar(year, month)
|
||||
const recordMap = {}
|
||||
if (records && records.length) {
|
||||
records.forEach(function (r) {
|
||||
var d = parseInt(r.date.split('-')[2])
|
||||
records.forEach((r) => {
|
||||
const d = parseInt(r.date.split('-')[2])
|
||||
recordMap[d] = (recordMap[d] || 0) + r.duration
|
||||
})
|
||||
}
|
||||
|
||||
var data = weeks.map(function (week) {
|
||||
return week.map(function (day) {
|
||||
const data = weeks.map((week) =>
|
||||
week.map((day) => {
|
||||
if (!day) return null
|
||||
var duration = recordMap[day] || 0
|
||||
var color = 'transparent'
|
||||
const duration = recordMap[day] || 0
|
||||
let 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 }
|
||||
return { day, duration, color }
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
this.setData({ weeks: data })
|
||||
},
|
||||
|
||||
onDayTap: function (e) {
|
||||
var day = e.currentTarget.dataset.day
|
||||
onDayTap(e) {
|
||||
const 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
|
||||
}
|
||||
const cell = this.data.weeks.flat().find(c => c && c.day === day)
|
||||
if (!cell || cell.duration <= 0) return
|
||||
this.triggerEvent('daytap', {
|
||||
year: this.properties.year,
|
||||
month: this.properties.month,
|
||||
day: day,
|
||||
day,
|
||||
duration: cell.duration
|
||||
})
|
||||
},
|
||||
|
||||
getHeatColor: function (cell) {
|
||||
getHeatColor(cell) {
|
||||
if (!cell || cell.duration <= 0) return 'transparent'
|
||||
var d = cell.duration
|
||||
const d = cell.duration
|
||||
if (d < 30) return '#FFE0D0'
|
||||
if (d < 60) return '#FFB088'
|
||||
if (d < 120) return '#FF6B35'
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var util = require('../../utils/util')
|
||||
const util = require('../../utils/util')
|
||||
|
||||
Component({
|
||||
properties: {
|
||||
@@ -17,8 +17,8 @@ Component({
|
||||
},
|
||||
|
||||
observers: {
|
||||
'remaining, status, duration, size, ringWidth, primaryColor': function () {
|
||||
var texts = { idle: '准备开始', running: '坚持住', paused: '已暂停', completed: '完成!' }
|
||||
'remaining, status, duration, size, ringWidth, primaryColor'() {
|
||||
const texts = { idle: '准备开始', running: '坚持住', paused: '已暂停', completed: '完成!' }
|
||||
this.setData({
|
||||
displayTime: util.formatTime(this.data.remaining),
|
||||
statusText: texts[this.data.status] || ''
|
||||
@@ -28,45 +28,40 @@ Component({
|
||||
},
|
||||
|
||||
lifetimes: {
|
||||
ready: function () {
|
||||
var self = this
|
||||
var query = this.createSelectorQuery()
|
||||
ready() {
|
||||
const query = this.createSelectorQuery()
|
||||
query.select('#ringCanvas')
|
||||
.fields({ node: true, size: true })
|
||||
.exec(function (res) {
|
||||
.exec((res) => {
|
||||
if (!res || !res[0] || !res[0].node) return
|
||||
var canvas = res[0].node
|
||||
var dpr = wx.getSystemInfoSync().pixelRatio || 2
|
||||
var displaySize = self.data.size
|
||||
const canvas = res[0].node
|
||||
const dpr = wx.getSystemInfoSync().pixelRatio || 2
|
||||
const displaySize = this.data.size
|
||||
canvas.width = displaySize * dpr
|
||||
canvas.height = displaySize * dpr
|
||||
self._ctx = canvas.getContext('2d')
|
||||
self._dpr = dpr
|
||||
self._draw()
|
||||
this._ctx = canvas.getContext('2d')
|
||||
this._dpr = dpr
|
||||
this._draw()
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
_draw: function () {
|
||||
var ctx = this._ctx
|
||||
_draw() {
|
||||
const ctx = this._ctx
|
||||
if (!ctx) return
|
||||
|
||||
var size = this.data.size
|
||||
var dpr = this._dpr
|
||||
var ringWidth = this.data.ringWidth
|
||||
var duration = this.data.duration
|
||||
var remaining = this.data.remaining
|
||||
var primaryColor = this.data.primaryColor
|
||||
const { size, ringWidth, duration, remaining, primaryColor } = this.data
|
||||
const dpr = this._dpr
|
||||
|
||||
var w = size * dpr
|
||||
var h = size * dpr
|
||||
const w = size * dpr
|
||||
const h = size * dpr
|
||||
ctx.clearRect(0, 0, w, h)
|
||||
|
||||
var cx = w / 2
|
||||
var cy = h / 2
|
||||
var radius = (size - ringWidth) / 2 * dpr
|
||||
var lw = ringWidth * dpr
|
||||
const cx = w / 2
|
||||
const cy = h / 2
|
||||
const radius = (size - ringWidth) / 2 * dpr
|
||||
const lw = ringWidth * dpr
|
||||
|
||||
// track ring
|
||||
ctx.beginPath()
|
||||
@@ -76,11 +71,11 @@ Component({
|
||||
ctx.lineCap = 'round'
|
||||
ctx.stroke()
|
||||
|
||||
// progress arc (clockwise from 12 o'clock, represents remaining time)
|
||||
var ratio = duration > 0 ? Math.max(0, Math.min(1, remaining / duration)) : 0
|
||||
// progress arc (clockwise from 12 o'clock)
|
||||
const ratio = duration > 0 ? Math.max(0, Math.min(1, remaining / duration)) : 0
|
||||
if (ratio > 0.001) {
|
||||
var startAngle = -Math.PI / 2
|
||||
var endAngle = startAngle + ratio * Math.PI * 2
|
||||
const startAngle = -Math.PI / 2
|
||||
const endAngle = startAngle + ratio * Math.PI * 2
|
||||
ctx.beginPath()
|
||||
ctx.arc(cx, cy, radius, startAngle, endAngle)
|
||||
ctx.strokeStyle = primaryColor
|
||||
|
||||
Reference in New Issue
Block a user