Skip to content
Snippets Groups Projects
day-range.js 4.47 KiB
Newer Older
Glenn Vorhes's avatar
Glenn Vorhes committed
'use strict';
Glenn Vorhes's avatar
Glenn Vorhes committed
Object.defineProperty(exports, "__esModule", {
    value: true
});
Glenn Vorhes's avatar
Glenn Vorhes committed
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
Glenn Vorhes's avatar
Glenn Vorhes committed
var _jquery = require('../jquery/jquery');
Glenn Vorhes's avatar
Glenn Vorhes committed
var _jquery2 = _interopRequireDefault(_jquery);
Glenn Vorhes's avatar
Glenn Vorhes committed
var _provide = require('../util/provide');
Glenn Vorhes's avatar
Glenn Vorhes committed
var _provide2 = _interopRequireDefault(_provide);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Glenn Vorhes's avatar
Glenn Vorhes committed
require('jquery-ui/datepicker');
Glenn Vorhes's avatar
Glenn Vorhes committed
var nm = (0, _provide2.default)('jQueryPlugin');
Glenn Vorhes's avatar
Glenn Vorhes committed
var DayRange = function () {
Glenn Vorhes's avatar
Glenn Vorhes committed
    /**
     * constructor for the date range
     * @param {number} dayRange number of days
     * @param {jQuery} jQueryRef reference to the jquery element
     */
Glenn Vorhes's avatar
Glenn Vorhes committed
    function DayRange(dayRange, jQueryRef) {
        _classCallCheck(this, DayRange);
Glenn Vorhes's avatar
Glenn Vorhes committed
        this._workingDayRange = dayRange - 1;
Glenn Vorhes's avatar
Glenn Vorhes committed
        var 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;">';
Glenn Vorhes's avatar
Glenn Vorhes committed
        jQueryRef.append(pickerHtml);
Glenn Vorhes's avatar
Glenn Vorhes committed
        this._$startDate = (0, _jquery2.default)('#start-date');
        this._$endDate = (0, _jquery2.default)('#end-date');
Glenn Vorhes's avatar
Glenn Vorhes committed
        this._$startDate.datepicker();
        this._$endDate.datepicker();
Glenn Vorhes's avatar
Glenn Vorhes committed
        this._startDate = null;
        this._endDate = null;
Glenn Vorhes's avatar
Glenn Vorhes committed
        var dte1 = new Date();
        dte1.setHours(0, 0, 0, 0);
        var dte2 = new Date(dte1.getTime());
        dte2.setDate(dte2.getDate() + dayRange);
        dte2.setHours(23, 59, 59, 0);
        this._maxDateRange = dte2 - dte1;
Glenn Vorhes's avatar
Glenn Vorhes committed
        var _this = this;
Glenn Vorhes's avatar
Glenn Vorhes committed
        //add event listeners
        this._$startDate.change(function () {
            _this.startDate = this.value;
        });
Glenn Vorhes's avatar
Glenn Vorhes committed
        this._$endDate.change(function () {
            _this.endDate = this.value;
        });
Glenn Vorhes's avatar
Glenn Vorhes committed
        // initialize
        this.endDate = new Date().getTime();
    }
Glenn Vorhes's avatar
Glenn Vorhes committed
    _createClass(DayRange, [{
        key: 'startDate',
        get: function get() {
            return this._startDate;
        },
        set: function set(val) {
            this._startDate = new Date(val);
            this._startDate.setHours(0, 0, 0, 0);
            this._$startDate.val(this._startDate.toLocaleDateString());

            if (this.endDate == null || this._endDate - this._startDate > this._maxDateRange || this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
                var tmpDate = new Date(this._startDate.getTime());
                tmpDate.setDate(tmpDate.getDate() + this._workingDayRange);
                this.endDate = tmpDate.getTime();
Glenn Vorhes's avatar
Glenn Vorhes committed
        }
    }, {
        key: 'endDate',
        get: function get() {
            return this._endDate;
        },
        set: function set(val) {
            this._endDate = new Date(val);
            this._endDate.setHours(23, 59, 59, 0);
            this._$endDate.val(this._endDate.toLocaleDateString());
            if (this._startDate == null || this._endDate - this.startDate > this._maxDateRange || this._endDate.getTime() - this._startDate.getTime() < 24 * 60 * 60 * 1000) {
                var tmpDate = new Date(this._endDate.getTime());
                tmpDate.setDate(tmpDate.getDate() - this._workingDayRange);
                this.startDate = tmpDate.getTime();
Glenn Vorhes's avatar
Glenn Vorhes committed
    return DayRange;
}();
Glenn Vorhes's avatar
Glenn Vorhes committed
nm.DayRange = DayRange;
var jQuery = _jquery2.default;
Glenn Vorhes's avatar
Glenn Vorhes committed
/**
 * Adds day range control
 * @param {number} dayRange the number of days
 * @returns {DayRange} the day range object
 */
jQuery.fn.dayRange = function (dayRange) {
    return new DayRange(dayRange, this);
};
Glenn Vorhes's avatar
Glenn Vorhes committed
exports.default = undefined;
module.exports = exports['default'];