UNPKG

@finos/legend-data-cube

Version:
170 lines 5.37 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { uuid } from '@finos/legend-shared'; import { action, computed, makeObservable, observable, runInAction, } from 'mobx'; export class LayoutConfiguration { title; contentRenderer; window = {}; constructor(title, contentRenderer) { this.contentRenderer = contentRenderer; this.title = title; } } export class WindowState { uuid = uuid(); configuration; onClose; ownerId; specification; constructor(configuration, onClose, ownerId) { this.configuration = configuration; this.onClose = onClose; this.ownerId = ownerId; } setSpecification(val) { this.specification = val; } } export const WINDOW_DEFAULT_OFFSET = 50; export const WINDOW_DEFAULT_WIDTH = 800; export const WINDOW_DEFAULT_HEIGHT = 600; export const WINDOW_DEFAULT_MIN_WIDTH = 300; export const WINDOW_DEFAULT_MIN_HEIGHT = 300; export const DEFAULT_TOOL_PANEL_WINDOW_CONFIG = { width: WINDOW_DEFAULT_WIDTH, height: WINDOW_DEFAULT_HEIGHT, minWidth: WINDOW_DEFAULT_MIN_WIDTH, minHeight: WINDOW_DEFAULT_MIN_HEIGHT, center: true, }; export const DEFAULT_ALERT_WINDOW_CONFIG = { width: 500, height: 200, minWidth: 200, minHeight: 80, center: true, }; export class DisplayState { layout; configuration; ownerId; window; constructor(layout, title, contentRenderer, ownerId) { makeObservable(this, { window: observable, isOpen: computed, open: action, close: action, }); this.layout = layout; this.configuration = new LayoutConfiguration(title, contentRenderer); this.configuration.window = DEFAULT_TOOL_PANEL_WINDOW_CONFIG; this.ownerId = ownerId; } get isOpen() { return Boolean(this.window); } open() { if (this.window) { this.layout.bringWindowFront(this.window); } else { this.window = new WindowState(this.configuration, () => runInAction(() => { this.window = undefined; }), this.ownerId); this.layout.newWindow(this.window, this.ownerId); } } close() { if (this.window) { this.layout.closeWindow(this.window); this.window = undefined; } } } export class LayoutManager { // TODO?: keep a hashmap, in parallel, for faster lookup windows = []; constructor() { makeObservable(this, { windows: observable, newWindow: action, bringWindowFront: action, closeWindow: action, closeWindows: action, }); } newDisplay(title, contentRenderer, windowConfiguration, ownerId) { const display = new DisplayState(this, title, contentRenderer, ownerId); if (windowConfiguration) { display.configuration.window = windowConfiguration; } return display; } newWindow(window, ownerId) { window.ownerId = window.ownerId ?? ownerId; this.windows.push(window); } bringWindowFront(window) { const matchingWindow = this.windows.find((w) => w.uuid === window.uuid); if (matchingWindow) { this.windows = this.windows.filter((w) => w.uuid !== window.uuid); this.windows.push(window); } } closeWindow(window) { const matchingWindow = this.windows.find((w) => w.uuid === window.uuid); if (matchingWindow) { this.windows = this.windows.filter((w) => w.uuid !== window.uuid); window.onClose?.(); } } closeWindows(windows) { this.windows = this.windows.filter((window) => !windows.find((w) => w.uuid === window.uuid)); windows.forEach((window) => window.onClose?.()); } } export class DataCubeLayoutService { uuid = uuid(); manager; constructor(manager) { makeObservable(this, { windows: computed, }); this.manager = manager ?? new LayoutManager(); } get windows() { return this.manager.windows.filter((window) => window.ownerId === this.uuid); } newDisplay(title, contentRenderer, windowConfiguration) { return this.manager.newDisplay(title, contentRenderer, windowConfiguration, this.uuid); } newWindow(window) { this.manager.newWindow(window, this.uuid); } bringWindowFront(window) { this.manager.bringWindowFront(window); } closeWindow(window) { this.manager.closeWindow(window); } dispose() { // close all windows owned by this service this.manager.closeWindows(this.windows); } } //# sourceMappingURL=DataCubeLayoutService.js.map