80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<script>
|
|
(function ($) {
|
|
function toggleBlocks() {
|
|
var t = $('#entry_type').val();
|
|
var isIng = t === 'ingredient';
|
|
$('#stock-entry-ingredient-block').toggle(isIng);
|
|
$('#stock-entry-quality-block').toggle(isIng);
|
|
$('#stock-entry-packaging-block').toggle(!isIng);
|
|
$('#price-per-kg-block').toggle(isIng);
|
|
$('#price-total-block').toggle(!isIng);
|
|
}
|
|
|
|
function initIngredientSelect2() {
|
|
var $el = $('#ingredient_id');
|
|
if ($el.data('select2')) {
|
|
$el.select2('destroy');
|
|
}
|
|
$el.select2({
|
|
theme: 'default',
|
|
width: '100%',
|
|
placeholder: '{{ __('Inhaltsstoff suchen…') }}',
|
|
allowClear: true,
|
|
ajax: {
|
|
url: $el.data('search-url'),
|
|
dataType: 'json',
|
|
delay: 250,
|
|
data: function (params) {
|
|
return {q: params.term || ''};
|
|
},
|
|
processResults: function (data) {
|
|
return {results: data.results || []};
|
|
},
|
|
cache: true
|
|
},
|
|
minimumInputLength: 1
|
|
});
|
|
}
|
|
|
|
function initPackagingSelect2() {
|
|
var $el = $('#packaging_item_id');
|
|
if ($el.data('select2')) {
|
|
$el.select2('destroy');
|
|
}
|
|
$el.select2({
|
|
theme: 'default',
|
|
width: '100%',
|
|
placeholder: '{{ __('Verpackungsartikel suchen…') }}',
|
|
allowClear: true,
|
|
ajax: {
|
|
url: $el.data('search-url'),
|
|
dataType: 'json',
|
|
delay: 250,
|
|
data: function (params) {
|
|
return {
|
|
q: params.term || '',
|
|
entry_type: $('#entry_type').val()
|
|
};
|
|
},
|
|
processResults: function (data) {
|
|
return {results: data.results || []};
|
|
},
|
|
cache: true
|
|
},
|
|
minimumInputLength: 0
|
|
});
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
toggleBlocks();
|
|
initIngredientSelect2();
|
|
initPackagingSelect2();
|
|
|
|
$('#entry_type').on('change', function () {
|
|
toggleBlocks();
|
|
$('#packaging_item_id').val(null).trigger('change');
|
|
initPackagingSelect2();
|
|
});
|
|
});
|
|
})(jQuery);
|
|
</script>
|