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

Expand parent nodes of selected item. Scroll menu to selected item. #310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions src/mixins/treeselectMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,14 @@ export default {
type: [ Number, String ],
default: 999,
},

/**
* Set selected option to the center of the menu, if possible
*/
scrollPositionOnCenter: {
type: Boolean,
default: false
}
},

data() {
Expand Down Expand Up @@ -940,6 +948,7 @@ export default {
} else {
this.forest.normalizedOptions = []
}
this.expandParentNodes()
},

getInstanceId() {
Expand Down Expand Up @@ -1453,6 +1462,9 @@ export default {
this.$nextTick(this.restoreMenuScrollPosition)
if (!this.options && !this.async) this.loadRootOptions()
this.toggleClickOutsideEvent(true)
if (this.scrollPositionOnCenter) {
this.$nextTick(this.scrollMenuOnCenter);
}
this.$emit('open', this.getInstanceId())
},

Expand Down Expand Up @@ -1771,12 +1783,16 @@ export default {
this.buildForestState()

if (nextState) {
this.expandParentNodes();
this.$emit('select', node.raw, this.getInstanceId())
} else {
this.$emit('deselect', node.raw, this.getInstanceId())
}

if (this.localSearch.active && nextState && (this.single || this.clearOnSelect)) {
if (this.scrollPositionOnCenter) {
this.$nextTick(this.scrollMenuOnCenter);
}
this.resetSearchQuery()
}

Expand Down Expand Up @@ -1918,16 +1934,36 @@ export default {
},

saveMenuScrollPosition() {
if (this.scrollPositionOnCenter) return;
const $menu = this.getMenu()
// istanbul ignore else
if ($menu) this.menu.lastScrollPosition = $menu.scrollTop
},

restoreMenuScrollPosition() {
if (this.scrollPositionOnCenter) return;
const $menu = this.getMenu()
// istanbul ignore else
if ($menu) $menu.scrollTop = this.menu.lastScrollPosition
},

scrollMenuOnCenter() {
const $option = document.querySelector(".vue-treeselect__option--selected");
const $menu = this.getMenu();

if ($option && $menu) {
const position = Math.max($option.offsetTop - (($menu.offsetHeight - $option.offsetHeight) / 2), 0);
$menu.scrollTop = position;
}
},

expandParentNodes() {
for (const id of this.forest.selectedNodeIds) {
for (const ancestor of this.forest.nodeMap[id].ancestors) {
ancestor.isExpanded = true;
}
}
}
},

created() {
Expand Down