2017-06-20 06:17:42 +00:00
|
|
|
import { Input, Component, Inject } from 'ng-metadata/core';
|
2017-04-05 18:01:55 +00:00
|
|
|
import * as moment from "moment";
|
|
|
|
|
2017-06-20 06:17:42 +00:00
|
|
|
|
2017-04-05 18:01:55 +00:00
|
|
|
/**
|
|
|
|
* A component that allows for selecting a time duration.
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'duration-input',
|
|
|
|
templateUrl: '/static/js/directives/ui/duration-input/duration-input.component.html'
|
|
|
|
})
|
|
|
|
export class DurationInputComponent implements ng.IComponentController {
|
2017-06-20 06:17:42 +00:00
|
|
|
|
2017-04-05 18:01:55 +00:00
|
|
|
@Input('<') public min: string;
|
|
|
|
@Input('<') public max: string;
|
|
|
|
@Input('=?') public value: string;
|
|
|
|
@Input('=?') public seconds: number;
|
|
|
|
|
|
|
|
private min_s: number;
|
|
|
|
private max_s: number;
|
|
|
|
|
2017-06-20 06:17:42 +00:00
|
|
|
constructor(@Inject('$scope') private $scope: ng.IScope) {
|
2017-04-05 18:01:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public $onInit(): void {
|
|
|
|
// TODO: replace this.
|
|
|
|
this.$scope.$watch(() => this.seconds, this.updateValue.bind(this));
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
public $onChanges(changes: ng.IOnChangesObject): void {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateValue(): void {
|
2017-06-20 06:17:42 +00:00
|
|
|
this.value = `${this.seconds}s`;
|
2017-04-05 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private refresh(): void {
|
|
|
|
this.min_s = this.toSeconds(this.min || '0s');
|
|
|
|
this.max_s = this.toSeconds(this.max || '1h');
|
|
|
|
|
|
|
|
if (this.value) {
|
2017-06-20 06:17:42 +00:00
|
|
|
this.seconds = this.toSeconds(this.value || '0s');
|
|
|
|
}
|
2017-04-05 18:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private durationExplanation(durationSeconds: string): string {
|
|
|
|
return moment.duration(parseInt(durationSeconds), 's').humanize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private toSeconds(durationStr: string): number {
|
|
|
|
var number = durationStr.substring(0, durationStr.length - 1);
|
|
|
|
var suffix = durationStr.substring(durationStr.length - 1);
|
|
|
|
return moment.duration(parseInt(number), <moment.unitOfTime.Base>suffix).asSeconds();
|
|
|
|
}
|
|
|
|
}
|