All files / dist/src ControlMapper.js

0% Statements 0/46
0% Branches 0/14
0% Functions 0/15
0% Lines 0/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                                                                                                                                                                                         
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const FieldSettings = require("./FieldSettings");
class ControlSchema {
}
exports.ControlSchema = ControlSchema;
class ControlMapper {
    constructor(_repository, ControlBaseType, _clientControlSettingsFactory, _defaultControlType, _defaultFieldSettingControlType) {
        this._repository = _repository;
        this.ControlBaseType = ControlBaseType;
        this._clientControlSettingsFactory = _clientControlSettingsFactory;
        this._defaultControlType = _defaultControlType;
        this._defaultFieldSettingControlType = _defaultFieldSettingControlType;
        this._contentTypeControlMaps = [];
        this._fieldSettingDefaults = new Map();
        this._contentTypeBoundfieldSettings = new Map();
        this._fieldSettingBoundClientSettingFactories = new Map();
    }
    getTypeSchema(contentType, actionName) {
        const schema = this._repository.GetSchema(contentType);
        if (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;
    }
    MapContentTypeToControl(contentType, control) {
        this._contentTypeControlMaps[contentType.name] = control;
        return this;
    }
    GetControlForContentType(contentType) {
        return this._contentTypeControlMaps[contentType.name] || this._defaultControlType;
    }
    SetupFieldSettingDefault(fieldSetting, setupControl) {
        this._fieldSettingDefaults.set(fieldSetting.name, setupControl);
        return this;
    }
    GetControlForFieldSetting(fieldSetting) {
        const fieldSettingSetup = this._fieldSettingDefaults.get(fieldSetting.Type);
        return fieldSettingSetup && fieldSettingSetup(fieldSetting) || this._defaultFieldSettingControlType;
    }
    SetupFieldSettingForControl(contentType, fieldName, setupControl, fieldSetting) {
        this._contentTypeBoundfieldSettings.set(`${contentType.name}-${fieldName}`, setupControl);
        return this;
    }
    GetControlForContentField(contentType, fieldName, actionName) {
        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}`)(fieldSetting);
        }
        else {
            return this.GetControlForFieldSetting(fieldSetting);
        }
    }
    SetClientControlFactory(fieldSettingType, factoryMethod) {
        this._fieldSettingBoundClientSettingFactories.set(fieldSettingType.name, factoryMethod);
        return this;
    }
    CreateClientSetting(fieldSetting) {
        const factoryMethod = this._fieldSettingBoundClientSettingFactories.get(fieldSetting.Type) || this._clientControlSettingsFactory;
        return factoryMethod(fieldSetting);
    }
    GetFullSchemaForContentType(contentType, actionName) {
        const schema = this.getTypeSchema(contentType, actionName);
        const mappings = schema.FieldSettings.map((f) => {
            const clientSetting = this.CreateClientSetting(f);
            const control = this.GetControlForContentField(contentType, f.Name, actionName);
            return {
                FieldSettings: f,
                ClientSettings: clientSetting,
                ControlType: control
            };
        });
        return {
            Schema: schema,
            ContentTypeControl: this.GetControlForContentType(contentType),
            FieldMappings: mappings
        };
    }
    GetFullSchemaForContent(content, actionName) {
        return this.GetFullSchemaForContentType(content.constructor, actionName);
    }
}
exports.ControlMapper = ControlMapper;
//# sourceMappingURL=ControlMapper.js.map