All files / src/viewcontrols content-view.ts

100% Statements 13/13
100% Branches 7/7
100% Functions 3/3
100% Lines 12/12
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          1x   1x   1x 1x                     1x           1x           4x           1x     34x   34x             1x 18x    
/**
 * @module ViewControls
 * 
 * 
 */ /** */
import { bindable, computedFrom } from 'aurelia-framework';
import { ActionName } from 'sn-client-js';
import { Content, ControlSchema, FieldSettings, ContentTypes } from 'sn-client-js';
 
import { AureliaBaseControl } from '../AureliaBaseControl';
import { AureliaControlMapper } from '../AureliaControlMapper';
 
 
/**
 * 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>
 * ```
 */
export class ContentView extends AureliaBaseControl {
 
    /**
     * 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';
 
    /**
     * @returns the Schema object, based on the binded content and the assigned actionName
     */
    @computedFrom('content', 'actionName')
    public get schema(): ControlSchema<AureliaBaseControl, FieldSettings.FieldSetting>{
 
        // ToDo: Reload content with required fields
        const contentType = this.content && (ContentTypes as any)[this.content.Type] || Content as {new(...args)};
 
        return this.content && AureliaControlMapper.GetFullSchemaForContentType(contentType, this.actionName);
    }
 
    /**
     * @returns the ViewControl, as resolved from the AureliaControlMapper
     */
    @computedFrom('schema')
    public get control(): {new(...args: any[])}{
        return this.schema && this.schema.ContentTypeControl;
    }
}