226 lines
7.2 KiB
JavaScript
226 lines
7.2 KiB
JavaScript
import Vue from 'vue'
|
|
import router from '@/router'
|
|
|
|
import api from '@/api'
|
|
import {
|
|
AllTags
|
|
} from '@/api/queries'
|
|
import {
|
|
parseNodesQueryParams,
|
|
getUniqueNodesIds,
|
|
getRelatedNodesIds,
|
|
buildTree
|
|
} from '@/store/utils.js'
|
|
|
|
|
|
export default {
|
|
state: {
|
|
tags: undefined,
|
|
strangeness: [0, 1, 2, 3, 4],
|
|
nodeDepartId: undefined,
|
|
trees: {},
|
|
mode: undefined,
|
|
nodebook: undefined
|
|
},
|
|
|
|
mutations: {
|
|
// ╷ ╶┬╴┌─╮┌─╮╭─┐┌─╮╷ ╷
|
|
// │ │ │╶┤├┬╯├─┤├┬╯╰─┤
|
|
// ╰─╴╶┴╴└─╯╵ ╰╵ ╵╵ ╰╶─╯
|
|
|
|
'SET_NODEBOOK' (state, nodes) {
|
|
state.nodebook = nodes
|
|
},
|
|
|
|
'ADD_NODEBOOK_NODE' (state, [stack, parentId, childId]) {
|
|
if (stack === undefined) {
|
|
stack = childId === undefined ? [parentId] : [parentId, childId]
|
|
state.nodebook.push(stack)
|
|
} else {
|
|
stack.push(childId)
|
|
}
|
|
},
|
|
|
|
'REMOVE_NODEBOOK_NODE' (state, [stack, nodeId]) {
|
|
if (stack[0] === nodeId) {
|
|
state.nodebook.splice(state.nodebook.indexOf(stack), 1)
|
|
} else {
|
|
stack.splice(stack.indexOf(nodeId), 1)
|
|
}
|
|
},
|
|
|
|
'ADD_NODE_TREE' (state, [id, tree]) {
|
|
Vue.set(state.trees, id, tree)
|
|
},
|
|
|
|
// ╭─╮┌─╮╶┬╴╶┬╴╭─╮╭╮╷╭─╴
|
|
// │ │├─╯ │ │ │ ││││╰─╮
|
|
// ╰─╯╵ ╵ ╶┴╴╰─╯╵╰╯╶─╯
|
|
|
|
'SET_TAGS' (state, tags) {
|
|
state.tags = tags
|
|
},
|
|
|
|
'SET_MODE' (state, mode) {
|
|
state.mode = mode
|
|
},
|
|
|
|
'SET_NODE_DEPART_ID' (state, id) {
|
|
state.nodeDepartId = id
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
// ╷ ╶┬╴┌─╮┌─╮╭─┐┌─╮╷ ╷
|
|
// │ │ │╶┤├┬╯├─┤├┬╯╰─┤
|
|
// ╰─╴╶┴╴└─╯╵ ╰╵ ╵╵ ╰╶─╯
|
|
|
|
async 'INIT_LIBRARY' ({ state, commit, dispatch, rootState }) {
|
|
const departIds = await dispatch('GET_ALL_NODES_IDS', 'depart')
|
|
await dispatch('GET_NODES', { ids: departIds, dataLevel: 'initial' })
|
|
dispatch('GET_ALL_TAGS')
|
|
return departIds
|
|
},
|
|
|
|
async 'INIT_LIBRARY_TREE' ({ state, commit, dispatch, rootState }) {
|
|
const ids = await dispatch('INIT_LIBRARY')
|
|
return dispatch('SET_NODE_DEPART_ID', ids[ids.length - 1])
|
|
},
|
|
|
|
async 'INIT_LIBRARY_MAP' ({ state, commit, dispatch }) {
|
|
const departIds = await dispatch('INIT_LIBRARY')
|
|
const departNodes = await dispatch('GET_NODES', { ids: departIds.slice(0, 3), dataLevel: 'partial' })
|
|
const relatedIds = getRelatedNodesIds(departNodes)
|
|
const relatedNodes = await dispatch('GET_NODES', { ids: relatedIds, dataLevel: 'partial' })
|
|
return [...departNodes, ...relatedNodes]
|
|
},
|
|
|
|
async 'INIT_LIBRARY_LIST' ({ state, commit, dispatch }) {
|
|
const departIds = await dispatch('INIT_LIBRARY')
|
|
return dispatch('GET_NODES', { ids: departIds, dataLevel: 'partial' })
|
|
},
|
|
|
|
async 'UPDATE_NODEBOOK' ({ state, commit, dispatch }, query) {
|
|
const { mode = 'tree', nodebook } = parseNodesQueryParams(query)
|
|
commit('SET_MODE', mode)
|
|
await dispatch('GET_NODES', { ids: [].concat(...nodebook), dataLevel: 'full' })
|
|
commit('SET_NODEBOOK', nodebook)
|
|
},
|
|
|
|
// ╭╮╷╭─╮┌─╮┌─╴╭─╴
|
|
// ││││ ││ │├─╴╰─╮
|
|
// ╵╰╯╰─╯└─╯╰─╴╶─╯
|
|
|
|
'UPDATE_QUERY_NODES' ({ state }, from) {
|
|
const query = {
|
|
mode: state.mode,
|
|
nodes: [...state.nodebook.map(ids => ids.join(','))]
|
|
}
|
|
router.push({ query })
|
|
},
|
|
|
|
async 'OPEN_NODE' ({ state, commit, dispatch }, [parentId, childId]) {
|
|
const stack = state.nodebook.find(stack => stack[0] === parentId)
|
|
if (stack && (childId === undefined || stack.includes(childId))) return
|
|
commit('ADD_NODEBOOK_NODE', [stack, parentId, childId])
|
|
dispatch('UPDATE_QUERY_NODES')
|
|
},
|
|
|
|
'CLOSE_NODE' ({ state, commit, dispatch }, [parentId, childId]) {
|
|
const stack = state.nodebook.find(stack => stack.includes(parentId) && (childId ? stack.includes(childId) : true))
|
|
commit('REMOVE_NODEBOOK_NODE', [stack, childId || parentId])
|
|
dispatch('UPDATE_QUERY_NODES')
|
|
},
|
|
|
|
// ╭─╮┌─╮╶┬╴╶┬╴╭─╮╭╮╷╭─╴
|
|
// │ │├─╯ │ │ │ ││││╰─╮
|
|
// ╰─╯╵ ╵ ╶┴╴╰─╯╵╰╯╶─╯
|
|
|
|
'GET_ALL_TAGS' ({ state, commit }) {
|
|
if (state.tags !== undefined) return state.tags
|
|
return api.query(AllTags).then(data => {
|
|
commit('SET_TAGS', data.tags)
|
|
return state.tags
|
|
})
|
|
},
|
|
|
|
async 'SET_NODE_DEPART_ID' ({ state, rootState, commit, dispatch, getters }, id) {
|
|
if (state.nodeDepartId === id) return
|
|
|
|
commit('SET_NODE_DEPART_ID', id)
|
|
|
|
if (!(id in state.trees)) {
|
|
const treeData = await api.queryRecursiveNodes([id])
|
|
const nodes = await dispatch('GET_NODES', { ids: getUniqueNodesIds(treeData), dataLevel: 'initial' })
|
|
commit('ADD_NODE_TREE', [id, buildTree(treeData, nodes)])
|
|
}
|
|
|
|
return getters.nodeTree
|
|
},
|
|
|
|
'UPDATE_QUERY_MODE' (store, mode) {
|
|
if (router.currentRoute.query.mode === mode) return
|
|
router.push({ query: { mode } })
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
mode: (state) => state.mode,
|
|
|
|
nodebook: (state, getters, rootState) => {
|
|
if (!state.nodebook) return []
|
|
return state.nodebook.map(([parentId, ...childrenIds]) => {
|
|
return {
|
|
parent: rootState.nodes[parentId],
|
|
children: childrenIds.map(id => rootState.nodes[id])
|
|
}
|
|
})
|
|
},
|
|
|
|
activeNodes: (state) => {
|
|
if (!state.nodebook) return []
|
|
return state.nodebook.reduce((acc, ids) => [...acc, ...ids], [])
|
|
},
|
|
|
|
tagsOptions: state => {
|
|
if (state.tags === undefined) return []
|
|
return state.tags.map(tag => ({ value: tag.id, text: tag.name }))
|
|
},
|
|
|
|
nodesDepartsOptions: (state, getters, rootState) => {
|
|
const departIds = rootState.ids.depart
|
|
if (departIds === undefined || rootState.nodes[departIds[0]] === undefined) return
|
|
return departIds.map(id => {
|
|
const text = rootState.nodes[id]
|
|
const firstAuthor = text.authors !== null ? text.authors[0].name : 'Pad de noms'
|
|
return {
|
|
text: `${firstAuthor}, ${text.title} (${id})`,
|
|
value: id
|
|
}
|
|
})
|
|
},
|
|
|
|
orderedTextsDepart: (state, getters, rootState) => {
|
|
const departIds = rootState.ids.depart
|
|
if (departIds === undefined || rootState.nodes[departIds[0]] === undefined) return
|
|
return rootState.ids.depart.map(id => rootState.nodes[id]).sort((a, b) => {
|
|
if (!b.authors) return -1
|
|
if (!a.authors) return +1
|
|
if (a.authors[0].last_name < b.authors[0].last_name) return -1
|
|
if (a.authors[0].last_name > b.authors[0].last_name) return 1
|
|
return 0
|
|
}).reduce((dict, text) => {
|
|
const firstChar = text.authors ? text.authors[0].last_name[0] : 'Pas de noms'
|
|
if (!(firstChar in dict)) dict[firstChar] = []
|
|
dict[firstChar].push(text)
|
|
return dict
|
|
}, {})
|
|
},
|
|
|
|
nodeTree: (state, rootState) => {
|
|
if (state.nodeDepartId === undefined) return
|
|
return state.trees[state.nodeDepartId]
|
|
}
|
|
}
|
|
}
|