2025-04-18 11:39:58 -04:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
|
|
|
|
// Connects to data-controller="rules"
|
|
|
|
export default class extends Controller {
|
|
|
|
static targets = [
|
|
|
|
"conditionTemplate",
|
|
|
|
"conditionGroupTemplate",
|
|
|
|
"actionTemplate",
|
|
|
|
"conditionsList",
|
|
|
|
"actionsList",
|
|
|
|
"effectiveDateInput",
|
|
|
|
];
|
|
|
|
|
2025-05-13 09:34:41 -05:00
|
|
|
connect() {
|
|
|
|
// Update condition prefixes on first connection (form render on edit)
|
|
|
|
this.updateConditionPrefixes();
|
|
|
|
}
|
|
|
|
|
2025-04-18 11:39:58 -04:00
|
|
|
addConditionGroup() {
|
|
|
|
this.#appendTemplate(
|
|
|
|
this.conditionGroupTemplateTarget,
|
|
|
|
this.conditionsListTarget,
|
|
|
|
);
|
2025-05-13 09:34:41 -05:00
|
|
|
this.updateConditionPrefixes();
|
2025-04-18 11:39:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addCondition() {
|
|
|
|
this.#appendTemplate(
|
|
|
|
this.conditionTemplateTarget,
|
|
|
|
this.conditionsListTarget,
|
|
|
|
);
|
2025-05-13 09:34:41 -05:00
|
|
|
this.updateConditionPrefixes();
|
2025-04-18 11:39:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addAction() {
|
|
|
|
this.#appendTemplate(this.actionTemplateTarget, this.actionsListTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearEffectiveDate() {
|
|
|
|
this.effectiveDateInputTarget.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#appendTemplate(templateEl, listEl) {
|
|
|
|
const html = templateEl.innerHTML.replaceAll(
|
|
|
|
"IDX_PLACEHOLDER",
|
|
|
|
this.#uniqueKey(),
|
|
|
|
);
|
|
|
|
|
|
|
|
listEl.insertAdjacentHTML("beforeend", html);
|
|
|
|
}
|
|
|
|
|
|
|
|
#uniqueKey() {
|
|
|
|
return Date.now();
|
|
|
|
}
|
2025-05-13 09:34:41 -05:00
|
|
|
|
|
|
|
// Updates the prefix visibility of all conditions and condition groups
|
|
|
|
// This is also called by the rule/conditions_controller when a subcondition is removed
|
|
|
|
updateConditionPrefixes() {
|
|
|
|
const conditions = Array.from(this.conditionsListTarget.children);
|
|
|
|
let conditionIndex = 0;
|
|
|
|
|
|
|
|
conditions.forEach((condition) => {
|
|
|
|
// Only process visible conditions, this prevents conditions that are marked for removal and hidden
|
|
|
|
// from being added to the index. This is important when editing a rule.
|
|
|
|
if (!condition.classList.contains('hidden')) {
|
|
|
|
const prefixEl = condition.querySelector('[data-condition-prefix]');
|
|
|
|
if (prefixEl) {
|
|
|
|
if (conditionIndex === 0) {
|
|
|
|
prefixEl.classList.add('hidden');
|
|
|
|
} else {
|
|
|
|
prefixEl.classList.remove('hidden');
|
|
|
|
}
|
|
|
|
conditionIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2025-04-18 11:39:58 -04:00
|
|
|
}
|