2020-10-16 15:17:33 +02:00
2020-11-01 23:23:36 +01:00
import EditorField from './editor-field.ts' ;
2020-10-18 01:21:11 +02:00
2020-10-16 15:17:33 +02:00
class Editor
{
# alias ;
2020-10-19 14:48:21 +02:00
# prefix ;
2020-10-18 23:47:59 +02:00
/**
* Overrides the string generation for the label
*/
2020-10-19 14:48:21 +02:00
templateLabel = field => this . # prefix + field ;
2020-10-18 23:47:59 +02:00
/**
* Overrides the string generation for the description
*/
2020-10-19 14:48:21 +02:00
templateDescription = field => this . # prefix + field + '_text' ;
2020-10-16 15:17:33 +02:00
tabs = [];
2020-10-18 01:21:11 +02:00
fields = [];
2020-10-16 15:17:33 +02:00
2020-10-23 13:22:41 +02:00
options = {
2020-11-17 15:44:04 +01:00
disabled : false ,
2020-11-19 18:09:59 +01:00
boxes : false ,
coreDatabase : true
2020-10-23 13:22:41 +02:00
};
2020-10-16 15:17:33 +02:00
2020-10-19 14:48:21 +02:00
constructor ( alias , prefix )
2020-10-16 15:17:33 +02:00
{
this . # alias = alias ;
2020-10-19 14:48:21 +02:00
this . # prefix = prefix || '' ;
2020-10-16 15:17:33 +02:00
}
get alias ()
{
return this . # alias ;
}
2020-10-18 01:21:11 +02:00
/**
* A tab within an editor
* @typedef {object} EditorTab
* @param {string} alias - Alias for the tab
* @param {string} name - Name of the tab (can be a translation)
* @param {number|function} [count] - Output a count indicator
* @param {boolean|function} [disabled] - Conditionally disable the tab and its content
*/
2020-10-16 15:17:33 +02:00
/**
2020-11-17 15:44:04 +01:00
* Add a new tab to the editor or returns the tab in case it was already added
2020-10-16 15:17:33 +02:00
* @param {string} alias - Alias for the tab
* @param {string} name - Name of the tab (can be a translation)
* @param {number|function} [count] - Output a count indicator
* @param {boolean|function} [disabled] - Conditionally disable the tab and its content
2020-10-18 01:21:11 +02:00
* @returns {EditorTab}
2020-10-16 15:17:33 +02:00
*/
tab ( alias , name , count , disabled )
{
if ( typeof disabled !== 'undefined' && typeof disabled !== 'boolean' && typeof disabled !== 'function' )
{
console . warn ( `[zero] editor.tab: the disabled property has to be of type [boolean, function, undefined]` );
return ;
}
if ( typeof count !== 'undefined' && typeof count !== 'number' && typeof count !== 'function' )
{
console . warn ( `[zero] editor.tab: the count property has to be of type [number, function, undefined]` );
return ;
}
2020-11-17 15:44:04 +01:00
let tab = this . tabs . find ( x => x . alias === alias );
2020-10-16 15:17:33 +02:00
2020-11-17 15:44:04 +01:00
if ( ! tab )
{
tab = this . _createTab ( alias , name , disabled , count );
this . tabs . push ( tab );
}
2020-10-16 15:17:33 +02:00
return tab ;
}
/**
2020-11-17 15:44:04 +01:00
* Add a new field to the editor or returns the field in case it was already added
2020-10-16 15:17:33 +02:00
* @param {string} path - Model path
* @param {object} [options] - Custom options
* @param {string} [options.label] - A custom label for this field (otherwise it's generated via `onLabelCreate`)
2020-10-18 23:47:59 +02:00
* @param {string} [options.hideLabel] - Hide the field label and make the content full-width
2020-10-16 15:17:33 +02:00
* @param {string} [options.description] - A custom description for this field (otherwise it's generated via `onDescriptionCreate`)
* @param {string} [options.helpText] - Display a help text below the field
* @param {boolean|function} [options.condition] - Conditionally hide the field
* @param {boolean|function} [options.disabled=false] - Conditionally disable the field
2020-11-19 18:09:59 +01:00
* @param {boolean} [options.coreDatabase] - Operate on the core database for this field (default is set by Editor.options.coreDatabase)
2020-10-18 01:21:11 +02:00
* @param {string|object} [options.tab] - Add this field to a tab (by passing the alias or the tab instance)
2020-10-16 15:17:33 +02:00
* @param {string} [options.class] - Append HTML class to the generated property
2020-10-18 01:21:11 +02:00
* @returns {EditorField}
2020-10-16 15:17:33 +02:00
*/
2020-10-18 01:21:11 +02:00
field ( path , options )
2020-10-16 15:17:33 +02:00
{
2020-11-19 18:09:59 +01:00
options = options || {};
2020-11-17 15:44:04 +01:00
let field = this . fields . find ( x => x . path === path );
if ( ! field )
{
2020-11-19 18:09:59 +01:00
if ( typeof options . coreDatabase === 'undefined' )
{
options . coreDatabase = this . options . coreDatabase ;
}
2020-11-17 15:44:04 +01:00
field = new EditorField ( path , options );
this . fields . push ( field );
}
else
{
2020-11-19 18:09:59 +01:00
field . options = { ... field . options , ... options };
2020-11-17 15:44:04 +01:00
}
2020-10-18 01:21:11 +02:00
return field ;
}
2020-11-17 15:44:04 +01:00
/**
* Conditionally disable this editor
* @param {function|boolean} [condition] - Optionally only disable this editor when a condition is fulfilled or reset the required state with true/false
*/
disabled ( condition )
{
this . options . disabled = condition ;
return this ;
}
2020-10-18 23:47:59 +02:00
/**
* Get fields for the specified tab
* @param {string|EditorTab} [tab] - Pass the tab or its alias
* @returns {EditorField[]}
*/
getFields ( tab )
{
const alias = typeof tab === 'undefined' ? null : ( typeof tab === 'string' ? tab : tab.alias );
return this . fields . filter ( x => ! alias ? true : x . options . tab === alias );
}
2020-10-22 15:45:34 +02:00
/**
* Removes a field by path name
* @param {string} path - Model path
*/
2020-10-23 13:07:00 +02:00
removeField ( path )
2020-10-22 15:45:34 +02:00
{
const field = this . fields . find ( x => x . path === path );
if ( field != null )
{
const index = this . fields . indexOf ( field );
this . fields . splice ( index , 1 );
}
}
2020-10-23 13:07:00 +02:00
/**
* Removes a tab from the editor
* @param {EditorTab|string} tab - The tab to remove
*/
removeTab ( tab )
{
const alias = typeof tab === 'object' ? tab.alias : tab ;
const foundTab = this . tabs . find ( x => x . alias === alias );
if ( foundTab != null )
{
const index = this . tabs . indexOf ( foundTab );
this . tabs . splice ( index , 1 );
this . fields . filter ( x => x . tab === alias ). forEach ( field => this . removeField ( field . path ));
}
}
2020-10-22 11:52:22 +02:00
/**
* Set another editor as the base for this editor (copies fields and tabs)
* @param {Editor} editor - Base editor
* @returns {Editor}
*/
setBase ( editor )
{
this . fields = editor . fields . map ( x => new EditorField ( x . path ). setBase ( x ));
this . tabs = editor . tabs . map ( x => this . _createTab ( x . alias , x . name , x . disabled , x . count ));
return this ;
}
2020-10-18 01:21:11 +02:00
/**
* Create a new tab instance
* @returns {EditorTab}
*/
_createTab ( alias , name , disabled , count )
{
return {
alias ,
name ,
disabled ,
count ,
field : ( path , options ) =>
{
options = options || {};
options . tab = alias ;
return this . field ( path , options );
2020-10-22 15:45:34 +02:00
},
2020-10-23 13:07:00 +02:00
removeField : path => this . removeField ( path )
2020-10-18 01:21:11 +02:00
};
2020-10-16 15:17:33 +02:00
}
2020-10-21 01:04:49 +02:00
2020-10-16 15:17:33 +02:00
};
export default Editor ;