|
1 | 1 | import $ from 'jquery'; |
2 | 2 | import _ from 'lodash'; |
3 | 3 | import UserSelectAutoComplete from 'vj/components/autocomplete/UserSelectAutoComplete'; |
4 | | -import { ActionDialog, ConfirmDialog } from 'vj/components/dialog'; |
| 4 | +import { ActionDialog, ConfirmDialog, prompt } from 'vj/components/dialog'; |
5 | 5 | import Notification from 'vj/components/notification'; |
6 | 6 | import { NamedPage } from 'vj/misc/Page'; |
7 | 7 | import { |
@@ -132,7 +132,76 @@ const page = new NamedPage('domain_group', () => { |
132 | 132 | Notification.success(i18n('Saved.')); |
133 | 133 | } |
134 | 134 |
|
| 135 | + async function handleClickImportGroups() { |
| 136 | + const result = await prompt(i18n('Import Groups'), { |
| 137 | + groups: { |
| 138 | + type: 'textarea', |
| 139 | + label: i18n('Format: one group per line, CSV: groupName,uid1,uid2,...'), |
| 140 | + rows: 10, |
| 141 | + required: true, |
| 142 | + placeholder: 'group1,1001,1002,1003\ngroup2,1004,1005', |
| 143 | + }, |
| 144 | + }); |
| 145 | + if (!result) return; |
| 146 | + const lines = String(result.groups || '').replace(/^\uFEFF/, '').split('\n'); |
| 147 | + const parsed = new Map<string, number[]>(); |
| 148 | + const errors: string[] = []; |
| 149 | + lines.forEach((raw, idx) => { |
| 150 | + const line = raw.trim(); |
| 151 | + if (!line) return; |
| 152 | + const [name, ...rest] = line.split(',').map((t) => t.trim()); |
| 153 | + if (!name) return; |
| 154 | + const uids: number[] = []; |
| 155 | + for (const t of rest) { |
| 156 | + if (!t) continue; |
| 157 | + const uid = +t; |
| 158 | + if (!Number.isInteger(uid)) { |
| 159 | + errors.push(i18n('Line {0}: Invalid UID "{1}".', idx + 1, t)); |
| 160 | + return; |
| 161 | + } |
| 162 | + uids.push(uid); |
| 163 | + } |
| 164 | + parsed.set(name, Array.from(new Set(uids))); |
| 165 | + }); |
| 166 | + if (errors.length) { |
| 167 | + Notification.error(errors.join('\n')); |
| 168 | + return; |
| 169 | + } |
| 170 | + if (!parsed.size) { |
| 171 | + Notification.error(i18n('No groups to import.')); |
| 172 | + return; |
| 173 | + } |
| 174 | + try { |
| 175 | + for (const [name, uids] of parsed) { |
| 176 | + await update(name, uids); |
| 177 | + } |
| 178 | + Notification.success(i18n('Imported successfully.')); |
| 179 | + await delay(1500); |
| 180 | + window.location.reload(); |
| 181 | + } catch (error) { |
| 182 | + Notification.error(error.message); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + async function handleClickExportGroups() { |
| 187 | + const rows: string[] = []; |
| 188 | + for (const gid of Object.keys(targets)) { |
| 189 | + const uids = targets[gid].value() as number[]; |
| 190 | + rows.push([gid, ...uids].join(',')); |
| 191 | + } |
| 192 | + await prompt(i18n('Export Groups'), { |
| 193 | + groups: { |
| 194 | + type: 'textarea', |
| 195 | + label: i18n('Copy the content below'), |
| 196 | + rows: 15, |
| 197 | + default: rows.join('\n'), |
| 198 | + }, |
| 199 | + }); |
| 200 | + } |
| 201 | + |
135 | 202 | $('[name="create_group"]').click(() => handleClickCreateGroup()); |
| 203 | + $('[name="import_groups"]').click(() => handleClickImportGroups()); |
| 204 | + $('[name="export_groups"]').click(() => handleClickExportGroups()); |
136 | 205 | $('[name="remove_selected"]').click(() => handleClickDeleteSelected()); |
137 | 206 | $('[name="save_all"]').on('click', () => handleClickSaveAll()); |
138 | 207 | }); |
|
0 commit comments