2017-07-27 22:59:07 +00:00
|
|
|
// @flow
|
|
|
|
import type {BaseModel} from './index';
|
2016-12-01 01:50:59 +00:00
|
|
|
import * as db from '../common/database';
|
2017-03-28 22:45:23 +00:00
|
|
|
import {DEFAULT_SIDEBAR_WIDTH, DEFAULT_PANE_WIDTH, DEFAULT_PANE_HEIGHT} from '../common/constants';
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
export const name = 'Workspace Meta';
|
|
|
|
export const type = 'WorkspaceMeta';
|
|
|
|
export const prefix = 'wrkm';
|
2017-06-15 17:08:24 +00:00
|
|
|
export const canDuplicate = false;
|
2016-12-01 01:50:59 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
type BaseWorkspaceMeta = {
|
|
|
|
activeRequestId: string | null,
|
|
|
|
activeEnvironmentId: string | null,
|
|
|
|
sidebarFilter: string,
|
|
|
|
sidebarHidden: boolean,
|
|
|
|
sidebarWidth: number,
|
|
|
|
paneWidth: number,
|
|
|
|
paneHeight: number,
|
|
|
|
hasSeen: boolean
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WorkspaceMeta = BaseWorkspaceMeta & BaseModel;
|
|
|
|
|
|
|
|
export function init (): BaseWorkspaceMeta {
|
2016-12-01 01:50:59 +00:00
|
|
|
return {
|
|
|
|
parentId: null,
|
|
|
|
activeRequestId: null,
|
|
|
|
activeEnvironmentId: null,
|
|
|
|
sidebarFilter: '',
|
|
|
|
sidebarHidden: false,
|
|
|
|
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,
|
2017-03-28 22:45:23 +00:00
|
|
|
paneWidth: DEFAULT_PANE_WIDTH,
|
2017-07-27 22:59:07 +00:00
|
|
|
paneHeight: DEFAULT_PANE_HEIGHT,
|
|
|
|
hasSeen: true
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
export function migrate (doc: WorkspaceMeta): WorkspaceMeta {
|
2016-12-01 01:50:59 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
export function create (patch: Object = {}): Promise<WorkspaceMeta> {
|
2016-12-01 01:50:59 +00:00
|
|
|
if (!patch.parentId) {
|
2017-07-27 22:59:07 +00:00
|
|
|
throw new Error(`New WorkspaceMeta missing parentId ${JSON.stringify(patch)}`);
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
export function update (workspaceMeta: WorkspaceMeta, patch: Object = {}): Promise<WorkspaceMeta> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.docUpdate(workspaceMeta, patch);
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
export async function getOrCreateByParentId (parentId: string): Promise<WorkspaceMeta> {
|
|
|
|
const doc = await db.getWhere(type, {parentId});
|
|
|
|
return doc || this.create({parentId});
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
export function all (): Promise<Array<WorkspaceMeta>> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|