2020-10-18 19:51:11 +02:00
import Text from '../editor/fields/text.vue' ;
import Currency from '../editor/fields/currency.vue' ;
import Number from '../editor/fields/number.vue' ;
import Rte from '../editor/fields/rte.vue' ;
import Select from '../editor/fields/select.vue' ;
import Textarea from '../editor/fields/textarea.vue' ;
import Toggle from '../editor/fields/toggle.vue' ;
2020-10-18 23:47:59 +02:00
import Alias from '../editor/fields/alias.vue' ;
import Output from '../editor/fields/output.vue' ;
import Checklist from '../editor/fields/checklist.vue' ;
import ColorPicker from '../editor/fields/colorpicker.vue' ;
import CountryPicker from '../editor/fields/countrypicker.vue' ;
2020-12-10 01:03:22 +01:00
import SpacePicker from '../editor/fields/spacepicker.vue' ;
2020-11-13 12:24:07 +01:00
import MailTemplatePicker from '../editor/fields/mailtemplatepicker.vue' ;
2020-10-18 23:47:59 +02:00
import CulturePicker from '../editor/fields/culturepicker.vue' ;
2020-10-20 16:00:55 +02:00
import DatePicker from '../editor/fields/datepicker.vue' ;
2020-10-18 23:47:59 +02:00
import DateRangePicker from '../editor/fields/daterangepicker.vue' ;
import IconPicker from '../editor/fields/iconPicker.vue' ;
import LanguagePicker from '../editor/fields/language.vue' ;
2020-10-29 21:44:22 +01:00
import PagePicker from '../editor/fields/pagepicker.vue' ;
2020-10-18 23:47:59 +02:00
import InputList from '../editor/fields/inputlist.vue' ;
import Media from '../editor/fields/media.vue' ;
import Modules from '../editor/fields/modules.vue' ;
import Nested from '../editor/fields/nested.vue' ;
import State from '../editor/fields/state.vue' ;
import Tags from '../editor/fields/tags.vue' ;
2020-10-18 19:51:11 +02:00
class EditorField
{
path = null ;
2020-10-21 01:04:49 +02:00
2020-10-18 19:51:11 +02:00
options = {
label : null ,
2020-10-18 23:47:59 +02:00
hideLabel : false ,
2020-10-18 19:51:11 +02:00
description : null ,
helpText : null ,
condition : null ,
disabled : false ,
tab : null ,
2020-11-19 18:09:59 +01:00
coreDatabase : false ,
2020-10-18 19:51:11 +02:00
class : ''
};
2020-10-21 01:04:49 +02:00
# preview = {
icon : 'fth-filter' ,
preview : x => x ,
hasValue : x => !! x
};
2020-10-18 19:51:11 +02:00
# component = null ;
# componentOptions = {};
# required = false ;
# isReadOnly = false ;
constructor ( path , options )
{
this . path = path ;
this . options = { ... this . options , ... options };
}
2020-10-18 23:47:59 +02:00
get component ()
{
return this . # component ;
}
get componentOptions ()
{
return this . # componentOptions ;
}
get isRequired ()
{
return this . # required ;
}
2020-10-21 01:04:49 +02:00
get previewOptions ()
{
return this . # preview ;
}
2020-10-18 23:47:59 +02:00
2020-10-22 11:52:22 +02:00
/**
* Set another editor field as the base for this editor (copies properties)
* @param {EditorField} field - Base editor field
* @returns {EditorField}
*/
setBase ( field )
{
this . path = field . path ;
this . options = { ... field . options };
this . # preview = { ... field . previewOptions };
this . # component = field . component ;
this . # componentOptions = field . componentOptions ;
this . # required = field . isRequired ;
return this ;
}
2020-10-18 19:51:11 +02:00
/**
*
*/
2020-11-13 12:24:07 +01:00
_setComponent ( component , options ? )
2020-10-18 19:51:11 +02:00
{
this . # component = component ;
this . # componentOptions = options || {};
return this ;
}
/**
* Set this field as required
* @param {function|boolean} [condition] - Optionally only require this field when a condition is fulfilled or reset the required state with true/false
*/
required ( condition )
{
if ( typeof condition === 'function' )
{
this . # required = condition ;
}
else if ( typeof condition === 'boolean' )
{
this . # required = condition ;
}
else
{
this . # required = true ;
}
return this ;
}
2020-10-20 13:27:17 +02:00
/**
* Conditionally render this field (this is an alternative method to the field options 'condition')
* @param {function} condition - function which returns a boolean and gets passed the current model
*/
when ( condition )
{
this . options . condition = condition ;
return this ;
}
2020-10-18 19:51:11 +02:00
/**
* Render a custom component
* @param {object} component - The custom vue component
* @param {object} [options] - Custom options
* @returns {EditorField}
*/
2020-10-18 23:47:59 +02:00
custom ( component , options )
2020-10-18 19:51:11 +02:00
{
return this . _setComponent ( component , { ... options });
}
/**
* Render a text input field
* @param {number} [maxLength] - Maximum length of the input
* @param {string} [placeholder] - Placeholder text (can be a translation)
* @returns {EditorField}
*/
text ( maxLength , placeholder )
{
return this . _setComponent ( Text , { maxLength , placeholder });
}
/**
* Render a currency input field
* @param {string} [placeholder] - Placeholder text (can be a translation)
* @returns {EditorField}
*/
currency ( placeholder )
{
return this . _setComponent ( Currency , { placeholder });
}
/**
* Render a number input field
* @param {number} [maxLength] - Maximum length of the input
* @param {string} [placeholder] - Placeholder text (can be a translation)
* @returns {EditorField}
*/
number ( maxLength , placeholder )
{
return this . _setComponent ( Number , { maxLength , placeholder });
}
/**
* Render a rich-text editor field
* @returns {EditorField}
*/
rte ()
{
return this . _setComponent ( Rte );
}
/**
* @typedef {object} EditorSelectItem
* @param {object} key - Key/Id of the item
* @param {string} value - Label/Value of the item (can be a translation)
*/
/**
* Render a select dropdown with the specified items
* @param {EditorSelectItem[]} items - Set items to pick from
* @returns {EditorField}
*/
select ( items )
{
return this . _setComponent ( Select , { items });
}
/**
* Render a text area
* @param {number} [maxLength] - Maximum length of the input
* @returns {EditorField}
*/
textarea ( maxLength )
{
return this . _setComponent ( Textarea , { maxLength });
}
/**
* Render a toggle
* @param {boolean} [negative] - Toggle with a negative color / red background
* @returns {EditorField}
*/
toggle ( negative )
{
return this . _setComponent ( Toggle , { negative });
}
/**
* Renders the field value
* @param {function} [render] - Render the output based on the given function
* @returns {EditorField}
*/
output ( render )
{
this . # isReadOnly = true ;
return this . _setComponent ( Output , { render });
}
/**
* Renders an input which generates an alias for a given name or an alternative custom alias
* @param {string} [namePath] - Optional path to the name value which is used to auto-generate the alias
* @returns {EditorField}
*/
alias ( namePath )
{
return this . _setComponent ( Alias , { namePath });
}
/**
* Renders an input which generates an alias for a given name or an alternative custom alias
* @param {EditorSelectItem[]|function} items - Set items to choose from, either via an array or a promise which returns such array
2020-10-20 13:27:17 +02:00
* @param {object} [options] - Custom options
* @param {number} [options.limit=100] - Maximum items to be checked
* @param {boolean} [options.reverse=false] - Reverse the checklist behaviour, so all items are checked by default and unchecking them adds them to the result list
* @param {string} [options.labelKey=value] - Object key to get the label
* @param {string} [options.idKey=key] - Object key to get the id
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2020-10-20 13:27:17 +02:00
checkList ( items , options )
2020-10-18 19:51:11 +02:00
{
2020-10-20 13:27:17 +02:00
return this . _setComponent ( Checklist , { items , ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Renders a HEX color picker
* @returns {EditorField}
*/
colorPicker ()
{
return this . _setComponent ( ColorPicker );
}
/**
* Renders a country picker
* @param {number} [limit=1] - Maximum items to be selected
* @returns {EditorField}
*/
countryPicker ( limit )
{
return this . _setComponent ( CountryPicker , { limit });
}
2020-12-10 01:03:22 +01:00
/**
* Renders a space picker
* @param {number} [limit=1] - Maximum items to be selected
* @returns {EditorField}
*/
spacePicker ( limit )
{
return this . _setComponent ( SpacePicker , { limit });
}
2020-10-18 19:51:11 +02:00
/**
* Renders a culture picker
* @returns {EditorField}
*/
2020-10-19 14:48:21 +02:00
culturePicker ()
2020-10-18 19:51:11 +02:00
{
return this . _setComponent ( CulturePicker );
}
2020-11-13 12:24:07 +01:00
/**
* Renders a mail template picker
* @param {number} [limit=1] - Maximum items to be selected
* @returns {EditorField}
*/
mailTemplatePicker ( limit )
{
return this . _setComponent ( MailTemplatePicker , { limit });
}
2020-10-20 16:00:55 +02:00
/**
* Renders a date picker
* @param {object} [options] - Custom options
* @param {string} [options.format] - Format the date output
* @param {boolean} [options.time=false] - Allow time input
* @param {string|Date} [options.maxDate] - Maximum selectable date
* @param {string|Date} [options.minDate] - Minimum selectable date
* @param {string} [options.amPm] - Render time as AM/PM
* @returns {EditorField}
*/
datePicker ( options )
{
return this . _setComponent ( DatePicker , { ... options });
}
2020-10-18 19:51:11 +02:00
/**
* Renders a date range picker
* @param {object} [options] - Custom options
* @param {string} [options.format] - Format the date output
* @param {boolean} [options.time=false] - Allow time input
* @param {string|Date} [options.maxDate] - Maximum selectable date
* @param {string|Date} [options.minDate] - Minimum selectable date
* @param {string} [options.fromLabel] - Label next to the "from" date input
* @param {string} [options.toLabel] - Label next to the "to" date input
* @param {string} [options.amPm] - Render time as AM/PM
* @param {string} [options.inline] - Don't render the range picker on an overlay
* @returns {EditorField}
*/
dateRangePicker ( options )
{
return this . _setComponent ( DateRangePicker , { ... options });
}
/**
* Pick an icon from the specified icon collection
* @param {string[]} [icons] - Custom icon set with icon class names
* @returns {EditorField}
*/
iconPicker ( icons )
{
return this . _setComponent ( IconPicker , { icons });
}
2020-10-29 21:44:22 +01:00
/**
* Renders a page picker
* @param {object} [options] - Custom options
* @param {number} [options.limit=1] - Limit of selection
* @returns {EditorField}
*/
pagePicker ( options )
{
return this . _setComponent ( PagePicker , { ... options });
}
2020-10-18 19:51:11 +02:00
/**
* Create a list of strings
* @param {number} [limit=10] - Limit the inputs
* @param {number} [maxItemLength=200] - Maximum length for an item input
* @param {string} [addLabel] - Label for the add button
* @returns {EditorField}
*/
2020-10-18 23:47:59 +02:00
inputList ( limit , maxItemLength , addLabel )
2020-10-18 19:51:11 +02:00
{
return this . _setComponent ( InputList , { limit , maxItemLength , addLabel });
}
/**
* Append tags to an entity
* @param {number} [limit=10] - Limit the tags
* @param {number} [maxItemLength=200] - Maximum length for a tag
* @returns {EditorField}
*/
tags ( limit , maxItemLength )
{
return this . _setComponent ( Tags , { limit , maxItemLength });
}
/**
* Pick a language
* @returns {EditorField}
*/
languagePicker ()
{
return this . _setComponent ( LanguagePicker );
}
/**
* Display a module renderer which allows you to select from defined modules
2020-10-20 16:00:55 +02:00
* @param {string[]} [tags] - Only allow selection of modules which match defined tags
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2020-10-20 16:00:55 +02:00
modules ( tags )
2020-10-18 19:51:11 +02:00
{
2020-10-20 16:00:55 +02:00
return this . _setComponent ( Modules , { tags });
2020-10-18 19:51:11 +02:00
}
/**
* Display a module renderer which allows you to select from defined modules
* @param {Editor} [editor] - Use the specified editor for each item
* @param {object} [options] - Custom options
* @param {number} [options.limit=100] - Limit the creation of items
* @param {string} [options.title] - Headline in the editor overlay
* @param {string} [options.addLabel] - Label for the add button
* @param {function} [options.itemLabel] - Function which generates the label for the current item
* @param {function} [options.itemDescription] - Function which generates the description for the current item
* @param {string|function} [options.itemIcon] - Static icon or function which generates the icon for the current item
* @param {object} [options.template] - Template which is used when adding an item
* @returns {EditorField}
*/
nested ( editor , options )
{
return this . _setComponent ( Nested , { editor , ... options });
}
/**
* Render a select as a button group
* @param {EditorSelectItem[]} items - Set items to choose from
* @returns {EditorField}
*/
state ( items )
{
return this . _setComponent ( State , { items });
}
/**
* Render a media upload + picker
* @param {object} [options] - Custom options
* @param {number} [options.limit=1] - Limit the media select count
* @param {boolean} [options.disallowSelect=false] - Disallow the selection (only upload) of media items
* @param {boolean} [options.disallowUpload=false] - Disallow upload (only selection) of media items
* @param {string[]} [options.fileExtensions] - Allow upload + selection only for the specified file extensions
* @param {number} [options.maxFileSize=10] - Maximum allowed file size for uploads in Mibibytes
* @returns {EditorField}
*/
media ( options )
{
return this . _setComponent ( Media , { ... options });
}
/**
* Render a media upload + picker
* @param {object} [options] - Custom options
* @param {number} [options.limit=1] - Limit the media select count
* @param {boolean} [options.disallowSelect=false] - Disallow the selection (only upload) of media items
* @param {boolean} [options.disallowUpload=false] - Disallow upload (only selection) of media items
* @param {string[]} [options.fileExtensions] - Allow upload + selection only for the specified file extensions
* @param {number} [options.maxFileSize=10] - Maximum allowed file size for uploads in Mibibytes
* @returns {EditorField}
*/
image ( options )
{
return this . _setComponent ( Media , { ... options , fileExtensions : [ '.jpg' , '.jpeg' , '.png' , '.webp' , '.svg' ] });
}
2020-10-21 01:04:49 +02:00
/**
* Create a preview for this field
* This is only used in list filters, ...
* @param {object} options - Custom options
* @param {number} options.icon - Custom icon
* @param {number} options.preview - Render the preview when this filter has been filled out
* @param {number} options.hasValue - Determine if the filter has a value or not
* @returns {EditorField}
*/
preview ( options )
{
this . # preview = { ... this . preview , ... options };
return this ;
}
2020-10-18 19:51:11 +02:00
}
export default EditorField ;