Skip to content

Commit

Permalink
Merge pull request #522 from MetaCell/feature/getWidget-on-layoutmanager
Browse files Browse the repository at this point in the history
Reintroduce "getWidget" for LayoutManager
  • Loading branch information
enicolasgomez authored Nov 22, 2023
2 parents 302f0fc + d160a97 commit 4a7a402
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions geppetto.js/geppetto-client/src/common/layout/LayoutManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let instance: LayoutManager = null;
*/

class LayoutManager {

model: Model;
/**
* Used to restore weights from the default layout
Expand All @@ -72,7 +72,7 @@ class LayoutManager {
tabsetIconFactory: TabsetIconFactory = null,
isMinimizeEnabled = false
) {

this.setLayout(model);
this.widgetFactory = new WidgetFactory(componentMap);
this.tabsetIconFactory = tabsetIconFactory
Expand Down Expand Up @@ -102,7 +102,7 @@ class LayoutManager {
* @param {Widget} widgetConfiguration widget to add
*/
addWidget(widgetConfiguration: Widget) {
if (getWidget(this.store, widgetConfiguration.id) && this.model.getNodeById(widgetConfiguration.id)) {
if (this.getWidget(widgetConfiguration.id) && this.model.getNodeById(widgetConfiguration.id)) {
return this.updateWidget(widgetConfiguration);
}
const { model } = this;
Expand All @@ -116,7 +116,7 @@ class LayoutManager {
widget2Node(widgetConfiguration),
widgetConfiguration.panelName,
DockLocation.CENTER,
widgetConfiguration.pos
widgetConfiguration.pos
)
);
}
Expand Down Expand Up @@ -267,7 +267,7 @@ class LayoutManager {

// This is a hack to unlock transitory state in the model before any other action is dispatched. See https://metacell.atlassian.net/browse/GEP-126
this.model.doAction(Actions.UPDATE_MODEL_ATTRIBUTES, {});

this.store = store;
this.widgetFactory.setStore(store)
this.minimizeHelper.setStore(store);
Expand All @@ -285,10 +285,10 @@ class LayoutManager {
break;
}
case layoutActions.UPDATE_WIDGET: {

let updatedWidget = this.updateWidget(action.data);
action = { ...action, data: updatedWidget };

break;
}
case layoutActions.DESTROY_WIDGET: {
Expand All @@ -301,7 +301,7 @@ class LayoutManager {
}
case layoutActions.ACTIVATE_WIDGET: {
action.data.status = WidgetStatus.ACTIVE;
const widget = getWidget(this.store, action.data.id)
const widget = this.getWidget(action.data.id)
widget.status = WidgetStatus.ACTIVE;
this.updateWidget(widget);
break;
Expand Down Expand Up @@ -345,11 +345,11 @@ class LayoutManager {
};

restoreWeight(node: FlexLayout.Node) {

if(node._getAttr("weight") == 0) {
node._setWeight(this.defaultWeights[node.getId()] ?? 50);
}

if(node.getParent()) {
this.restoreWeight(node.getParent());
}
Expand All @@ -361,8 +361,8 @@ class LayoutManager {
node._setWeight(0);
return true;
} else {


let empty = true;
for(let child of node.getChildren()) {
empty = this.fixRowRecursive(child) && empty;
Expand All @@ -373,7 +373,7 @@ class LayoutManager {
} else {
node._setWeight(0);
}

return empty;
}
}
Expand Down Expand Up @@ -420,6 +420,16 @@ class LayoutManager {
return Object.values(this.store.getState().widgets)
}

/**
* Get specific widget.
*
* @param id
* @private
*/
private getWidget(id): Widget {
return getWidget(this.store, id)
}

/**
* Handles state update related to actions in the flex layout
* (e.g. select or move tab)
Expand All @@ -434,14 +444,14 @@ class LayoutManager {
case Actions.SET_ACTIVE_TABSET:
break;
case Actions.SELECT_TAB:
const widget = getWidget(this.store, action.data.tabNode);
const widget = this.getWidget(action.data.tabNode);
if (widget && widget.status === WidgetStatus.MINIMIZED) {
this.minimizeHelper.restoreWidget(widget);
}

break;
case Actions.DELETE_TAB: {
if (getWidget(this.store, action.data.node).hideOnClose) {
if (this.getWidget(action.data.node).hideOnClose) {
// widget only minimized, won't be removed from layout nor widgets list
this.minimizeHelper.minimizeWidget(action.data.node);
defaultAction = false;
Expand Down Expand Up @@ -475,7 +485,7 @@ class LayoutManager {

const newModel = this.model.toJson();
if (oldModel !== newModel) {

this.store.dispatch(updateLayout(this.model));
}
}
Expand All @@ -488,7 +498,7 @@ class LayoutManager {
*/
private updateWidget(widget: Widget) {
const { model } = this;
const previousWidget = getWidget(this.store, widget.id);
const previousWidget = this.getWidget(widget.id);
const mergedWidget = { ...previousWidget, ...widget }

const widgetRestored = this.minimizeHelper.restoreWidgetIfNecessary(previousWidget, mergedWidget);
Expand All @@ -499,17 +509,17 @@ class LayoutManager {
this.widgetFactory.updateWidget(mergedWidget);

const node = this.model.getNodeById(widget.id);


if (node) {
model.doAction(Actions.updateNodeAttributes(mergedWidget.id, widget2Node(mergedWidget)));
if (mergedWidget.status == WidgetStatus.ACTIVE) {
model.doAction(FlexLayout.Actions.selectTab(mergedWidget.id));
}
if((widget.status == WidgetStatus.MAXIMIZED && !node.getParent().isMaximized()) ||
}
if((widget.status == WidgetStatus.MAXIMIZED && !node.getParent().isMaximized()) ||
(widget.status == WidgetStatus.ACTIVE && node.getParent().isMaximized())) {
this.model.doAction(FlexLayout.Actions.maximizeToggle(node.getParent().getId()));
}
}
else if(widget.status == WidgetStatus.MINIMIZED && !this.minimizeHelper.isMinimized(widget)) {
this.minimizeHelper.minimizeWidget(node.getId());
}
Expand Down

0 comments on commit 4a7a402

Please sign in to comment.