@@ -20,7 +20,9 @@ import {
2020} from '@dasch-swiss/dsp-js' ;
2121import { CacheService } from 'src/app/main/cache/cache.service' ;
2222import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens' ;
23+ import { existingNamesValidator } from 'src/app/main/directive/existing-name/existing-name.directive' ;
2324import { ErrorHandlerService } from 'src/app/main/error/error-handler.service' ;
25+ import { CustomRegex } from 'src/app/workspace/resource/values/custom-regex' ;
2426import { AutocompleteItem } from 'src/app/workspace/search/advanced-search/resource-and-property-selection/search-select-property/specify-property-value/operator' ;
2527import { DefaultProperties , DefaultProperty , PropertyCategory , PropertyInfoObject } from '../default-data/default-properties' ;
2628import { OntologyService } from '../ontology.service' ;
@@ -65,11 +67,17 @@ export class PropertyFormComponent implements OnInit {
6567 propertyForm : FormGroup ;
6668
6769 formErrors = {
70+ 'name' : '' ,
6871 'label' : '' ,
6972 'guiAttr' : ''
7073 } ;
7174
7275 validationMessages = {
76+ 'name' : {
77+ 'required' : 'Name is required.' ,
78+ 'existingName' : 'This name is already taken. Please choose another one.' ,
79+ 'pattern' : 'Name shouldn\'t start with a number or v + number and spaces or special characters (except dash, dot and underscore) are not allowed.'
80+ } ,
7381 'label' : {
7482 'required' : 'Label is required.' ,
7583 } ,
@@ -101,17 +109,23 @@ export class PropertyFormComponent implements OnInit {
101109 error = false ;
102110
103111 labels : StringLiteral [ ] = [ ] ;
112+ labelsTouched : boolean ;
104113 comments : StringLiteral [ ] = [ ] ;
105114 guiAttributes : string [ ] = [ ] ;
106115
116+ // list of existing property names
117+ existingNames : [ RegExp ] = [
118+ new RegExp ( 'anEmptyRegularExpressionWasntPossible' )
119+ ] ;
120+
107121 dspConstants = Constants ;
108122
109123 constructor (
110124 @Inject ( DspApiConnectionToken ) private _dspApiConnection : KnoraApiConnection ,
111125 private _cache : CacheService ,
112126 private _errorHandler : ErrorHandlerService ,
113127 private _fb : FormBuilder ,
114- private _ontologyService : OntologyService
128+ private _os : OntologyService
115129 ) { }
116130
117131 ngOnInit ( ) {
@@ -127,6 +141,16 @@ export class PropertyFormComponent implements OnInit {
127141 // a) in case of link value:
128142 // set list of resource classes from response; needed for linkValue
129143 this . resourceClasses = response . getAllClassDefinitions ( ) ;
144+
145+ // set list of all existing property names to avoid same name twice
146+ Object . entries ( this . ontology . properties ) . forEach (
147+ ( [ key ] ) => {
148+ const name = this . _os . getNameFromIri ( key ) ;
149+ this . existingNames . push (
150+ new RegExp ( '(?:^|W)' + name . toLowerCase ( ) + '(?:$|W)' )
151+ ) ;
152+ }
153+ ) ;
130154 } ,
131155 ( error : ApiResponseError ) => {
132156 this . _errorHandler . showMessage ( error ) ;
@@ -168,7 +192,7 @@ export class PropertyFormComponent implements OnInit {
168192
169193 // slice array
170194 // this slice value will be kept
171- // because there was the idea to shorten the array of restrcited elements
195+ // because there was the idea to shorten the array of restricted elements
172196 // in case e.g. richtext can't be changed to simple text, then we shouldn't list the simple text item
173197 const slice = 0 ;
174198
@@ -181,6 +205,14 @@ export class PropertyFormComponent implements OnInit {
181205 }
182206
183207 this . propertyForm = this . _fb . group ( {
208+ 'name' : new FormControl ( {
209+ value : ( this . propertyInfo . propDef ? this . _os . getNameFromIri ( this . propertyInfo . propDef . id ) : '' ) ,
210+ disabled : this . propertyInfo . propDef
211+ } , [
212+ Validators . required ,
213+ existingNamesValidator ( this . existingNames ) ,
214+ Validators . pattern ( CustomRegex . ID_NAME_REGEX )
215+ ] ) ,
184216 'propType' : new FormControl ( {
185217 value : this . propertyInfo . propType ,
186218 disabled : disablePropType || this . resClassIri
@@ -217,11 +249,9 @@ export class PropertyFormComponent implements OnInit {
217249 return ;
218250 }
219251
220- const form = this . propertyForm ;
221-
222252 Object . keys ( this . formErrors ) . map ( field => {
223253 this . formErrors [ field ] = '' ;
224- const control = form . get ( field ) ;
254+ const control = this . propertyForm . get ( field ) ;
225255 if ( control && control . dirty && ! control . valid ) {
226256 const messages = this . validationMessages [ field ] ;
227257 Object . keys ( control . errors ) . map ( key => {
@@ -232,14 +262,20 @@ export class PropertyFormComponent implements OnInit {
232262 } ) ;
233263 }
234264
235- handleData ( data : StringLiteral [ ] , type : string ) {
265+ handleData ( data : StringLiteral [ ] , type : 'label' | 'comment' ) {
236266
237267 switch ( type ) {
238- case 'labels ' :
268+ case 'label ' :
239269 this . labels = data ;
270+ const messages = this . validationMessages [ type ] ;
271+ this . formErrors [ type ] = '' ;
272+
273+ if ( this . labelsTouched && ! this . labels . length ) {
274+ this . formErrors [ type ] = messages [ 'required' ] ;
275+ }
240276 break ;
241277
242- case 'comments ' :
278+ case 'comment ' :
243279 this . comments = data ;
244280 break ;
245281 }
@@ -420,7 +456,7 @@ export class PropertyFormComponent implements OnInit {
420456 // create mode: new property incl. gui type and attribute
421457 // submit property
422458 // set resource property name / id: randomized string
423- const uniquePropName : string = this . _ontologyService . setUniqueName ( this . ontology . id ) ;
459+ // const uniquePropName: string = this._os .setUniqueName(this.ontology.id);
424460
425461 const onto = new UpdateOntology < CreateResourceProperty > ( ) ;
426462
@@ -429,7 +465,7 @@ export class PropertyFormComponent implements OnInit {
429465
430466 // prepare payload for property
431467 const newResProp = new CreateResourceProperty ( ) ;
432- newResProp . name = uniquePropName ;
468+ newResProp . name = this . propertyForm . controls [ 'name' ] . value ;
433469 newResProp . label = this . labels ;
434470 newResProp . comment = ( this . comments . length ? this . comments : this . labels ) ;
435471 const guiAttr = this . propertyForm . controls [ 'guiAttr' ] . value ;
@@ -488,7 +524,7 @@ export class PropertyFormComponent implements OnInit {
488524
489525 const propCard : IHasProperty = {
490526 propertyIndex : prop . id ,
491- cardinality : this . _ontologyService . translateCardinality ( this . propertyForm . value . multiple , this . propertyForm . value . required ) ,
527+ cardinality : this . _os . translateCardinality ( this . propertyForm . value . multiple , this . propertyForm . value . required ) ,
492528 guiOrder : this . guiOrder // add new property to the end of current list of properties
493529 } ;
494530
0 commit comments