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 ,
2021-01-13 13:51:55 +01:00
allTabs : false ,
2021-08-24 16:06:13 +02:00
vertical : true ,
2020-11-19 18:09:59 +01:00
coreDatabase : false ,
2021-09-10 14:52:28 +02:00
fieldset : null ,
fieldsetColumns : null ,
2021-09-14 10:14:54 +02:00
class : '' ,
onChange : null
2020-10-18 19:51:11 +02:00
};
2021-05-08 15:20:23 +02:00
_preview = {
2020-10-21 01:04:49 +02:00
icon : 'fth-filter' ,
preview : x => x ,
hasValue : x => !! x
};
2021-05-08 15:20:23 +02:00
_component = null ;
_componentOptions = {};
_required = false ;
_isReadOnly = false ;
2020-10-18 19:51:11 +02:00
constructor ( path , options )
{
this . path = path ;
this . options = { ... this . options , ... options };
}
2020-10-18 23:47:59 +02:00
get component ()
{
2021-05-08 15:20:23 +02:00
return this . _component ;
2020-10-18 23:47:59 +02:00
}
get componentOptions ()
{
2021-05-08 15:20:23 +02:00
return this . _componentOptions ;
2020-10-18 23:47:59 +02:00
}
get isRequired ()
{
2021-05-08 15:20:23 +02:00
return this . _required ;
2020-10-18 23:47:59 +02:00
}
2020-10-21 01:04:49 +02:00
get previewOptions ()
{
2021-05-08 15:20:23 +02:00
return this . _preview ;
2020-10-21 01:04:49 +02:00
}
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 };
2021-05-08 15:20:23 +02:00
this . _preview = { ... field . previewOptions };
this . _component = field . component ;
this . _componentOptions = field . componentOptions ;
this . _required = field . isRequired ;
2020-10-22 11:52:22 +02:00
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
{
2021-05-08 15:20:23 +02:00
this . _component = component ;
this . _componentOptions = options || {};
2020-10-18 19:51:11 +02:00
return this ;
}
2021-09-10 14:52:28 +02:00
/**
* Sets the column count for this field, only available within a an editor fieldset
* @param {number} columnCount - Column count between 1 and 12
* @returns {EditorField}
*/
cols ( columnCount )
{
this . options . fieldsetColumns = columnCount < 1 ? 1 : ( columnCount > 12 ? 12 : columnCount );
return this ;
}
2021-09-10 15:40:43 +02:00
/**
* Whether the input is below the headline or next to it
* @param {boolean} isVertical
* @returns {EditorField}
*/
vertical ( isVertical )
{
this . options . vertical = isVertical ;
return this ;
}
2021-03-29 13:21:33 +02:00
/**
* Set this field to disabled
*/
disabled ()
{
this . options . disabled = true ;
return this ;
}
2020-10-18 19:51:11 +02:00
/**
* 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' )
{
2021-05-08 15:20:23 +02:00
this . _required = condition ;
2020-10-18 19:51:11 +02:00
}
else if ( typeof condition === 'boolean' )
{
2021-05-08 15:20:23 +02:00
this . _required = condition ;
2020-10-18 19:51:11 +02:00
}
else
{
2021-05-08 15:20:23 +02:00
this . _required = true ;
2020-10-18 19:51:11 +02:00
}
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 ;
}
2021-09-14 10:14:54 +02:00
/**
* The expression argument is called when the value of the field changes
* @param {function} expression - function which is called
*/
onChange ( expression )
{
this . options . onChange = expression ;
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
2021-04-23 14:06:30 +02:00
* @param {string|function} [placeholder] - Placeholder text (can be a translation) or function
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
text ( maxLength , placeholder )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/text.vue' ), { maxLength , placeholder });
2020-10-18 19:51:11 +02:00
}
2021-01-15 13:56:50 +01:00
/**
* Render a password input field
* @param {number} [maxLength] - Maximum length of the input
2021-04-23 14:06:30 +02:00
* @param {string|function} [placeholder] - Placeholder text (can be a translation) or function
2021-01-15 13:56:50 +01:00
* @returns {EditorField}
*/
password ( maxLength , placeholder )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/password.vue' ), { maxLength , placeholder });
2021-01-15 13:56:50 +01:00
}
2021-09-23 10:58:14 +02:00
/**
* Render a password hash field
* @param {number} [maxLength] - Maximum length of the password
* @returns {EditorField}
*/
passwordHash ( maxLength )
{
return this . _setComponent (() => import ( '../editor/fields/password-hash.vue' ), { maxLength });
}
2020-10-18 19:51:11 +02:00
/**
* Render a currency input field
2021-04-23 14:06:30 +02:00
* @param {string|function} [placeholder] - Placeholder text (can be a translation) or function
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
currency ( placeholder )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/currency.vue' ), { placeholder });
2020-10-18 19:51:11 +02:00
}
/**
* Render a number input field
* @param {number} [maxLength] - Maximum length of the input
2021-04-23 14:06:30 +02:00
* @param {string|function} [placeholder] - Placeholder text (can be a translation) or function
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
number ( maxLength , placeholder )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/number.vue' ), { maxLength , placeholder });
2020-10-18 19:51:11 +02:00
}
/**
* Render a rich-text editor field
2021-06-21 13:41:48 +02:00
* @param {object} [options] - Custom options
* @param {number} [options.maxLength=null] - Maximum characters
* @param {string} [options.placeholder=null] - Placeholder text (can be a translation) or function
* @param {function} [options.setup=value] - Called on RTE setup
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2021-06-21 13:41:48 +02:00
rte ( options )
2020-10-18 19:51:11 +02:00
{
2021-06-21 13:41:48 +02:00
return this . _setComponent (() => import ( '../editor/fields/rte.vue' ), { ... options });
2020-10-18 19:51:11 +02:00
}
/**
* @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
2021-01-17 12:31:48 +01:00
* @param {EditorSelectItem[]|function} items - Set items to pick from
* @param {object} [options] - Custom options
* @param {number} [options.emptyOption=false] - Adds an empty option so the field can be blank
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2021-01-17 12:31:48 +01:00
select ( items , options )
2020-10-18 19:51:11 +02:00
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/select.vue' ), { items , ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Render a text area
* @param {number} [maxLength] - Maximum length of the input
* @returns {EditorField}
*/
textarea ( maxLength )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/textarea.vue' ), { maxLength });
2020-10-18 19:51:11 +02:00
}
/**
* Render a toggle
* @param {boolean} [negative] - Toggle with a negative color / red background
2021-10-20 14:00:24 +02:00
* @param {object} [options] - Custom options
* @param {number} [options.negative=false] - Display the toggle in red when set to TRUE
* @param {number} [options.onContent=null] - Text next to toggle when it is set to TRUE
* @param {number} [options.offContent=null] - Text next to toggle when it is set to FALSE
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2021-10-20 14:00:24 +02:00
toggle ( options )
2020-10-18 19:51:11 +02:00
{
2021-08-24 16:06:13 +02:00
this . options . vertical = false ;
2021-10-20 14:00:24 +02:00
return this . _setComponent (() => import ( '../editor/fields/toggle.vue' ), { ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Renders the field value
* @param {function} [render] - Render the output based on the given function
* @returns {EditorField}
*/
output ( render )
{
2021-05-08 15:20:23 +02:00
this . _isReadOnly = true ;
2021-08-16 10:30:45 +02:00
return this . _setComponent (() => import ( '../editor/fields/output.vue' ), { render });
2020-10-18 19:51:11 +02:00
}
/**
* 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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/alias.vue' ), { namePath });
2020-10-18 19:51:11 +02:00
}
/**
* 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
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/checklist.vue' ), { items , ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Renders a HEX color picker
* @returns {EditorField}
*/
colorPicker ()
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/colorpicker.vue' ));
2020-10-18 19:51:11 +02:00
}
/**
* Renders a country picker
* @param {number} [limit=1] - Maximum items to be selected
* @returns {EditorField}
*/
countryPicker ( limit )
{
2021-04-02 12:31:30 +02:00
return this . _setComponent (() => import ( '../editor/fields/countrypicker.vue' ), { limit });
2020-10-18 19:51:11 +02:00
}
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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/spacepicker.vue' ), { limit });
2020-12-10 01:03:22 +01:00
}
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
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/culturepicker.vue' ));
2020-10-18 19:51:11 +02:00
}
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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/mailtemplatepicker.vue' ), { limit });
2020-11-13 12:24:07 +01:00
}
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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/datepicker.vue' ), { ... options });
2020-10-20 16:00:55 +02:00
}
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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/daterangepicker.vue' ), { ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Pick an icon from the specified icon collection
2021-01-17 16:29:05 +01:00
* @param {string} [iconSetAlias] - Custom icon set alias (defined in ZeroOptions.Icons)
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
2021-01-17 16:29:05 +01:00
iconPicker ( iconSetAlias )
2020-10-18 19:51:11 +02:00
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/iconPicker.vue' ), { set : iconSetAlias });
2020-10-18 19:51:11 +02:00
}
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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/pagepicker.vue' ), { ... options });
2020-10-29 21:44:22 +01:00
}
2021-01-29 17:01:32 +01:00
/**
* Render a link picker
* @param {object} [options] - Custom options
* @param {number} [options.limit=1] - Limit of selection
2021-01-30 16:52:36 +01:00
* @param {boolean} [options.title=true] - Allow input of custom link title
* @param {boolean} [options.target=true] - Allow selection of the link target
* @param {boolean} [options.label=false] - Allow input of a custom label for button generation
* @param {boolean} [options.suffix=false] - Allow input of custom link URL suffix (query or hash)
* @param {string[]} [options.areas] - Limit link areas to the specified values (built-in are zero.pages, zero.media and zero.url)
2021-01-29 17:01:32 +01:00
* @returns {EditorField}
*/
linkPicker ( options )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/linkpicker.vue' ), { ... options });
2021-01-29 17:01:32 +01:00
}
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
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/inputlist.vue' ), { limit , maxItemLength , addLabel });
2020-10-18 19:51:11 +02:00
}
/**
* 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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/tags.vue' ), { limit , maxItemLength });
2020-10-18 19:51:11 +02:00
}
/**
* Pick a language
* @returns {EditorField}
*/
languagePicker ()
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/language.vue' ));
2020-10-18 19:51:11 +02:00
}
/**
* 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
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/modules.vue' ), { 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
2021-08-29 14:45:37 +02:00
* @param {object} [options.width=820] - Width of the overlay panel
2020-10-18 19:51:11 +02:00
* @returns {EditorField}
*/
nested ( editor , options )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/nested.vue' ), { editor , ... options });
2020-10-18 19:51:11 +02:00
}
/**
* Render a select as a button group
* @param {EditorSelectItem[]} items - Set items to choose from
* @returns {EditorField}
*/
state ( items )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/state.vue' ), { items });
2020-10-18 19:51:11 +02:00
}
/**
* 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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/media.vue' ), { ... options });
2020-10-18 19:51:11 +02:00
}
/**
* 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 )
{
2021-03-24 20:54:52 +01:00
return this . _setComponent (() => import ( '../editor/fields/media.vue' ), { ... options , fileExtensions : [ '.jpg' , '.jpeg' , '.png' , '.webp' , '.svg' ] });
2020-10-18 19:51:11 +02:00
}
2020-10-21 01:04:49 +02:00
2021-07-19 12:28:37 +02:00
/**
* Render a video (YouTube/vimeo) picker
* @param {number} [limit=1] - Limit the videos
* @returns {EditorField}
*/
video ( limit )
{
return this . _setComponent (() => import ( '../editor/fields/video.vue' ), { limit });
}
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
2021-09-23 15:04:36 +02:00
* @param {string} options.icon - Custom icon
* @param {string|function} options.preview - Render the preview when this filter has been filled out
* @param {boolean} options.hasValue - Determine if the filter has a value or not
2020-10-21 01:04:49 +02:00
* @returns {EditorField}
*/
preview ( options )
{
2021-05-08 15:20:23 +02:00
this . _preview = { ... this . preview , ... options };
2020-10-21 01:04:49 +02:00
return this ;
}
2020-10-18 19:51:11 +02:00
}
export default EditorField ;