/** * Class SubmissionShortFormTabView * Handles a form chunk as a local form by matching form fields with model data, * managing modes (read-only or edition) and handling form specific encoding rules. * Compatible with all proposal form versions. * * @author Michael Fallise * @version 1.0 */ class SubmissionShortFormTabView extends EICDomContent { options = { mode: 'read' } DOMContentLoaded(options) { this.components = ui.eicfy(this.el); //this.mode = options && options.mode ? options.mode: this.options.mode; this.form = new Form(this.el); if(options.model) this.model = options.model; this.setupRules(); if(!this.model && !this.model.data) { console.warn('SubmissionShortFormTabView: No model provided.') } } DOMContentFocused(options) { if(options) this.model = options.models.submission } /** * Validates the local form * Overridable. */ validate() { return this.form.validate(true); } /** * Initializes the local form with specific field rules * Overridable. */ setupRules() {} /** * Fills the local form with available Model data */ fill(mode) { this.mode = mode; let fields = this.form.scan(); for(let field of fields) { let component = this.el2component(field.el.dataset.path, field.el.name); if(component) component.disabled = this.mode == 'read'; let item = this.findField(field.el.dataset.path, field.el.name) if(item) { if(component.el.type == 'checkbox'){ component.el.checked = item == true; } else component.value = item; } } } /** * Retrieves a data field value based on path an name * @param {*} path * @param {*} name * @returns */ findField(path, name) { let levels = path && path != '' && path !== undefined ? path.split('.'): []; let tree = this.model.data; for(let level of levels) if(tree.hasOwnProperty(level)) tree = tree[level]; if(tree.hasOwnProperty(name)) return tree[name]; } /** * Converts a DOM Element into EICUI Component * @param {*} path * @param {*} name * @returns */ el2component(path, name) { return ( this.components .find(o => o.el.hasAttribute('name') && o.el.getAttribute('name') == name && ( !path || (path && o.el.dataset.path == path)) ) ) } } app.registerClass('SubmissionShortFormTabView', SubmissionShortFormTabView);