|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2024 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 | +qx.Class.define("osparc.store.Tags", { |
| 19 | + extend: qx.core.Object, |
| 20 | + type: "singleton", |
| 21 | + |
| 22 | + construct: function() { |
| 23 | + this.base(arguments); |
| 24 | + |
| 25 | + this.tagsCached = []; |
| 26 | + }, |
| 27 | + |
| 28 | + events: { |
| 29 | + "tagAdded": "qx.event.type.Data", |
| 30 | + "tagRemoved": "qx.event.type.Data", |
| 31 | + }, |
| 32 | + |
| 33 | + members: { |
| 34 | + tagsCached: null, |
| 35 | + |
| 36 | + fetchTags: function() { |
| 37 | + if (osparc.auth.Data.getInstance().isGuest()) { |
| 38 | + return new Promise(resolve => { |
| 39 | + resolve([]); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + return osparc.data.Resources.get("tags") |
| 44 | + .then(tagsData => { |
| 45 | + const tags = []; |
| 46 | + tagsData.forEach(tagData => { |
| 47 | + const tag = this.__addToCache(tagData); |
| 48 | + tags.push(tag); |
| 49 | + }); |
| 50 | + return tags; |
| 51 | + }); |
| 52 | + }, |
| 53 | + |
| 54 | + postTag: function(name, parentTagId = null, workspaceId = null) { |
| 55 | + const newTagData = { |
| 56 | + name, |
| 57 | + parentTagId, |
| 58 | + workspaceId, |
| 59 | + }; |
| 60 | + const params = { |
| 61 | + data: newTagData |
| 62 | + }; |
| 63 | + return osparc.data.Resources.getInstance().fetch("tags", "post", params) |
| 64 | + .then(tagData => { |
| 65 | + const tag = this.__addToCache(tagData); |
| 66 | + this.fireDataEvent("tagAdded", tag); |
| 67 | + return tag; |
| 68 | + }); |
| 69 | + }, |
| 70 | + |
| 71 | + deleteTag: function(tagId, workspaceId) { |
| 72 | + const params = { |
| 73 | + "url": { |
| 74 | + tagId |
| 75 | + } |
| 76 | + }; |
| 77 | + return osparc.data.Resources.getInstance().fetch("tags", "delete", params) |
| 78 | + .then(() => { |
| 79 | + const tag = this.getTag(tagId); |
| 80 | + if (tag) { |
| 81 | + this.__deleteFromCache(tagId, workspaceId); |
| 82 | + this.fireDataEvent("tagRemoved", tag); |
| 83 | + } |
| 84 | + }) |
| 85 | + .catch(console.error); |
| 86 | + }, |
| 87 | + |
| 88 | + putTag: function(tagId, updateData) { |
| 89 | + const params = { |
| 90 | + url: { |
| 91 | + tagId |
| 92 | + }, |
| 93 | + data: updateData |
| 94 | + }; |
| 95 | + return osparc.data.Resources.getInstance().fetch("tags", "update", params) |
| 96 | + .then(tagData => { |
| 97 | + this.__addToCache(tagData); |
| 98 | + }) |
| 99 | + .catch(console.error); |
| 100 | + }, |
| 101 | + |
| 102 | + getTag: function(tagId = null) { |
| 103 | + return this.tagsCached.find(f => f.getTagId() === tagId); |
| 104 | + }, |
| 105 | + |
| 106 | + __addToCache: function(tagData) { |
| 107 | + let tag = this.tagsCached.find(f => f.getTagId() === tagData["tagId"] && f.getWorkspaceId() === tagData["workspaceId"]); |
| 108 | + if (tag) { |
| 109 | + const props = Object.keys(qx.util.PropertyUtil.getProperties(osparc.data.model.Tag)); |
| 110 | + // put |
| 111 | + Object.keys(tagData).forEach(key => { |
| 112 | + if (props.includes(key)) { |
| 113 | + tag.set(key, tagData[key]); |
| 114 | + } |
| 115 | + }); |
| 116 | + } else { |
| 117 | + // get and post |
| 118 | + tag = new osparc.data.model.Tag(tagData); |
| 119 | + this.tagsCached.unshift(tag); |
| 120 | + } |
| 121 | + return tag; |
| 122 | + }, |
| 123 | + |
| 124 | + __deleteFromCache: function(tagId) { |
| 125 | + const idx = this.tagsCached.findIndex(f => f.getTagId() === tagId); |
| 126 | + if (idx > -1) { |
| 127 | + this.tagsCached.splice(idx, 1); |
| 128 | + return true; |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | +}); |
0 commit comments