(function ($) {
'use strict';
$(function () {
var button = $('#mw-select-gallery');
var input = $('#mw-gallery-ids');
var preview = $('#mw-gallery-preview');
if (button.length && input.length && typeof wp !== 'undefined' && wp.media) {
button.on('click', function (event) {
event.preventDefault();
var frame = wp.media({
title: 'MeinWeimar-Galerie auswählen',
button: { text: 'Galerie übernehmen' },
multiple: true,
library: { type: 'image' }
});
frame.on('select', function () {
var ids = [];
frame.state().get('selection').each(function (attachment) {
ids.push(attachment.id);
});
input.val(ids.join(','));
preview.text(ids.length ? (ids.length + ' Bilder ausgewählt') : 'Noch keine Bilder ausgewählt');
});
frame.open();
});
}
var stepper = $('[data-mw-content-stepper="listing"]');
var adminConfig = window.MeinWeimarAdminEditor || {};
var categoryRules = adminConfig.categoryRules || {};
var termSlugs = adminConfig.termSlugs || {};
function selectedCategorySlugs(route) {
var rule = categoryRules[route] || {};
var taxonomy = rule.taxonomy || '';
if (!taxonomy) return [];
var slugs = termSlugs[taxonomy] || {};
var selected = [];
$('input[name^="tax_input[' + taxonomy + ']"]:checked').each(function () {
var id = String($(this).val());
if (slugs[id]) selected.push(String(slugs[id]));
});
return selected;
}
function conditionValue(condition, rule) {
var fieldMap = rule.field_map || {};
var metaKey = fieldMap[condition.field] || '';
if (!metaKey) return '';
var control = $('[data-mw-field-key="' + metaKey + '"]');
if (!control.length) return '';
var value = control.first().val();
return Array.isArray(value) ? value.map(String) : String(value || '');
}
function conditionMatches(condition, selected, rule) {
if (!condition) return true;
if (Array.isArray(condition.all)) {
return condition.all.every(function (nested) {
return conditionMatches(nested, selected, rule);
});
}
if (Array.isArray(condition.any)) {
return condition.any.some(function (nested) {
return conditionMatches(nested, selected, rule);
});
}
if (condition.field === 'category') {
if (!selected.length) return true;
var categoryValues = (condition.values || []).map(String);
return condition.operator === 'in'
? selected.some(function (slug) { return categoryValues.indexOf(slug) !== -1; })
: true;
}
var value = conditionValue(condition, rule);
var values = Array.isArray(value) ? value : [value];
values = values.map(function (entry) {
return entry === null || typeof entry === 'undefined' ? '' : String(entry).trim();
}).filter(function (entry) { return entry !== ''; });
if (condition.operator === 'equals') return values.indexOf(String(condition.value || '').trim()) !== -1;
if (condition.operator === 'in') {
var allowed = (condition.values || []).map(String);
return values.some(function (entry) { return allowed.indexOf(entry) !== -1; });
}
if (condition.operator === 'not_empty') return values.length > 0;
return true;
}
function updateCategoryFields() {
if (!stepper.length) return;
var route = String(stepper.data('mw-content-route') || '');
var rule = categoryRules[route] || {};
var selected = selectedCategorySlugs(route);
Object.keys(rule.field_conditions || {}).forEach(function (key) {
var fieldRule = rule.field_conditions[key] || {};
var conditions = fieldRule.conditions || [];
var visible = conditions.every(function (condition) {
return conditionMatches(condition, selected, rule);
});
var control = $('[data-mw-field-key="' + key + '"]');
if (!control.length) return;
var wrapper = control.closest('p');
// The server may have rendered the initial state with the HTML
// `hidden` attribute. Keep the same canonical state when a dependency
// changes; `.toggle()` alone does not clear that attribute reliably.
wrapper.toggle(visible).prop('hidden', !visible);
var required = (fieldRule.required_when || []).some(function (condition) {
return conditionMatches(condition, selected, rule) && selected.length > 0;
});
control.prop('required', visible && required);
if (control.is('select')) {
var valueConditions = fieldRule.value_conditions || {};
control.find('option').each(function () {
var option = $(this);
var optionConditions = valueConditions[option.val()] || [];
var optionVisible = optionConditions.every(function (condition) {
return conditionMatches(condition, selected, rule);
});
option.prop('hidden', !optionVisible).prop('disabled', !optionVisible);
});
if (control.find('option:selected').prop('disabled')) {
control.val('');
}
}
});
}
if (stepper.length) {
updateCategoryFields();
$(document).on('change input', 'input[name^="tax_input["], select[name^="tax_input["]', updateCategoryFields);
$(document).on('change input', '[data-mw-field-key]', updateCategoryFields);
$(document).on('click', '#category-add-submit, .tagadd', function () {
window.setTimeout(updateCategoryFields, 100);
});
}
var occurrenceBox = $('[data-mw-event-occurrences]');
if (occurrenceBox.length) {
var rows = occurrenceBox.find('[data-mw-occurrence-rows]');
var template = occurrenceBox.find('[data-mw-occurrence-template]').html() || '';
occurrenceBox.on('click', '[data-mw-add-occurrence]', function () {
var index = rows.find('[data-mw-occurrence-row]').length;
rows.append(template.replace(/__INDEX__/g, String(index)));
});
occurrenceBox.on('click', '[data-mw-remove-occurrence]', function () {
$(this).closest('[data-mw-occurrence-row]').remove();
});
}
});
}(jQuery));
