UNPKG

@nuralyui/timepicker

Version:

NuralyUI TimePicker - A comprehensive time selection component with clock interface, multiple formats, and validation

175 lines 5.1 kB
/** * @license * Copyright 2023 Nuraly, Laabidi Aymen * SPDX-License-Identifier: MIT */ import { TimeUtils } from '../utils/time.utils.js'; import { TIME_PICKER_EVENTS } from '../timepicker.types.js'; /** * Controller for handling time selection logic */ export class TimePickerSelectionController { constructor(host) { this.host = host; this.selectedTime = null; } /** * Select a time value */ selectTime(time) { if (!this.host.validateTime(time)) { return; } const previousTime = this.selectedTime; this.selectedTime = Object.assign({}, time); // Dispatch time change event this.dispatchTimeChangeEvent(time, previousTime); // Request host update this.host.requestUpdate(); } /** * Get currently selected time */ getSelectedTime() { return this.selectedTime ? Object.assign({}, this.selectedTime) : null; } /** * Clear current selection */ clearSelection() { const previousTime = this.selectedTime; this.selectedTime = null; if (previousTime) { this.dispatchTimeChangeEvent(null, previousTime); this.host.requestUpdate(); } } /** * Check if a time is currently selected */ isTimeSelected(time) { if (!this.selectedTime) { return false; } return TimeUtils.isTimeEqual(time, this.selectedTime); } /** * Set time without triggering events (for internal use) */ setTimeInternal(time) { this.selectedTime = time ? Object.assign({}, time) : null; } /** * Update selected time with partial values */ updateTime(updates) { if (!this.selectedTime) { // Create new time with current time as base const currentTime = TimeUtils.getCurrentTime(); this.selectedTime = Object.assign(Object.assign({}, currentTime), updates); } else { // Update existing time this.selectedTime = Object.assign(Object.assign({}, this.selectedTime), updates); } if (this.host.validateTime(this.selectedTime)) { this.dispatchTimeChangeEvent(this.selectedTime, null); this.host.requestUpdate(); } } /** * Increment time component */ incrementTime(component, step = 1) { if (!this.selectedTime) { this.selectedTime = TimeUtils.getCurrentTime(); } let newTime; switch (component) { case 'hours': newTime = TimeUtils.addTime(this.selectedTime, step, 0, 0); break; case 'minutes': newTime = TimeUtils.addTime(this.selectedTime, 0, step, 0); break; case 'seconds': newTime = TimeUtils.addTime(this.selectedTime, 0, 0, step); break; default: return; } this.selectTime(newTime); } /** * Decrement time component */ decrementTime(component, step = 1) { this.incrementTime(component, -step); } /** * Set time from formatted string */ setTimeFromString(timeString) { const config = this.host.getConfig(); const parsedTime = TimeUtils.parseTimeString(timeString, config.format); if (parsedTime && this.host.validateTime(parsedTime)) { this.selectTime(parsedTime); return true; } return false; } /** * Get formatted time string */ getFormattedTime() { if (!this.selectedTime) { return ''; } return this.host.formatTime(this.selectedTime); } /** * Dispatch time change event */ dispatchTimeChangeEvent(newTime, previousTime) { const hostElement = this.host; if (!hostElement.dispatchEvent) { return; } const detail = { value: newTime ? this.host.formatTime(newTime) : '', timeValue: newTime, previousValue: previousTime ? this.host.formatTime(previousTime) : '', previousTimeValue: previousTime, }; const event = new CustomEvent(TIME_PICKER_EVENTS.TIME_CHANGE, { detail, bubbles: true, composed: true, }); hostElement.dispatchEvent(event); } /** * Reset to initial/default time */ reset() { this.clearSelection(); } /** * Set time to current time */ setToCurrentTime() { const currentTime = TimeUtils.getCurrentTime(); this.selectTime(currentTime); } /** * Round current time to nearest interval */ roundToInterval(intervalMinutes) { if (!this.selectedTime) { return; } const roundedTime = TimeUtils.roundToInterval(this.selectedTime, intervalMinutes); this.selectTime(roundedTime); } } //# sourceMappingURL=selection.controller.js.map