Skip to content

Commit

Permalink
wip(forms): refactor forms templating
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Jan 7, 2025
1 parent 34f7d8e commit 59533bc
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 201 deletions.
2 changes: 1 addition & 1 deletion umap/static/umap/css/form.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.umap-form-inline .formbox,
.umap-form-inline {
display: inline;
}
Expand Down Expand Up @@ -559,7 +560,6 @@ i.info {
clear: both;
margin-bottom: 20px;
overflow: hidden;
display: none;
}
.umap-color-picker span {
width: 20px;
Expand Down
2 changes: 1 addition & 1 deletion umap/static/umap/js/modules/data/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ export class DataLayer extends ServerStored {
{
label: translate('Data is browsable'),
handler: 'Switch',
helpEntries: 'browsable',
helpEntries: ['browsable'],
},
],
[
Expand Down
46 changes: 44 additions & 2 deletions umap/static/umap/js/modules/form/builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getClass from './fields.js'
import * as Utils from '../utils.js'
import { SCHEMA } from '../schema.js'
import { translate } from '../i18n.js'

export class Form {
constructor(obj, fields, properties) {
Expand Down Expand Up @@ -35,9 +36,8 @@ export class Form {
}

buildField(field) {
field.buildLabel()
field.buildTemplate()
field.build()
field.buildHelpText()
}

makeField(field) {
Expand Down Expand Up @@ -115,6 +115,14 @@ export class Form {
}

finish() {}

getTemplate(helper) {
return `
<div class="formbox" data-ref=container>
${helper.getTemplate()}
<small class="help-text" data-ref=helpText></small>
</div>`
}
}

export class MutatingForm extends Form {
Expand Down Expand Up @@ -190,6 +198,40 @@ export class MutatingForm extends Form {
}
}

getTemplate(helper) {
let template
if (helper.properties.inheritable) {
const extraClassName = helper.get(true) === undefined ? ' undefined' : ''
template = `
<div class="umap-field-${helper.name} formbox inheritable${extraClassName}">
<div class="header" data-ref=header>
<a href="#" class="button undefine" data-ref=undefine>${translate('clear')}</a>
<a href="#" class="button define" data-ref=define>${translate('define')}</a>
<span class="quick-actions show-on-defined" data-ref=actions></span>
${helper.getLabelTemplate()}
</div>
<div class="show-on-defined" data-ref=container>
${helper.getTemplate()}
<small class="help-text" data-ref=helpText></small>
</div>
</div>`
} else {
template = `
<div class="formbox umap-field-${helper.name}" data-ref=container>
${helper.getLabelTemplate()}
${helper.getTemplate()}
<small class="help-text" data-ref=helpText></small>
</div>`
}
return template
}

build() {
super.build()
this._umap.help.parse(this.form)
return this.form
}

finish(helper) {
helper.input?.blur()
}
Expand Down
Loading

0 comments on commit 59533bc

Please sign in to comment.