File size: 4,972 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
/* eslint class-methods-use-this: 0 object-curly-newline: 0 */
/**
* @name PropertyType
* @typedef {object} PropertyType
* @memberof BaseProperty
* @alias PropertyType
* @property {string} string default property type
* @property {string} float type of floating point numbers
* @property {string} number regular number
* @property {string} boolean boolean value
* @property {string} date date
* @property {string} datetime date with time
* @property {string} mixed type representing an object
* @property {string} reference many to one reference
* @property {string} richtext wysiwig editor
* @property {string} textarea resizable textarea input
* @property {string} password password field
*/
// Spacer
const TITLE_COLUMN_NAMES = ['title', 'name', 'subject', 'email']
export type PropertyType =
'string' | 'float' | 'number' | 'boolean' |
'date' | 'datetime' | 'mixed' | 'reference' | 'key-value' |
'richtext' | 'textarea' | 'password' | 'currency' | 'phone' | 'uuid';
// description
type BasePropertyAttrs = {
path: string;
type?: PropertyType;
isId?: boolean;
isSortable?: boolean;
position?: number;
}
/**
* Represents Resource Property
* @category Base
*/
class BaseProperty {
private _path: string
private _type: PropertyType
private _isId: boolean
private _isSortable: boolean
private _position: number
/**
* @param {object} options
* @param {string} options.path property path: usually it its key but when
* property is for an object the path can be
* divided to parts by dots: i.e.
* 'address.street'
* @param {PropertyType} [options.type='string']
* @param {boolean} [options.isId=false] true when field should be treated as an ID
* @param {boolean} [options.isSortable=true] if property should be sortable
*/
constructor({
path,
type = 'string',
isId = false,
isSortable = true,
position = 1,
}: BasePropertyAttrs) {
this._path = path
this._type = type
this._isId = isId
if (!this._path) {
throw new Error('you have to give path parameter when creating BaseProperty')
}
this._isSortable = isSortable
this._position = position
}
/**
* Name of the property
* @return {string} name of the property
*/
name(): string {
return this._path
}
path(): string {
return this.name()
}
position(): number {
return this._position === undefined ? 1 : this._position
}
/**
* Return type of a property
* @return {PropertyType}
*/
type(): PropertyType {
return this._type || 'string'
}
/**
* Return true if given property should be treated as a Record Title.
*
* @return {boolean}
*/
isTitle(): boolean {
return TITLE_COLUMN_NAMES.includes(this._path.toLowerCase())
}
/**
* Indicates if given property should be visible
*
* @return {Boolean}
*/
isVisible(): boolean {
return !this._path || !this._path.match('password')
}
/**
* Indicates if value of given property can be updated
*
* @return {boolean}
*/
isEditable(): boolean {
return true
}
/**
* Returns true if given property is a uniq key in a table/collection
*
* @return {boolean}
*/
isId(): boolean {
return !!this._isId
}
/**
* If property is a reference to a record of different resource
* it should contain {@link BaseResource.id} of this resource.
*
* When property is responsible for the field: 'user_id' in SQL database
* reference should be the name of the Resource which it refers to: `Users`
*/
reference(): string | null {
return null
}
/**
* Returns all available values which field can accept. It is used in case of
* enums
*
* @return {Array<String> | null} array of all available values or null when field
* is not an enum.
*/
availableValues(): Array<string> | null {
return null
}
/**
* Returns true when given property is an array
*
* @return {boolean}
*/
isArray(): boolean {
return false
}
/**
* Returns true when given property has draggable elements.
* Only usable for array properties.
*
* @return {boolean}
*/
isDraggable(): boolean {
return false
}
/**
* In case of `mixed` type returns all nested properties.
*
* @return {Array<BaseProperty>} sub properties
*/
subProperties(): Array<BaseProperty> {
return []
}
/**
* Indicates if given property can be sorted
*
* @return {boolean}
*/
isSortable(): boolean {
return this._isSortable
}
/**
* Indicates if given property is required
*/
isRequired(): boolean {
return false
}
}
export default BaseProperty
|