datetime-minute-picker.js 5.66 KB
/**
 * 动态表单分钟 datetime 一体选择(日/时/分三列滚轮)
 * @file 参考 tabRepairOrder/timeSelect,输出 YYYY-MM-DD HH:mm,无页面耦合
 */
const {
	endOfToday,
	formatMinutePickerValue,
	parseMinutePickerDefaultTime,
	buildMinutePickerDayList,
	resolveMinutePickerDayStart,
	resolveMinutePickerDayEnd
} = require('../../form-engine/datetime-picker-mode.js')

function pad2(n) {
	return `${n}`.padStart(2, '0')
}

Component({
	properties: {
		show: {
			type: Boolean,
			observer(visible) {
				if (visible) {
					this.initPicker()
				}
			}
		},
		title: {
			type: String,
			value: ''
		},
		defaultTime: {
			type: String,
			value: ''
		},
		/** now | today | 空 — 对齐 resolveMaxDateKind */
		maxDateKind: {
			type: String,
			value: ''
		}
	},

	data: {
		days: [],
		hours: [],
		minutes: [],
		dateValues: [0],
		hourValues: [0],
		minuteValues: [0],
		loading: true
	},

	methods: {
		shouldCapTimeToNow() {
			return this.properties.maxDateKind === 'now'
		},

		shouldCapDateToToday() {
			const kind = this.properties.maxDateKind
			return kind === 'now' || kind === 'today'
		},

		parseDefaultTime() {
			return parseMinutePickerDefaultTime(this.properties.defaultTime)
		},

		buildHourList(dateStr) {
			const todayStr = endOfToday()
			const isToday = dateStr === todayStr
			if (this.shouldCapTimeToNow() && isToday) {
				const now = new Date()
				const limit = now.getHours() + 1
				return Array.from({ length: limit }, (_, hour) => pad2(hour))
			}
			return Array.from({ length: 24 }, (_, hour) => pad2(hour))
		},

		buildMinuteList(dateStr, hourStr) {
			const todayStr = endOfToday()
			const isToday = dateStr === todayStr
			if (this.shouldCapTimeToNow() && isToday) {
				const now = new Date()
				const currentHour = pad2(now.getHours())
				if (hourStr !== currentHour) {
					return Array.from({ length: 60 }, (_, minute) => pad2(minute))
				}
				const limit = now.getMinutes() + 1
				return Array.from({ length: limit }, (_, minute) => pad2(minute))
			}
			return Array.from({ length: 60 }, (_, minute) => pad2(minute))
		},

		/**
		 * 同步滚轮选中项;二次 setData 规避 picker-view 首帧空白(对齐 timeSelect)
		 */
		syncCurrentSelection(days, hours, minutes, defaultTime) {
			const dateIndex = days.findIndex(day => day === defaultTime.dateStr)
			const hourStr = pad2(defaultTime.hour)
			const minuteStr = pad2(defaultTime.minute)
			let hourIndex = hours.findIndex(hour => hour === hourStr)
			if (hourIndex < 0) {
				hourIndex = Math.max(0, hours.length - 1)
			}
			let minuteIndex = minutes.findIndex(minute => minute === minuteStr)
			if (minuteIndex < 0) {
				minuteIndex = Math.max(0, minutes.length - 1)
			}
			const resolvedDateIndex = dateIndex >= 0 ? dateIndex : Math.max(0, days.length - 1)
			const resolvedHourIndex = hourIndex >= 0 ? hourIndex : Math.max(0, hours.length - 1)
			const resolvedMinuteIndex = minuteIndex >= 0 ? minuteIndex : Math.max(0, minutes.length - 1)

			this.setData(
				{
					days,
					hours,
					minutes,
					dateValues: [resolvedDateIndex],
					hourValues: [resolvedHourIndex],
					minuteValues: [resolvedMinuteIndex],
					loading: true
				},
				() => {
					setTimeout(() => {
						this.currentDate = days[resolvedDateIndex]
						this.currentHour = hours[resolvedHourIndex]
						this.currentMinute = minutes[resolvedMinuteIndex]
						this.setData({
							dateValues: [resolvedDateIndex],
							hourValues: [resolvedHourIndex],
							minuteValues: [resolvedMinuteIndex],
							loading: false
						})
					}, 0)
				}
			)
		},

		initPicker() {
			this.setData({ loading: true })
			const defaultTime = this.parseDefaultTime()
			const todayStr = endOfToday()
			const capToToday = this.shouldCapDateToToday()
			const startDay = resolveMinutePickerDayStart(defaultTime.dateStr)
			const endDay = resolveMinutePickerDayEnd({
				defaultDateStr: defaultTime.dateStr,
				todayStr,
				capToToday
			})
			const days = buildMinutePickerDayList(startDay, endDay)
			const hours = this.buildHourList(defaultTime.dateStr)
			const minutes = this.buildMinuteList(defaultTime.dateStr, pad2(defaultTime.hour))
			this.syncCurrentSelection(days, hours, minutes, defaultTime)
		},

		refreshHourMinuteColumns() {
			const hours = this.buildHourList(this.currentDate)
			const hourIndex = Math.max(
				0,
				hours.findIndex(hour => hour === this.currentHour)
			)
			this.currentHour = hours[hourIndex] || hours[hours.length - 1] || '00'
			const minutes = this.buildMinuteList(this.currentDate, this.currentHour)
			const minuteIndex = Math.max(
				0,
				minutes.findIndex(minute => minute === this.currentMinute)
			)
			this.currentMinute = minutes[minuteIndex] || minutes[minutes.length - 1] || '00'
			this.setData({
				hours,
				minutes,
				hourValues: [hourIndex],
				minuteValues: [minuteIndex]
			})
		},

		changeDate(e) {
			const index = e.detail.value[0]
			this.currentDate = this.data.days[index]
			this.refreshHourMinuteColumns()
		},

		changeHour(e) {
			const index = e.detail.value[0]
			this.currentHour = this.data.hours[index]
			const minutes = this.buildMinuteList(this.currentDate, this.currentHour)
			const minuteIndex = Math.max(
				0,
				minutes.findIndex(minute => minute === this.currentMinute)
			)
			this.currentMinute = minutes[minuteIndex] || minutes[minutes.length - 1] || '00'
			this.setData({
				minutes,
				minuteValues: [minuteIndex]
			})
		},

		changeMinutes(e) {
			const index = e.detail.value[0]
			this.currentMinute = this.data.minutes[index]
		},

		confirm() {
			const value = formatMinutePickerValue(
				this.currentDate,
				this.currentHour,
				this.currentMinute
			)
			this.triggerEvent('confirm', { value })
		},

		cancel() {
			this.triggerEvent('cancel')
		}
	}
})