All files / src ControlMapper.ts

97.92% Statements 47/48
87.5% Branches 14/16
100% Functions 15/15
97.87% Lines 46/47
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226                                                    1x           1x           1x     19x 19x 19x 19x 19x                   327x   327x 327x 5386x   5104x   141x   141x       327x     19x                 1x 1x                 56x 56x     56x     19x                       2x 2x               275x 275x     19x                                 1x 1x                               1903x   273x 1x   272x       19x               1x 1x                 269x 269x                         54x 54x 267x 267x 267x           54x                           1x      
/**
 *
 * @module ControlMapper
 *
 * @preferred
 *
 * @description Module for mapping controls content types and / or field settings to specified front-end controls
 *
 * ```
 * let controlMapper = new ControlMapper(MyBaseControlClass, myConfigFactory, DefaultViewComponent, DefaultFieldComponent)
 *       .SetupFieldSettingDefault(FieldSettings.ShortTextFieldSetting, (setting) => {
 *          switch (setting.Name) {
 *              case 'Name':
 *                  return NameField;
 *              case 'DisplayName':
 *                  return DisplayName;
 *              default:
 *                  break;
 *          }
 *          return ShortText;
 *       })
 *
 * ```
 */ /** */
 
import { IContent } from './Content';
import * as FieldSettings from './FieldSettings';
import { BaseRepository } from './Repository/index';
import { Schemas } from './SN';
 
export type ActionName = 'new' | 'edit' | 'view';
 
export class ControlSchema<TControlBaseType, TClientControlSettings> {
    public ContentTypeControl: { new(...args: any[]): TControlBaseType };
    public Schema: Schemas.Schema;
    public FieldMappings: { FieldSettings: FieldSettings.FieldSetting, ControlType: { new(...args: any[]): TControlBaseType }, ClientSettings: TClientControlSettings }[];
}
 
export class ControlMapper<TControlBaseType, TClientControlSettings> {
 
    constructor(
        private readonly _repository: BaseRepository,
        public readonly ControlBaseType: { new(...args: any[]): TControlBaseType },
        private readonly _clientControlSettingsFactory: (fieldSetting: FieldSettings.FieldSetting) => TClientControlSettings,
        private readonly _defaultControlType?: { new(...args: any[]): TControlBaseType },
        private readonly _defaultFieldSettingControlType?: { new(...args: any[]): TControlBaseType },
    ) {
    }
 
    /**
     * Method for getting a specified Schema object for a content type. The FieldSettings will be filtered based on the provided actionName.
     * @param contentType The type of the content (e.g. ContentTypes.Task)
     * @param actionName The name of the action. Can be 'new' / 'view' / 'edit'
     */
    private getTypeSchema<TContentType extends IContent>(contentType: { new(args: any[]): TContentType }, actionName: ActionName): Schemas.Schema {
        const schema = this._repository.GetSchema(contentType);
 
        Eif (actionName) {
            schema.FieldSettings = schema.FieldSettings.filter((s) => {
                switch (actionName) {
                    case 'new':
                        return s.VisibleNew === FieldSettings.FieldVisibility.Show;
                    case 'edit':
                        return s.VisibleEdit === FieldSettings.FieldVisibility.Show;
                    case 'view':
                        return s.VisibleBrowse === FieldSettings.FieldVisibility.Show;
                }
            });
        }
        return schema;
    }
 
    private _contentTypeControlMaps: Map<string, { new(...args: any[]): TControlBaseType }> = new Map();
 
    /**
     * Maps a specified Control to a Content type
     * @param content The Content to be mapped
     * @param control The Control for the content
     * @returns {ControlMapper}
     */
    public MapContentTypeToControl(contentType: { new(...args: any[]): IContent }, control: { new(...args: any[]): TControlBaseType }) {
        this._contentTypeControlMaps.set(contentType.name, control);
        return this;
    }
 
    /**
     *
     * @param content The content to get the control for.
     * @returns {TControlBaseType} The mapped control, Default if nothing is mapped.
     */
    public GetControlForContentType<TContentType extends IContent>(contentType: { new(...args: any[]): TContentType }): { new(...args: any[]): TControlBaseType } {
        const control = this._contentTypeControlMaps.get(contentType.name) || this._defaultControlType;
        Iif (!control) {
            throw Error('');
        }
        return control as ({ new(...args: any[]): TControlBaseType });
    }
 
    private _fieldSettingDefaults: Map<string, ((fieldSetting: FieldSettings.FieldSetting) => { new(...args: any[]): TControlBaseType })> = new Map();
 
    /**
     *
     * @param fieldSetting The FieldSetting to get the control for.
     * @param setupControl Callback method that returns a Control Type based on the provided FieldSetting
     * @returns the Mapper instance (can be used fluently)
     */
    public SetupFieldSettingDefault<TFieldSettingType extends FieldSettings.FieldSetting>(
        fieldSetting: { new(...args: any[]): TFieldSettingType },
        setupControl: (fieldSetting: TFieldSettingType) => { new(...args: any[]): TControlBaseType }
    ) {
        this._fieldSettingDefaults.set(fieldSetting.name, setupControl as (fieldSetting: FieldSettings.FieldSetting) => { new(...args: any[]): TControlBaseType });
        return this;
    }
 
