File

src/comps/duration-input/duration-input.component.ts

Implements

OnInit

Metadata

selector app-duration-input
styleUrls duration-input.component.css
templateUrl ./duration-input.component.html

Inputs

setDuration

Outputs

durationChange $event type: EventEmitter

Constructor

constructor()

Methods

ngOnInit
ngOnInit()
Returns : void
calcSeconds
calcSeconds()
Returns : void
increment
increment()
Returns : void
decrement
decrement()
Returns : void
notifyChanges
notifyChanges()
Returns : void
updateDisplay
updateDisplay()
Returns : void
inputTime
inputTime(e: any)
Returns : void
keyUp
keyUp(e: any)
Returns : void
setFocus
setFocus(event: any, field: any)
Returns : void
mouseDownIncrement
mouseDownIncrement()
Returns : void
mouseUpIncrement
mouseUpIncrement()
Returns : void
mouseDownDecrement
mouseDownDecrement()
Returns : void
mouseUpDecrement
mouseUpDecrement()
Returns : void
padLeft
padLeft(n: any)
Returns : void

Properties

duration
duration: number
Default value : 0
focus
focus: string
Default value : second
focusedItem
focusedItem: any
hours
hours: number
Default value : 0
hoursOutput
hoursOutput: string
Default value : 00
minutes
minutes: number
Default value : 0
minutesOutput
minutesOutput: string
Default value : 00
prevDuration
prevDuration: number
Default value : 0
seconds
seconds: number
Default value : 1
secondsOutput
secondsOutput: string
Default value : 01
timer
timer: any
import {Component, EventEmitter, Input, OnInit, Output} from "@angular/core";

@Component({
    selector: 'app-duration-input',
    templateUrl: './duration-input.component.html',
    styleUrls: ['./duration-input.component.css']
})
export class DurationInputComponent implements OnInit {
    hours = 0;
    minutes = 0;
    seconds = 1;
    hoursOutput = "00";
    minutesOutput = "00";
    secondsOutput = "01";
    focus = "second";
    focusedItem;
    timer;
    duration = 0;
    prevDuration = 0;

    @Input()
    set setDuration(i_duration: number) {
        i_duration = Math.round(i_duration);
        if (this.duration == i_duration || i_duration == -1) return;
        console.log(`>>>>>>>> setting new duration old ${this.duration} > ${i_duration}`);
        this.duration = i_duration;
        this.calcSeconds();
        this.updateDisplay();
    }

    @Output() durationChange = new EventEmitter<Object>();

    constructor() {
    }

    ngOnInit() {
        document.body.onmouseup = () => {
            clearInterval(this.timer);
        }
        this.calcSeconds();
        this.updateDisplay();
    }

    calcSeconds() {
        var totalSeconds = this.duration;
        this.hours = Math.floor(totalSeconds / 3600);
        totalSeconds -= this.hours * 3600;
        this.minutes = Math.floor(totalSeconds / 60);
        totalSeconds -= this.minutes * 60;
        this.seconds = totalSeconds;
    }

    increment() {
        // console.log('increment');
        if (this.focusedItem) {
            this.focusedItem.focus();
        }
        switch (this.focus) {
            case "hour":
                ++this.hours;
                break;
            case "minute":
                ++this.minutes;
                break;
            case "second":
                ++this.seconds;
                break;
            default:
                break;
        }
        if (this.seconds == 60) {
            this.seconds = 0;
            this.minutes++;
        }
        if (this.minutes == 60) {
            this.minutes = 0;
            this.hours++;
        }
        if (this.hours > 24) {
            this.hours = 0;
        }
        this.updateDisplay();
    }

    decrement() {
        // console.log('decrement');
        switch (this.focus) {
            case "hour":
                if (--this.hours < 0) {
                    this.hours = 24;
                }
                break;
            case "minute":
                if (this.minutes == 0 && this.hours == 0) break;
                if (--this.minutes == -1) {
                    this.minutes = 59;
                    this.hours--;
                }
                break;
            case "second":
                if (this.seconds == 0 && this.minutes == 0 && this.hours == 0) break;
                if (--this.seconds == -1) {
                    this.seconds = 59;
                    if (--this.minutes == -1) {
                        this.minutes = 59;
                        this.hours--;
                    }
                }
                break;
            default:
                break;
        }
        this.updateDisplay();
    }

    notifyChanges() {
        const newDuration = this.hours * 60 * 60 + this.minutes * 60 + this.seconds;
        if (newDuration != this.prevDuration) {
            this.prevDuration = newDuration;
            // console.log('change emitted ' + newDuration);
            this.durationChange.emit(newDuration);
        }

    }

    updateDisplay() {
        this.secondsOutput = this.padLeft(this.seconds);
        this.minutesOutput = this.padLeft(this.minutes);
        this.hoursOutput = this.padLeft(this.hours);
    }

    inputTime(e) {
        if (!/[0-9]/.test(e.key)) {
            e.preventDefault();
        } else {

        }
    }

    keyUp(e) {
        this.seconds = +this.secondsOutput;
        this.minutes = +this.minutesOutput;
        this.hours = +this.hoursOutput;
        if (!this.hours) {
            this.hours = 0;
        }
        if (this.hours > 24) {
            this.hours = 24;
        }

        if (!this.minutes) {
            this.minutes = 0;
        }
        if (this.minutes > 59) {
            this.minutes = 59;
        }

        if (!this.seconds) {
            this.seconds = 0;
        }
        if (this.seconds > 59) {
            this.seconds = 59;
        }
        this.updateDisplay();
    }

    setFocus(event, field) {
        this.focus = field;
        this.focusedItem = event.target;
    }

    mouseDownIncrement() {
        this.increment();
        // this.timer = setInterval(() => this.increment(), 150);
    }

    mouseUpIncrement() {
        // clearInterval(this.timer);
    }

    mouseDownDecrement() {
        this.decrement();
        // this.timer = setInterval(() => this.decrement(), 150);
    }

    mouseUpDecrement() {
        clearInterval(this.timer);
    }

    padLeft(n) {
        n = n.toString();
        n = "00".substring(0, 2 - n.length) + "" + n.toString();
        n = n.substring(n.length - 2);
        return n;
    }


}
<div class="wrap">
    <div (mouseleave)="notifyChanges()">
        <div style="float: left">
            <input type="text" name="hour" [(ngModel)]="hoursOutput" (focus)="setFocus($event, 'hour')" (keypress)="inputTime($event)" (keyup)="keyUp($event)" />
            <input type="text" name="minute" [(ngModel)]="minutesOutput" (focus)="setFocus($event, 'minute')" (keypress)="inputTime($event)" (keyup)="keyUp($event)" />
            <input type="text" name="second" [(ngModel)]="secondsOutput" (focus)="setFocus($event, 'second')" (keypress)="inputTime($event)" (keyup)="keyUp($event)" />
        </div>
        <div style="float: left">
            <a style="display: block" (mousedown)="mouseDownIncrement()" (mouseup)="mouseUpIncrement()">
                <i class="fa fa-plus"></i>
            </a>
            <a style="display: block" (mousedown)="mouseDownDecrement()" (mouseup)="mouseUpDecrement()">
                <i class="fa fa-minus"></i>
            </a>
        </div>

    </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""