Skip to content
Snippets Groups Projects
DateRange.js 4.68 KiB
Newer Older
glennvorhes's avatar
glennvorhes committed
"use strict";
Glenn Vorhes's avatar
Glenn Vorhes committed
/**
 * Created by glenn on 6/12/2017.
 */
Object.defineProperty(exports, "__esModule", { value: true });
Glenn Vorhes's avatar
Glenn Vorhes committed
const React = require("react");
Glenn Vorhes's avatar
Glenn Vorhes committed
require("jquery-ui");
Glenn Vorhes's avatar
Glenn Vorhes committed
const makeGuid_1 = require("../util/makeGuid");
const fixDate = require("./helpers/dateFormat");
const DatePick_1 = require("./DatePick");
Glenn Vorhes's avatar
Glenn Vorhes committed
function stringToDate(dte) {
    if (dte.getTime) {
        return dte;
    }
    else {
        return new Date(dte);
    }
}
Glenn Vorhes's avatar
Glenn Vorhes committed
class DateRange extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.startId = makeGuid_1.default();
        this.endId = makeGuid_1.default();
        this.versionTwoStart = new Date(2017, 1, 1);
        this.maxRange = Math.round(this.props.maxRange) - 1;
        this.minRange = typeof this.props['minRange'] == 'number' ? Math.round(this.props['minRange']) : 1;
        if (this.minRange > this.maxRange) {
Glenn Vorhes's avatar
Glenn Vorhes committed
            throw "DateRange component: Max range must be greater than min range";
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        if (this.props.initialEnd) {
            this.end = stringToDate(this.props.initialEnd);
Glenn Vorhes's avatar
Glenn Vorhes committed
        }
        else {
Glenn Vorhes's avatar
Glenn Vorhes committed
            this.end = new Date();
Glenn Vorhes's avatar
Glenn Vorhes committed
        }
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.end.setHours(0, 0, 0);
        this.start = new Date(this.end.getTime());
        this.start.setDate(this.start.getDate() - this.maxRange);
        this.setNumDays();
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    setNumDays() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.numDays = Math.round((this.end.getTime() - this.start.getTime()) / (1000 * 60 * 60 * 24)) + 1;
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    componentDidMount() {
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.startInput = document.getElementById(this.startId);
        this.endInput = document.getElementById(this.endId);
        this.props.callback(this.start, this.end, this.version);
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    get needReset() {
        return this.numDays > this.maxRange || this.numDays < this.minRange;
    }
    get versionSpan() {
        if (this.start < this.versionTwoStart && this.end >= this.versionTwoStart) {
            return true;
        }
        else if (fixDate.dateToString(this.versionTwoStart) === fixDate.dateToString(this.end) && this.start < this.versionTwoStart) {
            return true;
        }
        return false;
    }
    get version() {
        if (fixDate.dateToString(this.start) == fixDate.dateToString(this.versionTwoStart)) {
            return 2;
        }
        else if (this.start >= this.versionTwoStart) {
            return 2;
        }
        return 1;
    }
    finalizeChange() {
        if (this.props.npmrds) {
            if (this.versionSpan) {
                this.start = this.previousStart;
                this.end = this.previousEnd;
                this.startInput.value = fixDate.dateToString(this.start);
                this.endInput.value = fixDate.dateToString(this.end);
                this.setNumDays();
                alert("Start and End dates must not span version break: " + fixDate.dateToString(this.versionTwoStart));
                return;
            }
        }
        this.props.callback(this.start, this.end, this.version);
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    setStart(s) {
glennvorhes's avatar
glennvorhes committed
        this.previousStart = new Date(this.start.getTime());
        this.previousEnd = new Date(this.end.getTime());
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.start = s;
        this.setNumDays();
        if (this.needReset) {
glennvorhes's avatar
glennvorhes committed
            this.end = new Date(this.start.getTime());
Glenn Vorhes's avatar
Glenn Vorhes committed
            if (this.numDays > this.maxRange) {
                this.end.setDate(this.end.getDate() + this.maxRange);
            }
            else {
                this.end.setDate(this.end.getDate() + this.minRange - 1);
            }
            this.endInput.value = fixDate.dateToString(this.end);
            this.setNumDays();
        }
        this.finalizeChange();
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    setEnd(s) {
glennvorhes's avatar
glennvorhes committed
        this.previousStart = new Date(this.start.getTime());
        this.previousEnd = new Date(this.end.getTime());
Glenn Vorhes's avatar
Glenn Vorhes committed
        this.end = s;
        this.setNumDays();
        if (this.needReset) {
glennvorhes's avatar
glennvorhes committed
            this.start = new Date(this.end.getTime());
Glenn Vorhes's avatar
Glenn Vorhes committed
            if (this.numDays > this.maxRange) {
                this.start.setDate(this.start.getDate() - this.maxRange);
            }
            else {
                this.start.setDate(this.start.getDate() - this.minRange + 1);
            }
            this.startInput.value = fixDate.dateToString(this.start);
            this.setNumDays();
        }
        this.finalizeChange();
Glenn Vorhes's avatar
Glenn Vorhes committed
    }
    render() {
        return React.createElement("div", { className: "date-range" },
            React.createElement(DatePick_1.default, { id: this.startId, label: "Start", initialDate: this.start, change: (s) => { this.setStart(s); } }),
            React.createElement(DatePick_1.default, { id: this.endId, label: "End", initialDate: this.end, change: (s) => { this.setEnd(s); } }));
    }
}
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.DateRange = DateRange;
//# sourceMappingURL=DateRange.js.map