3
3
* Copyright: 2020 IT'IS Foundation - https://itis.swiss
4
4
* License: MIT - https://opensource.org/licenses/MIT
5
5
* Authors: Ignacio Pascual (ignapas)
6
+ * Odei Maiz (odeimaiz)
6
7
*/
7
8
8
9
/**
9
10
* Tag manager server to manage one resource's related tags.
10
11
*/
11
12
qx . Class . define ( "osparc.component.form.tag.TagManager" , {
12
13
extend : osparc . ui . window . SingletonWindow ,
13
- construct : function ( selectedTags , attachment , resourceName , resourceId ) {
14
+ construct : function ( studyData , attachment , resourceName , resourceId ) {
14
15
this . base ( arguments , "tagManager" , this . tr ( "Apply Tags" ) ) ;
15
16
this . set ( {
16
17
layout : new qx . ui . layout . VBox ( ) ,
@@ -28,25 +29,33 @@ qx.Class.define("osparc.component.form.tag.TagManager", {
28
29
this . __attachment = attachment ;
29
30
this . __resourceName = resourceName ;
30
31
this . __resourceId = resourceId ;
31
- this . __selectedTags = new qx . data . Array ( selectedTags ) ;
32
+ this . __studyData = studyData ;
33
+ this . __selectedTags = new qx . data . Array ( studyData [ "tags" ] ) ;
32
34
this . __renderLayout ( ) ;
33
35
this . __attachEventHandlers ( ) ;
34
36
this . open ( ) ;
35
37
} ,
38
+
36
39
events : {
40
+ "updateTags" : "qx.event.type.Data" ,
37
41
"changeSelected" : "qx.event.type.Data"
38
42
} ,
43
+
39
44
properties : {
40
45
liveUpdate : {
41
46
check : "Boolean" ,
47
+ event : "changeLiveUpdate" ,
42
48
init : true
43
49
}
44
50
} ,
51
+
45
52
members : {
53
+ __studyData : null ,
46
54
__attachment : null ,
47
55
__resourceName : null ,
48
56
__resourceId : null ,
49
57
__selectedTags : null ,
58
+
50
59
__renderLayout : function ( ) {
51
60
const filterBar = new qx . ui . toolbar . ToolBar ( ) ;
52
61
const filter = new osparc . component . filter . TextFilter ( "name" , "studyBrowserTagManager" ) . set ( {
@@ -57,6 +66,7 @@ qx.Class.define("osparc.component.form.tag.TagManager", {
57
66
width : "100%"
58
67
} ) ;
59
68
this . add ( filterBar ) ;
69
+
60
70
const buttonContainer = new qx . ui . container . Composite ( new qx . ui . layout . VBox ( ) ) ;
61
71
this . add ( buttonContainer , {
62
72
flex : 1
@@ -72,7 +82,22 @@ qx.Class.define("osparc.component.form.tag.TagManager", {
72
82
textAlign : "center"
73
83
} ) ) ;
74
84
}
85
+
86
+ const buttons = new qx . ui . container . Composite ( new qx . ui . layout . HBox ( ) . set ( {
87
+ alignX : "right"
88
+ } ) ) ;
89
+ const saveButton = new osparc . ui . form . FetchButton ( this . tr ( "Save" ) ) ;
90
+ osparc . utils . Utils . setIdToWidget ( saveButton , "saveTagsBtn" ) ;
91
+ saveButton . addListener ( "execute" , e => {
92
+ this . __save ( saveButton ) ;
93
+ } , this ) ;
94
+ buttons . add ( saveButton ) ;
95
+ this . bind ( "liveUpdate" , buttons , "visibility" , {
96
+ converter : value => value ? "excluded" : "visible"
97
+ } ) ;
98
+ this . add ( buttons ) ;
75
99
} ,
100
+
76
101
/**
77
102
* If the attachment (element close to which the TagManager is being rendered) is already on the DOM,
78
103
* this function calculates where the TagManager should render, taking into account the window edges.
@@ -98,44 +123,94 @@ qx.Class.define("osparc.component.form.tag.TagManager", {
98
123
this . center ( ) ;
99
124
}
100
125
} ,
126
+
101
127
__tagButton : function ( tag ) {
102
- const button = new osparc . component . form . tag . TagToggleButton ( tag , this . __selectedTags . includes ( tag . id ) ) ;
103
- button . addListener ( "changeValue" , evt => {
128
+ const tagButton = new osparc . component . form . tag . TagToggleButton ( tag , this . __selectedTags . includes ( tag . id ) ) ;
129
+ tagButton . addListener ( "changeValue" , evt => {
104
130
const selected = evt . getData ( ) ;
105
131
if ( this . isLiveUpdate ( ) ) {
106
- button . setFetching ( true ) ;
107
- const params = {
108
- url : {
109
- tagId : tag . id ,
110
- studyUuid : this . __resourceId
111
- }
112
- } ;
132
+ tagButton . setFetching ( true ) ;
113
133
if ( selected ) {
114
- osparc . data . Resources . fetch ( "studies" , "addTag" , params )
115
- . then ( ( ) => this . __selectedTags . push ( tag . id ) )
116
- . catch ( err => {
117
- console . error ( err ) ;
118
- button . setValue ( false ) ;
119
- } )
120
- . finally ( ( ) => button . setFetching ( false ) ) ;
134
+ this . __saveAddTag ( tag . id , tagButton ) ;
121
135
} else {
122
- osparc . data . Resources . fetch ( "studies" , "removeTag" , params )
123
- . then ( ( ) => this . __selectedTags . remove ( tag . id ) )
124
- . catch ( err => {
125
- console . error ( err ) ;
126
- button . setValue ( true ) ;
127
- } )
128
- . finally ( ( ) => button . setFetching ( false ) ) ;
136
+ this . __saveRemoveTag ( tag . id , tagButton ) ;
129
137
}
130
138
} else if ( selected ) {
131
139
this . __selectedTags . push ( tag . id ) ;
132
140
} else {
133
141
this . __selectedTags . remove ( tag . id ) ;
134
142
}
135
143
} , this ) ;
136
- button . subscribeToFilterGroup ( "studyBrowserTagManager" ) ;
137
- return button ;
144
+ tagButton . subscribeToFilterGroup ( "studyBrowserTagManager" ) ;
145
+ return tagButton ;
146
+ } ,
147
+
148
+ __getAddTagPromise : function ( tagId ) {
149
+ const params = {
150
+ url : {
151
+ tagId,
152
+ studyUuid : this . __resourceId
153
+ }
154
+ } ;
155
+ return osparc . data . Resources . fetch ( "studies" , "addTag" , params ) ;
156
+ } ,
157
+
158
+ __getRemoveTagPromise : function ( tagId ) {
159
+ const params = {
160
+ url : {
161
+ tagId,
162
+ studyUuid : this . __resourceId
163
+ }
164
+ } ;
165
+ return osparc . data . Resources . fetch ( "studies" , "removeTag" , params ) ;
166
+ } ,
167
+
168
+ __saveAddTag : function ( tagId , tagButton ) {
169
+ this . __getAddTagPromise ( tagId )
170
+ . then ( ( ) => this . __selectedTags . push ( tagId ) )
171
+ . catch ( err => {
172
+ console . error ( err ) ;
173
+ tagButton . setValue ( false ) ;
174
+ } )
175
+ . finally ( ( ) => tagButton . setFetching ( false ) ) ;
176
+ } ,
177
+
178
+ __saveRemoveTag : function ( tagId , tagButton ) {
179
+ this . __getRemoveTagPromise ( tagId )
180
+ . then ( ( ) => this . __selectedTags . remove ( tagId ) )
181
+ . catch ( err => {
182
+ console . error ( err ) ;
183
+ tagButton . setValue ( true ) ;
184
+ } )
185
+ . finally ( ( ) => tagButton . setFetching ( false ) ) ;
138
186
} ,
187
+
188
+ __save : async function ( saveButton ) {
189
+ saveButton . setFetching ( true ) ;
190
+
191
+ // call them sequentially
192
+ let updatedStudy = null ;
193
+ for ( let i = 0 ; i < this . __selectedTags . length ; i ++ ) {
194
+ const tagId = this . __selectedTags . getItem ( i ) ;
195
+ if ( ! this . __studyData [ "tags" ] . includes ( tagId ) ) {
196
+ updatedStudy = await this . __getAddTagPromise ( tagId )
197
+ . then ( updatedData => updatedData ) ;
198
+ }
199
+ }
200
+ for ( let i = 0 ; i < this . __studyData [ "tags" ] . length ; i ++ ) {
201
+ const tagId = this . __studyData [ "tags" ] [ i ] ;
202
+ if ( ! this . __selectedTags . includes ( tagId ) ) {
203
+ updatedStudy = await this . __getRemoveTagPromise ( tagId )
204
+ . then ( updatedData => updatedData ) ;
205
+ }
206
+ }
207
+
208
+ saveButton . setFetching ( false ) ;
209
+ if ( updatedStudy ) {
210
+ this . fireDataEvent ( "updateTags" , updatedStudy ) ;
211
+ }
212
+ } ,
213
+
139
214
__attachEventHandlers : function ( ) {
140
215
this . addListener ( "appear" , ( ) => {
141
216
this . __updatePosition ( ) ;
0 commit comments