all files / src/domUtil/ day-range.ts

22.64% Statements 12/53
0% Branches 0/14
12.5% Functions 1/8
21.15% Lines 11/52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                            
import provide from '../util/provide';
import $ = require('jquery');
import 'jquery-ui';
 
let nm = provide('domUtil');
 
export class DayRange {
    _workingDayRange: number;
    _$startDate: JQuery;
    _$endDate: JQuery;
    _maxDateRange: number;
    _startDate: Date;
    _endDate: Date;
 
    /**
     * constructor for the date range
     * @param {number} dayRange number of days
     * @param {jQuery|HTMLElement|*} jQueryRef reference to the jquery element
     */
    constructor(jQueryRef: JQuery, dayRange: number) {
        this._workingDayRange = dayRange - 1;
 
        let pickerHtml = '<label for="start-date" style="width: 78px; display: inline-block; margin:5px;">Start Date</label>' +
            '<input type="text" readonly id="start-date" class="date-pick"  style="width: 90px;">' +
            '<br><label for="end-date" style="width: 78px; display: inline-block;  margin:5px;">End Date</label>' +
            '<input type="text" readonly id="end-date" class="date-pick" style="width: 90px;">';
 
        jQueryRef.append(pickerHtml);
 
        this._$startDate = $('#start-date');
        this._$endDate = $('#end-date');
 
        this._$startDate.datepicker();
        this._$endDate.datepicker();
 
        this._startDate = null;
        this._endDate = null;
 
        let dte1 = new Date();
        dte1.setHours(0, 0, 0, 0);
        let dte2 = new Date(dte1.getTime());
        dte2.setDate(dte2.getDate() + dayRange);
        dte2.setHours(23, 59, 59, 0);
        this._maxDateRange = dte2.getTime() - dte1.getTime();
 
        let _this = this;
 
        //add event listeners
        this._$startDate.change(function () {
            _this.startDate = this.value;
        });
 
        this._$endDate.change(function () {
            _this.endDate = this.value;
        });
 
        // initialize
        this.endDate = new Date();
    }
 
    get startDate(): Date {
        return this._startDate;
    }
 
    /**
     *
     * @param val
     */
    set startDate(val: Date) {
        if (typeof val == 'string') {
            val = new Date(val as string);
        }
 
        this._startDate = val;
        this._startDate.setHours(0, 0, 0, 0);
        this._$startDate.val(this._startDate.toLocaleDateString());
 
        if (
            this.endDate == null ||
            this._endDate.getTime() - this._startDate.getTime() > this._maxDateRange ||
            this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
            let tmpDate = new Date(this._startDate.getTime());
            tmpDate.setDate(tmpDate.getDate() + this._workingDayRange);
            this.endDate = new Date(tmpDate.getTime());
        }
    }
 
    get endDate(): Date {
        return this._endDate;
    }
 
 
    set endDate(val: Date) {
        if (typeof val == 'string') {
            val = new Date(val as string);
        }
 
        this._endDate = val;
        this._endDate.setHours(23, 59, 59, 0);
        this._$endDate.val(this._endDate.toLocaleDateString());
        if (this._startDate == null || this._endDate.getTime() - this.startDate.getTime() > this._maxDateRange || this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
            let tmpDate = new Date(this._endDate.getTime());
            tmpDate.setDate(tmpDate.getDate() - this._workingDayRange);
            this.startDate = new Date(tmpDate.getTime());
        }
    }
}
 
nm.DayRange = DayRange;
 
export default DayRange;