-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorting: Added sort set form manager UI JS
Extracted much code to be shared with the shelf books management UI
- Loading branch information
1 parent
bf8a84a
commit d28278b
Showing
13 changed files
with
168 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {Component} from "./component.js"; | ||
import Sortable from "sortablejs"; | ||
import {buildListActions, sortActionClickListener} from "../services/dual-lists"; | ||
|
||
|
||
export class SortSetManager extends Component { | ||
|
||
protected input!: HTMLInputElement; | ||
protected configuredList!: HTMLElement; | ||
protected availableList!: HTMLElement; | ||
|
||
setup() { | ||
this.input = this.$refs.input as HTMLInputElement; | ||
this.configuredList = this.$refs.configuredOperationsList; | ||
this.availableList = this.$refs.availableOperationsList; | ||
|
||
this.initSortable(); | ||
|
||
const listActions = buildListActions(this.availableList, this.configuredList); | ||
const sortActionListener = sortActionClickListener(listActions, this.onChange.bind(this)); | ||
this.$el.addEventListener('click', sortActionListener); | ||
} | ||
|
||
initSortable() { | ||
const scrollBoxes = [this.configuredList, this.availableList]; | ||
for (const scrollBox of scrollBoxes) { | ||
new Sortable(scrollBox, { | ||
group: 'sort-set-operations', | ||
ghostClass: 'primary-background-light', | ||
handle: '.handle', | ||
animation: 150, | ||
onSort: this.onChange.bind(this), | ||
}); | ||
} | ||
} | ||
|
||
onChange() { | ||
const configuredOpEls = Array.from(this.configuredList.querySelectorAll('[data-id]')); | ||
this.input.value = configuredOpEls.map(elem => elem.getAttribute('data-id')).join(','); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Service for helping manage common dual-list scenarios. | ||
* (Shelf book manager, sort set manager). | ||
*/ | ||
|
||
type ListActionsSet = Record<string, ((item: HTMLElement) => void)>; | ||
|
||
export function buildListActions( | ||
availableList: HTMLElement, | ||
configuredList: HTMLElement, | ||
): ListActionsSet { | ||
return { | ||
move_up(item) { | ||
const list = item.parentNode as HTMLElement; | ||
const index = Array.from(list.children).indexOf(item); | ||
const newIndex = Math.max(index - 1, 0); | ||
list.insertBefore(item, list.children[newIndex] || null); | ||
}, | ||
move_down(item) { | ||
const list = item.parentNode as HTMLElement; | ||
const index = Array.from(list.children).indexOf(item); | ||
const newIndex = Math.min(index + 2, list.children.length); | ||
list.insertBefore(item, list.children[newIndex] || null); | ||
}, | ||
remove(item) { | ||
availableList.appendChild(item); | ||
}, | ||
add(item) { | ||
configuredList.appendChild(item); | ||
}, | ||
}; | ||
} | ||
|
||
export function sortActionClickListener(actions: ListActionsSet, onChange: () => void) { | ||
return (event: MouseEvent) => { | ||
const sortItemAction = (event.target as Element).closest('.scroll-box-item button[data-action]') as HTMLElement|null; | ||
if (sortItemAction) { | ||
const sortItem = sortItemAction.closest('.scroll-box-item') as HTMLElement; | ||
const action = sortItemAction.dataset.action; | ||
if (!action) { | ||
throw new Error('No action defined for clicked button'); | ||
} | ||
|
||
const actionFunction = actions[action]; | ||
actionFunction(sortItem); | ||
|
||
onChange(); | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.