import { $gettext, $gettextInterpolate } from '../lib/gettext.js';
// Allow fieldsets to collapse
$(document).on(
'click',
'form.default fieldset.collapsable legend,form.default.collapsable fieldset legend',
function() {
$(this)
.closest('fieldset')
.toggleClass('collapsed');
}
);
// Display a visible hint that indicates how many characters the user may
// input if the element has a maxlength restriction.
$(document).on('focus', 'form.default [maxlength]:not(.no-hint)', function() {
if (!$(this).is('textarea,input') || $(this).data('length-hint') || $(this).is('[readonly],[disabled]')) {
return;
}
var width = $(this).outerWidth(true),
hint = $('
').hide(),
wrap = $('
').width(width),
timeout = null;
$(this).wrap(wrap);
hint.text($gettext('Zeichen verbleibend: '));
hint.append('');
hint.insertBefore(this);
$(this)
.focus(function() {
clearTimeout(timeout);
timeout = setTimeout(function() {
hint.finish().show('slide', { direction: 'down' }, 300);
}, 200);
})
.blur(function() {
clearTimeout(timeout);
timeout = setTimeout(function() {
hint.finish().hide('slide', { direction: 'down' }, 300);
}, 200);
})
.on('focus propertychange change keyup', function() {
var count = $(this).val().length,
max = parseInt($(this).attr('maxlength'), 10);
hint.find('.length-hint-counter').text(max - count);
});
$(this).data('length-hint', true);
setTimeout(
function() {
$(this).focus();
}.bind(this),
0
);
});
// Automatic form submission handler when a select has changed it's value.
// Due to accessibility issues, an intuitive select[onchange=form.submit()]
// leads to terrible behaviour when invoked not by mouse. The form is
// submitted upon _every_ change, including key strokes.
// Thus, we need to overwrite this behaviour. Breakdown of this solution:
//
// - Only submit when the value has actually changed
// - Always submit when pressing enter (keycode 13)
// - Always check for change on blur event
//
// - Store whether the element was activated by click event
// - If so, submit upon next change event
// - Otherwise submit when enter has been pressed
//
// Be aware: All select[onchange*="submit()"] will be rewritten to
// select.submit-upon-select and have the onchange attribute removed.
// This might lead to unexpected behaviour.
// Ensure, every .submit-upon-select has an defaultSelected option.
$(document)
.on('focus', 'select[onchange*="submit()"]', function() {
$(this)
.removeAttr('onchange')
.addClass('submit-upon-select');
})
.on('click mousedown', 'select.submit-upon-select', function(event) {
// Firefox and Chrome handle click events on selects differently,
// thus we need the mousedown event and the click event is needed for
// select2 elements. Please do not change!
$(this).data('wasClicked', true);
})
.on('change', 'select.submit-upon-select', function(event) {
// Trigger blur event if element was clicked in the beginning
if ($(this).data('wasClicked')) {
$(this).trigger('blur');
}
})
.on('focusout keyup keypress keydown select', 'select.submit-upon-select', function(event) {
var shouldSubmit = event.type === 'keyup' ? event.which === 13 : $(this).data('wasClicked'),
is_default = $('option:selected', this).prop('defaultSelected');
// Submit only if value has changed and either enter was pressed or
// select was opened by click
if (!is_default && shouldSubmit) {
if ($(this).data('formaction')) {
$(this.form).attr('action', $(this).data('formaction'));
}
$(this.form).submit();
$('option', this).prop('defaultSelected', false).filter(':selected').prop('defaultSelected', true);
return false;
}
});
STUDIP.ready((event) => {
$('.submit-upon-select', event.target).each(function() {
var has_default_selected =
$('option', this).filter(function() {
return this.defaultSelected;
}).length > 0;
if (!has_default_selected) {
$('option', this)
.first()
.prop('defaultSelected', true);
}
});
});
// Use select2 for crossbrowser compliant select styling and
// handling
$.fn.select2.amd.define('select2/i18n/de', [], function() {
return {
inputTooLong: function(e) {
var t = e.input.length - e.maximum;
return $gettext('Bitte %u Zeichen weniger eingeben').replace('%u', t);
},
inputTooShort: function(e) {
var t = e.minimum - e.input.length;
return $gettext('Bitte %u Zeichen mehr eingeben').replace('%u', t);
},
loadingMore: function() {
return $gettext('Lade mehr Ergebnisse...');
},
maximumSelected: function(e) {
var t = [
$gettext('Sie können nur %u Eintrag auswählen'),
$gettext('Sie können nur %u Einträge auswählen')
];
return t[e.maximum === 1 ? 0 : 1].replace('%u', e.maximum);
},
noResults: function() {
return $gettext('Keine Übereinstimmungen gefunden');
},
searching: function() {
return $gettext('Suche...');
}
};
});
$.fn.select2.defaults.set('language', 'de');
function createSelect2(element) {
if ($(element).data('select2')) {
return;
}
let select_classes = $(element)
.removeClass('select2-awaiting')
.attr('class'),
option = $('