Skip to content

Commit

Permalink
Fix visa disabling process: now visa validates only current title lev…
Browse files Browse the repository at this point in the history
…el 1
  • Loading branch information
maatinito committed Oct 25, 2023
1 parent 033cc67 commit eb93a09
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions app/javascript/controllers/visa_controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApplicationController } from '~/controllers/application_controller';
import { ApplicationController } from "~/controllers/application_controller";

Check failure on line 1 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"~/controllers/application_controller"` with `'~/controllers/application_controller'`

export class VisaController extends ApplicationController {
connect(): void {
this.on('change', () => this.debounce(this.load, 200));
this.on("change", () => this.debounce(this.load, 200));

Check failure on line 5 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"change"` with `'change'`
const target = this.element as HTMLInputElement;
if (target.checked) {
this.updateVisibilityOfAboveFields(target);
Expand All @@ -11,44 +11,59 @@ export class VisaController extends ApplicationController {
}
}

static CHAMP_SELECTOR = '.editable-champ';
static REPETITION = 'editable-champ-repetition';
static CHAMP_SELECTOR = ".editable-champ";

Check failure on line 14 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `".editable-champ"` with `'.editable-champ'`
static REPETITION = "editable-champ-repetition";

Check failure on line 15 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"editable-champ-repetition"` with `'editable-champ-repetition'`
static CHECKED_VISA_SELECTOR = "input[data-controller='visa']:checked";
static CHILD_VISA_SELECTOR =
':scope > div > ' + VisaController.CHECKED_VISA_SELECTOR;
":scope > div > " + VisaController.CHECKED_VISA_SELECTOR;

Check failure on line 18 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `":scope·>·div·>·"` with `':scope·>·div·>·'`

private updateVisibilityOfAboveFields(visa: HTMLInputElement) {
if (visa == null) return;

let champ: Element | null | undefined;
champ = visa.closest(VisaController.CHAMP_SELECTOR);
while ((champ = champ?.previousElementSibling) != null) {
this.updateVisibility(champ, visa.checked);
let element: Element | null | undefined;
element = visa.closest(VisaController.CHAMP_SELECTOR);
while ((element = element?.previousElementSibling) != null) {
if (this.isTitle(element)) {
if (this.isSubTitle(element)) {
element = element.parentElement;
}
} else { // champ

Check failure on line 30 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Insert `⏎·······`
this.updateVisibility(element, visa.checked);

if (
!visa.checked &&
champ.classList.contains(VisaController.REPETITION)
) {
this.updateVisibilityOfRepetition(champ);
} else if (this.checked_visa(champ)) {
break;
if (!visa.checked && this.isRepetition(element)) {
this.updateVisibilityOfRepetition(element);
} else if (this.checked_visa(element)) {
break;
}
}
}
}

private isTitle(element: Element) {
return element.tagName === "LEGEND";

Check failure on line 43 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"LEGEND"` with `'LEGEND'`
}

private isSubTitle(champ: Element) {
return champ.children.length > 0 && champ.children[0].tagName !== "H2";

Check failure on line 47 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"H2"` with `'H2'`
}

private isRepetition(champ: Element) {
return champ.classList.contains(VisaController.REPETITION);
}

private updateVisibilityOfRepetition(champ: Element) {
champ
.querySelectorAll<HTMLInputElement>(VisaController.CHECKED_VISA_SELECTOR)
.forEach((node) => this.updateVisibilityOfAboveFields(node));
}

private updateVisibility(champ: Element, checked: boolean) {
const visibility = checked ? 'hidden' : 'visible';
const visibility = checked ? "hidden" : "visible";

Check failure on line 61 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"hidden"·:·"visible"` with `'hidden'·:·'visible'`
champ
.querySelectorAll<HTMLInputElement>('input, select, button, textarea')
.querySelectorAll<HTMLInputElement>("input, select, button, textarea")

Check failure on line 63 in app/javascript/controllers/visa_controller.ts

View workflow job for this annotation

GitHub Actions / Linters

Replace `"input,·select,·button,·textarea"` with `'input,·select,·button,·textarea'`
.forEach((node) => (node.disabled = checked));
champ
.querySelectorAll<HTMLInputElement>('a.fr-btn')
.querySelectorAll<HTMLInputElement>("a.fr-btn")
.forEach((node) => (node.style.visibility = visibility));
}

Expand Down

0 comments on commit eb93a09

Please sign in to comment.