All files / src/viewcontrols ContentView.ts

100% Statements 21/21
100% Branches 7/7
100% Functions 4/4
100% Lines 18/18
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          1x   1x 1x 1x                     1x   4x 4x               1x           4x     1x         3x     4x   4x   7x 7x 7x   7x               1x          
/**
 * @module ViewControls
 * 
 * 
 */ /** */
import { bindable, autoinject } from 'aurelia-framework';
import { ActionName } from 'sn-client-js';
import { Content, ControlSchema, FieldSettings, ContentTypes } from 'sn-client-js';
import { ControlMappingService, ControlNameResolverService } from '../services';
import { GenericContent } from 'sn-client-js/dist/src/ContentTypes';
 
/**
 * A very top level View Control, works with a single Content and based on the AureliaControlMapper
 * 
 * Usage:
 * ```html
 *  <content-view content.bind='contentInstance'></content-view>
 * ```
 */
@autoinject
 export class ContentView  {
    constructor(
        private controlMappingService: ControlMappingService,
        private ControlNameResolverService: ControlNameResolverService
    ) {
    }
 
    /**
     * The bindable Content instance
     */
    @bindable
    public content: Content;
    
    /**
     * @returns the ActionName, it is based on the Content state ('view' by default, 'new' if the content is not saved yet)
     */
    @bindable
    public actionName: ActionName  = 'view';
 
    @bindable
    controlName: string;
 
 
 
    contentChanged(){
        this.contextChange();
    } 
    actionNameChanged(){
        this.contextChange();
    }
    public contextChange: () => void = () => {
        // ToDo: Reload content with required fields
        const contentType = this.content && (ContentTypes as any)[this.content.Type] || GenericContent as {new(...args)};
        const schema = this.content && this.controlMappingService.GetMappings(this.content.GetRepository()).GetFullSchemaForContentType(contentType, this.actionName);
        this.controlName = this.ControlNameResolverService.getNameForControl(schema && schema.ContentTypeControl);
 
        this.model = {
            schema,
            content: this.content,
            actionName: this.actionName
        };
    }
 
    @bindable
    public model: { 
        schema: ControlSchema<{}, FieldSettings.FieldSetting>,
        actionName: ActionName,
        content: Content
    };
}