    /**
     * @returns {TControlBaseType} The specified FieldSetting control
     * @param fieldSetting The FieldSetting to get the control class.
     */
    public GetControlForFieldSetting<TFieldSettingType extends FieldSettings.FieldSetting>(fieldSetting: TFieldSettingType): { new(...args: any[]): TControlBaseType } {
        const fieldSettingSetup = this._fieldSettingDefaults.get(fieldSetting.Type) as (fieldSetting: any) => { new(...args: any[]): TControlBaseType };
        return fieldSettingSetup && fieldSettingSetup(fieldSetting) || this._defaultFieldSettingControlType;
    }
 
    private _contentTypeBoundfieldSettings: Map<string, ((fieldSetting: FieldSettings.FieldSetting) => { new(...args: any[]): TControlBaseType })> = new Map();
 
    /**
     *
     * @param contentType The Content Type
     * @param fieldName The name of the field on the Content Type
     * @param setupControl The callback function that will setup the Control
     * @param fieldSetting Optional type hint for the FieldSetting
     */
 
    public SetupFieldSettingForControl<TFieldSettingType extends FieldSettings.FieldSetting, TContentType extends IContent, TField extends keyof TContentType>(
        contentType: { new(...args: any[]): TContentType },
        fieldName: TField,
        setupControl: (fieldSetting: TFieldSettingType) => { new(...args: any[]): TControlBaseType },
        fieldSetting?: { new(...args: any[]): TFieldSettingType },
 
    ) {
        this._contentTypeBoundfieldSettings.set(`${contentType.name}-${fieldName}`, setupControl as any);
        return this;
    }
 
    /**
     *
     * @param contentType The type of the content (e.g. ContentTypes.Task)
     * @param fieldName The name of the field (must be one of the ContentType's fields), e.g. 'DisplayName'
     * @param actionName The name of the Action (can be 'new' / 'edit' / 'view')
     * @returns The assigned Control constructor or the default Field control
     */
    public GetControlForContentField<TContentType extends IContent, TField extends keyof TContentType>(
        contentType: { new(...args: any[]): TContentType },
        fieldName: TField,
        actionName: ActionName
    ): { new(...args: any[]): TControlBaseType } {
 
        const fieldSetting = this.getTypeSchema(contentType, actionName).FieldSettings.filter((s) => s.Name === fieldName)[0];
 
        if (this._contentTypeBoundfieldSettings.has(`${contentType.name}-${fieldName}`)) {
            return (this._contentTypeBoundfieldSettings.get(`${contentType.name}-${fieldName}`) as any)(fieldSetting);
        } else {
            return this.GetControlForFieldSetting(fieldSetting);
        }
    }
 
    private _fieldSettingBoundClientSettingFactories: Map<string, ((setting: FieldSettings.FieldSetting) => TClientControlSettings)> = new Map();
 
    /**
     * Sets up a Factory method to create library-specific settings from FieldSettings per type
     * @param fieldSettingType The type of the FieldSetting (e.g. FieldSettings.ShortTextFieldSetting)
     * @param factoryMethod The factory method that constructs or transforms the Settings object
     */
    public SetClientControlFactory<TFieldSetting extends FieldSettings.FieldSetting>(fieldSettingType: { new(...args: any[]): TFieldSetting }, factoryMethod: (setting: TFieldSetting) => TClientControlSettings) {
        this._fieldSettingBoundClientSettingFactories.set(fieldSettingType.name, factoryMethod as any);
        return this;
    }
 
    /**
     * Creates a ClientSetting from a specified FieldSetting based on the assigned Factory method
     * @param fieldSetting The FieldSetting object that should be used for creating the new Setting entry
     * @returns the created or transformed Client Setting
     */
    public CreateClientSetting<TFieldSetting extends FieldSettings.FieldSetting>(fieldSetting: TFieldSetting) {
        const factoryMethod = this._fieldSettingBoundClientSettingFactories.get(fieldSetting.Type) || this._clientControlSettingsFactory;
        return factoryMethod(fieldSetting);
    }
 
    /**
     * Gets the full ControlSchema object for a specific ContentType
     * @param contentType The type of the Content (e.g. ContentTypes.Task)
     * @param actionName The name of the Action (can be 'new' / 'edit' / 'view')
     * @returns the fully created ControlSchema
     */
    public GetFullSchemaForContentType<TContentType extends IContent, K extends keyof TContentType>(
        contentType: { new(...args: any[]): TContentType },
        actionName: ActionName):
        ControlSchema<TControlBaseType, TClientControlSettings> {
        const schema = this.getTypeSchema(contentType, actionName);
        const mappings = schema.FieldSettings.map((f) => {
            const clientSetting: TClientControlSettings = this.CreateClientSetting(f);
            const control: { new(...args: any[]): TControlBaseType } = this.GetControlForContentField<TContentType, K>(contentType, f.Name as K, actionName);
            return {
                FieldSettings: f,
                ClientSettings: clientSetting,
                ControlType: control
            };
        });
        return {
            Schema: schema,
            ContentTypeControl: this.GetControlForContentType(contentType),
            FieldMappings: mappings
        };
    }
 
    /**
     * Gets the full ControlSchema object for a specific Content
     * @param contentType The type of the Content (e.g. ContentTypes.Task)
     * @param actionName The name of the Action (can be 'new' / 'edit' / 'view')
     * @returns the fully created ControlSchema
     */
    public GetFullSchemaForContent<TContentType extends IContent>(content: TContentType, actionName: ActionName) {
        return this.GetFullSchemaForContentType(content.constructor as { new(...args: any[]): TContentType }, actionName);
    }
}