bookmarks/src/store/index.js

117 lines
2.5 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
import Vuex, { Store } from 'vuex'
import Mutations from './mutations'
import Actions from './actions'
import { privateRoutes, publicRoutes } from '../router'
2019-08-29 09:05:29 +00:00
Vue.use(Vuex)
2019-08-29 09:05:29 +00:00
export { mutations } from './mutations'
2019-08-29 09:05:29 +00:00
export { actions } from './actions'
2019-08-29 09:05:29 +00:00
export default new Store({
mutations: Mutations,
actions: Actions,
state: {
public: false,
authToken: null,
2019-08-29 09:05:29 +00:00
fetchState: {
page: 0,
query: {},
reachedEnd: false,
2019-08-29 09:05:29 +00:00
},
loading: {
tags: false,
folders: false,
bookmarks: false,
createBookmark: false,
saveBookmark: false,
createFolder: false,
saveFolder: false,
2019-08-29 09:05:29 +00:00
},
error: null,
notification: null,
2019-08-29 09:05:29 +00:00
settings: {
viewMode: 'list',
sorting: 'lastmodified',
limit: 0,
2019-08-29 09:05:29 +00:00
},
bookmarks: [],
bookmarksById: {},
sharesById: {},
2019-08-29 09:05:29 +00:00
tags: [],
folders: [],
foldersById: {},
tokensByFolder: {},
countsByFolder: {},
2019-08-29 09:05:29 +00:00
selection: {
folders: [],
bookmarks: [],
2019-08-29 09:05:29 +00:00
},
displayNewBookmark: false,
displayNewFolder: false,
displayMoveDialog: false,
sidebar: null,
viewMode: 'list',
2019-08-29 09:05:29 +00:00
},
getters: {
getBookmark: state => id => {
return state.bookmarksById[id]
2019-08-29 09:05:29 +00:00
},
getFolder: state => id => {
if (Number(id) === -1) {
return [{ id: '-1', children: state.folders }]
2019-08-29 09:05:29 +00:00
}
return findFolder(id, state.folders)
},
getSharesOfFolder: state => folderId => {
2020-03-30 12:31:54 +00:00
return Object.values(state.sharesById).filter(share => share.folderId === folderId)
},
getTokenOfFolder: state => folderId => {
return state.tokensByFolder[folderId]
},
getRoutes: state => () => {
if (state.public) {
return publicRoutes
}
return privateRoutes
},
getPermissionsForFolder: (state, getters) => folderId => {
const path = getters.getFolder(folderId)
for (let i = 0; i < path.length; i++) {
const shares = getters.getSharesOfFolder(path[i].id)
if (shares.length) {
return shares[0]
}
}
return {}
},
getPermissionsForBookmark: (state, getters) => bookmarkId => {
const bookmark = getters.getBookmark(bookmarkId)
if (!bookmark) {
return {}
}
return getters.getPermissionsForFolder(bookmark.folders[0])
},
},
})
2019-08-29 09:05:29 +00:00
function findFolder(id, children) {
if (!children || !children.length) return []
const folders = children.filter(folder => Number(folder.id) === Number(id))
2019-08-29 09:05:29 +00:00
if (folders.length) {
return folders
2019-08-29 09:05:29 +00:00
} else {
for (const child of children) {
const folders = findFolder(id, child.children)
2019-08-29 09:05:29 +00:00
if (folders.length) {
folders.push(child)
return folders
2019-08-29 09:05:29 +00:00
}
}
return []
2019-08-29 09:05:29 +00:00
}
}