2022-06-27 18:52:48 +02:00
class CCPInputWidgetEditorController extends HTMLElement {
# input = null
# index = null
# type = null
# delete _icon = `
< svg style = "width:24px;height:24px;pointer-events: none;" viewBox = "0 0 24 24" >
< path d = "M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z" / >
< / s v g >
`
2022-11-04 17:43:02 +01:00
2022-06-27 18:52:48 +02:00
constructor ( ) {
super ( ) ;
}
connectedCallback ( ) {
}
get index ( ) {
return this . # index
}
2022-11-04 18:22:39 +01:00
computeDefaultInputType ( ) {
if ( this . # input . schema . format === "secret" ) {
return "password"
} else if ( this . # input . schema . format === "date" ) {
return "date"
} else if ( this . # input . schema . format === "time" ) {
2023-02-06 12:08:33 +01:00
return "time"
2022-11-04 18:22:39 +01:00
} else if ( this . # input . schema . format === "dateTime" ) {
2023-02-06 13:54:09 +01:00
return "datetime-local"
2022-11-04 18:22:39 +01:00
}
return "text"
}
isSelectedFormat ( fmt ) {
2023-02-06 12:08:33 +01:00
return this . # input . schema . format === fmt
2022-11-04 18:22:39 +01:00
}
2023-02-06 13:54:09 +01:00
renderDefaultByType ( ) {
if ( this . # input . schema . format === "code" ) {
return `
< textarea rows = "5" name = "default" class = "form-control" placeholder = "default" > $ { this . # input . schema . default } < / t e x t a r e a >
`
} else {
return `
< input type = "${this.computeDefaultInputType()}" value = "${this.#input.schema.default}" name = "default" class = "form-control" placeholder = "default" / >
`
}
}
2023-03-07 12:38:24 +01:00
renderDeleteButton ( ) {
2023-03-07 12:40:03 +01:00
return ` <button data-index=" ${ this . # index } " name="delete-input" title="Delete" class="btn btn-danger ccp-toolbar-button">
2023-03-07 12:38:24 +01:00
$ { this . # delete _icon }
2023-03-07 12:40:03 +01:00
< / b u t t o n > `
2023-03-07 12:38:24 +01:00
}
2023-02-06 13:54:09 +01:00
render ( input , i , reopen ) {
2022-06-27 18:52:48 +02:00
this . # index = i
this . # input = input
this . # type = input . schema . enum ? "enum" : "string"
this . innerHTML = `
2023-02-06 13:54:09 +01:00
< details $ { reopen ? 'open' : '' } >
2022-06-27 18:52:48 +02:00
< summary class = "mb-3" >
2023-06-07 18:08:28 +02:00
< input class = "form-control" style = "width:auto;display:inline" required = "required" name = "id" value = "${this.#input.id}" title = "Id of input" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2023-03-07 12:41:15 +01:00
$ { input . id !== 'ccpimage' ? this . renderDeleteButton ( ) : '' }
2022-06-27 18:52:48 +02:00
< / s u m m a r y >
< div style = "padding-left: 1rem;border-left: 1px solid gray;" >
< div class = "row mb-3" >
< div class = "col" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Title" >
2023-06-07 18:06:19 +02:00
< input name = "title" class = "form-control" placeholder = "title" value = "${this.#input.title}" required = "required" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< / d i v >
< div class = "mb-3" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "description" >
2023-06-07 18:06:19 +02:00
< input name = "description" class = "form-control" placeholder = "description" value = "${this.#input.description}" required = "required" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< div class = "row mb-3" >
< div class = "col-3" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Type" >
2022-06-27 18:52:48 +02:00
< label > Type < / l a b e l >
2023-06-07 18:06:19 +02:00
< select name = "type" class = "form-control" placeholder = "type" value = "${this.#input.schema.type}" $ { input . id === 'ccpimage' ? 'readonly' : '' } >
2022-06-27 18:52:48 +02:00
< option value = "string" > String < / o p t i o n >
< option value = "enum" > Enum < / o p t i o n >
< / s e l e c t >
< / d i v >
< / d i v >
< div class = "col" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Minimum" >
2022-06-27 18:52:48 +02:00
< label > Min < / l a b e l >
2023-06-07 18:06:19 +02:00
< input value = "${this.#input.minOccurs}" type = "number" min = "0" step = "1" name = "minOccurs" value = "${this.#input.minOccurs}" required = "required" class = "form-control" placeholder = "minOccurs" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< div class = "col" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Maximum" >
2022-06-27 18:52:48 +02:00
< label > Max < / l a b e l >
2023-06-07 18:06:19 +02:00
< input value = "${this.#input.maxOccurs}" type = "number" min = "0" step = "1" name = "maxOccurs" value = "${this.#input.maxOccurs}" required = "required" class = "form-control" placeholder = "maxOccurs" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< / d i v >
< div name = "string-input" class = "${this.#type === 'enum' ? 'd-none' : ''} row mb-3" >
< div class = "col-3" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Format" >
2022-06-27 18:52:48 +02:00
< label > Format < / l a b e l >
2023-06-07 18:06:19 +02:00
< select value = "${this.#input.schema.format}" name = "format" class = "form-control" $ { input . id === 'ccpimage' ? 'readonly' : '' } >
2023-02-06 12:08:33 +01:00
< option value = "none" $ { this . isSelectedFormat ( 'none' ) ? "selected" : "" } > None < / o p t i o n >
< option value = "date" $ { this . isSelectedFormat ( 'date' ) ? "selected" : "" } > Date < / o p t i o n >
< option value = "time" $ { this . isSelectedFormat ( 'time' ) ? "selected" : "" } > Time < / o p t i o n >
< option value = "dateTime" $ { this . isSelectedFormat ( 'dateTime' ) ? "selected" : "" } > Date time < / o p t i o n >
< option value = "code" $ { this . isSelectedFormat ( 'code' ) ? "selected" : "" } > Code < / o p t i o n >
< option value = "file" $ { this . isSelectedFormat ( 'file' ) ? "selected" : "" } > File < / o p t i o n >
< option value = "secret" $ { this . isSelectedFormat ( 'secret' ) ? "selected" : "" } > Secret < / o p t i o n >
2022-06-27 18:52:48 +02:00
< / s e l e c t >
< / d i v >
< / d i v >
2022-07-18 18:08:53 +02:00
< div class = "col" title = "Mime type" >
2022-06-27 18:52:48 +02:00
< label > Mime < / l a b e l >
< div class = "form-field" >
2023-06-07 18:06:19 +02:00
< input value = "${this.#input.schema.contentMediaType}" name = "contentMediaType" class = "form-control" placeholder = "mime" $ { input . id === 'ccpimage' ? 'readonly' : '' } / >
2023-06-07 18:02:17 +02:00
< / d i v >
< / d i v >
< div class = "col" title = "Read only" >
< label > Read only < / l a b e l >
< div class = "form-field" >
2023-06-07 18:28:26 +02:00
< input type = "checkbox" $ { this . # input . schema . readOnly ? 'checked' : '' } name = "readonly" class = "form-check-input" >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< / d i v >
< div name = "enum-input" class = "${this.#type !== 'enum' ? 'd-none' : ''} mb-3" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "options" >
2023-02-06 12:27:06 +01:00
< input name = "options" class = "form-control" type = "text" placeholder = "option" value = "${this.#input.schema.enum ? this.#input.schema.enum.join(',') : ''}" / >
2022-06-27 18:52:48 +02:00
< small class = "form-text text-muted" > Comma separated list of options < / s m a l l >
< / d i v >
< / d i v >
2022-11-04 16:37:15 +01:00
< div name = "input-default" class = "mb-3" >
2022-07-18 18:08:53 +02:00
< div class = "form-field" title = "Default value" >
2023-02-06 13:54:09 +01:00
$ { this . renderDefaultByType ( ) }
< span style = "user-select:none;position:relative;top:-1.6rem;float:right;cursor:pointer" name = "password_toggle" class = "${this.isSelectedFormat('secret') ? 'inline' : 'd-none'}" > & # 128065 ; < / s p a n >
2022-06-27 18:52:48 +02:00
< / d i v >
< / d i v >
< / d i v >
< / d e t a i l s >
2022-11-04 18:22:39 +01:00
`
2022-11-04 17:43:02 +01:00
this . addEventListener ( "click" , ev => {
const ename = ev . target . getAttribute ( "name" )
if ( ename === "password_toggle" ) {
const w = this . querySelector ( "div[name=input-default] input[name=default]" )
w . type = ( w . type === "password" ? "" : "password" )
ev . preventDefault ( )
}
} )
2022-06-27 18:52:48 +02:00
this . addEventListener ( "input" , ev => {
const val = ev . target . value
const ename = ev . target . getAttribute ( "name" )
if ( ename === "id" ) {
this . # input . id = val
}
else if ( ename === "title" ) {
this . # input . title = val
}
else if ( ename === "description" ) {
2022-07-18 18:26:27 +02:00
this . # input . description = val
2022-06-27 18:52:48 +02:00
}
else if ( ename === "minOccurs" ) {
this . # input . minOccurs = val
}
else if ( ename === "maxOccurs" ) {
this . # input . maxOccurs = val
}
else if ( ename === "format" ) {
this . # input . schema . format = val
2023-02-06 13:54:09 +01:00
this . render ( this . # input , this . # index , true )
/ * t h i s . q u e r y S e l e c t o r ( " d i v [ n a m e = i n p u t - d e f a u l t ] s p a n [ n a m e = p a s s w o r d _ t o g g l e ] " ) . c l a s s L i s t . a d d ( " d - n o n e " )
2022-11-04 17:43:02 +01:00
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = ""
2022-11-04 16:37:15 +01:00
if ( this . # input . schema . format === "secret" ) {
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = "password"
2022-11-04 17:43:02 +01:00
this . querySelector ( "div[name=input-default] span[name=password_toggle]" ) . classList . remove ( "d-none" )
} else if ( this . # input . schema . format === "date" ) {
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = "date"
} else if ( this . # input . schema . format === "time" ) {
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = "time"
} else if ( this . # input . schema . format === "dateTime" ) {
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = "dateTime"
2022-11-30 16:04:46 +01:00
} else if ( this . # input . schema . format === "file" ) {
this . querySelector ( "div[name=input-default] input[name=default]" ) . type = "file"
2023-02-06 13:54:09 +01:00
} * /
2022-06-27 18:52:48 +02:00
}
else if ( ename === "contentMediaType" ) {
this . # input . schema . contentMediaType = val
}
else if ( ename === "options" ) {
this . # input . schema . enum = val . split ( "," )
}
else if ( ename === "default" ) {
this . # input . schema . default = val
}
2023-06-07 18:02:17 +02:00
else if ( ename === "readonly" ) {
this . # input . schema . readOnly = ev . target . checked
}
2022-06-27 18:52:48 +02:00
else if ( ename === "type" ) {
this . # type = ev . target . value
if ( this . # type === "enum" ) {
this . querySelector ( "div[name=string-input]" ) . classList . add ( "d-none" )
this . querySelector ( "div[name=enum-input]" ) . classList . remove ( "d-none" )
this . # input . schema . enum = this . querySelector ( "input[name=options]" ) . value . split ( "," )
} else if ( this . # type === "string" ) {
this . querySelector ( "div[name=enum-input]" ) . classList . add ( "d-none" )
this . querySelector ( "div[name=string-input]" ) . classList . remove ( "d-none" )
delete this . # input . schema [ 'enum' ]
}
}
} )
}
}
2023-02-06 12:08:33 +01:00
window . customElements . define ( 'd4s-ccp-input-editor' , CCPInputWidgetEditorController ) ;