| 1 |
/** |
| 2 |
* Displays or hides settings in the UI, depending on which settings are enabled |
| 3 |
* or disabled. |
| 4 |
* |
| 5 |
* @author ConvertKit |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Displays or hides settings in the UI, depending on which settings are enabled |
| 10 |
* or disabled. |
| 11 |
* |
| 12 |
* @since 2.2.4 |
| 13 |
*/ |
| 14 |
document.addEventListener('DOMContentLoaded', function () { |
| 15 |
// Update settings and refresh UI when a setting is changed. |
| 16 |
const sourceInputs = document.querySelectorAll( |
| 17 |
'.convertkit-conditional-display' |
| 18 |
); |
| 19 |
sourceInputs.forEach(function (input) { |
| 20 |
input.addEventListener('change', function () { |
| 21 |
convertKitConditionallyDisplaySettings(this); |
| 22 |
}); |
| 23 |
|
| 24 |
convertKitConditionallyDisplaySettings(input); |
| 25 |
}); |
| 26 |
}); |
| 27 |
|
| 28 |
/** |
| 29 |
* Shows all table rows on a ConvertKit settings screen, and then hides |
| 30 |
* table rows related to a setting, if that setting is disabled. |
| 31 |
* |
| 32 |
* @since 2.2.4 |
| 33 |
* |
| 34 |
* @param {Object} input Element interacted with. |
| 35 |
*/ |
| 36 |
function convertKitConditionallyDisplaySettings(input) { |
| 37 |
const rows = document.querySelectorAll('table.form-table tr'); |
| 38 |
|
| 39 |
switch (input.type) { |
| 40 |
case 'checkbox': |
| 41 |
// Show all rows. |
| 42 |
rows.forEach((row) => (row.style.display = '')); |
| 43 |
|
| 44 |
// Don't do anything else if the checkbox is checked. |
| 45 |
if (input.checked) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Iterate through the table rows, hiding any settings. |
| 50 |
rows.forEach(function (row) { |
| 51 |
// Skip if this table row is for the setting we've just checked/unchecked. |
| 52 |
if (row.querySelector(`[id = "${input.id}"]`)) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Hide this row if the input, select, link or span element within the row has the CSS class of the setting ID. |
| 57 |
if ( |
| 58 |
row.querySelector( |
| 59 |
`input.${input.id}, select.${input.id}, a.${input.id}, span.${input.id}` |
| 60 |
) |
| 61 |
) { |
| 62 |
row.style.display = 'none'; |
| 63 |
} |
| 64 |
}); |
| 65 |
break; |
| 66 |
|
| 67 |
default: |
| 68 |
// Group pattern: source has data-conditional-class-prefix; rows opt |
| 69 |
// into the group by declaring a CSS class of the form |
| 70 |
// "<prefix><value>". Rows in the group whose class matches the |
| 71 |
// source's current value are shown; other rows in the group are |
| 72 |
// hidden. Rows without any matching prefix class are left alone. |
| 73 |
if (input.dataset.conditionalClassPrefix) { |
| 74 |
const prefix = input.dataset.conditionalClassPrefix; |
| 75 |
const expectedClass = prefix + input.value; |
| 76 |
rows.forEach(function (row) { |
| 77 |
const inGroup = Array.from(row.classList).some((c) => |
| 78 |
c.startsWith(prefix) |
| 79 |
); |
| 80 |
if (!inGroup) { |
| 81 |
return; |
| 82 |
} |
| 83 |
row.style.display = row.classList.contains(expectedClass) |
| 84 |
? '' |
| 85 |
: 'none'; |
| 86 |
}); |
| 87 |
break; |
| 88 |
} |
| 89 |
|
| 90 |
// Single-target pattern: source has data-conditional-element (id) |
| 91 |
// and data-conditional-value; the matching row is shown when the |
| 92 |
// source value equals the expected value and hidden otherwise. |
| 93 |
rows.forEach(function (row) { |
| 94 |
// Skip if this table row is for the setting we've just changed. |
| 95 |
if (row.querySelector(`[id = "${input.id}"]`)) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
if ( |
| 100 |
row.querySelector( |
| 101 |
`input#${input.dataset.conditionalElement}` |
| 102 |
) |
| 103 |
) { |
| 104 |
if (input.value !== input.dataset.conditionalValue) { |
| 105 |
row.style.display = 'none'; |
| 106 |
} else { |
| 107 |
row.style.display = ''; |
| 108 |
} |
| 109 |
} |
| 110 |
}); |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
|