|
| 1 | +export default class extends HTMLElement { |
| 2 | + connectedCallback () { |
| 3 | + this.targetId = this.dataset.targetId |
| 4 | + this.fieldId = this.dataset.fieldId |
| 5 | + this.action = (this.dataset.action || '').trim() |
| 6 | + this.expectedValue = this.dataset.value |
| 7 | + |
| 8 | + this.targetEl = document.getElementById(this.targetId) |
| 9 | + this.sourceEl = document.getElementById(this.fieldId) |
| 10 | + |
| 11 | + this.bindListeners() |
| 12 | + |
| 13 | + this.evaluateAndApply() |
| 14 | + } |
| 15 | + |
| 16 | + disconnectedCallback () { |
| 17 | + this.unbindListeners() |
| 18 | + } |
| 19 | + |
| 20 | + bindListeners () { |
| 21 | + this.eventsFor(this.sourceEl).forEach((ev) => { |
| 22 | + this.sourceEl.addEventListener(ev, this.evaluateAndApply) |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + unbindListeners () { |
| 27 | + this.eventsFor(this.sourceEl).forEach((ev) => { |
| 28 | + this.sourceEl.removeEventListener(ev, this.evaluateAndApply) |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + eventsFor (el) { |
| 33 | + if (!el) return [] |
| 34 | + |
| 35 | + const tag = el.tagName.toLowerCase() |
| 36 | + |
| 37 | + if (tag === 'textarea') return ['input'] |
| 38 | + if (tag === 'input') return ['input', 'change'] |
| 39 | + |
| 40 | + return ['change'] |
| 41 | + } |
| 42 | + |
| 43 | + evaluateAndApply = () => { |
| 44 | + const fieldConditions = document.querySelectorAll(`field-condition[data-target-id="${this.targetId}"]`) |
| 45 | + |
| 46 | + const result = [...fieldConditions].reduce((acc, cond) => { |
| 47 | + if (cond.dataset.operation === 'or') { |
| 48 | + acc.push(acc.pop() || cond.checkCondition()) |
| 49 | + } else { |
| 50 | + acc.push(cond.checkCondition()) |
| 51 | + } |
| 52 | + |
| 53 | + return acc |
| 54 | + }, []) |
| 55 | + |
| 56 | + this.apply(!result.includes(false)) |
| 57 | + } |
| 58 | + |
| 59 | + checkCondition () { |
| 60 | + const action = this.action |
| 61 | + const actual = this.getSourceValue() |
| 62 | + const expected = this.expectedValue |
| 63 | + |
| 64 | + if (action === 'empty' || action === 'unchecked') return this.isEmpty(actual) |
| 65 | + if (action === 'not_empty' || action === 'checked') return !this.isEmpty(actual) |
| 66 | + |
| 67 | + if (action === 'equal') { |
| 68 | + const list = Array.isArray(actual) ? actual : [actual] |
| 69 | + return list.filter((v) => v !== null && v !== undefined).map(String).includes(String(expected)) |
| 70 | + } |
| 71 | + |
| 72 | + if (action === 'contains') return this.contains(actual, expected) |
| 73 | + |
| 74 | + if (action === 'not_equal') { |
| 75 | + const list = Array.isArray(actual) ? actual : [actual] |
| 76 | + return !list.filter((v) => v !== null && v !== undefined).map(String).includes(String(expected)) |
| 77 | + } |
| 78 | + |
| 79 | + if (action === 'does_not_contain') return !this.contains(actual, expected) |
| 80 | + |
| 81 | + return true |
| 82 | + } |
| 83 | + |
| 84 | + getSourceValue () { |
| 85 | + const el = this.sourceEl |
| 86 | + |
| 87 | + if (!el) return |
| 88 | + |
| 89 | + const tag = el.tagName.toLowerCase() |
| 90 | + const type = (el.getAttribute('type') || '').toLowerCase() |
| 91 | + |
| 92 | + if (tag === 'select') return el.value |
| 93 | + if (tag === 'textarea') return el.value |
| 94 | + if (tag === 'input' && type === 'checkbox') return el.checked ? (el.value || '1') : null |
| 95 | + if (tag === 'input') return el.value |
| 96 | + |
| 97 | + return el.value ?? null |
| 98 | + } |
| 99 | + |
| 100 | + isEmpty (obj) { |
| 101 | + if (obj == null) return true |
| 102 | + |
| 103 | + if (Array.isArray(obj)) { |
| 104 | + return obj.length === 0 |
| 105 | + } |
| 106 | + |
| 107 | + if (typeof obj === 'string') { |
| 108 | + return obj.trim().length === 0 |
| 109 | + } |
| 110 | + |
| 111 | + if (typeof obj === 'object') { |
| 112 | + return Object.keys(obj).length === 0 |
| 113 | + } |
| 114 | + |
| 115 | + if (obj === false) { |
| 116 | + return true |
| 117 | + } |
| 118 | + |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + contains (actual, expected) { |
| 123 | + if (expected === null || expected === undefined) return false |
| 124 | + |
| 125 | + const exp = String(expected) |
| 126 | + |
| 127 | + if (Array.isArray(actual)) return actual.filter((v) => v !== null && v !== undefined).map(String).includes(exp) |
| 128 | + |
| 129 | + if (typeof actual === 'string') return actual.includes(exp) |
| 130 | + |
| 131 | + return actual !== null && actual !== undefined && String(actual) === exp |
| 132 | + } |
| 133 | + |
| 134 | + apply (passed) { |
| 135 | + const controls = this.targetEl.matches('input, select, textarea, button') |
| 136 | + ? [this.targetEl] |
| 137 | + : Array.from(this.targetEl.querySelectorAll('input, select, textarea, button')) |
| 138 | + |
| 139 | + if (passed) { |
| 140 | + this.targetEl.style.display = '' |
| 141 | + this.targetEl.labels.forEach((label) => { label.style.display = '' }) |
| 142 | + |
| 143 | + controls.forEach((c) => (c.disabled = false)) |
| 144 | + } else { |
| 145 | + this.targetEl.style.display = 'none' |
| 146 | + this.targetEl.labels.forEach((label) => { label.style.display = 'none' }) |
| 147 | + |
| 148 | + controls.forEach((c) => (c.disabled = true)) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments