| 155 | }; |
| 156 | // Render at most 50 entries at once, and only expand objects on demand. |
| 157 | const children = (samples, parts, parent, fields = false) => { |
| 158 | const entries = new Map(); |
| 159 | samples.forEach(sample => { |
| 160 | if (!picker.isContainer(sample)) return; |
| 161 | Object.entries(sample).forEach(([key, value]) => { |
| 162 | if (!entries.has(key)) entries.set(key, []); |
| 163 | entries.get(key).push(value); |
| 164 | }); |
| 165 | }); |
| 166 | const items = [...entries]; |
| 167 | let offset = 0; |
| 168 | const more = action(t('analytics', 'Show more'), () => render()); |
| 169 | const render = () => { |
| 170 | more.remove(); |
| 171 | items.slice(offset, offset + 50).forEach(([key, values]) => { |
| 172 | const path = [...parts, key]; |
| 173 | const value = values.find(picker.isContainer) ?? values[0]; |
| 174 | if (!picker.isContainer(value)) { |
| 175 | choose(key, path, value, parent); |
| 176 | return; |
| 177 | } |
| 178 | const details = document.createElement('details'); |
| 179 | const summary = document.createElement('summary'); |
| 180 | const isArray = Array.isArray(value); |
| 181 | const name = document.createElement('span'); |
| 182 | name.textContent = key + ' (' + Object.keys(value).length + ')'; |
| 183 | summary.appendChild(name); |
| 184 | details.appendChild(summary); |
| 185 | details.addEventListener('toggle', () => { |
| 186 | if (!details.open || details.dataset.loaded) return; |
| 187 | details.dataset.loaded = 'true'; |
| 188 | if (!fields && isArray && value.some(item => picker.isContainer(item))) { |
| 189 | // TRANSLATORS: JSON selection action; {name} is an array or group name from the source data, e.g. orders. |
| 190 | details.appendChild(action(t('analytics', 'Use all {name} and choose the relevant fields', {name: key}), () => showFields(value, path), picker.selectable(path))); |
| 191 | } else if (!fields && Object.values(value).every(item => !picker.isContainer(item))) { |
| 192 | details.appendChild(action(t('analytics', 'Use entries as rows'), () => { |
| 193 | rowPath = null; |
| 194 | selected = [path.join('/')]; |
| 195 | update(); |
| 196 | tree.querySelectorAll('input[type="checkbox"]').forEach(box => { box.checked = false; }); |
| 197 | }, picker.selectable(path))); |
| 198 | } |
| 199 | if (Object.keys(value).length === 0) { |
| 200 | const empty = document.createElement('p'); |
| 201 | empty.className = 'userGuidance'; |
| 202 | empty.textContent = t('analytics', 'No sample rows available.'); |
| 203 | details.appendChild(empty); |
| 204 | } else if (parts.length < 20) { |
| 205 | children(fields ? values : [value], path, details, fields); |
| 206 | } else { |
| 207 | const hint = document.createElement('p'); |
| 208 | hint.className = 'userGuidance'; |
| 209 | hint.textContent = t('analytics', 'This nesting is too deep to browse. Enter the Object path manually.'); |
| 210 | details.appendChild(hint); |
| 211 | } |
| 212 | }); |
| 213 | parent.appendChild(details); |
| 214 | }); |