Skip to content

Commit 3ac99ec

Browse files
committed
promises for templates
1 parent 8cf2c45 commit 3ac99ec

File tree

6 files changed

+118
-104
lines changed

6 files changed

+118
-104
lines changed

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -190,28 +190,30 @@ qx.Class.define("osparc.dashboard.NewPlusMenu", {
190190
},
191191

192192
__addHypertools: function() {
193-
const hypertools = osparc.store.Templates.getInstance().getTemplatesByType(osparc.data.model.StudyUI.HYPERTOOL_TYPE);
194-
if (hypertools.length) {
195-
const hypertoolsMenuButton = this.self().createMenuButton("@FontAwesome5Solid/star/16", this.tr("Hypertools"));
196-
this.addAt(hypertoolsMenuButton, this.__itemIdx);
197-
this.__itemIdx++;
198-
199-
const hypertoolsMenu = new qx.ui.menu.Menu().set({
200-
appearance: "menu-wider",
201-
});
202-
hypertoolsMenuButton.setMenu(hypertoolsMenu);
203-
204-
hypertools.forEach(templateData => {
205-
const hypertoolButton = this.self().createMenuButton(templateData["icon"], templateData["name"]);
206-
hypertoolButton.addListener("tap", () => {
207-
this.fireDataEvent("newStudyFromTemplateClicked", {
208-
templateData,
209-
newStudyLabel: templateData["name"],
193+
osparc.store.Templates.getTemplatesHypertools()
194+
.then(hypertools => {
195+
if (hypertools.length) {
196+
const hypertoolsMenuButton = this.self().createMenuButton("@FontAwesome5Solid/star/16", this.tr("Hypertools"));
197+
this.addAt(hypertoolsMenuButton, this.__itemIdx);
198+
this.__itemIdx++;
199+
200+
const hypertoolsMenu = new qx.ui.menu.Menu().set({
201+
appearance: "menu-wider",
210202
});
211-
});
212-
hypertoolsMenu.add(hypertoolButton);
203+
hypertoolsMenuButton.setMenu(hypertoolsMenu);
204+
205+
hypertools.forEach(templateData => {
206+
const hypertoolButton = this.self().createMenuButton(templateData["icon"], templateData["name"]);
207+
hypertoolButton.addListener("tap", () => {
208+
this.fireDataEvent("newStudyFromTemplateClicked", {
209+
templateData,
210+
newStudyLabel: templateData["name"],
211+
});
212+
});
213+
hypertoolsMenu.add(hypertoolButton);
214+
});
215+
}
213216
});
214-
}
215217
},
216218

217219
__addOtherTabsAccess: function() {
@@ -321,24 +323,26 @@ qx.Class.define("osparc.dashboard.NewPlusMenu", {
321323
},
322324

323325
__addFromTemplateButton: function(buttonConfig) {
324-
const templates = osparc.store.Templates.getInstance().getTemplates();
325-
const menuButton = this.self().createMenuButton(null, buttonConfig["title"]);
326-
osparc.utils.Utils.setIdToWidget(menuButton, buttonConfig["idToWidget"]);
327-
// disable it until found in templates store
328-
menuButton.setEnabled(false);
329-
330-
let templateMetadata = templates.find(t => t.name === buttonConfig["expectedTemplateLabel"]);
331-
if (templateMetadata) {
332-
menuButton.setEnabled(true);
333-
menuButton.addListener("tap", () => {
334-
this.fireDataEvent("newStudyFromTemplateClicked", {
335-
templateData: templateMetadata,
336-
newStudyLabel: buttonConfig["newStudyLabel"],
337-
});
326+
osparc.store.Templates.getTemplates()
327+
.then(templates => {
328+
const menuButton = this.self().createMenuButton(null, buttonConfig["title"]);
329+
osparc.utils.Utils.setIdToWidget(menuButton, buttonConfig["idToWidget"]);
330+
// disable it until found in templates store
331+
menuButton.setEnabled(false);
332+
333+
let templateMetadata = templates.find(t => t.name === buttonConfig["expectedTemplateLabel"]);
334+
if (templateMetadata) {
335+
menuButton.setEnabled(true);
336+
menuButton.addListener("tap", () => {
337+
this.fireDataEvent("newStudyFromTemplateClicked", {
338+
templateData: templateMetadata,
339+
newStudyLabel: buttonConfig["newStudyLabel"],
340+
});
341+
});
342+
this.__addIcon(menuButton, buttonConfig["icon"], templateMetadata);
343+
this.__addFromResourceButton(menuButton, buttonConfig["category"]);
344+
}
338345
});
339-
this.__addIcon(menuButton, buttonConfig["icon"], templateMetadata);
340-
this.__addFromResourceButton(menuButton, buttonConfig["category"]);
341-
}
342346
},
343347

344348
__addFromServiceButton: function(buttonConfig) {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ qx.Class.define("osparc.dashboard.NewStudies", {
3636
});
3737
this._add(this.__flatList);
3838

39-
const templates = osparc.store.Templates.getInstance().getTemplates()
40-
this.__newStudies = newButtonsInfo.filter(newButtonInfo => {
41-
if (newButtonInfo.showDisabled) {
42-
return true;
43-
}
44-
return templates.find(t => t.name === newButtonInfo.expectedTemplateLabel);
45-
});
39+
osparc.store.Templates.getTemplates()
40+
.then(templates => {
41+
this.__newStudies = newButtonsInfo.filter(newButtonInfo => {
42+
if (newButtonInfo.showDisabled) {
43+
return true;
44+
}
45+
return templates.find(t => t.name === newButtonInfo.expectedTemplateLabel);
46+
});
4647

47-
this.setGroupBy("category");
48+
this.setGroupBy("category");
49+
});
4850
},
4951

5052
properties: {

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -912,24 +912,26 @@ qx.Class.define("osparc.dashboard.StudyBrowser", {
912912
newPlansBtn.setEnabled(true);
913913

914914
newPlansBtn.addListener("tap", () => {
915-
const templates = osparc.store.Templates.getInstance().getTemplates();
916-
if (templates) {
917-
const newStudies = new osparc.dashboard.NewStudies(newStudiesConfig);
918-
const winTitle = this.tr("New Plan");
919-
const win = osparc.ui.window.Window.popUpInWindow(newStudies, winTitle, osparc.dashboard.NewStudies.WIDTH+40, 300).set({
920-
clickAwayClose: false,
921-
resizable: true
922-
});
923-
newStudies.addListener("newStudyClicked", e => {
924-
win.close();
925-
const templateInfo = e.getData();
926-
const templateData = templates.find(t => t.name === templateInfo.expectedTemplateLabel);
927-
if (templateData) {
928-
this.__newPlanBtnClicked(templateData, templateInfo.newStudyLabel);
915+
osparc.store.Templates.getTemplates()
916+
.then(templates => {
917+
if (templates) {
918+
const newStudies = new osparc.dashboard.NewStudies(newStudiesConfig);
919+
const winTitle = this.tr("New Plan");
920+
const win = osparc.ui.window.Window.popUpInWindow(newStudies, winTitle, osparc.dashboard.NewStudies.WIDTH+40, 300).set({
921+
clickAwayClose: false,
922+
resizable: true
923+
});
924+
newStudies.addListener("newStudyClicked", e => {
925+
win.close();
926+
const templateInfo = e.getData();
927+
const templateData = templates.find(t => t.name === templateInfo.expectedTemplateLabel);
928+
if (templateData) {
929+
this.__newPlanBtnClicked(templateData, templateInfo.newStudyLabel);
930+
}
931+
});
932+
osparc.utils.Utils.setIdToWidget(win, "newStudiesWindow");
929933
}
930934
});
931-
osparc.utils.Utils.setIdToWidget(win, "newStudiesWindow");
932-
}
933935
});
934936
}
935937
},

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ qx.Class.define("osparc.dashboard.TemplateBrowser", {
3636
}
3737
this._resourcesInitialized = true;
3838

39-
osparc.store.Templates.getInstance().fetchAllTemplates()
39+
osparc.store.Templates.fetchAllTemplates()
4040
.then(() => {
4141
this._resourcesList = [];
4242
this.getChildControl("resources-layout");
@@ -84,17 +84,13 @@ qx.Class.define("osparc.dashboard.TemplateBrowser", {
8484
__reloadTemplates: function(useCache) {
8585
this.__tasksToCards();
8686

87-
const templatesStore = osparc.store.Templates.getInstance();
8887
if (useCache) {
89-
const templates = templatesStore.getTemplates();
90-
this.__setResourcesToList(templates);
88+
osparc.store.Templates.getTemplates()
89+
.then(templates => this.__setResourcesToList(templates));
9190
} else {
92-
templatesStore.fetchAllTemplates()
91+
osparc.store.Templates.fetchAllTemplates()
9392
.then(templates => this.__setResourcesToList(templates))
94-
.catch(err => {
95-
console.error(err);
96-
this.__setResourcesToList([]);
97-
});
93+
.catch(() => this.__setResourcesToList([]));
9894
}
9995
},
10096

services/static-webserver/client/source/class/osparc/desktop/organizations/TemplatesList.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ qx.Class.define("osparc.desktop.organizations.TemplatesList", {
8686
item.subscribeToFilterGroup("organizationTemplatesList");
8787
item.addListener("openMoreInfo", e => {
8888
const templateId = e.getData()["key"];
89-
const templateData = osparc.store.Templates.getInstance().getTemplate(templateId);
90-
if (templateData) {
91-
templateData["resourceType"] = "template";
92-
const resourceDetails = new osparc.dashboard.ResourceDetails(templateData).set({
93-
showOpenButton: false
89+
osparc.store.Templates.getTemplate(templateId)
90+
.then(templateData => {
91+
if (templateData) {
92+
templateData["resourceType"] = "template";
93+
const resourceDetails = new osparc.dashboard.ResourceDetails(templateData).set({
94+
showOpenButton: false
95+
});
96+
osparc.dashboard.ResourceDetails.popUpInWindow(resourceDetails);
97+
}
9498
});
95-
osparc.dashboard.ResourceDetails.popUpInWindow(resourceDetails);
96-
}
9799
});
98100
}
99101
});
@@ -110,14 +112,16 @@ qx.Class.define("osparc.desktop.organizations.TemplatesList", {
110112
return;
111113
}
112114

113-
const groupId = orgModel.getGroupId();
114-
const templates = osparc.store.Templates.getInstance().getTemplates();
115-
const orgTemplates = templates.filter(template => groupId in template["accessRights"]);
116-
orgTemplates.forEach(orgTemplate => {
117-
const orgTemplateCopy = osparc.utils.Utils.deepCloneObject(orgTemplate);
118-
orgTemplateCopy["orgId"] = groupId;
119-
templatesModel.append(qx.data.marshal.Json.createModel(orgTemplateCopy));
120-
});
115+
osparc.store.Templates.getTemplates()
116+
.then(templates => {
117+
const groupId = orgModel.getGroupId();
118+
const orgTemplates = templates.filter(template => groupId in template["accessRights"]);
119+
orgTemplates.forEach(orgTemplate => {
120+
const orgTemplateCopy = osparc.utils.Utils.deepCloneObject(orgTemplate);
121+
orgTemplateCopy["orgId"] = groupId;
122+
templatesModel.append(qx.data.marshal.Json.createModel(orgTemplateCopy));
123+
});
124+
});
121125
}
122126
}
123127
});

services/static-webserver/client/source/class/osparc/store/Templates.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,46 @@
1616
************************************************************************ */
1717

1818
qx.Class.define("osparc.store.Templates", {
19-
extend: qx.core.Object,
20-
type: "singleton",
19+
type: "static",
2120

22-
construct: function() {
23-
this.base(arguments);
24-
25-
this.__templates = [];
26-
},
27-
28-
members: {
29-
__templates: null,
21+
statics: {
22+
__templatesCached: [],
23+
__templatesPromisesCached: null,
3024

3125
fetchAllTemplates: function() {
32-
if (this.__templates.length) {
33-
return new Promise(resolve => resolve(this.__templates));
34-
}
35-
36-
return osparc.data.Resources.getInstance().getAllPages("templates")
26+
return this.__templatesPromisesCached = osparc.data.Resources.getInstance().getAllPages("templates")
3727
.then(templates => {
3828
this.__templates = templates;
3929
return templates;
30+
})
31+
.catch(err => {
32+
osparc.FlashMessenger.logError(err);
33+
})
34+
.finally(() => {
35+
this.__templatesPromisesCached = null;
4036
});
4137
},
4238

4339
getTemplates: function() {
44-
return this.__templates;
40+
if (this.__templatesPromisesCached) {
41+
return this.__templatesPromisesCached;
42+
}
43+
44+
return new Promise(resolve => resolve(this.__templates));
4545
},
4646

47-
getTemplatesByType: function(type) {
48-
return this.__templates.filter(t => osparc.study.Utils.extractTemplateType(t) === type);
47+
getTemplatesHypertools: function() {
48+
return this.getTemplates()
49+
.then(templates => {
50+
return templates.filter(t => osparc.study.Utils.extractTemplateType(t) === osparc.data.model.StudyUI.HYPERTOOL_TYPE);
51+
});
4952
},
5053

5154
getTemplate: function(templateId) {
52-
return this.__templates.find(t => t.uuid === templateId);
55+
return this.getTemplates()
56+
.then(templates => {
57+
return templates.find(t => t.uuid === templateId);
58+
});
5359
},
5460
}
5561
});

0 commit comments

Comments
 (0)