All files / src/viewcontrols generic-view.ts

100% Statements 21/21
77.78% Branches 7/9
100% Functions 3/3
100% Lines 19/19
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          1x 1x   1x 1x 1x                       1x           1x           1x           1x         51x 51x               2x 2x 2x       6x 2x 2x   6x 6x    
/**
 * @module ViewControls
 * 
 */ /** */
 
import { bindable, autoinject } from 'aurelia-framework';
import { Content, ControlSchema, FieldSettings, ActionName, ContentTypes } from 'sn-client-js';
 
import { AureliaBaseControl } from '../AureliaBaseControl';
import { AureliaControlMapper } from '../AureliaControlMapper';
import { ValidationController, ValidationControllerFactory } from 'aurelia-validation';
 
/**
 * 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 extends AureliaBaseControl {
    
    /**
     * The bindable Content instance
     */
    @bindable
    public content: Content;
 
    /**
     * the bindable ControlSchema object
     */
    @bindable
    public schema: ControlSchema<AureliaBaseControl, FieldSettings.FieldSetting>;
 
    /**
     * The optional ActionName
     */
    @bindable 
    actionName?: ActionName;
 
    controller: ValidationController;
 
    constructor(controllerFactory: ValidationControllerFactory) {
        super();
        this.controller = controllerFactory.createForCurrentScope();
    }
 
    /**
     * 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<AureliaBaseControl, FieldSettings.FieldSetting>, content: Content, actionName: ActionName }) {
        this.schema = model.schema;
        this.content = model.content;
        this.actionName = model.actionName;
    }
 
    actionNameChanged(newName: ActionName, oldName: ActionName) {
        if (newName === 'view'){
            const savedFields = this.content.SavedFields;
            Object.assign(this.content, savedFields);
        }
        const contentType = this.content && (ContentTypes as any)[this.content.Type] || Content as { new(...args) };
        this.schema = this.content && AureliaControlMapper.GetFullSchemaForContentType(contentType, this.actionName || 'view');
    }
}