|
| 1 | +export class PopupModel { |
| 2 | + element: HTMLDivElement; |
| 3 | + |
| 4 | + constructor(element: HTMLDivElement) { |
| 5 | + this.element = element; |
| 6 | + } |
| 7 | + |
| 8 | + getLabelIdByText = (labelText: string): string => { |
| 9 | + const labels = Array.from(this.element.querySelectorAll('label')); |
| 10 | + |
| 11 | + const label = labels.find((l) => l?.textContent?.trim()?.startsWith(labelText)); |
| 12 | + |
| 13 | + if (!label) { |
| 14 | + throw new Error(`Label with text "${labelText}" not found`); |
| 15 | + } |
| 16 | + |
| 17 | + const forId = label.getAttribute('for'); |
| 18 | + if (!forId) { |
| 19 | + throw new Error(`Label with text "${labelText}" has no "for" attribute`); |
| 20 | + } |
| 21 | + return forId; |
| 22 | + }; |
| 23 | + |
| 24 | + getInputByLabel = (labelText: string): HTMLInputElement => { |
| 25 | + const forId = this.getLabelIdByText(labelText); |
| 26 | + |
| 27 | + const input = this.element.querySelector(`input#${forId}`) as HTMLInputElement; |
| 28 | + |
| 29 | + if (!input) { |
| 30 | + throw new Error(`Input with id "${forId}" not found`); |
| 31 | + } |
| 32 | + |
| 33 | + return input; |
| 34 | + }; |
| 35 | + |
| 36 | + setInputValueByLabel = (labelText: string, value: string): HTMLInputElement => { |
| 37 | + const input = this.getInputByLabel(labelText); |
| 38 | + if (!input) { |
| 39 | + throw new Error(`Input with label "${labelText}" not found`); |
| 40 | + } |
| 41 | + input.value = ''; |
| 42 | + |
| 43 | + value.split('').forEach((char) => { |
| 44 | + input.value += char; |
| 45 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 46 | + input.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: char })); |
| 47 | + input.dispatchEvent(new KeyboardEvent('keypress', { bubbles: true, key: char })); |
| 48 | + input.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true, key: char })); |
| 49 | + }); |
| 50 | + |
| 51 | + input.dispatchEvent(new Event('change', { bubbles: true })); |
| 52 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 53 | + |
| 54 | + return input; |
| 55 | + }; |
| 56 | + |
| 57 | + getSwitchByName = (name: string): HTMLInputElement => { |
| 58 | + const hiddenInput = this.element.querySelector<HTMLInputElement>(`input[name=${name}]`); |
| 59 | + |
| 60 | + if (!hiddenInput) { |
| 61 | + throw new Error(`Switch with name "${name}" not found`); |
| 62 | + } |
| 63 | + |
| 64 | + return hiddenInput; |
| 65 | + }; |
| 66 | + |
| 67 | + selectRadio = (value: string): Element | null => { |
| 68 | + const group = this.element.querySelector('[role="radiogroup"]'); |
| 69 | + if (!group) throw new Error('Radiogroup not found'); |
| 70 | + |
| 71 | + const radios = Array.from(group.querySelectorAll('[role="radio"]')); |
| 72 | + |
| 73 | + const target = radios.find((radio) => { |
| 74 | + const label = radio.getAttribute('aria-label')?.trim(); |
| 75 | + const text = radio.textContent?.trim(); |
| 76 | + return label === value || text === value; |
| 77 | + }); |
| 78 | + |
| 79 | + if (!target) throw new Error(`Radio with value "${value}" not found`); |
| 80 | + |
| 81 | + radios.forEach((r) => { |
| 82 | + r.setAttribute('aria-checked', 'false'); |
| 83 | + r.classList.remove('dx-item-selected', 'dx-radiobutton-checked'); |
| 84 | + }); |
| 85 | + |
| 86 | + target.setAttribute('aria-checked', 'true'); |
| 87 | + target.classList.add('dx-item-selected', 'dx-radiobutton-checked'); |
| 88 | + |
| 89 | + target.dispatchEvent(new MouseEvent('click', { bubbles: true })); |
| 90 | + |
| 91 | + return target; |
| 92 | + }; |
| 93 | + |
| 94 | + getSelectedRadio = (): HTMLElement | null => this.element.querySelector('[role="radio"][aria-checked="true"]'); |
| 95 | + |
| 96 | + getSelectedRadioValue = (): string | null => { |
| 97 | + const selected = this.getSelectedRadio(); |
| 98 | + return selected?.getAttribute('aria-label') ?? selected?.textContent?.trim() ?? null; |
| 99 | + }; |
| 100 | + |
| 101 | + getForm = (): HTMLElement | null => this.element.querySelector('.dx-form'); |
| 102 | + |
| 103 | + getTitle = (): HTMLElement | null => document.querySelector('.dx-popup-title'); |
| 104 | + |
| 105 | + getDoneButton = (): HTMLButtonElement => { |
| 106 | + const doneButton = this.element.querySelector('.dx-button.dx-popup-done') as HTMLButtonElement; |
| 107 | + if (!doneButton) { |
| 108 | + throw new Error('Done button not found'); |
| 109 | + } |
| 110 | + return doneButton; |
| 111 | + }; |
| 112 | + |
| 113 | + getCancelButton = (): HTMLButtonElement => { |
| 114 | + const cancelButton = this.element.querySelector('.dx-button.dx-popup-cancel') as HTMLButtonElement; |
| 115 | + if (!cancelButton) { |
| 116 | + throw new Error('Cancel button not found'); |
| 117 | + } |
| 118 | + return cancelButton; |
| 119 | + }; |
| 120 | + |
| 121 | + getFormEditor = (fieldName: string): HTMLElement | null => { |
| 122 | + const form = this.getForm(); |
| 123 | + if (form === null) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + return form.querySelector(`[data-field="${fieldName}"]`); |
| 127 | + }; |
| 128 | + |
| 129 | + getEditSeriesButton = (): HTMLElement => { |
| 130 | + const editSeriesButton = document.querySelector('[aria-label="Edit series"]') as HTMLElement; |
| 131 | + if (!editSeriesButton) { |
| 132 | + throw new Error('Edit series button not found'); |
| 133 | + } |
| 134 | + return editSeriesButton; |
| 135 | + }; |
| 136 | +} |
0 commit comments