|
1 | | -$(() => { |
2 | | - $('.js-filter-select').toArray().forEach(async (el) => { |
3 | | - const $select = $(el); |
4 | | - const $form = $select.closest('form'); |
5 | | - const $formFilters = $form.find('.form--filter'); |
6 | | - const $saveButton = $form.find('.filter-save'); |
7 | | - const $isDefaultCheckbox = $form.find('.filter-is-default'); |
8 | | - const categoryId = $isDefaultCheckbox.val()?.toString(); |
9 | | - let defaultFilter = await QPixel.defaultFilter(categoryId); |
10 | | - const $deleteButton = $form.find('.filter-delete'); |
11 | | - |
12 | | - // Enables/Disables Save & Delete buttons programatically |
13 | | - async function computeEnables() { |
14 | | - const filters = await QPixel.filters(); |
15 | | - const filterName = $select.val()?.toString(); |
16 | | - |
17 | | - // Nothing set |
18 | | - if (!filterName) { |
19 | | - $saveButton.prop('disabled', true); |
20 | | - $deleteButton.prop('disabled', true); |
21 | | - return; |
22 | | - } |
| 1 | +document.addEventListener('DOMContentLoaded', () => { |
| 2 | + $('.js-filter-select') |
| 3 | + .toArray() |
| 4 | + .forEach(async (el) => { |
| 5 | + const $select = $(el); |
| 6 | + const $form = $select.closest('form'); |
| 7 | + const $formFilters = $form.find('.form--filter'); |
| 8 | + const $saveButton = $form.find('.filter-save'); |
| 9 | + const $isDefaultCheckbox = $form.find('.filter-is-default'); |
| 10 | + const categoryId = $isDefaultCheckbox.val()?.toString(); |
| 11 | + let defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; |
| 12 | + const $deleteButton = $form.find('.filter-delete'); |
| 13 | + |
| 14 | + // Enables/Disables Save & Delete buttons programatically |
| 15 | + async function computeEnables() { |
| 16 | + const filters = await QPixel.filters(); |
| 17 | + const filterName = $select.val()?.toString(); |
| 18 | + |
| 19 | + // Nothing set |
| 20 | + if (!filterName) { |
| 21 | + $saveButton.prop('disabled', true); |
| 22 | + $deleteButton.prop('disabled', true); |
| 23 | + return; |
| 24 | + } |
23 | 25 |
|
24 | | - const filter = filters[filterName] |
| 26 | + const filter = filters[filterName]; |
25 | 27 |
|
26 | | - // New filter |
27 | | - if (!filter) { |
28 | | - $saveButton.prop('disabled', false); |
29 | | - $deleteButton.prop('disabled', true); |
30 | | - return; |
31 | | - } |
| 28 | + // New filter |
| 29 | + if (!filter) { |
| 30 | + $saveButton.prop('disabled', false); |
| 31 | + $deleteButton.prop('disabled', true); |
| 32 | + return; |
| 33 | + } |
32 | 34 |
|
33 | | - // Not a new filter |
34 | | - $deleteButton.prop('disabled', filter.system); |
| 35 | + // Not a new filter |
| 36 | + $deleteButton.prop('disabled', filter.system); |
35 | 37 |
|
36 | | - const hasChanges = [...$formFilters].some((el) => { |
37 | | - const filterValue = filter[el.dataset.name]; |
38 | | - let elValue = /** @type {string | undefined[]} */ ($(el).val()); |
39 | | - if (filterValue?.constructor == Array) { |
40 | | - elValue = elValue ?? []; |
41 | | - return filterValue.length != elValue.length || filterValue.some((v, i) => v[1] != elValue[i]); |
42 | | - } |
43 | | - else { |
44 | | - return filterValue ? filterValue != elValue : elValue; |
45 | | - } |
46 | | - }); |
47 | | - const defaultStatusChanged = $isDefaultCheckbox.prop('checked') != (defaultFilter === $select.val()); |
48 | | - $saveButton.prop('disabled', !defaultStatusChanged && (filter.system || !hasChanges)); |
49 | | - } |
50 | | - |
51 | | - async function initializeSelect() { |
52 | | - defaultFilter = await QPixel.defaultFilter(categoryId); |
53 | | - $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); |
54 | | - const filters = await QPixel.filters(); |
55 | | - |
56 | | - function template(option) { |
57 | | - if (option.id == '') { return 'Default'; } |
58 | | - |
59 | | - const filter = filters[option.id]; |
60 | | - const name = `<span>${option.text}</span>`; |
61 | | - const systemIndicator = filter?.system |
62 | | - ? ' <span has-font-size-caption">(System)</span>' |
63 | | - : ''; |
64 | | - const newIndicator = !filter |
65 | | - ? ' <span has-font-size-caption">(New)</span>' |
66 | | - : ''; |
67 | | - return $(name + systemIndicator + newIndicator); |
| 38 | + const hasChanges = [...$formFilters].some((el) => { |
| 39 | + const filterValue = filter[el.dataset.name]; |
| 40 | + let elValue = /** @type {string | undefined[]} */ ($(el).val()); |
| 41 | + if (filterValue?.constructor == Array) { |
| 42 | + elValue = elValue ?? []; |
| 43 | + return filterValue.length != elValue.length || filterValue.some((v, i) => v[1] != elValue[i]); |
| 44 | + } |
| 45 | + else { |
| 46 | + return filterValue ? filterValue != elValue : elValue; |
| 47 | + } |
| 48 | + }); |
| 49 | + const defaultStatusChanged = $isDefaultCheckbox.prop('checked') != (defaultFilter === $select.val()); |
| 50 | + $saveButton.prop('disabled', !defaultStatusChanged && (filter.system || !hasChanges)); |
68 | 51 | } |
69 | 52 |
|
70 | | - // Clear out any old options |
71 | | - $select.children().filter((_, /** @type{HTMLOptionElement} */ option) => { |
72 | | - return option.value && !filters[option.value]; |
73 | | - }).detach(); |
| 53 | + async function initializeSelect() { |
| 54 | + defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; |
| 55 | + $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); |
| 56 | + const filters = await QPixel.filters(); |
74 | 57 |
|
75 | | - $select.select2({ |
76 | | - data: Object.keys(filters).map((filterName) => { |
77 | | - return { |
78 | | - id: filterName, |
79 | | - text: filterName |
| 58 | + function template(option) { |
| 59 | + if (option.id == '') { |
| 60 | + return 'Default'; |
80 | 61 | } |
81 | | - }), |
82 | | - tags: true, |
83 | | - templateResult: template, |
84 | | - templateSelection: template |
85 | | - }); |
86 | 62 |
|
87 | | - $select.on('select2:select', /** @type {(event: Select2.Event) => void} */ (async (evt) => { |
88 | | - const filterName = evt.params.data.id; |
89 | | - const preset = filters[filterName]; |
| 63 | + const filter = filters[option.id]; |
| 64 | + const name = `<span>${option.text}</span>`; |
| 65 | + const systemIndicator = filter?.system ? ' <span has-font-size-caption">(System)</span>' : ''; |
| 66 | + const newIndicator = !filter ? ' <span has-font-size-caption">(New)</span>' : ''; |
| 67 | + return $(name + systemIndicator + newIndicator); |
| 68 | + } |
90 | 69 |
|
91 | | - $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); |
| 70 | + // Clear out any old options |
| 71 | + $select |
| 72 | + .children() |
| 73 | + .filter((_, /** @type{HTMLOptionElement} */ option) => { |
| 74 | + return option.value && !filters[option.value]; |
| 75 | + }) |
| 76 | + .detach(); |
| 77 | + |
| 78 | + $select.select2({ |
| 79 | + data: Object.keys(filters).map((filterName) => { |
| 80 | + return { |
| 81 | + id: filterName, |
| 82 | + text: filterName, |
| 83 | + }; |
| 84 | + }), |
| 85 | + tags: true, |
| 86 | + templateResult: template, |
| 87 | + templateSelection: template, |
| 88 | + }); |
| 89 | + |
| 90 | + $select.on( |
| 91 | + 'select2:select', |
| 92 | + /** @type {(event: Select2.Event) => void} */ ( |
| 93 | + async (evt) => { |
| 94 | + const filterName = evt.params.data.id; |
| 95 | + const preset = filters[filterName]; |
| 96 | + |
| 97 | + $isDefaultCheckbox.prop('checked', defaultFilter === $select.val()); |
| 98 | + computeEnables(); |
| 99 | + |
| 100 | + // Name is not one of the presets, i.e user is creating a new preset |
| 101 | + if (!preset) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + for (const [name, value] of Object.entries(preset)) { |
| 106 | + const $el = $form.find(`.form--filter[data-name=${name}]`); |
| 107 | + if (value?.constructor == Array) { |
| 108 | + $el.val(null); |
| 109 | + for (const val of value) { |
| 110 | + $el.append(new Option(val[0], val[1].toString(), false, true)); |
| 111 | + } |
| 112 | + $el.trigger('change'); |
| 113 | + } |
| 114 | + else { |
| 115 | + $el.val(/** @type {string} */ (value)).trigger('change'); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + ), |
| 120 | + ); |
92 | 121 | computeEnables(); |
| 122 | + } |
93 | 123 |
|
94 | | - // Name is not one of the presets, i.e user is creating a new preset |
95 | | - if (!preset) { |
96 | | - return; |
97 | | - } |
| 124 | + initializeSelect(); |
98 | 125 |
|
99 | | - for (const [name, value] of Object.entries(preset)) { |
100 | | - const $el = $form.find(`.form--filter[data-name=${name}]`); |
101 | | - if (value?.constructor == Array) { |
102 | | - $el.val(null); |
103 | | - for (const val of value) { |
104 | | - $el.append(new Option(val[0], val[1].toString(), false, true)); |
105 | | - } |
106 | | - $el.trigger('change'); |
107 | | - } |
108 | | - else { |
109 | | - $el.val(/** @type {string} */ (value)).trigger('change'); |
110 | | - } |
| 126 | + // Enable saving when the filter is changed |
| 127 | + $formFilters.on('change', computeEnables); |
| 128 | + $isDefaultCheckbox.on('change', computeEnables); |
| 129 | + |
| 130 | + async function saveFilter() { |
| 131 | + if (!$form[0].reportValidity()) { |
| 132 | + return; |
111 | 133 | } |
112 | | - })); |
113 | | - computeEnables(); |
114 | | - } |
115 | 134 |
|
116 | | - initializeSelect(); |
| 135 | + const filter = /** @type {QPixelFilter} */ ({}); |
117 | 136 |
|
118 | | - // Enable saving when the filter is changed |
119 | | - $formFilters.on('change', computeEnables); |
120 | | - $isDefaultCheckbox.on('change', computeEnables); |
| 137 | + for (const el of $formFilters) { |
| 138 | + filter[el.dataset.name] = $(el).val(); |
| 139 | + } |
121 | 140 |
|
122 | | - async function saveFilter() { |
123 | | - if (!$form[0].reportValidity()) { return; } |
| 141 | + await QPixel.setFilter($select.val()?.toString(), filter, categoryId, $isDefaultCheckbox.prop('checked')); |
124 | 142 |
|
125 | | - const filter = /** @type {QPixelFilter} */({}); |
| 143 | + defaultFilter = categoryId ? await QPixel.defaultFilter(categoryId) : null; |
126 | 144 |
|
127 | | - for (const el of $formFilters) { |
128 | | - filter[el.dataset.name] = $(el).val(); |
| 145 | + // Reinitialize to get new options |
| 146 | + await initializeSelect(); |
129 | 147 | } |
130 | 148 |
|
131 | | - await QPixel.setFilter($select.val()?.toString(), filter, categoryId, $isDefaultCheckbox.prop('checked')); |
132 | | - defaultFilter = await QPixel.defaultFilter(categoryId); |
| 149 | + $saveButton.on('click', saveFilter); |
133 | 150 |
|
134 | | - // Reinitialize to get new options |
135 | | - await initializeSelect(); |
136 | | - } |
137 | | - |
138 | | - $saveButton.on('click', saveFilter); |
| 151 | + function clear() { |
| 152 | + $select.val(null).trigger('change'); |
| 153 | + $form.find('.form--filter').val(null).trigger('change'); |
| 154 | + $isDefaultCheckbox.prop('checked', false); |
| 155 | + computeEnables(); |
| 156 | + } |
139 | 157 |
|
140 | | - function clear() { |
141 | | - $select.val(null).trigger('change'); |
142 | | - $form.find('.form--filter').val(null).trigger('change'); |
143 | | - $isDefaultCheckbox.prop('checked', false); |
144 | | - computeEnables(); |
145 | | - } |
| 158 | + $deleteButton?.on('click', async (_evt) => { |
| 159 | + if (confirm(`Are you sure you want to delete ${$select.val()}?`)) { |
| 160 | + await QPixel.deleteFilter($select.val()?.toString()); |
| 161 | + // Reinitialize to get new options |
| 162 | + await initializeSelect(); |
| 163 | + clear(); |
| 164 | + } |
| 165 | + }); |
146 | 166 |
|
147 | | - $deleteButton?.on('click', async (_evt) => { |
148 | | - if (confirm(`Are you sure you want to delete ${$select.val()}?`)) { |
149 | | - await QPixel.deleteFilter($select.val()?.toString()); |
150 | | - // Reinitialize to get new options |
151 | | - await initializeSelect(); |
152 | | - clear(); |
153 | | - } |
| 167 | + $form.find('.filter-clear').on('click', clear); |
154 | 168 | }); |
155 | | - |
156 | | - $form.find('.filter-clear').on('click', clear); |
157 | | - }); |
158 | 169 | }); |
0 commit comments