Newer
Older
import { $gettext } from './lib/gettext.js';
import eventBus from "./lib/event-bus";
/**
* This file contains extensions/adjustments for jQuery UI.
*/
(function ($, STUDIP) {
$.widget( "ui.dialog", $.ui.dialog, {
_allowInteraction: function( event ) {
return hasParentWhich(isCKBodyWrapper)(event.target) || this._super( event );
},
});
function hasParentWhich(predicate) {
return function tryParent(element) {
if (!element?.parentElement) {
return false;
}
return predicate(element) || tryParent(element.parentElement);
};
}
function isCKBodyWrapper(element) {
return element?.classList?.contains('ck-body-wrapper');
}
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
/**
* Setup and refine date picker, add automated handling for .has-date-picker
* and [data-date-picker].
* Note: [date-datepicker] would be a way better selector but unfortunately
* jQuery UI's Datepicker itself stores vital data in the the "datepicker"
* data() variable, so we cannot use it and need to use "date-picker"
* instead.
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
* @since Stud.IP 3.4
*/
'use strict';
// Exit if datepicker is undefined (which it should never be)
if ($.datepicker === undefined) {
return;
}
// Exit if datetimepicker is undefined (which it should never be)
if ($.ui.timepicker === undefined) {
return;
}
// Setup Stud.IP's own datepicker extensions
STUDIP.UI = STUDIP.UI || {};
STUDIP.UI.Datepicker = {
selector: '.has-date-picker,[data-date-picker]',
// Initialize all datepickers that not yet been initialized (e.g. in dialogs)
init: function () {
$(this.selector).filter(function () {
return $(this).data('date-picker-init') === undefined;
}).each(function () {
$(this).data('date-picker-init', true).datepicker();
});
},
// Apply registered handlers. Take care: This happens upon before a
// picker is shown as well as after a date has been selected.
refresh: function () {
$(this.selector).each(function () {
var element = this,
options = $(element).data().datePicker;
if (options) {
$.each(options, function (key, value) {
if (STUDIP.UI.Datepicker.dataHandlers[key] !== undefined) {
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
STUDIP.UI.Datepicker.dataHandlers[key].call(element, value);
}
});
}
});
}
};
// Define handlers for any data-datepicker option
STUDIP.UI.Datepicker.dataHandlers = {
// Ensure this date is not later (<=) than another date by setting
// the maximum allowed date the other date.
// This will also set this date to the maximum allowed date if it
// currently later than the allowed maximum date.
'<=': function (selector, offset) {
var this_date = $(this).datepicker('getDate'),
max_date = null,
temp,
adjustment = 0;
if ($(this).data().datePicker.offset) {
temp = $(this).data().datePicker.offset;
adjustment = parseInt($(temp).val(), 10);
}
// Get max date by either actual dates or maxDate options on
// all matching elements
if (selector === 'today') {
max_date = new Date();
} else {
$(selector).each(function () {
var date = $(this).datepicker('getDate') || $(this).datepicker('option', 'maxDate');
if (date && (!max_date || date < max_date)) {
max_date = new Date(date);
}
});
}
// Set max date and adjust current date if neccessary
if (max_date) {
max_date.setTime(max_date.getTime() - (offset || 0) * 24 * 60 * 60 * 1000);
temp = new Date(max_date);
temp.setDate(temp.getDate() - adjustment);
if (this_date && this_date > max_date) {
$(this).datepicker('setDate', temp);
}
$(this).datepicker('option', 'maxDate', max_date);
} else {
$(this).datepicker('option', 'maxDate', null);
}
},
// Ensure this date is earlier (<) than another date by setting the
// maximum allowed date to the other date - 1 day.
// This will also set this date to the maximum allowed date - 1 day
// if it is currently later than the allowed maximum date.
'<': function (selector) {
STUDIP.UI.Datepicker.dataHandlers['<='].call(this, selector, 1);
},
// Ensure this date is not earlier (>=) than another date by setting
// the minimum allowed date to the other date.
// This will also set this date to the minimum allowed date if it is
// currently earlier than the allowed minimum date.
'>=': function (selector, offset) {
var this_date = $(this).datepicker('getDate'),
min_date = null,
temp,
adjustment = 0;
if ($(this).data().datePicker.offset) {
temp = $(this).data().datePicker.offset;
adjustment = parseInt($(temp).val(), 10);
}
// Get min date by either actual dates or minDate options on
// all matching elements
if (selector === 'today') {
min_date = new Date();
} else {
$(selector).each(function () {
var date = $(this).datepicker('getDate') || $(this).datepicker('option', 'minDate');
if (date && (!min_date || date > min_date)) {
min_date = new Date(date);
}
});
}
// Set min date and adjust current date if neccessary
if (min_date) {
min_date.setTime(min_date.getTime() + (offset || 0) * 24 * 60 * 60 * 1000);
temp = new Date(min_date);
temp.setDate(temp.getDate() + adjustment);
if (this_date && this_date < min_date) {
$(this).datepicker('setDate', temp);
}
$(this).datepicker('option', 'minDate', min_date);
} else {
$(this).datepicker('option', 'minDate', null);
}
},
// Ensure this date is later (>) than another date by setting the
// minimum allowed date to the other date + 1 day.
// This will also set this date to the minimum allowed date + 1 day
// if it is currently earlier than the allowed minimum date.
'>': function (selector) {
STUDIP.UI.Datepicker.dataHandlers['>='].call(this, selector, 1);
}
};
STUDIP.UI.DateTimepicker = {
selector: '.has-datetime-picker,[data-datetime-picker]',
// Initialize all datetimepickers that not yet been initialized (e.g. in dialogs)
init: function () {
$(this.selector).filter(function () {
return $(this).data('datetime-picker-init') === undefined;
}).each(function () {
$(this).data('datetime-picker-init', true).datetimepicker();
});
},
// Apply registered handlers. Take care: This happens upon before a
// picker is shown as well as after a date has been selected.
refresh: function () {
$(this.selector).each(function () {
var element = this,
options = $(element).data().datetimePicker;
if (options) {
$.each(options, function (key, value) {
if (STUDIP.UI.DateTimepicker.dataHandlers[key] !== undefined) {
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
STUDIP.UI.DateTimepicker.dataHandlers[key].call(element, value);
}
});
}
});
}
};
// Define handlers for any data-datepicker option
STUDIP.UI.DateTimepicker.dataHandlers = {
// Ensure this date is not later (<=) than another date by setting
// the maximum allowed date the other date.
// This will also set this date to the maximum allowed date if it
// currently later than the allowed maximum date.
'<=': function (selector, offset) {
var this_date = $(this).datetimepicker('getDate'),
max_date = null,
temp;
if ((offset === undefined) && $(selector).data('offset')) {
temp = $(selector).data('offset');
offset = parseInt($(temp).val(), 10);
}
// Get max date by either actual dates or maxDate options on
// all matching elements
if (selector === 'today') {
max_date = new Date();
max_date.setHours(0, 23, 59, 59);
} else {
$(selector).each(function () {
var date = $(this).datetimepicker('getDate') || $(this).datetimepicker('option', 'maxDate');
if (date && (!max_date || date < max_date)) {
max_date = new Date(date);
}
});
}
// Set max date and adjust current date if neccessary
if (max_date) {
max_date.setTime(max_date.getTime() - (offset || 0) * 24 * 60 * 60 * 1000);
if (this_date && this_date > max_date) {
$(this).datetimepicker('setDate', max_date);
}
$(this).datetimepicker('option', 'maxDate', max_date);
} else {
$(this).datetimepicker('option', 'maxDate', null);
}
},
// Ensure this date is earlier (<) than another date by setting the
// maximum allowed date to the other date - 1 day.
// This will also set this date to the maximum allowed date - 1 day
// if it is currently later than the allowed maximum date.
'<': function (selector) {
STUDIP.UI.DateTimepicker.dataHandlers['<='].call(this, selector, 1);
},
// Ensure this date is not earlier (>=) than another date by setting
// the minimum allowed date to the other date.
// This will also set this date to the minimum allowed date if it is
// currently earlier than the allowed minimum date.
'>=': function (selector, offset) {
var this_date = $(this).datetimepicker('getDate'),
min_date = null,
temp;
if ((offset === undefined) && $(selector).data('offset')) {
temp = $(selector).data('offset');
offset = parseInt($(temp).val(), 10);
}
// Get min date by either actual dates or minDate options on
// all matching elements
if (selector === 'today') {
min_date = new Date();
min_date.setHours(0, 0, 0);
} else {
$(selector).each(function () {
var date = $(this).datetimepicker('getDate') || $(this).datetimepicker('option', 'minDate');
if (date && (!min_date || date > min_date)) {
min_date = new Date(date);
}
});
}
// Set min date and adjust current date if neccessary
if (min_date) {
min_date.setTime(min_date.getTime() + (offset || 0) * 24 * 60 * 60 * 1000);
if (this_date && this_date < min_date) {
$(this).datetimepicker('setDate', min_date);
}
$(this).datetimepicker('option', 'minDate', min_date);
} else {
$(this).datetimepicker('option', 'minDate', null);
}
},
// Ensure this date is later (>) than another date by setting the
// minimum allowed date to the other date + 1 day.
// This will also set this date to the minimum allowed date + 1 day
// if it is currently earlier than the allowed minimum date.
'>': function (selector) {
STUDIP.UI.DateTimepicker.dataHandlers['>='].call(this, selector, 1);
}
};
STUDIP.UI.Timepicker = {
selector: '.has-time-picker,[data-time-picker]',
// Initialize all datetimepickers that not yet been initialized (e.g. in dialogs)
init: function () {
$(this.selector).filter(function () {
return $(this).data('time-picker-init') === undefined;
}).each(function () {
$(this).addClass('hasTimepicker').data('time-picker-init', true).timepicker();
});
},
// Apply registered handlers. Take care: This happens upon before a
// picker is shown as well as after a date has been selected.
refresh: function () {
$(this.selector).each(function () {
var element = this,
options = $(element).data().timePicker;
if (options) {
$.each(options, function (key, value) {
if (STUDIP.UI.Timepicker.dataHandlers[key] !== undefined) {
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
STUDIP.UI.Timepicker.dataHandlers[key].call(element, value);
}
});
}
});
}
};
STUDIP.UI.Timepicker.parseTime = (time) => {
const split = time.split(':');
return {
hour: parseInt(split[0], 10),
minute: parseInt(split[1], 10)
};
};
STUDIP.UI.Timepicker.createTime = (hours, minutes) => {
return ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2);
};
STUDIP.UI.Timepicker.setTime = (time) => {
const date = new Date();
const parsed = STUDIP.UI.Timepicker.parseTime(time);
date.setHours(parsed.hour);
date.setMinutes(parsed.minute);
return date;
};
// Define handlers for any data-time-picker option
// TODO: This don't work well if at all. We should probably switch to
// another (date) timepicker
STUDIP.UI.Timepicker.dataHandlers = {
// Ensure this time is not later (<=) than another time by setting
// the maximum allowed time on the other time.
// This will also set this time to the maximum allowed time if it is
// currently later than the allowed maximum time.
'<=': function (selector, offset) {
var this_time = this.value;
var max_time = null;
var temp;
if ((offset === undefined) && $(selector).data('offset')) {
temp = $(selector).data('offset');
offset = parseInt($(temp).val(), 10);
}
// Get max time by either actual times
$(selector).each(function () {
var time = this.value;
if (time && (!max_time || time < max_time)) {
max_time = time;
}
});
// Set max time and adjust current time if neccessary
if (max_time) {
const parsed = STUDIP.UI.Timepicker.parseTime(max_time);
max_time = STUDIP.UI.Timepicker.createTime(
Math.min(23, Math.max(0, parsed.hour - (offset || 0))),
parsed.minute
);
console.log('max time:', this_time, max_time);
if (this_time && this_time > max_time) {
$(this).timepicker(STUDIP.UI.Timepicker.parseTime(max_time));
}
$(this).timepicker({
maxTime: STUDIP.UI.Timepicker.setTime(max_time)
});
} else {
$(this).timepicker({
maxTime: null
});
}
},
// Ensure this date is earlier (<) than another date by setting the
// maximum allowed date to the other date - 1 day.
// This will also set this date to the maximum allowed date - 1 day
// if it is currently later than the allowed maximum date.
'<': function (selector) {
STUDIP.UI.Timepicker.dataHandlers['<='].call(this, selector, 1);
},
// Ensure this date is not earlier (>=) than another date by setting
// the minimum allowed date to the other date.
// This will also set this date to the minimum allowed date if it is
// currently earlier than the allowed minimum date.
'>=': function (selector, offset) {
var this_time = this.value;
var min_time = null;
var temp;
if ((offset === undefined) && $(selector).data('offset')) {
temp = $(selector).data('offset');
offset = parseInt($(temp).val(), 10);
}
// Get min time by either actual times
$(selector).each(function () {
var time = this.value;
if (time && (!min_time || time > min_time)) {
min_time = time;
}
});
// Set min time and adjust current time if neccessary
if (min_time) {
const parsed = STUDIP.UI.Timepicker.parseTime(min_time);
min_time = STUDIP.UI.Timepicker.createTime(
Math.min(23, Math.max(0, parsed.hour + (offset || 0))),
parsed.minute
);
console.log('min time:', this_time, min_time);
if (this_time && this_time < min_time) {
$(this).timepicker(STUDIP.UI.Timepicker.parseTime(min_time));
}
$(this).timepicker({
minTime: STUDIP.UI.Timepicker.setTime(min_time)
});
} else {
$(this).timepicker({
minTime: null
});
}
},
// Ensure this date is later (>) than another date by setting the
// minimum allowed date to the other date + 1 day.
// This will also set this date to the minimum allowed date + 1 day
// if it is currently earlier than the allowed minimum date.
'>': function (selector) {
STUDIP.UI.Timepicker.dataHandlers['>='].call(this, selector, 1);
}
};
// Apply defaults including date picker handlers
const defaults = {
beforeShow (input) {
STUDIP.UI.Datepicker.refresh();
STUDIP.UI.DateTimepicker.refresh();
STUDIP.UI.Timepicker.refresh();
if ($(input).parents('.ui-dialog').length > 0) {
return;
}
$(input).css({
'position': 'relative',
'z-index': 1002
});
},
showButtonPanel: true,
onSelect: function (value, instance) {
if (value !== instance.lastVal) {
$(this).change();
}
}
$.datepicker.setDefaults({
...defaults,
beforeShow (input) {
// Don't lose original behaviour
defaults.beforeShow(input);
if ($(input).parents('.ui-dialog').length > 0) {
$('.ui-dialog-content').bind('scroll.datepicker-scroll', _.debounce($.proxy(DpHideOnScroll, null, input), 100, {leading:true, trailing:false}));
}
$(window).bind('scroll.datepicker-scroll', _.debounce($.proxy(DpHideOnScroll, null, input), 100, {leading:true, trailing:false}));
if ($(input).closest('#sidebar').length === 0) {
return;
}
const button = input.nextElementSibling;
if (button && button.matches('input[type="submit"]')) {
button.style.position = 'relative';
button.style.zIndex = input.style.zIndex;
}
},
onClose (date, inst) {
$(this).one('click.picker', function () {
$(this).datepicker('show');
}).on('blur', function () {
$(this).off('click.picker');
});
if ($(this).parents('.ui-dialog').length > 0) {
$('.ui-dialog-content').unbind('scroll.datepicker-scroll');
} else {
$(window).unbind('scroll.datepicker-scroll');
}
}
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
});
$.timepicker.setDefaults(defaults);
eventBus.on('studip:set-locale', () => {
const locale = {
closeText: $gettext('Schließen'),
prevText: $gettext('Zurück'),
nextText: $gettext('Vor'),
currentText: $gettext('Jetzt'),
monthNames: [
$gettext('Januar'),
$gettext('Februar'),
$gettext('März'),
$gettext('April'),
$gettext('Mai'),
$gettext('Juni'),
$gettext('Juli'),
$gettext('August'),
$gettext('September'),
$gettext('Oktober'),
$gettext('November'),
$gettext('Dezember')
],
monthNamesShort: [
$gettext('Jan'),
$gettext('Feb'),
$gettext('Mär'),
$gettext('Apr'),
$gettext('Mai'),
$gettext('Jun'),
$gettext('Jul'),
$gettext('Aug'),
$gettext('Sep'),
$gettext('Okt'),
$gettext('Nov'),
$gettext('Dez')
],
dayNames: [
$gettext('Sonntag'),
$gettext('Montag'),
$gettext('Dienstag'),
$gettext('Mittwoch'),
$gettext('Donnerstag'),
$gettext('Freitag'),
$gettext('Samstag')
],
dayNamesShort: [
$gettext('So'),
$gettext('Mo'),
$gettext('Di'),
$gettext('Mi'),
$gettext('Do'),
$gettext('Fr'),
$gettext('Sa')
],
weekHeader: $gettext('Wo'),
dateFormat: 'dd.mm.yy',
firstDay: 1,
isRTL: false,
showMonthAfterYear: false,
yearSuffix: '',
changeMonth: true,
changeYear: true,
timeOnlyTitle: $gettext('Zeit wählen'),
timeText: $gettext('Zeit'),
hourText: $gettext('Stunde'),
minuteText: $gettext('Minute'),
secondText: $gettext('Sekunde'),
millisecText: $gettext('Millisekunde'),
microsecText: $gettext('Mikrosekunde'),
timezoneText: $gettext('Zeitzone'),
timeFormat: $gettext('HH:mm'),
amNames: [$gettext('vorm.'), 'AM', 'A'],
pmNames: [$gettext('nachm.'), 'PM', 'P']
};
// Set dayNamesMin to dayNamesShort since they are equal
locale.dayNamesMin = locale.dayNamesShort;
$.datepicker.setDefaults(locale);
$.timepicker.setDefaults(locale);
});
var DpHideOnScroll = function () {
var input = arguments[0];
$(input).blur();
$(input).datepicker('hide');
}
// Jump to current date when "Now" is clicked
var _gotoToday = $.datepicker._gotoToday;
$.datepicker._gotoToday = function(id) {
_gotoToday.call(this, id);
this._selectDate(id);
};
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Attach global focus handler on date picker elements
$(document).on('focus', STUDIP.UI.Datepicker.selector, () => {
STUDIP.UI.Datepicker.init();
});
// Attach global focus handler on datetime picker elements
$(document).on('focus', STUDIP.UI.DateTimepicker.selector, () => {
STUDIP.UI.DateTimepicker.init();
});
// Attach global focus handler on time picker elements
$(document).on('focus', STUDIP.UI.Timepicker.selector, (event) => {
if (!$(event.target).attr('pattern')) {
$(event.target).attr('pattern', '^[012]\\d:[0-5]\\d$');
}
STUDIP.UI.Timepicker.init();
}).on('keyup', STUDIP.UI.Timepicker.selector, (event) => {
const input = event.target;
input.value = input.value.replace(/:+$/, ':');
const value = input.value.trim();
if (value.length === 2 && event.which !== 8) {
input.value += ':';
}
}).on('blur', STUDIP.UI.Timepicker.selector, (event) => {
if (event.target.checkValidity()) {
return;
}
const input = event.target;
let value = input.value.trim();
value = value.replace(/:/g, '');
if (['0', '1', '2'].indexOf(value.substr(0, 1)) === -1) {
value = '0' + value;
}
value = (value + '00').substr(0, 4);
input.value = value.substr(0, 2) + ':' + value.substr(2, 2);
});
}(jQuery, STUDIP));