Skip to content

Commit

Permalink
Merge pull request #259 from LoganDark/to-json
Browse files Browse the repository at this point in the history
Make toJson a public API
  • Loading branch information
nealus authored Oct 2, 2021
2 parents b987152 + dbd0c32 commit 2d27134
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions examples/demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ function TabStorage({tab, layout}: {tab: TabNode, layout: FlexLayout.Layout}) {
width: listBounds.width,
height: listBounds.height,
callback: () => {
const json = dragging instanceof TabNode ? dragging._toJson() as IJsonTabNode : dragging
const json = dragging instanceof TabNode ? dragging.toJson() as IJsonTabNode : dragging

setStoredTabs(tabs => [...tabs, json])

Expand All @@ -541,7 +541,7 @@ function TabStorage({tab, layout}: {tab: TabNode, layout: FlexLayout.Layout}) {
width: itemRect.width,
height: 0,
callback: () => {
const json = dragging instanceof TabNode ? dragging._toJson() as IJsonTabNode : dragging
const json = dragging instanceof TabNode ? dragging.toJson() as IJsonTabNode : dragging

setStoredTabs(tabs => {
const newTabs = [...tabs]
Expand Down
6 changes: 3 additions & 3 deletions src/model/BorderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Orientation from "../Orientation";
import Rect from "../Rect";
import IDraggable from "./IDraggable";
import IDropTarget from "./IDropTarget";
import { IJsonBorderNode } from "./IJsonModel";
import Model, { ILayoutMetrics } from "./Model";
import Node from "./Node";
import SplitterNode from "./SplitterNode";
Expand Down Expand Up @@ -355,12 +356,11 @@ class BorderNode extends Node implements IDropTarget {
this._model._tidy();
}

/** @hidden @internal */
_toJson() {
toJson(): IJsonBorderNode {
const json: any = {};
BorderNode._attributeDefinitions.toJson(json, this._attributes);
json.location = this._location.getName();
json.children = this._children.map((child) => (child as TabNode)._toJson());
json.children = this._children.map((child) => (child as TabNode).toJson());
return json;
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/BorderSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BorderSet {

/** @hidden @internal */
_toJson() {
return this._borders.map((borderNode) => borderNode._toJson());
return this._borders.map((borderNode) => borderNode.toJson());
}

/** @hidden @internal */
Expand Down
8 changes: 3 additions & 5 deletions src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,15 @@ class Model {
* @returns {IJsonModel} json object that represents this model
*/
toJson(): IJsonModel {
const json: any = { global: {}, layout: {} };
Model._attributeDefinitions.toJson(json.global, this._attributes);
const global: any = {};
Model._attributeDefinitions.toJson(global, this._attributes);

// save state of nodes
this.visitNodes((node) => {
node._fireEvent("save", undefined);
});

json.borders = this._borders._toJson();
json.layout = (this._root as RowNode)._toJson();
return json as IJsonModel;
return { global, borders: this._borders._toJson(), layout: (this._root as RowNode).toJson() };
}

getSplitterSize() {
Expand Down
5 changes: 3 additions & 2 deletions src/model/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DropInfo from "../DropInfo";
import Orientation from "../Orientation";
import Rect from "../Rect";
import IDraggable from "./IDraggable";
import { IJsonBorderNode, IJsonRowNode, IJsonTabNode, IJsonTabSetNode } from "./IJsonModel";
import Model, { ILayoutMetrics } from "./Model";

abstract class Node {
Expand Down Expand Up @@ -92,6 +93,8 @@ abstract class Node {
delete this._listeners[event];
}

abstract toJson(): IJsonRowNode | IJsonBorderNode | IJsonTabSetNode | IJsonTabNode | undefined;

/** @hidden @internal */
_setId(id: string) {
this._attributes.id = id;
Expand Down Expand Up @@ -283,8 +286,6 @@ abstract class Node {
abstract _updateAttrs(json: any): void;
/** @hidden @internal */
abstract _getAttributeDefinitions(): AttributeDefinitions;
/** @hidden @internal */
abstract _toJson(): any;
}

export default Node;
6 changes: 3 additions & 3 deletions src/model/RowNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Rect from "../Rect";
import BorderNode from "./BorderNode";
import IDraggable from "./IDraggable";
import IDropTarget from "./IDropTarget";
import { IJsonRowNode } from "./IJsonModel";
import Model, { ILayoutMetrics } from "./Model";
import Node from "./Node";
import SplitterNode from "./SplitterNode";
Expand Down Expand Up @@ -506,14 +507,13 @@ class RowNode extends Node implements IDropTarget {
this._model._tidy();
}

/** @hidden @internal */
_toJson() {
toJson(): IJsonRowNode {
const json: any = {};
RowNode._attributeDefinitions.toJson(json, this._attributes);

json.children = [];
this._children.forEach((child) => {
json.children.push(child._toJson());
json.children.push(child.toJson());
});

return json;
Expand Down
3 changes: 1 addition & 2 deletions src/model/SplitterNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class SplitterNode extends Node {
return new AttributeDefinitions();
}

/** @hidden @internal */
_toJson(): any {
toJson(): undefined {
return undefined;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/TabNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import AttributeDefinitions from "../AttributeDefinitions";
import Rect from "../Rect";
import BorderNode from "./BorderNode";
import IDraggable from "./IDraggable";
import { IJsonTabNode } from "./IJsonModel";
import Model, { ILayoutMetrics } from "./Model";
import Node from "./Node";
import TabSetNode from "./TabSetNode";
Expand Down Expand Up @@ -174,8 +175,7 @@ class TabNode extends Node implements IDraggable {
this._fireEvent("close", {});
}

/** @hidden @internal */
_toJson() {
toJson(): IJsonTabNode {
const json = {};
TabNode._attributeDefinitions.toJson(json, this._attributes);
return json;
Expand Down
6 changes: 3 additions & 3 deletions src/model/TabSetNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Rect from "../Rect";
import BorderNode from "./BorderNode";
import IDraggable from "./IDraggable";
import IDropTarget from "./IDropTarget";
import { IJsonTabSetNode } from "./IJsonModel";
import Model, { ILayoutMetrics } from "./Model";
import Node from "./Node";
import RowNode from "./RowNode";
Expand Down Expand Up @@ -430,11 +431,10 @@ class TabSetNode extends Node implements IDraggable, IDropTarget {
this._model._tidy();
}

/** @hidden @internal */
_toJson(): any {
toJson(): IJsonTabSetNode {
const json: any = {};
TabSetNode._attributeDefinitions.toJson(json, this._attributes);
json.children = this._children.map((child) => child._toJson());
json.children = this._children.map((child) => child.toJson());

if (this.isActive()) {
json.active = true;
Expand Down

0 comments on commit 2d27134

Please sign in to comment.