To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit db549bb2 authored by Sandro Lutz's avatar Sandro Lutz Committed by Sandro Lutz
Browse files

Add reset() to FilterViewComponent (see #36)

parent 2050b1fb
......@@ -66,10 +66,25 @@ import { Button, Checkbox, Dropdown, TextField, RadioGroup } from '../components
* onclick: () => dosomething(),
* },
* },
* {
* type: 'button',
* label: 'search button',
* events: {
* onclick: 'search',
* },
* },
* {
* type: 'button',
* label: 'reset button',
* events: {
* onclick: 'reset',
* },
* },
* ]
* ```
*
* Default behavior of buttons is to trigger `onchange`.
* Other configurable values are `search` and `reset`.
*/
export default class FilterViewComponent {
......@@ -83,12 +98,24 @@ export default class FilterViewComponent {
this.values[field.key] = field.default || '';
});
}
this.fields = vnode.attrs.fields;
}
notify() {
this.onchange(this.values);
}
reset() {
this.fields.forEach(field => {
if (field.default instanceof Array) {
this.values[field.key] = JSON.parse(JSON.stringify(field.default || []));
} else {
this.values[field.key] = field.default || '';
}
});
this.notify();
}
_createTextField(field) {
const min_length = field.min_length || 0;
this.values[field.key] = this.values[field.key] || field.default || '';
......@@ -107,7 +134,8 @@ export default class FilterViewComponent {
_createCheckboxGroup(field) {
const items = [];
this.values[field.key] = this.values[field.key] || field.default || [];
this.values[field.key] =
this.values[field.key] || JSON.parse(JSON.stringify(field.default || []));
if (field.label) {
items.push(m('h4', field.label));
......@@ -202,24 +230,27 @@ export default class FilterViewComponent {
return m(Dropdown, options);
}
_createButton(options) {
const events = options.events || {};
if (!events.onclick) {
// default onclick behavior
events.onclick = () => this.notify();
_createButton(field) {
const options = { label: field.label };
options.events = field.events || {};
if (!options.events.onclick || options.events.onclick === 'search') {
// default onclick behavior / search behavior
options.events.onclick = () => this.notify();
} else if (options.events.onclick === 'reset') {
// reset behavior
options.events.onclick = () => this.reset();
}
if (field.className) {
options.className = field.className;
}
return m(Button, {
label: options.label,
events,
});
return m(Button, options);
}
view(vnode) {
const { fields } = vnode.attrs;
view() {
const views = [];
fields.forEach(field => {
this.fields.forEach(field => {
if (field.type === 'text') {
views.push(this._createTextField(field));
} else if (field.type === 'checkbox') {
......
......@@ -47,6 +47,7 @@
"email": "Email",
"email_invalid": "Ungültige Email-Adresse",
"search": "Suchen",
"reset": "Zurücksetzen",
"frontpage.whats_hot": "Was ist brandaktuell?",
"frontpage.social_media": "Folge uns auf Social Media",
"legal-notice": "Impressum",
......
......@@ -47,6 +47,7 @@
"email": "Email",
"email_invalid": "Not a valid email address",
"search": "Search",
"reset": "Reset",
"frontpage.whats_hot": "What's HOT right now?",
"frontpage.social_media": "Join us on social media!",
"legal-notice": "Legal Notice",
......
......@@ -44,6 +44,14 @@ export default class EventList extends FilteredListPage {
type: 'button',
label: i18n('search'),
},
{
type: 'button',
label: i18n('reset'),
className: 'flat-button',
events: {
onclick: 'reset',
},
},
{
type: 'checkbox',
key: 'price',
......
......@@ -114,6 +114,17 @@ export default class StudydocList {
{
type: 'button',
label: i18n('search'),
events: {
onclick: 'search',
},
},
{
type: 'button',
label: i18n('reset'),
className: 'flat-button',
events: {
onclick: 'reset',
},
},
{
type: 'checkbox',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment