@@ -14,15 +14,25 @@ import { CacheService } from '../../main/cache/cache.service';
1414} )
1515export class ProjectFormComponent implements OnInit {
1616
17- loading : boolean = true ;
18-
19- errorMessage : any ;
20-
21- @Input ( ) projectcode : string ;
17+ /**
18+ * Param of project form component:
19+ * Optional projectcode; if exists we are in edit mode
20+ * otherwise we build empty form to create new project
21+ */
22+ @Input ( ) projectcode ?: string ;
2223
24+ /**
25+ * Output of project form component:
26+ * emits info to parent that dialog box was closed
27+ */
2328 @Output ( ) closeDialog : EventEmitter < any > = new EventEmitter < any > ( ) ;
2429
2530 project : ReadProject ;
31+ description : StringLiteral [ ] ;
32+
33+ loading = true ;
34+
35+ errorMessage : any ;
2636
2737 // is the logged-in user system admin?
2838 sysAdmin : boolean = false ;
@@ -40,7 +50,6 @@ export class ProjectFormComponent implements OnInit {
4050 ] ;
4151 shortcodeRegex = / ^ [ 0 - 9 A - F a - f ] + $ / ;
4252
43- description : StringLiteral [ ] ;
4453 /**
4554 * some restrictions and rules for
4655 * description, shortcode, shortname and keywords
@@ -84,8 +93,7 @@ export class ProjectFormComponent implements OnInit {
8493 'longname' : '' ,
8594 'shortcode' : '' ,
8695 'description' : ''
87- // 'institution': '',
88- // 'keywords': '',
96+ // 'institution': ''
8997 } ;
9098
9199 validationMessages = {
@@ -110,10 +118,7 @@ export class ProjectFormComponent implements OnInit {
110118 'required' : 'A description is required.' ,
111119 'maxlength' : 'Description cannot be more than ' + this . descriptionMaxLength + ' characters long.'
112120 }
113- // 'institution': {},
114- // 'keywords': {
115- // 'required': 'At least one keyword is required.'
116- // }
121+ // 'institution': {}
117122 } ;
118123
119124 constructor (
@@ -126,11 +131,30 @@ export class ProjectFormComponent implements OnInit {
126131
127132 ngOnInit ( ) {
128133
129- // get a list of all projects and create an array of the short names, but only in "create new" mode
130- // the short name should be unique and with the array list, we can prevent
131- // to have the same short name; proof it with the ForbiddenName directive
132- if ( ! this . projectcode ) {
134+ // if projectcode exists, we are in edit mode
135+ // otherwise create new project
136+ if ( this . projectcode ) {
137+ // edit existing project
138+ // get origin project data first
139+ this . _dspApiConnection . admin . projectsEndpoint . getProjectByShortcode ( this . projectcode ) . subscribe (
140+ ( response : ApiResponseData < ProjectResponse > ) => {
141+ // save the origin project data in case of reset
142+ this . project = response . body . project ;
143+
144+ this . buildForm ( this . project ) ;
145+
146+ this . loading = false ;
147+ } ,
148+ ( error : ApiResponseError ) => {
149+ this . errorMessage = error ;
150+ }
151+ ) ;
152+
153+ } else {
133154 // create new project
155+
156+ // to avoid dublicate shortcodes or shortnames
157+ // we have to create a list of already exisiting short codes and names
134158 this . _dspApiConnection . admin . projectsEndpoint . getProjects ( ) . subscribe (
135159 ( response : ApiResponseData < ProjectsResponse > ) => {
136160
@@ -149,58 +173,43 @@ export class ProjectFormComponent implements OnInit {
149173 }
150174 ) ;
151175
152- if ( this . project === undefined ) {
153- this . project = new ReadProject ( ) ;
154- this . project . status = true ;
155- }
176+ this . project = new ReadProject ( ) ;
177+ this . project . status = true ;
156178
157179 this . buildForm ( this . project ) ;
158180
159181 this . loading = false ;
160182
161- } else {
162- // edit mode
163- this . sysAdmin = this . _session . getSession ( ) . user . sysAdmin ;
164- this . _dspApiConnection . admin . projectsEndpoint . getProjectByShortcode ( this . projectcode ) . subscribe (
165- ( response : ApiResponseData < ProjectResponse > ) => {
166- this . project = response . body . project ;
167-
168- this . buildForm ( this . project ) ;
169-
170- this . loading = false ;
171- } ,
172- ( error : ApiResponseError ) => {
173- this . errorMessage = error ;
174- }
175- ) ;
176183 }
177184 }
178185
179186 /**
187+ * Build form with project data
188+ * Project data contains exising data (edit mode)
189+ * or no data (create mode) => new ReadProject()
180190 *
181- * @param project Project data: "empty" means "create new project",
182- * but if there are project data, it means edit mode
191+ * @param project
183192 */
184193 buildForm ( project : ReadProject ) : void {
185194 // if project is defined, we're in the edit mode
186195 // otherwise "create new project" mode is active
187- // edit mode is true, when a project id (iri) exists
188- const editMode : boolean = ( ! ! project . id ) ;
196+ // edit mode is true, when a projectcode exists
197+
198+ // disabled is true, if project status is false (= archived);
199+ const disabled : boolean = ( ! project . status ) ;
189200
190- if ( ! editMode ) {
201+ // separate description
202+ if ( ! this . projectcode ) {
191203 this . description = [ new StringLiteral ( ) ] ;
192204 this . formErrors [ 'description' ] = '' ;
193205 }
194206
195- // disabled is true, if project status is false (= archived);
196- const disabled : boolean = ( project . id !== undefined && ! project . status ) ;
197-
198207 // separate list of keywords
199208 this . keywords = project . keywords ;
200209
201210 this . form = this . _fb . group ( {
202211 'shortname' : new FormControl ( {
203- value : project . shortname , disabled : editMode
212+ value : project . shortname , disabled : ( this . projectcode )
204213 } , [
205214 Validators . required ,
206215 Validators . minLength ( this . shortnameMinLength ) ,
@@ -214,42 +223,33 @@ export class ProjectFormComponent implements OnInit {
214223 Validators . required
215224 ] ) ,
216225 'shortcode' : new FormControl ( {
217- value : project . shortcode , disabled : ( editMode && project . shortcode !== null )
226+ value : project . shortcode , disabled : ( ( this . projectcode ) && project . shortcode !== null )
218227 } , [
219228 Validators . required ,
220229 Validators . minLength ( this . shortcodeMinLength ) ,
221230 Validators . maxLength ( this . shortcodeMaxLength ) ,
222231 existingNamesValidator ( this . existingShortcodes ) ,
223232 Validators . pattern ( this . shortcodeRegex )
224233 ] ) ,
225- // 'description': new FormControl({
226- // value: project.description,
227- // disabled: disabled
228- // }, [
229- // Validators.maxLength(this.descriptionMaxLength),
230- // Validators.required
231- // ]),
232- // 'institution': new FormControl({
233- // value: project.institution, disabled: disabled
234- // }),
235234 'logo' : new FormControl ( {
236235 value : project . logo , disabled : disabled
237236 } ) ,
238237 'status' : [ true ] ,
239238 'selfjoin' : [ false ] ,
240239 'keywords' : new FormControl ( {
240+ // must be empty (even in edit mode), because of the mat-chip-list
241241 value : [ ] , disabled : disabled
242- } ) // must be empty (even in edit mode), because of the mat-chip-list
242+ } )
243243 } ) ;
244244
245245 this . form . valueChanges
246246 . subscribe ( data => this . onValueChanged ( data ) ) ;
247247 }
248248
249249 /**
250+ * This method is for the form error handling
250251 *
251252 * @param data Data which changed.
252- * This method is for the form error handling
253253 */
254254 onValueChanged ( data ?: any ) {
255255
@@ -272,9 +272,12 @@ export class ProjectFormComponent implements OnInit {
272272 } ) ;
273273 }
274274
275+ /**
276+ * Gets string literal
277+ * @param data
278+ */
275279 getStringLiteral ( data : StringLiteral [ ] ) {
276280 this . description = data ;
277-
278281 if ( ! this . description . length ) {
279282 this . formErrors [ 'description' ] = this . validationMessages [ 'description' ] . required ;
280283 } else {
@@ -302,7 +305,6 @@ export class ProjectFormComponent implements OnInit {
302305 }
303306
304307 removeKeyword ( keyword : any ) : void {
305-
306308 const index = this . keywords . indexOf ( keyword ) ;
307309
308310 if ( index >= 0 ) {
@@ -349,6 +351,7 @@ export class ProjectFormComponent implements OnInit {
349351 this . _dspApiConnection . admin . projectsEndpoint . updateProject ( this . project . id , projectData ) . subscribe (
350352 ( response : ApiResponseData < ProjectResponse > ) => {
351353
354+ // this.originProject = response.body.project;
352355 this . project = response . body . project ;
353356 this . buildForm ( this . project ) ;
354357
@@ -359,13 +362,6 @@ export class ProjectFormComponent implements OnInit {
359362
360363 this . loading = false ;
361364
362- // redirect to project page
363- /*
364- this._router.navigateByUrl('/project', {skipLocationChange: true}).then(() =>
365- this._router.navigate(['/project/' + this.form.controls['shortcode'].value])
366- );
367- */
368-
369365 } ,
370366 ( error : ApiResponseError ) => {
371367 this . errorMessage = error ;
@@ -406,7 +402,7 @@ export class ProjectFormComponent implements OnInit {
406402 ( response : ApiResponseData < UserResponse > ) => {
407403
408404 this . loading = false ;
409- this . closeMessage ( ) ;
405+ this . closeDialog . emit ( ) ;
410406 // redirect to (new) project page
411407 this . _router . navigateByUrl ( '/project' , { skipLocationChange : true } ) . then ( ( ) =>
412408 this . _router . navigate ( [ '/project/' + this . form . controls [ 'shortcode' ] . value ] )
@@ -519,6 +515,8 @@ export class ProjectFormComponent implements OnInit {
519515 }
520516
521517 /**
518+ * @deprecated Maybe we can reactivate later.
519+ *
522520 * Reset the form
523521 */
524522 resetForm ( ev : Event , project ?: ReadProject ) {
@@ -533,7 +531,4 @@ export class ProjectFormComponent implements OnInit {
533531 // this.form.controls['keywords'].setValue(this.keywords);
534532 }
535533
536- closeMessage ( ) {
537- this . closeDialog . emit ( ) ;
538- }
539534}
0 commit comments