aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/schedule.js
blob: b7c9d3700345717a7075871513e6d4f20b036ec8 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { $gettext } from './gettext.js';
import Calendar from './calendar.js';
import Dialog from './dialog.js';

const Schedule = {
    inst_changed: false,

    /**
     * this function is called, when an entry shall be created in the calendar
     *
     * @param  object  the empty entry in the calendar
     * @param  int     the day that has been clicked
     * @param  int     the start-hour that has been clicked
     * @param  int     the end-hour that has been chosen
     */
    newEntry: function(entry, day, start_hour, end_hour) {
        /*
        // do not allow creation of new entry, if one of the following popups is visible!
        if (jQuery('#edit_sem_entry').is(':visible') ||
            jQuery('#edit_entry').is(':visible') ||
            jQuery('#edit_inst_entry').is(':visible')) {
            jQuery(entry).remove();
            return;
        }
        */

        // if there is already an entry set, kick him first before showing a new one
        if (this.entry) {
            jQuery(this.entry).fadeOut('fast');
            jQuery(this.entry).remove();
        }

        this.entry = entry;

        if (!Schedule.new_entry_template) {
            jQuery.get(STUDIP.URLHelper.getURL('dispatch.php/calendar/schedule/entry'), function(data) {
                Schedule.new_entry_template = data;
                Schedule.showEntryDialog(Schedule.new_entry_template, day, start_hour, end_hour);
            });
        } else {
            Schedule.showEntryDialog(Schedule.new_entry_template, day, start_hour, end_hour);
        }
    },

    /**
     * this function is called, when an entry shall be created in the calendar
     * and the template-data has been loaded
     *
     * @param  string  the html for the new-entry dialog
     * @param  int     the day that has been clicked
     * @param  int     the start-hour that has been clicked
     * @param  int     the end-hour that has been chosen
     */
    showEntryDialog: function(template, day, start_hour, end_hour) {
        // do not open dialog, if no new-entry-marker is present
        if ($('#schedule_entry_new').length === 0) return;

        Dialog.show(template, {
            title: $gettext('Neuen Termin eintragen'),
            origin: this
        });

        $(this).on('dialog-close', function() {
            $('#schedule_entry_new').remove();
        });

        // fill values of overlay
        jQuery('input[name=entry_start]').val(start_hour + ':00');
        jQuery('input[name=entry_end]').val(end_hour + ':00');
        jQuery('select[name=entry_day]').val(parseInt(day) + 1);
    },

    /**
     * this function morphs from the quick-add box for adding a new entry to the schedule
     * to the larger box with more details to edit
     *
     * @return: void
     */
    showDetails: function() {
        // set the values for detailed view
        jQuery('select[name=entry_day]').val(Number(jQuery('#new_entry_day').val()) + 1);
        jQuery('input[name=entry_start_hour]').val(parseInt(jQuery('#new_entry_start_hour').val(), 10));
        jQuery('input[name=entry_start_minute]').val('00');
        jQuery('input[name=entry_end_hour]').val(parseInt(jQuery('#new_entry_end_hour').val(), 10));
        jQuery('input[name=entry_end_minute]').val('00');

        jQuery('input[name=entry_title]').val(jQuery('#entry_title').val());
        jQuery('textarea[name=entry_content]').val(jQuery('#entry_content').val());

        jQuery('#edit_entry_drag').html(jQuery('#new_entry_drag').html());

        // morph to the detailed view
        jQuery('#schedule_new_entry').animate(
            {
                left: Math.floor(jQuery(window).width() / 4), // for safari
                width: '50%',
                top: '180px'
            },
            500,
            function() {
                jQuery('#edit_entry').fadeIn(400, function() {
                    // reset the box
                    jQuery('#schedule_new_entry').css({
                        display: 'none',
                        left: 0,
                        width: '400px',
                        top: 0,
                        height: '230px',
                        'margin-left': 0
                    });
                });
            }
        );
    },

    /**
     * show a popup conatining the details of the passed seminar
     * at the passed cycle
     *
     * @param  string  the seminar to be shown
     * @param  string  the cycle-id of the regular time-entry to be shown
     *                 (a seminar can have multiple of these
     */
    showSeminarDetails: function(seminar_id, cycle_id) {
        jQuery.get(
            STUDIP.URLHelper.getURL('dispatch.php/calendar/schedule/entryajax/' + seminar_id + '/' + cycle_id),
            function(data) {
                Dialog.show(data, {
                    title: $gettext('Veranstaltungsdetails')
                });
            }
        );

        Calendar.click_in_progress = false;
    },

    /**
     * show a popup with the details of a regular schedule entry with passed id
     *
     * @param  string  the id of the schedule-entry
     */
    showScheduleDetails: function(id) {
        jQuery.get(STUDIP.URLHelper.getURL('dispatch.php/calendar/schedule/entry/' + id), function(data) {
            Dialog.show(data, {
                title: $gettext('Termindetails bearbeiten')
            });
        });

        Calendar.click_in_progress = false;
    },

    /**
     * show a popup with the details of a group entry, containing several seminars
     *
     * @param  string  the id of the grouped entry to be displayed
     */
    showInstituteDetails: function(id) {
        jQuery.get(STUDIP.URLHelper.getURL('dispatch.php/calendar/schedule/groupedentry/' + id + '/true'), function(
            data
        ) {
            Dialog.show(data, {
                title: $gettext('Veranstaltungsdetails')
            });
        });

        Calendar.click_in_progress = false;
    },

    /**
     * hide a seminar-entry in the schedule (admin-version)
     *
     * @param  string  the seminar to be shown
     * @param  string  the cycle-id of the regular time-entry to be shown
     *                 (a seminar can have multiple of these
     */
    instSemUnbind: function(seminar_id, cycle_id) {
        Schedule.inst_changed = true;
        jQuery.ajax({
            type: 'GET',
            url: STUDIP.URLHelper.getURL(
                'dispatch.php/calendar/schedule/adminbind/' + seminar_id + '/' + cycle_id + '/0/true'
            )
        });

        jQuery('#' + seminar_id + '_' + cycle_id + '_hide').fadeOut('fast', function() {
            jQuery('#' + seminar_id + '_' + cycle_id + '_show').fadeIn('fast');
        });
    },

    /**
     * make a hidden seminar-entry visible in the schedule again
     *
     * @param  string  the seminar to be shown
     * @param  string  the cycle-id of the regular time-entry to be shown
     *                 (a seminar can have multiple of these
     */
    instSemBind: function(seminar_id, cycle_id) {
        Schedule.inst_changed = true;
        jQuery.ajax({
            type: 'GET',
            url: STUDIP.URLHelper.getURL(
                'dispatch.php/calendar/schedule/adminbind/' + seminar_id + '/' + cycle_id + '/1/true'
            )
        });

        jQuery('#' + seminar_id + '_' + cycle_id + '_show').fadeOut('fast', function() {
            jQuery('#' + seminar_id + '_' + cycle_id + '_hide').fadeIn('fast');
        });
    },

    /**
     * hide the popup of grouped-entry, containing a list of seminars.
     * returns true if the visiblity of one of the entries has been changed,
     * false otherwise
     *
     * @param  object  the element to be hidden
     *
     * @return  bool  true if the visibility of one seminar hase changed, false otherwise
     */
    hideInstOverlay: function(element) {
        if (Schedule.inst_changed) {
            return true;
        }
        jQuery(element).fadeOut('fast');

        Calendar.click_in_progress = false;

        return false;
    },

    /**
     * calls Calendar.checkTimeslot to check that the time is valid
     *
     * @param  bool  returns true if the time is valid, false otherwise
     */
    checkFormFields: function() {
        if (
            !Calendar.checkTimeslot(
                jQuery('#schedule_entry_hours > input[name=entry_start_hour]'),
                jQuery('#schedule_entry_hours > input[name=entry_start_minute]'),
                jQuery('#schedule_entry_hours > input[name=entry_end_hour]'),
                jQuery('#schedule_entry_hours > input[name=entry_end_minute]')
            )
        ) {
            jQuery('#schedule_entry_hours').addClass('invalid');
            jQuery('#schedule_entry_hours > span[class=invalid_message]').show();
            return false;
        }

        return true;
    }
};

export default Schedule;