Skip to content

Commit e395992

Browse files
✨ [Frontend] Pretty JSON objects (#7710)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 501de76 commit e395992

File tree

8 files changed

+163
-26
lines changed

8 files changed

+163
-26
lines changed

services/static-webserver/client/source/class/osparc/dashboard/CardBase.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,12 @@ qx.Class.define("osparc.dashboard.CardBase", {
11371137
return this.self().filterClassifiers(checks, classifiers);
11381138
},
11391139

1140-
_shouldApplyFilter: function(data) {
1141-
let filterId = "searchBarFilter";
1140+
__curateFilterId: function(filterId) {
11421141
if (this.isPropertyInitialized("resourceType")) {
11431142
switch (this.getResourceType()) {
1143+
case "tutorial":
1144+
filterId += "-template";
1145+
break;
11441146
case "hypertool":
11451147
filterId += "-service";
11461148
break;
@@ -1149,6 +1151,11 @@ qx.Class.define("osparc.dashboard.CardBase", {
11491151
break;
11501152
}
11511153
}
1154+
return filterId;
1155+
},
1156+
1157+
_shouldApplyFilter: function(data) {
1158+
const filterId = this.__curateFilterId("searchBarFilter");
11521159
data = filterId in data ? data[filterId] : data;
11531160
if (this._filterText(data.text)) {
11541161
return true;
@@ -1169,17 +1176,7 @@ qx.Class.define("osparc.dashboard.CardBase", {
11691176
},
11701177

11711178
_shouldReactToFilter: function(data) {
1172-
let filterId = "searchBarFilter";
1173-
if (this.isPropertyInitialized("resourceType")) {
1174-
switch (this.getResourceType()) {
1175-
case "hypertool":
1176-
filterId += "-service";
1177-
break;
1178-
default:
1179-
filterId += "-" + this.getResourceType();
1180-
break;
1181-
}
1182-
}
1179+
const filterId = this.__curateFilterId("searchBarFilter");
11831180
data = filterId in data ? data[filterId] : data;
11841181
if (data.text && data.text.length > 1) {
11851182
return true;

services/static-webserver/client/source/class/osparc/dashboard/Dashboard.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ qx.Class.define("osparc.dashboard.Dashboard", {
5252
osparc.wrapper.Svg.getInstance().init();
5353
osparc.wrapper.JsonDiffPatch.getInstance().init();
5454
osparc.wrapper.JsonTreeViewer.getInstance().init();
55+
osparc.wrapper.JsonFormatter.getInstance().init();
5556
osparc.wrapper.DOMPurify.getInstance().init();
5657
osparc.wrapper.RadialMenu.getInstance().init()
5758
.then(loaded => {

services/static-webserver/client/source/class/osparc/jobs/Info.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ qx.Class.define("osparc.jobs.Info", {
2424

2525
this._setLayout(new qx.ui.layout.VBox());
2626

27-
const jobInfoViewer = this.getChildControl("job-info-viewer");
28-
jobInfoViewer.setJson(info);
27+
const divId = "job-info-viewer";
28+
const htmlEmbed = osparc.wrapper.JsonFormatter.getInstance().createContainer(divId);
29+
this._add(htmlEmbed, {
30+
flex: 1
31+
});
32+
this.addListener("appear", () => {
33+
osparc.wrapper.JsonFormatter.getInstance().setJson(info, divId);
34+
});
2935
},
3036

3137
statics: {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* ************************************************************************
2+
3+
osparc - the simcore frontend
4+
5+
https://osparc.io
6+
7+
Copyright:
8+
2022 IT'IS Foundation, https://itis.swiss
9+
10+
License:
11+
MIT: https://opensource.org/licenses/MIT
12+
13+
Authors:
14+
* Odei Maiz (odeimaiz)
15+
16+
************************************************************************ */
17+
18+
/**
19+
* @asset(jsonFormatter/json-formatter-2.5.23.js)
20+
* @asset(jsonFormatter/json-formatter-2.5.23.css)
21+
* @ignore(JSONFormatter)
22+
*/
23+
24+
/**
25+
* A qooxdoo wrapper for
26+
* <a href='https://azimi.me/json-formatter-js/' target='_blank'>JSONFormatter</a>
27+
*/
28+
29+
qx.Class.define("osparc.wrapper.JsonFormatter", {
30+
extend: qx.core.Object,
31+
type: "singleton",
32+
33+
properties: {
34+
libReady: {
35+
nullable: false,
36+
init: false,
37+
check: "Boolean"
38+
}
39+
},
40+
41+
statics: {
42+
NAME: "JSONFormatter",
43+
VERSION: "2.5.23",
44+
URL: "https://azimi.me/json-formatter-js/",
45+
},
46+
47+
members: {
48+
init: function() {
49+
return new Promise((resolve, reject) => {
50+
if (this.getLibReady()) {
51+
resolve();
52+
return;
53+
}
54+
55+
const jsonFormatterCss = "jsonFormatter/json-formatter-2.5.23.css";
56+
const jsonFormatterCssUri = qx.util.ResourceManager.getInstance().toUri(jsonFormatterCss);
57+
qx.module.Css.includeStylesheet(jsonFormatterCssUri);
58+
59+
// initialize the script loading
60+
const jsonFormatterPath = "jsonFormatter/json-formatter-2.5.23.js";
61+
const dynLoader = new qx.util.DynamicScriptLoader([
62+
jsonFormatterPath
63+
]);
64+
65+
dynLoader.addListenerOnce("ready", () => {
66+
console.log(jsonFormatterPath + " loaded");
67+
this.setLibReady(true);
68+
resolve();
69+
}, this);
70+
71+
dynLoader.addListener("failed", e => {
72+
const data = e.getData();
73+
console.error("failed to load " + data.script);
74+
reject(data);
75+
}, this);
76+
77+
dynLoader.start();
78+
});
79+
},
80+
81+
createContainer: function(divId) {
82+
const container = new qx.ui.embed.Html("<div id='"+divId+"'></div>");
83+
84+
// Inject custom CSS for the JSONFormatter container
85+
const styleId = "json-formatter-custom-style";
86+
if (!document.getElementById(styleId)) {
87+
const color = qx.theme.manager.Color.getInstance().resolve("text");
88+
const style = document.createElement("style");
89+
style.id = styleId;
90+
style.innerHTML = `
91+
#${divId} * {
92+
color: ${color} !important; /* Use osparc text color */
93+
font-family: "Manrope", sans-serif !important; /* Use osparc font */
94+
}
95+
#${divId} .json-formatter-key {
96+
font-size: 13px !important; /* Actually keeping the default size */
97+
}
98+
#${divId} .json-formatter-constructor-name {
99+
display: none !important; /* Hide "Object" and "Array(n)" labels */
100+
}
101+
`;
102+
document.head.appendChild(style);
103+
}
104+
105+
return container
106+
},
107+
108+
setJson: function(jsonObject, divId) {
109+
// Remove previous content
110+
const container = document.getElementById(divId);
111+
container.innerHTML = "";
112+
113+
// Render JSON
114+
const formatter = new JSONFormatter(jsonObject, 2); // 2 = expand depth
115+
container.appendChild(formatter.render());
116+
},
117+
}
118+
});

services/static-webserver/client/source/class/osparc/wrapper/JsonTreeViewer.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ qx.Class.define("osparc.wrapper.JsonTreeViewer", {
5353
members: {
5454
init: function() {
5555
// initialize the script loading
56-
let jsonTreeViewerPath = "jsontreeviewer/jsonTree.js";
57-
let jsonTreeViewerCss = "jsontreeviewer/jsonTree.css";
58-
let jsonTreeViewerCssUri = qx.util.ResourceManager.getInstance().toUri(jsonTreeViewerCss);
56+
const jsonTreeViewerPath = "jsontreeviewer/jsonTree.js";
57+
const jsonTreeViewerCss = "jsontreeviewer/jsonTree.css";
58+
const jsonTreeViewerCssUri = qx.util.ResourceManager.getInstance().toUri(jsonTreeViewerCss);
5959
qx.module.Css.includeStylesheet(jsonTreeViewerCssUri);
60-
let dynLoader = new qx.util.DynamicScriptLoader([
60+
const dynLoader = new qx.util.DynamicScriptLoader([
6161
jsonTreeViewerPath
6262
]);
6363

@@ -67,16 +67,16 @@ qx.Class.define("osparc.wrapper.JsonTreeViewer", {
6767
}, this);
6868

6969
dynLoader.addListener("failed", e => {
70-
let data = e.getData();
70+
const data = e.getData();
7171
console.error("failed to load " + data.script);
7272
}, this);
7373

7474
dynLoader.start();
7575
},
7676

77-
print: function(data, wrapper) {
78-
jsonTree.create(data, wrapper);
79-
// tree.expand();
77+
print: function(jsonObj, domEl) {
78+
const tree = jsonTree.create(jsonObj, domEl);
79+
tree.expand();
8080
}
8181
}
8282
});

services/static-webserver/client/source/resource/jsonFormatter/json-formatter-2.5.23.css

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)