add shuffle mecanism to LibraryMap

This commit is contained in:
axolotle
2021-06-29 19:10:46 +02:00
parent 28d1f32579
commit 3507c9a7ce
7 changed files with 77 additions and 23 deletions
+2 -1
View File
@@ -25,7 +25,8 @@
"tree-map": "Arborescent", "tree-map": "Arborescent",
"card-map": "Aléatoire", "card-map": "Aléatoire",
"card-list": "Alphabétique" "card-list": "Alphabétique"
} },
"shuffle": "Mélanger"
}, },
"filters": { "filters": {
"title": "Filtres", "title": "Filtres",
+1 -1
View File
@@ -1,5 +1,5 @@
<template> <template>
<b-overlay :show="filteredNodes === undefined" class="h-100"> <b-overlay :show="filteredNodes === undefined" class="h-100" z-index="0">
<div class="library-list"> <div class="library-list">
<div class="library-list-container" @click="onContainerClick"> <div class="library-list-container" @click="onContainerClick">
<div v-for="([char, nodes]) in filteredNodes" :key="char"> <div v-for="([char, nodes]) in filteredNodes" :key="char">
+27 -11
View File
@@ -1,8 +1,11 @@
<template> <template>
<b-overlay :show="nodes === undefined" class="h-100"> <b-overlay :show="nodes === undefined" class="h-100" z-index="0">
<map-zoom id="library-map" :min-zoom="0.2" :max-zoom="2"> <map-zoom
id="library-map" ref="map"
:min-zoom="0.2" :max-zoom="2" :initial-zoom="0.5"
>
<foreignObject <foreignObject
v-for="(node, i) in filteredNodes" :key="i" v-for="node in filteredNodes" :key="'f-' + node.data.id"
:style="`--x: ${node.x}px; --y: ${node.y}px; --r: ${node.rotate}deg;`" :style="`--x: ${node.x}px; --y: ${node.y}px; --r: ${node.rotate}deg;`"
> >
<node-view <node-view
@@ -53,7 +56,7 @@ export default {
}, },
computed: { computed: {
...mapGetters(['search', 'tags', 'strangeness']), ...mapGetters(['randomNodes', 'search', 'tags', 'strangeness']),
filteredNodes () { filteredNodes () {
if (!this.nodes) return if (!this.nodes) return
@@ -91,17 +94,30 @@ export default {
} }
}, },
created () { watch: {
// JS-BP randomNodes (randomNodes) {
const radius = window.innerWidth < 769 ? 700 / 1.5 : 700 if (!randomNodes) {
this.$store.dispatch('INIT_LIBRARY_MAP').then(nodes => { this.nodes = undefined
this.nodes = nodes.map((node, i) => { return
}
// JS-BP
const radius = window.innerWidth < 769 ? 700 / 1.5 : 700
this.nodes = randomNodes.map((node, i) => {
return { x: 0, rotate: randomUniform(-25, 25)(), data: node } return { x: 0, rotate: randomUniform(-25, 25)(), data: node }
}) })
this.simulation.nodes(this.nodes) this.simulation.nodes(this.nodes)
.force('attract', forceManyBody()) .force('attract', forceManyBody().strength(1))
.force('collision', forceCollide().radius(radius / 2)) .force('collision', forceCollide().radius(radius / 2))
}) .alpha(0.5).restart()
this.$refs.map.reset()
}
},
created () {
this.$store.dispatch('INIT_LIBRARY_MAP')
} }
} }
</script> </script>
+8
View File
@@ -11,6 +11,14 @@
name="mode-select" :aria-describedby="ariaDescribedby" name="mode-select" :aria-describedby="ariaDescribedby"
buttons button-variant="outline-dark" buttons button-variant="outline-dark"
/> />
<b-button
v-if="activeMode === 'map'"
variant="outline-dark" class="d-block mt-2"
@click="$store.dispatch('ROLL_RANDOM_NODES')"
>
{{ $t('options.display.shuffle') }}
</b-button>
</b-form-group> </b-form-group>
<!-- LIST + MAP ONLY --> <!-- LIST + MAP ONLY -->
+1 -1
View File
@@ -1,5 +1,5 @@
<template> <template>
<b-overlay :show="nodeTree.nodes.length === 0" class="h-100"> <b-overlay :show="nodeTree.nodes.length === 0" class="h-100" z-index="0">
<map-zoom id="library-tree" :min-zoom="0.3" :max-zoom="1"> <map-zoom id="library-tree" :min-zoom="0.3" :max-zoom="1">
<path <path
v-for="link in nodeTree.links" v-for="link in nodeTree.links"
+26 -9
View File
@@ -6,6 +6,7 @@ import {
parseNodesQueryParams, parseNodesQueryParams,
getUniqueNodesIds, getUniqueNodesIds,
getRelatedNodesIds, getRelatedNodesIds,
getRandomIds,
buildTree buildTree
} from '@/store/utils.js' } from '@/store/utils.js'
@@ -13,6 +14,7 @@ import {
export default { export default {
state: { state: {
trees: {}, trees: {},
randomNodes: undefined,
nodebook: undefined, nodebook: undefined,
// LibraryOptions options // LibraryOptions options
@@ -89,6 +91,10 @@ export default {
state.nodeDepartId = id state.nodeDepartId = id
}, },
'SET_RANDOM_NODES' (state, ids) {
state.randomNodes = ids
},
'SET_SEARCH' (state, str) { 'SET_SEARCH' (state, str) {
state.search = str state.search = str
} }
@@ -114,14 +120,8 @@ export default {
}, },
async 'INIT_LIBRARY_MAP' ({ state, commit, dispatch }) { async 'INIT_LIBRARY_MAP' ({ state, commit, dispatch }) {
const departIds = await dispatch('INIT_LIBRARY') await dispatch('INIT_LIBRARY')
const ids = departIds.slice(0, 3) return dispatch('ROLL_RANDOM_NODES')
const departNodes = await dispatch('GET_NODES', { ids, dataLevel: 'partial' })
const relatedIds = getRelatedNodesIds(departNodes, ids)
const relatedNodes = await dispatch('GET_NODES', { ids: relatedIds, dataLevel: 'partial' })
const nodes = [...departNodes, ...relatedNodes]
commit('SET_TAGS_OPTIONS', nodes)
return nodes
}, },
async 'INIT_LIBRARY_LIST' ({ state, commit, dispatch }) { async 'INIT_LIBRARY_LIST' ({ state, commit, dispatch }) {
@@ -186,6 +186,17 @@ export default {
return getters.nodeTree return getters.nodeTree
}, },
async 'ROLL_RANDOM_NODES' ({ state, rootState, commit, dispatch }) {
commit('SET_RANDOM_NODES', undefined)
const ids = getRandomIds(rootState.ids.depart)
const departNodes = await dispatch('GET_NODES', { ids, dataLevel: 'partial' })
const relatedIds = getRelatedNodesIds(departNodes, ids)
const relatedNodes = await dispatch('GET_NODES', { ids: relatedIds, dataLevel: 'partial' })
const nodes = [...departNodes, ...relatedNodes]
commit('SET_RANDOM_NODES', [...ids, ...relatedIds])
commit('SET_TAGS_OPTIONS', nodes)
},
'UPDATE_QUERY_MODE' (store, mode) { 'UPDATE_QUERY_MODE' (store, mode) {
if (router.currentRoute.query.mode === mode) return if (router.currentRoute.query.mode === mode) return
router.push({ query: { mode } }) router.push({ query: { mode } })
@@ -255,8 +266,14 @@ export default {
}) })
}, },
// LibraryMap
randomNodes: (state, getters, rootState) => {
if (state.randomNodes === undefined) return
return state.randomNodes.map(id => rootState.nodes[id])
},
// LibraryTree // LibraryTree
nodeTree: (state, rootState) => { nodeTree: state => {
return state.trees[state.nodeDepartId] || { nodes: [], links: [] } return state.trees[state.nodeDepartId] || { nodes: [], links: [] }
}, },
+12
View File
@@ -74,6 +74,18 @@ export function getRelatedNodesIds (nodes, ignored) {
} }
export function getRandomIds (ids, count = 3) {
const randomIds = []
while (randomIds.length < count) {
const id = ids[Math.floor(Math.random() * ids.length)]
if (!randomIds.includes(id)) {
randomIds.push(id)
}
}
return randomIds
}
export function buildTree (treeData, nodesData) { export function buildTree (treeData, nodesData) {
const uniqueIds = [] const uniqueIds = []
const nodes = [] const nodes = []