All files / src/viewcontrols GenericView.ts

100% Statements 24/24
83.33% Branches 10/12
100% Functions 5/5
100% Lines 22/22
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          1x 1x 1x 1x                       1x           1x             1x           1x     8x 24x                 65x 65x                 2x 2x 2x 2x         6x 2x 2x     6x 6x 6x    
/**
 * @module ViewControls
 * 
 */ /** */
 
import { bindable, autoinject } from 'aurelia-framework';
import { Content, ControlSchema, FieldSettings, ActionName, ContentTypes } from 'sn-client-js';
import { ControlNameResolverService, ControlMappingService } from '../services'
import { GenericContent } from 'sn-client-js/dist/src/ContentTypes';
 
/**
 * A generic View Control, works based on a single Content, renders FieldControls based on the AureliaControlMapper and the provided Schema. Also responsible to aggregating validation data
 * 
 * Usage:
 * ```html
 *  <generic-view content.bind='contentInstance' schema.bind='contentSchema'></generic-view>
 * ```
 */
 
@autoinject
export class GenericView {
    
    /**
     * The bindable Content instance
     */
    @bindable
    public content: Content;
    
 
    /**
     * the bindable ControlSchema object
     */
    @bindable
    public schema: ControlSchema<Object, FieldSettings.FieldSetting>;
 
    /**
     * The optional ActionName
     */
    @bindable 
    actionName?: ActionName;
 
    private parseSchema(){
        this.schema.FieldMappings = this.schema && this.schema.FieldMappings && this.schema.FieldMappings.map(m => {
            return {
                ...m,
                ControlTypeName: this.ControlNameResolverService.getNameForControl(m.ControlType),
            }
        });
    }
 
 
    constructor(
        private ControlNameResolverService: ControlNameResolverService,
        private ControlMappingService: ControlMappingService
    ) {
    }
 
    /**
     * a general activation method, that can be used with composition
     * @param { schema: ControlSchema<AureliaBaseControl, FieldSettings.FieldSetting>, content: Content, actionName: ActionName } model The model to be provided to activate the Control
     */
    activate(model: { schema: ControlSchema<Object, FieldSettings.FieldSetting>, content: Content, actionName: ActionName }) {
        this.content = model.content;
        this.schema = model.schema;
        this.actionName = model.actionName;
        this.parseSchema();
 
    }
 
    actionNameChanged(newName: ActionName, oldName: ActionName) {
        if (newName === 'view'){
            const savedFields = this.content.SavedFields;
            Object.assign(this.content, savedFields);
        }
        // ToDo: Check
        const contentType = this.content && (ContentTypes as any)[this.content.Type] || GenericContent as { new(...args) };
        this.schema = this.content && this.ControlMappingService.GetMappings(this.content.GetRepository()).GetFullSchemaForContentType(contentType, this.actionName || 'view');
        this.parseSchema();
    }
}