all files / base/ calendar-context.js

100% Statements 48/48
100% Branches 10/10
100% Functions 8/8
100% Lines 47/47
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   43053× 43053× 43053× 43053× 43053× 43053× 43053×   43053× 43053×   43053× 43053× 43053× 13758× 13758× 13758× 13758× 20368× 20368× 20368× 70× 70× 12× 12×     20368× 20356×       43053×   43053× 43053× 49× 49× 49× 49× 49×         49× 60×       2554753× 2554753×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var CalendarContext = (function () {
        function CalendarContext(parent, calendar) {
            this.defaultHolidays = [];
            this.sortedDefaultHolidays = [];
            this.exceptionDateSet = new Set();
            this.exceptionsRanges = [];
            this.parent = parent;
            this.calendar = calendar;
            this.initialize();
        }
        CalendarContext.prototype.initialize = function () {
            this.buildDefaultHolidays();
            this.buildExceptionsCollection();
        };
        CalendarContext.prototype.buildDefaultHolidays = function () {
            var holidays = this.calendar['propName'] === 'projectCalendar' ? this.parent.calendarModule.holidays : this.calendar.holidays;
            var overrides = this.calendar.exceptions;
            for (var i = 0; i < holidays.length; i++) {
                var holiday = holidays[i];
                var fromDate = holiday.from ? new Date(holiday.from) : new Date(holiday.to);
                var toDate = holiday.to ? new Date(holiday.to) : new Date(holiday.from);
                for (var d = new Date(fromDate); d <= toDate; d.setDate(d.getDate() + 1)) {
                    var timestamp = new Date(d).setHours(0, 0, 0, 0);
                    var isOverridden = false;
                    for (var j = 0; j < overrides.length; j++) {
                        var overrideDate = new Date(overrides[j].from).setHours(0, 0, 0, 0);
                        if (overrideDate === timestamp) {
                            isOverridden = true;
                            break;
                        }
                    }
                    if (!isOverridden) {
                        this.defaultHolidays.push(timestamp);
                    }
                }
            }
            this.sortedDefaultHolidays = this.defaultHolidays.slice().sort(function (a, b) { return a - b; });
        };
        CalendarContext.prototype.buildExceptionsCollection = function () {
            var overrides = this.calendar.exceptions;
            for (var i = 0; i < overrides.length; i++) {
                var override = overrides[i];
                var fromDate = new Date(override.from);
                var toDate = new Date(override.to);
                var id = "exception_" + i;
                this.exceptionsRanges.push({
                    id: id,
                    from: fromDate,
                    to: toDate
                });
                for (var d = new Date(fromDate); d <= toDate; d.setDate(d.getDate() + 1)) {
                    this.exceptionDateSet.add(new Date(d).setHours(0, 0, 0, 0));
                }
            }
        };
        CalendarContext.prototype.getExceptionForDate = function (date) {
            var timestamp = new Date(date.getTime()).setHours(0, 0, 0, 0);
            return this.exceptionDateSet.has(timestamp);
        };
        return CalendarContext;
    }());
    exports.CalendarContext = CalendarContext;
});