Add some comments

This commit is contained in:
2020-05-07 01:52:24 +03:00
parent ce22ceb808
commit 76a52f0c9f
5 changed files with 31 additions and 7 deletions
+4
View File
@@ -10,6 +10,10 @@ import {
import { ClassInfo, classMap } from 'lit-html/directives/class-map'; import { ClassInfo, classMap } from 'lit-html/directives/class-map';
import { Period } from '../types'; import { Period } from '../types';
/**
* Renders a Time Period selector with an input for a value and two arrows for step-chaning
* the value up and down.
*/
@customElement('time-period') @customElement('time-period')
export class TimePeriodComponent extends LitElement { export class TimePeriodComponent extends LitElement {
static readonly EVENT_TOGGLE = 'toggle'; static readonly EVENT_TOGGLE = 'toggle';
+3
View File
@@ -10,6 +10,9 @@ import {
import { TimeUnit } from '../models/time-unit'; import { TimeUnit } from '../models/time-unit';
import { Direction } from '../types'; import { Direction } from '../types';
/**
* Renders an AM/PM picker.
*/
@customElement('time-unit') @customElement('time-unit')
export class TimeUnitComponent extends LitElement { export class TimeUnitComponent extends LitElement {
static readonly EVENT_UPDATE = 'update'; static readonly EVENT_UPDATE = 'update';
+3
View File
@@ -2,6 +2,9 @@ import { DEFAULT_HOUR_STEP } from '../const';
import { HourMode } from '../types'; import { HourMode } from '../types';
import { TimeUnit } from './time-unit'; import { TimeUnit } from './time-unit';
/**
* Represents the hour value of a datetime.
*/
export class Hour extends TimeUnit { export class Hour extends TimeUnit {
private static readonly VALUE_LIMIT = 24; private static readonly VALUE_LIMIT = 24;
+3
View File
@@ -1,6 +1,9 @@
import { DEFAULT_MINUTE_STEP } from '../const'; import { DEFAULT_MINUTE_STEP } from '../const';
import { TimeUnit } from './time-unit'; import { TimeUnit } from './time-unit';
/**
* * Represents the minute value of a datetime.
*/
export class Minute extends TimeUnit { export class Minute extends TimeUnit {
private static readonly VALUE_LIMIT = 60; private static readonly VALUE_LIMIT = 60;
+18 -7
View File
@@ -1,8 +1,11 @@
import { Direction } from '../types'; import { Direction } from '../types';
/**
* Represents a single unit of a datetime oobject - e.g. hours or minutes.
*/
export abstract class TimeUnit { export abstract class TimeUnit {
/** /**
* Return true if the valueStr can be set as a value of this instance. * Whether {@param valueStr} can be set as a value of this instance.
*/ */
protected abstract isValidString(valueStr: string): boolean; protected abstract isValidString(valueStr: string): boolean;
@@ -23,28 +26,36 @@ export abstract class TimeUnit {
return this._value; return this._value;
} }
/**
* Sets a value from {@param stringValue}, if it can be parsed and is valid.
* @param stringValue
*/
setStringValue(stringValue: string): void { setStringValue(stringValue: string): void {
if (this.isValidString(stringValue)) { if (this.isValidString(stringValue)) {
this.setValue(parseInt(stringValue)); this.setValue(parseInt(stringValue));
} }
} }
/**
* Updates the value in {@param direction} by the step size specified in the constructoor.
* @param direction
*/
stepUpdate(direction: Direction): void { stepUpdate(direction: Direction): void {
direction === Direction.UP ? this.increment() : this.decrement(); direction === Direction.UP ? this.increment() : this.decrement();
} }
increment(): void { toString(): string {
return this.value < 10 ? `0${this.value}` : this.value.toString();
}
private increment(): void {
this.setValue(this.value + this._step); this.setValue(this.value + this._step);
} }
decrement(): void { private decrement(): void {
this.setValue(this.value - this._step); this.setValue(this.value - this._step);
} }
toString(): string {
return this.value < 10 ? `0${this.value}` : this.value.toString();
}
protected setValue(newValue: number): void { protected setValue(newValue: number): void {
if (newValue >= this._limit || newValue < 0) { if (newValue >= this._limit || newValue < 0) {
newValue = (newValue + this._limit) % this._limit; newValue = (newValue + this._limit) % this._limit;