2020-05-17 19:34:56 +02:00
|
|
|
'use strict';
|
2020-05-18 18:06:32 +02:00
|
|
|
import Connection from '@/ipc-api/Connection';
|
2020-06-05 21:00:15 +02:00
|
|
|
import { uidGen } from 'common/libs/utilities';
|
|
|
|
|
2020-05-31 17:56:33 +02:00
|
|
|
function remapStructure (structure) {
|
|
|
|
const databases = structure.map(table => table.TABLE_SCHEMA)
|
|
|
|
.filter((value, index, self) => self.indexOf(value) === index);
|
|
|
|
|
|
|
|
return databases.map(db => {
|
|
|
|
return {
|
2020-06-05 21:00:15 +02:00
|
|
|
name: db,
|
2020-05-31 17:56:33 +02:00
|
|
|
tables: structure.filter(table => table.TABLE_SCHEMA === db)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2020-05-17 19:34:56 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
strict: true,
|
|
|
|
state: {
|
|
|
|
workspaces: [],
|
2020-05-18 18:06:32 +02:00
|
|
|
selected_workspace: null
|
2020-05-17 19:34:56 +02:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getSelected: state => {
|
2020-05-18 18:06:32 +02:00
|
|
|
if (state.selected_workspace) return state.selected_workspace;
|
2020-05-17 19:34:56 +02:00
|
|
|
if (state.workspaces.length) return state.workspaces[0].uid;
|
|
|
|
return null;
|
|
|
|
},
|
2020-05-20 18:00:14 +02:00
|
|
|
getWorkspace: state => uid => {
|
|
|
|
const workspace = state.workspaces.filter(workspace => workspace.uid === uid);
|
|
|
|
return workspace.length ? workspace[0] : {};
|
|
|
|
},
|
|
|
|
getConnected: state => {
|
|
|
|
return state.workspaces
|
|
|
|
.filter(workspace => workspace.connected)
|
|
|
|
.map(workspace => workspace.uid);
|
|
|
|
}
|
2020-05-17 19:34:56 +02:00
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
SELECT_WORKSPACE (state, uid) {
|
2020-05-18 18:06:32 +02:00
|
|
|
state.selected_workspace = uid;
|
2020-05-17 19:34:56 +02:00
|
|
|
},
|
2020-05-19 18:06:05 +02:00
|
|
|
ADD_CONNECTED (state, { uid, structure }) {
|
2020-05-20 18:00:14 +02:00
|
|
|
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, structure, connected: true } : workspace);
|
2020-05-17 19:34:56 +02:00
|
|
|
},
|
|
|
|
REMOVE_CONNECTED (state, uid) {
|
2020-05-20 18:00:14 +02:00
|
|
|
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, structure: {}, connected: false } : workspace);
|
|
|
|
},
|
2020-05-31 17:56:33 +02:00
|
|
|
REFRESH_STRUCTURE (state, { uid, structure }) {
|
2020-05-20 18:00:14 +02:00
|
|
|
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, structure } : workspace);
|
2020-05-18 18:06:32 +02:00
|
|
|
},
|
|
|
|
ADD_WORKSPACE (state, workspace) {
|
|
|
|
state.workspaces.push(workspace);
|
2020-06-05 21:00:15 +02:00
|
|
|
},
|
|
|
|
CHANGE_BREADCRUMBS (state, { uid, breadcrumbs }) {
|
|
|
|
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, breadcrumbs } : workspace);
|
|
|
|
},
|
|
|
|
NEW_TAB (state, uid) {
|
|
|
|
const newTab = {
|
|
|
|
uid: uidGen(),
|
|
|
|
selected: false
|
|
|
|
};
|
|
|
|
state.workspaces = state.workspaces.map(workspace => {
|
|
|
|
if (workspace.uid === uid) {
|
|
|
|
return {
|
|
|
|
...workspace,
|
|
|
|
tabs: [...workspace.tabs, newTab]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return workspace;
|
|
|
|
});
|
2020-05-17 19:34:56 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
selectWorkspace ({ commit }, uid) {
|
|
|
|
commit('SELECT_WORKSPACE', uid);
|
|
|
|
},
|
2020-05-19 18:06:05 +02:00
|
|
|
async connectWorkspace ({ dispatch, commit }, connection) {
|
|
|
|
try {
|
|
|
|
const { status, response } = await Connection.connect(connection);
|
|
|
|
if (status === 'error')
|
|
|
|
dispatch('notifications/addNotification', { status, message: response }, { root: true });
|
|
|
|
else
|
2020-05-31 17:56:33 +02:00
|
|
|
commit('ADD_CONNECTED', { uid: connection.uid, structure: remapStructure(response) });
|
2020-05-19 18:06:05 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
|
|
|
|
}
|
2020-05-17 19:34:56 +02:00
|
|
|
},
|
2020-05-20 18:00:14 +02:00
|
|
|
async refreshStructure ({ dispatch, commit }, uid) {
|
|
|
|
try {
|
|
|
|
const { status, response } = await Connection.refresh(uid);
|
|
|
|
if (status === 'error')
|
|
|
|
dispatch('notifications/addNotification', { status, message: response }, { root: true });
|
|
|
|
else
|
2020-05-31 17:56:33 +02:00
|
|
|
commit('REFRESH_STRUCTURE', { uid, structure: remapStructure(response) });
|
2020-05-20 18:00:14 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
|
|
|
|
}
|
|
|
|
},
|
2020-06-05 21:00:15 +02:00
|
|
|
removeConnected ({ commit }, uid) {
|
2020-05-18 18:06:32 +02:00
|
|
|
Connection.disconnect(uid);
|
2020-05-17 19:34:56 +02:00
|
|
|
commit('REMOVE_CONNECTED', uid);
|
2020-05-18 18:06:32 +02:00
|
|
|
},
|
2020-06-05 21:00:15 +02:00
|
|
|
addWorkspace ({ commit, dispatch, getters }, uid) {
|
2020-05-18 18:06:32 +02:00
|
|
|
const workspace = {
|
|
|
|
uid,
|
2020-05-19 18:06:05 +02:00
|
|
|
connected: false,
|
|
|
|
tabs: [],
|
2020-06-05 21:00:15 +02:00
|
|
|
structure: {},
|
|
|
|
breadcrumbs: {}
|
2020-05-18 18:06:32 +02:00
|
|
|
};
|
2020-06-05 21:00:15 +02:00
|
|
|
|
2020-05-18 18:06:32 +02:00
|
|
|
commit('ADD_WORKSPACE', workspace);
|
2020-06-05 21:00:15 +02:00
|
|
|
|
|
|
|
if (!getters.getWorkspace(uid).tabs.length)
|
|
|
|
dispatch('newTab', uid);
|
|
|
|
},
|
|
|
|
changeBreadcrumbs ({ commit, getters }, payload) {
|
|
|
|
commit('CHANGE_BREADCRUMBS', { uid: getters.getSelected, breadcrumbs: payload });
|
|
|
|
},
|
|
|
|
newTab ({ commit }, uid) {
|
|
|
|
commit('NEW_TAB', uid);
|
2020-05-17 19:34:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|