-
-
Notifications
You must be signed in to change notification settings - Fork 400
Adds multi-select support #1360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
28b188f
6e5b010
db8e592
a7d7404
284fe47
5b32607
c762599
64bfc8c
867ab51
9ee9b5e
cad76be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,10 @@ class GenPageBrowserClass { | |
| this.runAfterUpdate = []; | ||
| this.refreshHandler = (callback) => callback(); | ||
| this.checkIsSmall(); | ||
| this.allowMultiSelect = false; | ||
| this.multiSelectActive = false; | ||
| this.multiSelectToggleButton = null; | ||
| this.multiSelectActionSelect = null; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -138,6 +142,7 @@ class GenPageBrowserClass { | |
| this.chunksRendered = 0; | ||
| this.folder = folder; | ||
| this.selected = null; | ||
| this.clearMultiSelection(); | ||
| this.update(false, callback); | ||
| } | ||
|
|
||
|
|
@@ -455,6 +460,9 @@ class GenPageBrowserClass { | |
| } | ||
| let img = document.createElement('img'); | ||
| img.addEventListener('click', () => { | ||
| if (this.handleMultiSelectTileClick(div)) { | ||
| return; | ||
| } | ||
| this.select(file, div); | ||
| }); | ||
| img.classList.add('image-block-img-inner'); | ||
|
|
@@ -495,13 +503,22 @@ class GenPageBrowserClass { | |
| else { | ||
| textBlock.classList.add('image-preview-text-large'); | ||
| } | ||
| textBlock.addEventListener('click', (e) => { | ||
| if (this.handleMultiSelectTileClick(div, e)) { | ||
| return; | ||
| } | ||
| this.select(file, div); | ||
| }); | ||
| div.appendChild(textBlock); | ||
| } | ||
| else if (this.format == 'List') { | ||
| div.className += ' browser-list-entry'; | ||
| let textBlock = createSpan(null, 'browser-list-entry-text'); | ||
| textBlock.innerText = desc.display || desc.name; | ||
| textBlock.addEventListener('click', () => { | ||
| if (this.handleMultiSelectTileClick(div)) { | ||
| return; | ||
| } | ||
| this.select(file, div); | ||
| }); | ||
| div.appendChild(textBlock); | ||
|
|
@@ -520,6 +537,9 @@ class GenPageBrowserClass { | |
| textBlock.style.width = `calc(${percent}% - ${imgAdj}rem)`; | ||
| textBlock.innerHTML = detail; | ||
| textBlock.addEventListener('click', () => { | ||
| if (this.handleMultiSelectTileClick(div)) { | ||
| return; | ||
| } | ||
| this.select(file, div); | ||
| }); | ||
| div.appendChild(textBlock); | ||
|
|
@@ -704,6 +724,37 @@ class GenPageBrowserClass { | |
| this.headerBar.appendChild(formatSelector); | ||
| this.headerBar.appendChild(buttons); | ||
| refreshButton.onclick = this.refresh.bind(this); | ||
| if (this.allowMultiSelect) { | ||
| this.multiSelectToggleButton = document.createElement('button'); | ||
| this.multiSelectToggleButton.type = 'button'; | ||
| this.multiSelectToggleButton.id = `${this.id}_multiselect_toggle`; | ||
| this.multiSelectToggleButton.className = 'refresh-button translate translate-no-text browser-multiselect-toggle'; | ||
| this.multiSelectToggleButton.title = 'Toggle multi-select mode'; | ||
| this.multiSelectToggleButton.innerHTML = '✓'; | ||
| this.multiSelectToggleButton.addEventListener('click', () => { | ||
| this.setMultiSelectActive(!this.multiSelectActive); | ||
| }); | ||
| this.multiSelectActionSelect = document.createElement('select'); | ||
| this.multiSelectActionSelect.id = `${this.id}_multiselect_action`; | ||
| this.multiSelectActionSelect.className = 'browser-format-selector browser-multiselect-action-select'; | ||
| this.multiSelectActionSelect.title = 'Bulk action'; | ||
| let placeholderOpt = document.createElement('option'); | ||
| placeholderOpt.value = ''; | ||
| placeholderOpt.className = 'translate'; | ||
| placeholderOpt.innerText = translate('Actions...'); | ||
| this.multiSelectActionSelect.appendChild(placeholderOpt); | ||
| this.multiSelectActionSelect.style.display = 'none'; | ||
| this.multiSelectActionSelect.addEventListener('change', () => { | ||
| let choice = this.multiSelectActionSelect.value; | ||
| if (!choice) { | ||
| return; | ||
| } | ||
| this.runMultiSelectAction(choice); | ||
| this.multiSelectActionSelect.value = ''; | ||
| }); | ||
| this.upButton.insertAdjacentElement('afterend', this.multiSelectToggleButton); | ||
| this.multiSelectToggleButton.insertAdjacentElement('afterend', this.multiSelectActionSelect); | ||
| } | ||
| this.fullContentDiv.appendChild(this.headerBar); | ||
| this.contentDiv = createDiv(`${this.id}-content`, 'browser-content-container'); | ||
| this.contentDiv.addEventListener('scroll', () => { | ||
|
|
@@ -769,6 +820,8 @@ class GenPageBrowserClass { | |
| applyTranslations(this.headerBar); | ||
| if (!this.noContentUpdates) { | ||
| this.buildContentList(this.contentDiv, files); | ||
| this.applyMultiSelectVisuals(); | ||
| this.syncMultiSelectHeader(); | ||
| browserUtil.makeVisible(this.contentDiv); | ||
| if (scrollOffset) { | ||
| this.contentDiv.scrollTop = scrollOffset; | ||
|
|
@@ -783,4 +836,206 @@ class GenPageBrowserClass { | |
| this.builtEvent(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns multi-select items. | ||
| */ | ||
| getMultiSelectedItems() { | ||
| if (!this.contentDiv) { | ||
| return []; | ||
| } | ||
| return [...this.contentDiv.querySelectorAll(':scope > .browser-multiselect-item-selected[data-name]')]; | ||
| } | ||
|
|
||
| /** | ||
| * Clears multi-selected items. | ||
| */ | ||
| clearMultiSelection() { | ||
| if (!this.allowMultiSelect) { | ||
| return; | ||
| } | ||
| for (let item of this.getMultiSelectedItems()) { | ||
| item.classList.remove('browser-multiselect-item-selected'); | ||
| } | ||
| this.syncMultiSelectHeader(); | ||
| } | ||
|
|
||
| /** | ||
| * Turns multi-select mode on or off; exiting clears the selection. | ||
| */ | ||
| setMultiSelectActive(active) { | ||
| if (!this.allowMultiSelect) { | ||
| return; | ||
| } | ||
| this.multiSelectActive = active; | ||
| if (!active) { | ||
| this.clearMultiSelection(); | ||
| } | ||
| else { | ||
| this.syncMultiSelectHeader(); | ||
| } | ||
| this.applyMultiSelectVisuals(); | ||
| } | ||
|
|
||
| /** | ||
| * Handles an item click while multi-select mode is active. | ||
| */ | ||
| handleMultiSelectTileClick(div, event = null) { | ||
| if (!this.multiSelectActive || !this.allowMultiSelect) { | ||
| return false; | ||
| } | ||
| if (event) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| } | ||
| div.classList.toggle('browser-multiselect-item-selected'); | ||
| this.syncMultiSelectHeader(); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns files in the current listing that are multi-selected. | ||
| */ | ||
| getMultiSelectedFiles() { | ||
| if (!this.lastFiles) { | ||
| return []; | ||
| } | ||
| let selectedNames = new Set(this.getMultiSelectedItems().map(entry => entry.dataset.name)); | ||
| let out = []; | ||
| for (let file of this.lastFiles) { | ||
| if (selectedNames.has(file.name)) { | ||
| out.push(file); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Labels for bulk actions shared by every selected item, respecting `can_multi` / `multi_only`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like AI slop. Misinterpreted keywords and then proudly documented respect for the misunderstood keyword. |
||
| */ | ||
| getCommonMultiSelectActionLabels() { | ||
| let files = this.getMultiSelectedFiles(); | ||
| if (files.length == 0) { | ||
| return []; | ||
| } | ||
| let eligiblePerFile = []; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually wtf is this whole function? Why was this built like this at all??? wat. |
||
| for (let file of files) { | ||
| let desc = this.describe(file); | ||
| let labels = new Set(); | ||
| for (let button of desc.buttons) { | ||
| if (button.can_multi && (button.max_selected == null || files.length <= button.max_selected)) { | ||
| labels.add(button.label); | ||
| } | ||
| } | ||
| eligiblePerFile.push(labels); | ||
| } | ||
| let first = eligiblePerFile[0]; | ||
| let common = []; | ||
| for (let label of first) { | ||
| if (eligiblePerFile.every(s => s.has(label))) { | ||
| common.push(label); | ||
| } | ||
| } | ||
| common.sort((a, b) => a.localeCompare(b)); | ||
| return common; | ||
| } | ||
|
|
||
| /** | ||
| * Off: ✓ ✓ | ||
| * On: ☑ ☑ | ||
| */ | ||
| syncMultiSelectToggleAppearance() { | ||
| if (!this.multiSelectToggleButton) { | ||
| return; | ||
| } | ||
| this.multiSelectToggleButton.classList.toggle('browser-multiselect-toggle-active', this.multiSelectActive); | ||
| this.multiSelectToggleButton.innerHTML = this.multiSelectActive ? '☑' : '✓'; | ||
| } | ||
|
|
||
| /** | ||
| * Updates multi-select toggle state and action dropdown. | ||
| */ | ||
| syncMultiSelectHeader() { | ||
| this.syncMultiSelectToggleAppearance(); | ||
| if (!this.multiSelectActionSelect) { | ||
| return; | ||
| } | ||
| let show = this.multiSelectActive && this.getMultiSelectedItems().length > 0; | ||
| this.multiSelectActionSelect.style.display = show ? '' : 'none'; | ||
| if (!show) { | ||
| return; | ||
| } | ||
| while (this.multiSelectActionSelect.options.length > 1) { | ||
| this.multiSelectActionSelect.remove(1); | ||
| } | ||
| this.multiSelectActionSelect.value = ''; | ||
| for (let label of this.getCommonMultiSelectActionLabels()) { | ||
| let opt = document.createElement('option'); | ||
| opt.value = label; | ||
| opt.className = 'translate'; | ||
| opt.innerText = translate(label); | ||
| this.multiSelectActionSelect.appendChild(opt); | ||
| } | ||
| applyTranslations(this.multiSelectActionSelect); | ||
| } | ||
|
|
||
| /** | ||
| * Runs a multi-select action once per selected item. | ||
| */ | ||
| runMultiSelectAction(label) { | ||
| let files = this.getMultiSelectedFiles(); | ||
| let failed = 0; | ||
| for (let file of files) { | ||
| let div = this.getVisibleEntry(file.name); | ||
| let desc = this.describe(file); | ||
| let button = null; | ||
| for (let b of desc.buttons) { | ||
| if (b.label == label && b.onclick) { | ||
| button = b; | ||
| break; | ||
| } | ||
| } | ||
| if (!button) { | ||
| failed++; | ||
| console.error(`No bulk action '${label}' for ${file.name}`); | ||
| continue; | ||
| } | ||
| try { | ||
| if (div) { | ||
| button.onclick(div); | ||
| } | ||
| else { | ||
| button.onclick(null); | ||
| } | ||
| } | ||
| catch (err) { | ||
| console.error('Browser bulk action error:', err); | ||
| failed++; | ||
| } | ||
| } | ||
| if (failed > 0) { | ||
| showError(`Bulk action finished: ${failed} of ${files.length} failed - see console for details.`); | ||
| } | ||
| if (label == 'Delete') { | ||
| this.setMultiSelectActive(false); | ||
| } | ||
| else { | ||
| this.applyMultiSelectVisuals(); | ||
| this.syncMultiSelectHeader(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Applies multi-select highlight classes to visible rows. | ||
| */ | ||
| applyMultiSelectVisuals() { | ||
| if (this.multiSelectActive || !this.contentDiv) { | ||
| return; | ||
| } | ||
| for (let child of this.contentDiv.children) { | ||
| if (child.dataset && child.dataset.name) { | ||
| child.classList.remove('browser-multiselect-item-selected'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bug in your original implementation:
let metaParsed = JSON.parse(metadata);