Skip to content

Commit 0a4e449

Browse files
committed
feat(twig): add var out for forms
1 parent 41a0ebe commit 0a4e449

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/modules/types/display/template-twig/controller.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ define(['jquery', 'modules/default/defaultcontroller'], function ($, Default) {
2727
renderedHtml: {
2828
label: 'Rendered HTML',
2929
type: 'string'
30+
},
31+
form: {
32+
label: 'Output form'
3033
}
3134
};
3235

@@ -35,6 +38,16 @@ define(['jquery', 'modules/default/defaultcontroller'], function ($, Default) {
3538
label: 'Html was rendered',
3639
refVariable: ['renderedHtml'],
3740
refAction: ['renderedHtml']
41+
},
42+
onFormChanged: {
43+
label: 'Form changed',
44+
refVariable: ['form'],
45+
refAction: ['form']
46+
},
47+
onFormSubmitted: {
48+
label: 'Form submitted',
49+
refVariable: ['form'],
50+
refAction: ['form']
3851
}
3952
};
4053

@@ -78,5 +91,22 @@ define(['jquery', 'modules/default/defaultcontroller'], function ($, Default) {
7891
}, 0);
7992
};
8093

94+
Controller.prototype.onFormChanged = function (out) {
95+
this._doForm('onFormChanged', out);
96+
};
97+
98+
Controller.prototype.onFormSubmitted = function (out) {
99+
this._doForm('onFormSubmitted', out);
100+
};
101+
102+
Controller.prototype._doForm = function (event, out) {
103+
var obj = new DataObject();
104+
105+
for (let i = 0; i < out.length; i++) {
106+
obj.setChildSync(out[i].name.split('.'), out[i].value);
107+
this.createDataFromEvent(event, 'form', obj);
108+
}
109+
};
110+
81111
return Controller;
82112
});

src/modules/types/display/template-twig/view.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ define([
1919
'user-select': this.module.getConfigurationCheckbox('selectable', 'yes') ? 'initial' : 'none'
2020
});
2121

22+
var submit = this.submit.bind(this);
23+
24+
this.dom.on('input', 'input,textarea', submit);
25+
this.dom.on('submit', 'form', function (e) {
26+
submit('submit');
27+
e.preventDefault();
28+
});
29+
this.dom.on('change', 'input', submit);
30+
2231
this._values = new DataObject();
2332
this.template = Twig.twig({
2433
data: this.module.getConfiguration('template')
@@ -39,6 +48,35 @@ define([
3948
this.resolveReady();
4049
this.render();
4150
},
51+
submit: function (type) {
52+
var inputs = this.dom.find('input,textarea');
53+
var out = inputs.map(function () {
54+
const {name, value, type} = this;
55+
return {name, value, type, dom: this};
56+
}).toArray().filter(o => {
57+
if (!o.name) return false;
58+
if (o.type === 'radio' && !o.dom.checked) return false;
59+
return true;
60+
});
61+
62+
out.forEach(o => {
63+
switch (o.type) {
64+
case 'number':
65+
case 'range':
66+
o.value = +o.value;
67+
break;
68+
case 'checkbox':
69+
o.value = o.dom.checked;
70+
break;
71+
}
72+
});
73+
74+
if (type === 'submit') {
75+
this.module.controller.onFormSubmitted(out);
76+
} else {
77+
this.module.controller.onFormChanged(out);
78+
}
79+
},
4280
update: {
4381
value: function (value, name) {
4482
/*

0 commit comments

Comments
 (0)