'use strict' class FeattleEditor { constructor(editorEl) { this.editorEl = editorEl this.format = JSON.parse(editorEl.attr('data-format')) this.initialValue = JSON.parse(editorEl.attr('data-value')) this.sourceEl = null this.getValue = null if (this.format.tag === 'Bool') { this._prepareBool() } else if (this.format.tag === 'Integer') { this._prepareNumber(true) } else if (this.format.tag === 'Float') { this._prepareNumber(false) } else if (this.format.tag === 'String' && this.format.content.tag === 'Any') { this._prepareString() } else if (this.format.tag === 'String' && this.format.content.tag === 'Pattern') { this._prepareString(this.format.content.content) } else if (this.format.tag === 'String' && this.format.content.tag === 'Choices') { this._prepareChoices(this.format.content.content) } else if (this.format.tag === 'Optional') { this._prepareOptional(this.format.content) } else { this._prepareOther() } } _prepareBool() { this.sourceEl = this._newSwitch(this.editorEl, 'Value', this.initialValue) this.getValue = () => this.sourceEl.prop('checked') } _prepareNumber(isInteger) { this.sourceEl = $('', { 'class': 'form-control', type: 'number', step: isInteger ? '' : 'any', val: this.initialValue }) this.editorEl.append(this.sourceEl) this.getValue = () => Number(this.sourceEl.val()) } _prepareString(pattern) { this.sourceEl = $('', { 'class': 'form-control', pattern: pattern, val: this.initialValue }) this.editorEl.append(this.sourceEl) this.getValue = () => this.sourceEl.val() } _prepareChoices(choices) { let radioGroupId = String(Math.random()) this.sourceEl = $(choices.map(choice => { let radioId = String(Math.random()) return $('
', { 'class': 'custom-control custom-radio', append: [ $('', { type: 'radio', name: radioGroupId, id: radioId, 'class': 'custom-control-input', value: choice, checked: choice === this.initialValue }), $('