<commit_before>﻿import * as React from 'react'
import { Dic } from '../Globals'
import { Binding, LambdaMemberType, getTypeInfos, EntityKind, KindOfType } from '../Reflection'
import { ModifiableEntity } from '../Signum.Entities'
import * as Navigator from '../Navigator'
import { ViewReplacer } from '../Frames/ReactVisitor'
import { ValueLine, EntityLine, EntityCombo, EntityList, EntityDetail, EntityStrip, EntityRepeater, TypeContext, EntityCheckboxList, EntityTable } from '../Lines'

export default class DynamicComponent extends React.Component<{ ctx: TypeContext<ModifiableEntity> }, void> {

    render() {

        const subContexts = this.subContext(this.props.ctx);

        const components = subContexts.map(ctx => DynamicComponent.getAppropiateComponent(ctx)).filter(a => !!a).map(a => a!);

        const result = React.createElement("div", undefined, ...components);

        const es = Navigator.getSettings(this.props.ctx.value.Type);
        
        if (es && es.viewOverrides && es.viewOverrides.length) {
            const replacer = new ViewReplacer(result, this.props.ctx);
            es.viewOverrides.forEach(vo => vo(replacer));
            return replacer.result;
        } else {
            return result;
        }   
    }


    subContext(ctx: TypeContext<ModifiableEntity>): TypeContext<any>[] {

        const members = ctx.propertyRoute.subMembers();

        const result = Dic.map(members, (field, m) => ctx.subCtx(field));

        return result;
    }

    static specificComponents: {
        [typeName: string]: (ctx: TypeContext<any>) => React.ReactElement<any> | undefined;
    } = {};

    static getAppropiateComponent(ctx: TypeContext<any>): React.ReactElement<any> | undefined {
        const mi = ctx.propertyRoute.member!;

        if (mi.name == "Id" || mi.notVisible == true)
            return undefined;

        const tr = ctx.propertyRoute.typeReference();        

        const sc = DynamicComponent.specificComponents[tr.name];
        if (sc) {
            const result = sc(ctx);
            if (result)
                return result;
        }
        
        let tis = getTypeInfos(tr);

        if (tis.length == 1 && tis[0] == undefined)
            tis = []; 

        if (tr.isCollection) {
            if (tr.name == "[ALL]")
                return <EntityStrip ctx={ctx} />;

            if (tr.isEmbedded || tis.every(t => t.entityKind == "Part" || t.entityKind == "SharedPart"))
                return <EntityTable ctx={ctx}/>;
            else if (tis.every(t => t.isLowPopulation == true))
                return <EntityCheckboxList ctx ={ctx}/>;
            else
                return <EntityStrip ctx={ctx}/>;
        }

        if (tr.name == "[ALL]")
            return <EntityLine ctx={ctx}/>;

        if (tis.length) {
            if (tis.length == 1 && tis.first().kind == "Enum")
                return <ValueLine ctx={ctx}/>;

            if (tis.every(t => t.entityKind == "Part" || t.entityKind == "SharedPart"))
                return <EntityDetail ctx={ctx} />;

            if (tis.every(t => t.isLowPopulation == true))
                return <EntityCombo ctx={ctx}/>;           

            return <EntityLine ctx={ctx}/>;
        }

        if (tr.isEmbedded)
            return <EntityDetail ctx={ctx} />;

        if (ValueLine.getValueLineType(tr) != undefined)
            return <ValueLine ctx={ctx} />;

        return undefined;
    }

}<commit_msg>fix DynamicComponent for List of ImplementedBy noting<commit_after>﻿import * as React from 'react'
import { Dic } from '../Globals'
import { Binding, LambdaMemberType, getTypeInfos, EntityKind, KindOfType } from '../Reflection'
import { ModifiableEntity } from '../Signum.Entities'
import * as Navigator from '../Navigator'
import { ViewReplacer } from '../Frames/ReactVisitor'
import { ValueLine, EntityLine, EntityCombo, EntityList, EntityDetail, EntityStrip, EntityRepeater, TypeContext, EntityCheckboxList, EnumCheckboxList, EntityTable } from '../Lines'

export default class DynamicComponent extends React.Component<{ ctx: TypeContext<ModifiableEntity> }, void> {

    render() {

        const subContexts = this.subContext(this.props.ctx);

        const components = subContexts.map(ctx => DynamicComponent.getAppropiateComponent(ctx)).filter(a => !!a).map(a => a!);

        const result = React.createElement("div", undefined, ...components);

        const es = Navigator.getSettings(this.props.ctx.value.Type);
        
        if (es && es.viewOverrides && es.viewOverrides.length) {
            const replacer = new ViewReplacer(result, this.props.ctx);
            es.viewOverrides.forEach(vo => vo(replacer));
            return replacer.result;
        } else {
            return result;
        }   
    }


    subContext(ctx: TypeContext<ModifiableEntity>): TypeContext<any>[] {

        const members = ctx.propertyRoute.subMembers();

        const result = Dic.map(members, (field, m) => ctx.subCtx(field));

        return result;
    }

    static specificComponents: {
        [typeName: string]: (ctx: TypeContext<any>) => React.ReactElement<any> | undefined;
    } = {};

    static getAppropiateComponent(ctx: TypeContext<any>): React.ReactElement<any> | undefined {
        const mi = ctx.propertyRoute.member!;

        if (mi.name == "Id" || mi.notVisible == true)
            return undefined;

        const tr = ctx.propertyRoute.typeReference();        

        const sc = DynamicComponent.specificComponents[tr.name];
        if (sc) {
            const result = sc(ctx);
            if (result)
                return result;
        }
        
        let tis = getTypeInfos(tr);

        if (tis.length == 1 && tis[0] == undefined)
            tis = []; 

        if (tr.isCollection) {
            if (tr.name == "[ALL]")
                return <EntityStrip ctx={ctx} />;

            if (tis.length) {
                if (tis.length == 1 && tis.first().kind == "Enum")
                    return <EnumCheckboxList ctx={ctx} />;

                if (tr.isEmbedded || tis.every(t => t.entityKind == "Part" || t.entityKind == "SharedPart"))
                    return <EntityTable ctx={ctx} />;

                if (tis.every(t => t.isLowPopulation == true))
                    return <EntityCheckboxList ctx={ctx} />;

                return <EntityStrip ctx={ctx} />;
            }

            return undefined; 

        } else {

            if (tr.name == "[ALL]")
                return <EntityLine ctx={ctx} />;

            if (tis.length) {
                if (tis.length == 1 && tis.first().kind == "Enum")
                    return <ValueLine ctx={ctx} />;

                if (tis.every(t => t.entityKind == "Part" || t.entityKind == "SharedPart"))
                    return <EntityDetail ctx={ctx} />;

                if (tis.every(t => t.isLowPopulation == true))
                    return <EntityCombo ctx={ctx} />;

                return <EntityLine ctx={ctx} />;
            }

            if (tr.isEmbedded)
                return <EntityDetail ctx={ctx} />;

            if (ValueLine.getValueLineType(tr) != undefined)
                return <ValueLine ctx={ctx} />;

            return undefined;
        }
    }

}
{'fraction_non_alphanumeric': 0.13723051409618575, 'fraction_numerical': 0.0009673852957435047, 'mean_word_length': 2.099357601713062, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2486499', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2007, 'n_words': 469}