aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/calendar.js
blob: e36988b2b60c69b9e6848f6bfe5e20dca2a139a1 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { $gettext } from '../lib/gettext';
import eventBus from "./event-bus.ts";

eventBus.on('studip:set-locale', () => {
    Calendar.day_names = [
        $gettext('Montag'),
        $gettext('Dienstag'),
        $gettext('Mittwoch'),
        $gettext('Donnerstag'),
        $gettext('Freitag'),
        $gettext('Samstag'),
        $gettext('Sonntag'),
    ];
});

/* ------------------------------------------------------------------------
 * calendar gui
 * ------------------------------------------------------------------------ */
const Calendar = {
    cell_height: 20,
    the_entry_content: null,
    entry: null,
    click_start_hour: -1,
    click_entry: null,
    click_in_progress: false,

    day_names: [
        'Montag',
        'Dienstag',
        'Mittwoch',
        'Donnerstag',
        'Freitag',
        'Samstag',
        'Sonntag',
    ],

    /**
     * this function is called, whenever an existing entry in the
     * calendar is clicked. It calls the passed function with the
     * calculcate id of the clicked element
     *
     * @param  object  a function or a reference to a function
     * @param  object  the element in the dom, that has been clicked
     * @param  object  the click-event itself
     */
    clickEngine: function(func, target, event) {
        event.cancelBubble = true;
        var id = jQuery(target).parent()[0].id;
        id = id.substr(id.lastIndexOf('_') + 1);
        func(id);
    },

    /**
     * check, that the submited input-field cotains of a valid hour
     *
     * @param  object  the input-element to check
     */
    validateHour: function(element) {
        var hour = parseInt(jQuery(element).val(), 10);

        if (hour > 23) {
            hour = 23;
        }
        if (hour < 0 || isNaN(hour)) {
            hour = 0;
        }

        jQuery(element).val(hour);
    },

    /**
     * check, that the submited input-field cotains of a valid minute
     *
     * @param  object  the input-element to check
     */
    validateMinute: function(element) {
        var minute = parseInt(jQuery(element).val(), 10);

        if (minute > 59) {
            minute = 59;
        }
        if (minute < 0 || isNaN(minute)) {
            minute = 0;
        }

        jQuery(element).val(minute);
    },

    /**
     * checks if at least one day is selected
     *
     * @return: bool true if selected days > 0
     */
    validateNumberOfDays: function() {
        var days = $("input[name='days[]']:checked")
            .map(function() {
                return $(this).val();
            })
            .get();
        if (days.length === 0) {
            jQuery('.settings > span[class=invalid_message]').show();
            return false;
        } else {
            return true;
        }
    },

    /**
     * check, that the submitted input-fields contain a valid time-range
     *
     * @param  object  the input-element to check (start-hour)
     * @param  object  the input-element to check (start-minute)
     * @param  object  the input-element to check (end-hour)
     * @param  object  the input-element to check (end-minute)
     *
     * @return: bool true if valid time-range, false otherwise
     */
    checkTimeslot: function(start_hour, start_minute, end_hour, end_minute) {
        if (
            parseInt(start_hour.val(), 10) * 100 + parseInt(start_minute.val(), 10) >=
            parseInt(end_hour.val(), 10) * 100 + parseInt(end_minute.val(), 10)
        ) {
            return false;
        }

        return true;
    },

    parseDateFromString: function(date_string) {
        if (!date_string) {
            //Nothing that can be done.
            return null;
        }
        let string_parts = date_string.split(' ');
        if (string_parts.length !== 2) {
            //Invalid format.
            return null;
        }
        let date_parts = string_parts[0].split('.');
        if (date_parts.length !== 3) {
            //Invalid format.
            return null;
        }
        let time_parts = string_parts[1].split(':');
        if (time_parts.length !== 2) {
            //Invalid format.
            return null;
        }
        let date = new Date(
            parseInt(date_parts[2]),
            parseInt(date_parts[1]) - 1,
            parseInt(date_parts[0])
        );
        date.setHours(parseInt(time_parts[0]));
        date.setMinutes(parseInt(time_parts[1]));
        date.setSeconds(0);

        return date;
    }
};

export default Calendar;