|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 | { |
3 | 3 | const getOptionGroupName = (option) => option.parentElement.label; |
4 | 4 | const SelectBox = { |
5 | 5 | cache: {}, |
6 | | - init: function(id) { |
| 6 | + init: function (id) { |
7 | 7 | const box = document.getElementById(id); |
8 | 8 | SelectBox.cache[id] = []; |
9 | 9 | const cache = SelectBox.cache[id]; |
10 | 10 | for (const node of box.options) { |
11 | 11 | const group = getOptionGroupName(node); |
12 | | - cache.push({group, value: node.value, text: node.text, displayed: 1}); |
| 12 | + cache.push({ |
| 13 | + group, |
| 14 | + value: node.value, |
| 15 | + text: node.text, |
| 16 | + displayed: 1, |
| 17 | + }); |
13 | 18 | } |
14 | 19 | // Only sort if there are any groups (to preserve existing behavior for non-grouped selects) |
15 | | - if (cache.some(item => item.group)) { |
| 20 | + if (cache.some((item) => item.group)) { |
16 | 21 | SelectBox.sort(id); |
17 | 22 | } |
18 | 23 | }, |
19 | | - redisplay: function(id) { |
| 24 | + redisplay: function (id) { |
20 | 25 | // Repopulate HTML select box from cache |
21 | 26 | const box = document.getElementById(id); |
22 | 27 | const scroll_value_from_top = box.scrollTop; |
23 | | - box.innerHTML = ''; |
| 28 | + box.innerHTML = ""; |
24 | 29 | let node = box; |
25 | 30 | let currentOptgroup = null; |
26 | 31 | for (const option of SelectBox.cache[id]) { |
27 | 32 | if (option.displayed) { |
28 | 33 | // Create a new optgroup when the group changes |
29 | 34 | if (option.group && option.group !== currentOptgroup) { |
30 | 35 | currentOptgroup = option.group; |
31 | | - node = document.createElement('optgroup'); |
32 | | - node.setAttribute('label', option.group); |
| 36 | + node = document.createElement("optgroup"); |
| 37 | + node.setAttribute("label", option.group); |
33 | 38 | box.appendChild(node); |
34 | 39 | } else if (!option.group && currentOptgroup !== null) { |
35 | 40 | // Back to ungrouped options |
36 | 41 | currentOptgroup = null; |
37 | 42 | node = box; |
38 | 43 | } |
39 | | - const new_option = new Option(option.text, option.value, false, false); |
| 44 | + const new_option = new Option( |
| 45 | + option.text, |
| 46 | + option.value, |
| 47 | + false, |
| 48 | + false, |
| 49 | + ); |
40 | 50 | // Shows a tooltip when hovering over the option |
41 | 51 | new_option.title = option.text; |
42 | 52 | node.appendChild(new_option); |
43 | 53 | } |
44 | 54 | } |
45 | 55 | box.scrollTop = scroll_value_from_top; |
46 | 56 | }, |
47 | | - filter: function(id, text) { |
| 57 | + filter: function (id, text) { |
48 | 58 | // Redisplay the HTML select box, displaying only the choices containing ALL |
49 | 59 | // the words in text. (It's an AND search.) |
50 | 60 | const tokens = text.toLowerCase().split(/\s+/); |
|
62 | 72 | }, |
63 | 73 | get_hidden_node_count(id) { |
64 | 74 | const cache = SelectBox.cache[id] || []; |
65 | | - return cache.filter(node => node.displayed === 0).length; |
| 75 | + return cache.filter((node) => node.displayed === 0).length; |
66 | 76 | }, |
67 | | - delete_from_cache: function(id, value) { |
| 77 | + delete_from_cache: function (id, value) { |
68 | 78 | let delete_index = null; |
69 | 79 | const cache = SelectBox.cache[id]; |
70 | 80 | for (const [i, node] of cache.entries()) { |
|
75 | 85 | } |
76 | 86 | cache.splice(delete_index, 1); |
77 | 87 | }, |
78 | | - add_to_cache: function(id, option) { |
79 | | - SelectBox.cache[id].push({group: option.group, value: option.value, text: option.text, displayed: 1}); |
| 88 | + add_to_cache: function (id, option) { |
| 89 | + SelectBox.cache[id].push({ |
| 90 | + group: option.group, |
| 91 | + value: option.value, |
| 92 | + text: option.text, |
| 93 | + displayed: 1, |
| 94 | + }); |
80 | 95 | }, |
81 | | - cache_contains: function(id, value) { |
| 96 | + cache_contains: function (id, value) { |
82 | 97 | // Check if an item is contained in the cache |
83 | 98 | for (const node of SelectBox.cache[id]) { |
84 | 99 | if (node.value === value) { |
|
87 | 102 | } |
88 | 103 | return false; |
89 | 104 | }, |
90 | | - move: function(from, to) { |
| 105 | + move: function (from, to) { |
91 | 106 | const from_box = document.getElementById(from); |
92 | 107 | for (const option of from_box.options) { |
93 | 108 | const option_value = option.value; |
94 | | - if (option.selected && SelectBox.cache_contains(from, option_value)) { |
| 109 | + if ( |
| 110 | + option.selected && |
| 111 | + SelectBox.cache_contains(from, option_value) |
| 112 | + ) { |
95 | 113 | const group = getOptionGroupName(option); |
96 | | - SelectBox.add_to_cache(to, {group, value: option_value, text: option.text, displayed: 1}); |
| 114 | + SelectBox.add_to_cache(to, { |
| 115 | + group, |
| 116 | + value: option_value, |
| 117 | + text: option.text, |
| 118 | + displayed: 1, |
| 119 | + }); |
97 | 120 | SelectBox.delete_from_cache(from, option_value); |
98 | 121 | } |
99 | 122 | } |
100 | 123 | // Only sort if there are any groups (to preserve existing behavior for non-grouped selects) |
101 | | - if (SelectBox.cache[to].some(item => item.group)) { |
| 124 | + if (SelectBox.cache[to].some((item) => item.group)) { |
102 | 125 | SelectBox.sort(to); |
103 | 126 | } |
104 | 127 | SelectBox.redisplay(from); |
105 | 128 | SelectBox.redisplay(to); |
106 | 129 | }, |
107 | | - move_all: function(from, to) { |
| 130 | + move_all: function (from, to) { |
108 | 131 | const from_box = document.getElementById(from); |
109 | 132 | for (const option of from_box.options) { |
110 | 133 | const option_value = option.value; |
111 | 134 | if (SelectBox.cache_contains(from, option_value)) { |
112 | 135 | const group = getOptionGroupName(option); |
113 | | - SelectBox.add_to_cache(to, {group, value: option_value, text: option.text, displayed: 1}); |
| 136 | + SelectBox.add_to_cache(to, { |
| 137 | + group, |
| 138 | + value: option_value, |
| 139 | + text: option.text, |
| 140 | + displayed: 1, |
| 141 | + }); |
114 | 142 | SelectBox.delete_from_cache(from, option_value); |
115 | 143 | } |
116 | 144 | } |
117 | 145 | // Only sort if there are any groups (to preserve existing behavior for non-grouped selects) |
118 | | - if (SelectBox.cache[to].some(item => item.group)) { |
| 146 | + if (SelectBox.cache[to].some((item) => item.group)) { |
119 | 147 | SelectBox.sort(to); |
120 | 148 | } |
121 | 149 | SelectBox.redisplay(from); |
122 | 150 | SelectBox.redisplay(to); |
123 | 151 | }, |
124 | | - sort: function(id) { |
125 | | - SelectBox.cache[id].sort(function(a, b) { |
126 | | - a = (a.group && a.group.toLowerCase() || '') + a.text.toLowerCase(); |
127 | | - b = (b.group && b.group.toLowerCase() || '') + b.text.toLowerCase(); |
| 152 | + sort: function (id) { |
| 153 | + SelectBox.cache[id].sort(function (a, b) { |
| 154 | + a = |
| 155 | + ((a.group && a.group.toLowerCase()) || "") + |
| 156 | + a.text.toLowerCase(); |
| 157 | + b = |
| 158 | + ((b.group && b.group.toLowerCase()) || "") + |
| 159 | + b.text.toLowerCase(); |
128 | 160 | if (a > b) { |
129 | 161 | return 1; |
130 | 162 | } |
131 | 163 | if (a < b) { |
132 | 164 | return -1; |
133 | 165 | } |
134 | 166 | return 0; |
135 | | - } ); |
| 167 | + }); |
136 | 168 | }, |
137 | | - select_all: function(id) { |
| 169 | + select_all: function (id) { |
138 | 170 | const box = document.getElementById(id); |
139 | 171 | for (const option of box.options) { |
140 | 172 | option.selected = true; |
141 | 173 | } |
142 | | - } |
| 174 | + }, |
143 | 175 | }; |
144 | 176 | window.SelectBox = SelectBox; |
145 | 177 | } |
0 commit comments