All files / sn-client-js/src ControlMapper.ts

100% Statements 46/46
92.86% Branches 13/14
100% Functions 15/15
100% Lines 45/45
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                                                  1x 1x 1x       1x           1x     19x 19x 19x 19x                   333x   333x 333x 19469x   18269x   600x   600x       333x     19x                 1x 1x                 56x       19x                         2x 2x               281x 281x     19x                                 1x 1x                                 1975x   279x 1x     278x       19x               1x 1x                 275x 275x                         54x 54x 273x 273x 273x           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 { Content } from './Content';
import * as FieldSettings from './FieldSettings';
import { Schemas } from './SN';
 
export type ActionName = 'new' | 'edit' | 'view';
 
export class ControlSchema<TControlBaseType, TClientControlSettings> {
    ContentTypeControl: {new(...args: any[]): TControlBaseType};
    Schema: Schemas.Schema<Content>;
    FieldMappings: {FieldSettings: FieldSettings.FieldSetting, ControlType: {new(...args: any[]): TControlBaseType}, ClientSettings: TClientControlSettings}[];
}
 
export class ControlMapper<TControlBaseType, TClientControlSettings> {
 
    constructor(
        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 Content>(contentType: { new (args: any[]): TContentType }, actionName: ActionName): Schemas.Schema<TContentType> {
        const schema = new Schemas.Schema<TContentType>(Content.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: { new (...args: any[]): TControlBaseType }[] = [];
 
    /**
     * 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[]): Content }, control: { new (...args: any[]): TControlBaseType }) {
        this.contentTypeControlMaps[contentType.name as any] = 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 Content>(contentType: { new (...args: any[]): TContentType }) {
        return this.contentTypeControlMaps[contentType.name as any] || this.defaultControlType;
    }
 
 
    private fieldSettingDefaults: ((fieldSetting: FieldSettings.FieldSetting) => { new (...args: any[]): TControlBaseType })[] = [];
 
 
    /**
     * 
     * @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[fieldSetting.name as any] = setupControl;
        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[fieldSetting.constructor.name as any];
        return fieldSettingSetup && fieldSettingSetup(fieldSetting) || this.defaultFieldSettingControlType;
    }
 
    private contentTypeBoundfieldSettings: ((fieldSetting: FieldSettings.FieldSetting) => { new (...args: any[]): TControlBaseType })[] = [];
 
    /**
     * 
     * @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 Content, 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[`${contentType.name}-${fieldName}` as any] = setupControl;
        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 Content, 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[`${contentType.name}-${fieldName}` as any]) {
            return this.contentTypeBoundfieldSettings[`${contentType.name}-${fieldName}` as any](fieldSetting);
        }
        else {
            return this.GetControlForFieldSetting(fieldSetting);
        }
    }
 
    private fieldSettingBoundClientSettingFactories: ((setting: FieldSettings.FieldSetting) => TClientControlSettings)[] = []
    
    /**
     * 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[fieldSettingType.name as any] = factoryMethod;
        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[fieldSetting.constructor.name as any] || 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 Content, 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 Content>(content: TContentType, actionName: ActionName){
        return this.GetFullSchemaForContentType(content.constructor as {new(...args: any[])}, actionName);
    }
}