antares/src/renderer/store/modules/workspaces.store.js

335 lines
11 KiB
JavaScript
Raw Normal View History

'use strict';
2020-05-18 18:06:32 +02:00
import Connection from '@/ipc-api/Connection';
2020-09-25 12:39:58 +02:00
import Database from '@/ipc-api/Database';
import { uidGen } from 'common/libs/uidGen';
const tabIndex = [];
2020-12-03 13:00:54 +01:00
let lastBreadcrumbs = {};
2020-06-05 21:00:15 +02:00
export default {
namespaced: true,
strict: true,
state: {
workspaces: [],
2020-12-03 13:00:54 +01:00
selected_workspace: null,
has_unsaved_changes: false
},
getters: {
getSelected: state => {
2020-05-18 18:06:32 +02:00
if (state.selected_workspace) return state.selected_workspace;
if (state.workspaces.length) return state.workspaces[0].uid;
return null;
},
2020-05-20 18:00:14 +02:00
getWorkspace: state => uid => {
return state.workspaces.find(workspace => workspace.uid === uid);
},
2020-09-25 12:39:58 +02:00
getDatabaseVariable: state => (uid, name) => {
return state.workspaces.find(workspace => workspace.uid === uid).variables.find(variable => variable.name === name);
},
getWorkspaceTab: (state, getters) => tUid => {
if (!getters.getSelected) return;
const workspace = state.workspaces.find(workspace => workspace.uid === getters.getSelected);
if ('tabs' in workspace)
return workspace.tabs.find(tab => tab.uid === tUid);
return {};
2020-05-20 18:00:14 +02:00
},
getConnected: state => {
return state.workspaces
.filter(workspace => workspace.connected)
.map(workspace => workspace.uid);
}
},
mutations: {
SELECT_WORKSPACE (state, uid) {
2020-05-18 18:06:32 +02:00
state.selected_workspace = uid;
},
2020-12-01 16:48:20 +01:00
ADD_CONNECTED (state, { uid, client, dataTypes, indexTypes, structure }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
client,
dataTypes,
2020-12-01 16:48:20 +01:00
indexTypes,
2020-11-13 12:39:40 +01:00
structure,
connected: true
}
: workspace);
},
REMOVE_CONNECTED (state, uid) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure: {},
connected: false
}
: workspace);
2020-05-20 18:00:14 +02:00
},
2020-05-31 17:56:33 +02:00
REFRESH_STRUCTURE (state, { uid, structure }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
structure
}
: workspace);
2020-05-18 18:06:32 +02:00
},
2020-09-25 12:39:58 +02:00
REFRESH_COLLATIONS (state, { uid, collations }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
collations
}
: workspace);
2020-09-25 12:39:58 +02:00
},
REFRESH_VARIABLES (state, { uid, variables }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
variables
}
: workspace);
},
2020-11-16 17:16:39 +01:00
REFRESH_ENGINES (state, { uid, engines }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
engines
}
: 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 }) {
2020-11-13 12:39:40 +01:00
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid
? {
...workspace,
breadcrumbs
}
: workspace);
2020-06-05 21:00:15 +02:00
},
NEW_TAB (state, uid) {
tabIndex[uid] = tabIndex[uid] ? ++tabIndex[uid] : 1;
2020-06-05 21:00:15 +02:00
const newTab = {
uid: uidGen('T'),
index: tabIndex[uid],
2020-06-12 18:10:45 +02:00
selected: false,
type: 'query',
fields: [],
keyUsage: []
2020-06-05 21:00:15 +02:00
};
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: [...workspace.tabs, newTab]
};
}
else
return workspace;
});
2020-06-12 18:10:45 +02:00
},
2020-08-19 18:20:57 +02:00
REMOVE_TAB (state, { uid, tab: tUid }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === uid) {
return {
...workspace,
tabs: workspace.tabs.filter(tab => tab.uid !== tUid)
};
}
else
return workspace;
});
},
2020-06-12 18:10:45 +02:00
SELECT_TAB (state, { uid, tab }) {
state.workspaces = state.workspaces.map(workspace => workspace.uid === uid ? { ...workspace, selected_tab: tab } : workspace);
},
SET_TAB_FIELDS (state, { cUid, tUid, fields }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === cUid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
return { ...tab, fields };
else
return tab;
})
};
}
else
return workspace;
});
},
SET_TAB_KEY_USAGE (state, { cUid, tUid, keyUsage }) {
state.workspaces = state.workspaces.map(workspace => {
if (workspace.uid === cUid) {
return {
...workspace,
tabs: workspace.tabs.map(tab => {
if (tab.uid === tUid)
return { ...tab, keyUsage };
else
return tab;
})
};
}
else
return workspace;
});
}
},
actions: {
2020-11-13 12:39:40 +01:00
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-11-13 12:39:40 +01:00
let dataTypes = [];
2020-12-01 16:48:20 +01:00
let indexTypes = [];
2020-11-13 12:39:40 +01:00
switch (connection.client) {
case 'mysql':
case 'maria':
dataTypes = require('common/data-types/mysql');
2020-12-01 16:48:20 +01:00
indexTypes = require('common/index-types/mysql');
2020-11-13 12:39:40 +01:00
break;
}
2020-11-13 15:04:51 +01:00
commit('ADD_CONNECTED', {
2020-11-13 12:39:40 +01:00
uid: connection.uid,
client: connection.client,
dataTypes,
2020-12-01 16:48:20 +01:00
indexTypes,
2020-11-13 12:39:40 +01:00
structure: response
});
dispatch('refreshCollations', connection.uid);
2020-09-25 12:39:58 +02:00
dispatch('refreshVariables', connection.uid);
2020-11-16 17:16:39 +01:00
dispatch('refreshEngines', connection.uid);
}
2020-05-19 18:06:05 +02:00
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-05-20 18:00:14 +02:00
async refreshStructure ({ dispatch, commit }, uid) {
try {
2020-09-25 12:39:58 +02:00
const { status, response } = await Database.getStructure(uid);
2020-05-20 18:00:14 +02:00
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_STRUCTURE', { uid, structure: response });
2020-05-20 18:00:14 +02:00
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
async refreshCollations ({ dispatch, commit }, uid) {
try {
2020-09-25 12:39:58 +02:00
const { status, response } = await Database.getCollations(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_COLLATIONS', { uid, collations: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-09-25 12:39:58 +02:00
async refreshVariables ({ dispatch, commit }, uid) {
try {
const { status, response } = await Database.getVariables(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_VARIABLES', { uid, variables: response });
}
catch (err) {
dispatch('notifications/addNotification', { status: 'error', message: err.stack }, { root: true });
}
},
2020-11-16 17:16:39 +01:00
async refreshEngines ({ dispatch, commit }, uid) {
try {
const { status, response } = await Database.getEngines(uid);
if (status === 'error')
dispatch('notifications/addNotification', { status, message: response }, { root: true });
else
commit('REFRESH_ENGINES', { uid, engines: response });
}
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);
commit('REMOVE_CONNECTED', uid);
2020-06-13 18:14:32 +02:00
commit('SELECT_TAB', { uid, tab: 0 });
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,
2020-06-13 18:14:32 +02:00
selected_tab: 0,
tabs: [{
2020-08-19 18:20:57 +02:00
uid: 'data',
type: 'table',
fields: [],
keyUsage: []
},
{
uid: 'prop',
type: 'table',
fields: [],
keyUsage: []
}],
2020-06-05 21:00:15 +02:00
structure: {},
2020-09-25 12:39:58 +02:00
variables: [],
collations: [],
2020-06-05 21:00:15 +02:00
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
2020-08-19 18:20:57 +02:00
if (getters.getWorkspace(uid).tabs.length < 3)
2020-06-05 21:00:15 +02:00
dispatch('newTab', uid);
},
changeBreadcrumbs ({ commit, getters }, payload) {
2020-12-03 13:00:54 +01:00
const breadcrumbsObj = {
schema: null,
table: null,
trigger: null,
procedure: null,
scheduler: null
};
const hasLastChildren = Object.keys(lastBreadcrumbs).filter(b => b !== 'schema').some(b => lastBreadcrumbs[b]);
const hasChildren = Object.keys(payload).filter(b => b !== 'schema').some(b => payload[b]);
if (lastBreadcrumbs.schema === payload.schema && hasLastChildren && !hasChildren) return;
2020-12-03 13:00:54 +01:00
if (lastBreadcrumbs.schema !== payload.schema)
Database.useSchema({ uid: getters.getSelected, schema: payload.schema });
2020-12-03 13:00:54 +01:00
commit('CHANGE_BREADCRUMBS', { uid: getters.getSelected, breadcrumbs: { ...breadcrumbsObj, ...payload } });
lastBreadcrumbs = { ...breadcrumbsObj, ...payload };
2020-06-05 21:00:15 +02:00
},
newTab ({ commit }, uid) {
commit('NEW_TAB', uid);
2020-06-12 18:10:45 +02:00
},
2020-08-19 18:20:57 +02:00
removeTab ({ commit }, payload) {
commit('REMOVE_TAB', payload);
},
2020-06-12 18:10:45 +02:00
selectTab ({ commit }, payload) {
commit('SELECT_TAB', payload);
},
setTabFields ({ commit }, payload) {
commit('SET_TAB_FIELDS', payload);
},
setTabKeyUsage ({ commit }, payload) {
commit('SET_TAB_KEY_USAGE', payload);
}
}
};