Skip to content
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

Add automatic alphabetic sort option to topic tree #4423

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,40 @@
</div>
<VSlideXTransition>
<div v-if="selected.length">
<IconButton
<KIconButton
v-if="canEdit"
icon="edit"
:text="$tr('editSelectedButton')"
data-test="edit-selected-btn"
@click="editNodes(selected)"
/>
<IconButton
<KIconButton
icon="clipboard"
:text="$tr('copySelectedButton')"
data-test="copy-selected-to-clipboard-btn"
@click="copyToClipboard(selected)"
/>
<IconButton
<KIconButton
v-if="canEdit"
icon="move"
:text="$tr('moveSelectedButton')"
data-test="move-selected-btn"
@click="openMoveModal"
/>
<IconButton
<KIconButton
v-if="canEdit"
icon="copy"
:text="$tr('duplicateSelectedButton')"
data-test="duplicate-selected-btn"
@click="duplicateNodes(selected)"
/>
<IconButton
<KIconButton
v-if="canEdit"
icon="sort"
:text="$tr('SortAlphabetically')"
@click="sortNodes(selected)"
/>
<KIconButton
v-if="canEdit"
icon="remove"
:text="$tr('deleteSelectedButton')"
Expand Down Expand Up @@ -435,6 +441,44 @@
clearSelections() {
this.selected = [];
},

sortNodes(selected) {
const selectedNodes = selected.map(id => this.getContentNode(id));
const orderedNodes = selectedNodes.sort(this.compareNodeTitles);
GarvitSinghal47 marked this conversation as resolved.
Show resolved Hide resolved

const reversedNodes = orderedNodes.reverse();

const nodeX = this.findNodeBeforeFirstSelected(orderedNodes, selected);

const targetParent = this.node.id;
const targetNode = nodeX || targetParent;
const targetPosition = nodeX
? RELATIVE_TREE_POSITIONS.RIGHT
: RELATIVE_TREE_POSITIONS.FIRST_CHILD;

const nodeIdsToMove = reversedNodes.map(node => String(node.id));

this.moveContentNodes({
id__in: nodeIdsToMove,
target: targetNode,
position: targetPosition,
});
},

findNodeBeforeFirstSelected(nodes, selected) {
for (let i = 1; i < nodes.length; i++) {
if (selected.includes(nodes[i])) {
return nodes[i - 1];
}
}
return null;
},

compareNodeTitles(nodeA, nodeB) {
const titleA = nodeA.title.toLowerCase();
const titleB = nodeB.title.toLowerCase();
return titleA.localeCompare(titleB);
},
updateTitleForPage() {
let detailTitle;
const topicTitle = this.getTitle(this.getContentNode(this.topicId));
Expand Down Expand Up @@ -707,6 +751,7 @@
},
$trs: {
addTopic: 'New folder',
SortAlphabetically: 'Sort alphabetically',
addExercise: 'New exercise',
uploadFiles: 'Upload files',
importFromChannels: 'Import from channels',
Expand Down