@ -1,12 +1,14 @@
import Component from '../../../../core/components/component/Component' ;
import ModalSidebar from '../../../../core/components/modal-sidebar/ModalSibebar' ;
import FormControl from '../../../../core/components/form-control/FormControl' ;
import { EVENTS , MODES , CODE _EDITOR _MODE } from '../../../../core/consts' ;
import { EVENTS , MODES , CODE _EDITOR _MODE , FORM _TYPES } from '../../../../core/consts' ;
import './ApiTableViewForm.css' ;
import storageApi from '../../api/StorageServiceAPI' ;
import routeService from '../../../../services/RouteService' ;
import { HooksComponent } from '../hooks-component/HooksComponent' ;
import { FormCodeEditor } from '../../../../core/components/form-code-editor/FormCodeEditor' ;
import userInfoService from '../../../../services/UserInfoService' ;
import usersServiceApi from '../../../users/api/UsersServiceAPI' ;
const TITLE _MODES = {
[ MODES . Create ] : 'Создание нового хранилища' ,
@ -31,37 +33,6 @@ class ApiTableViewForm extends Component {
this . title . textContent = 'Информация о хранилище' ;
this . inputs = [
this . keyInput = this . createComponent ( FormControl , this . form , {
id : 'api-key-input' ,
label : 'Название хранилища' ,
pattern : '^[a-zA-Z][a-zA-Z0-9_-]{3,}$' ,
required : true ,
} ) ,
this . serviceNameInput = this . createComponent ( FormControl , this . form , {
id : 'api-service-name-input' ,
label : 'Название сервиса' ,
required : true ,
} ) ,
this . authorInput = this . createComponent ( FormControl , this . form , {
id : 'api-author-input' ,
label : 'Автор хранилища' ,
required : true ,
} ) ,
this . descriptionInput = this . createComponent ( FormControl , this . form , {
id : 'api-description-input' ,
label : 'Краткое описание' ,
required : true ,
} ) ,
this . valueInput = this . createComponent ( FormCodeEditor , this . form , {
id : 'api-value-input' ,
className : 'Api__value-input' ,
label : 'Содержимое хранилища' ,
mode : CODE _EDITOR _MODE . JSON ,
required : true ,
} ) ,
] ;
this . buttons = [
{
button : this . createButton = this . mainNode . querySelector ( '.ApiViewForm__create' ) ,
@ -70,10 +41,12 @@ class ApiTableViewForm extends Component {
{
button : this . saveButton = this . mainNode . querySelector ( '.ApiViewForm__save' ) ,
modes : [ MODES . Edit ] ,
onlyOwner : true ,
} ,
{
button : this . editButton = this . mainNode . querySelector ( '.ApiViewForm__edit' ) ,
modes : [ MODES . View ] ,
onlyOwner : true ,
} ,
{
button : this . cancelButton = this . mainNode . querySelector ( '.ApiViewForm__cancel' ) ,
@ -82,6 +55,7 @@ class ApiTableViewForm extends Component {
{
button : this . deleteButton = this . mainNode . querySelector ( '.ApiViewForm__delete' ) ,
modes : [ MODES . View , MODES . Edit ] ,
onlyOwner : true ,
}
] ;
@ -97,31 +71,74 @@ class ApiTableViewForm extends Component {
this . addEventListener ( this . cancelButton , EVENTS . CLICK , this . handleCloseModal ) ;
this . hooksContainer = this . createComponent ( HooksComponent , this . mainNode ) ;
this . form . insertAdjacentElement ( 'afterend' , this . hooksContainer . mainNode ) ;
this . addSubscribe ( routeService , EVENTS . ROUTE _SEARCH _CHANGE , this . setForm ) ;
this . renderInputs ( ) ;
}
renderInputs = async ( ) => {
const users = await usersServiceApi . request ( ) ;
this . inputs = [
this . keyInput = this . createComponent ( FormControl , this . form , {
id : 'api-key-input' ,
label : 'Название хранилища' ,
pattern : '^[a-zA-Z][a-zA-Z0-9_-]{3,}$' ,
required : true ,
} ) ,
this . serviceNameInput = this . createComponent ( FormControl , this . form , {
id : 'api-service-name-input' ,
label : 'Название сервиса' ,
required : true ,
} ) ,
this . authorInput = this . createComponent ( FormControl , this . form , {
id : 'api-author-input' ,
label : 'Автор хранилища' ,
type : FORM _TYPES . SELECT ,
options : users . map ( ( user ) => ( {
value : user . login ,
text : user . login ,
} ) ) ,
} ) ,
this . descriptionInput = this . createComponent ( FormControl , this . form , {
id : 'api-description-input' ,
label : 'Краткое описание' ,
required : true ,
} ) ,
this . valueInput = this . createComponent ( FormCodeEditor , this . form , {
id : 'api-value-input' ,
className : 'Api__value-input' ,
label : 'Содержимое хранилища' ,
mode : CODE _EDITOR _MODE . JSON ,
required : true ,
} ) ,
this . hideSelect = this . createComponent ( FormControl , this . form , {
id : 'api-hide-select' ,
label : 'Видимость хранилища' ,
type : FORM _TYPES . SELECT ,
options : [
{ value : true , text : 'Скрыть' } ,
{ value : false , text : 'Показать' } ,
] ,
} ) ,
] ;
this . inputs . forEach ( ( input ) => {
input . disabled ( true ) ;
} ) ;
this . hooksContainer = this . createComponent ( HooksComponent , this . mainNode ) ;
this . form . insertAdjacentElement ( 'afterend' , this . hooksContainer . mainNode ) ;
this . addSubscribe ( routeService , EVENTS . ROUTE _SEARCH _CHANGE , ( { query } ) => {
const { mode , key } = query ;
this . setForm ( mode , key ) ;
this . setVisibleHooks ( mode ) ;
} ) ;
const { query : { mode , key } } = routeService . getUrlData ( ) ;
if ( mode ) {
this . setForm ( mode , key ) ;
}
this . setForm ( ) ;
}
handleCloseModal = ( ) => {
routeService . pushQuery ( { } , true ) ;
}
setVisibleHooks = ( mode ) => {
const method = mode !== MODES . View ? 'add' : 'remove' ;
setVisibleHooks = ( mode , isAvailable ) => {
const isHide = mode !== MODES . View || ! isAvailable ;
const method = isHide ? 'add' : 'remove' ;
this . hooksContainer . mainNode . classList [ method ] ( 'invisible' ) ;
}
@ -146,17 +163,26 @@ class ApiTableViewForm extends Component {
} ;
}
setInputDisabled = ( mode ) => {
setInputDisabled = ( mode , isAdmin ) => {
const disabled = ! EDIT _MODES . includes ( mode ) ;
this . inputs . forEach ( ( input ) => {
const disabledLogin = this . keyInput === input && mode === MODES . Edit ;
input . disabled ( disabled ? disabled : disabledLogin ) ;
if ( input === this . keyInput ) {
const disabledKey = mode !== MODES . Create ;
input . disabled ( disabled ? disabled : disabledKey ) ;
return ;
}
if ( input === this . authorInput ) {
const disabledAuthor = mode !== MODES . Create || ! isAdmin ;
input . disabled ( disabled ? disabled : disabledAuthor ) ;
return ;
}
input . disabled ( disabled ) ;
} ) ;
}
showButtons = ( mode ) => {
this . buttons . forEach ( ( { button , modes } ) => {
const isShow = modes . includes ( mode ) ;
showButtons = ( mode , isAvailable ) => {
this . buttons . forEach ( ( { button , modes , onlyOwner }) => {
const isShow = modes . includes ( mode ) && ( onlyOwner ? isAvailable : true ) ;
button . style . display = isShow ? 'inline-block' : 'none' ;
} ) ;
}
@ -202,6 +228,7 @@ class ApiTableViewForm extends Component {
service _name : this . serviceNameInput . getValue ( ) ,
author : this . authorInput . getValue ( ) ,
description : this . descriptionInput . getValue ( ) ,
hide : this . hideSelect . getValue ( ) ,
} ) ;
this . clearForm ( ) ;
@ -217,6 +244,7 @@ class ApiTableViewForm extends Component {
service _name : this . serviceNameInput . getValue ( ) ,
author : this . authorInput . getValue ( ) ,
description : this . descriptionInput . getValue ( ) ,
hide : this . hideSelect . getValue ( ) ,
} ) ;
this . clearForm ( ) ;
routeService . pushQuery ( { } , true ) ;
@ -235,17 +263,22 @@ class ApiTableViewForm extends Component {
this . keyInput . setValue ( key ) ;
}
setForm = async ( mode , storeKey ) => {
const { key , value , description , service _name , author } = await this . loadStore ( storeKey ) ;
setForm = async ( ) => {
const { query : { mode , key : storeKey } } = routeService . getUrlData ( ) ;
const { key , value , description , service _name , author , hide } = await this . loadStore ( storeKey ) ;
const { login , is _admin } = await userInfoService . getUserInfo ( ) ;
const isAvailableEdit = author === login || is _admin ;
this . setSidebarTitle ( mode , storeKey ) ;
this . setKeyInput ( mode , key ) ;
this . serviceNameInput . setValue ( service _name ) ;
this . authorInput . setValue ( author ) ;
this . authorInput . setValue ( author || login ) ;
this . descriptionInput . setValue ( description ) ;
this . valueInput . setValue ( this . stringifyValue ( value ) ) ;
this . hideSelect . setValue ( hide ) ;
this . setInputDisabled ( mode ) ;
this . showButtons ( mode ) ;
this . setInputDisabled ( mode , is _admin );
this . showButtons ( mode , isAvailableEdit );
this . setVisibleHooks ( mode , isAvailableEdit ) ;
if ( mode ) {
this . sidebar . show ( ) ;