Commit 4b5ab697ec45dfb7f4b3e8edcd3f18841d7a9d62
1 parent
91a41f0f
prototype collapsed subform descriptions + subform adding on subform type selection
Showing
3 changed files
with
68 additions
and
2 deletions
entries/forms.py
... | ... | @@ -233,7 +233,7 @@ class FormFactory(object): |
233 | 233 | @staticmethod |
234 | 234 | def make_layout(title, components): |
235 | 235 | return layout.Layout(layout.Fieldset( |
236 | - '{} <span class="collapsed-info"></span> <button class="btn collapse-button btn-xs btn-secondary" data-collapsed="false" type="button">{}</button><button class="btn remove-button btn-xs btn-danger" type="button">{}</button>'.format(title, _('Zwiń'), _('Usuń')), | |
236 | + '{} <span class="collapsed-info text-info"></span> <button class="btn collapse-button btn-xs btn-secondary" data-collapsed="false" type="button">{}</button><button class="btn remove-button btn-xs btn-danger" type="button">{}</button>'.format(title, _('Zwiń'), _('Usuń')), | |
237 | 237 | *components |
238 | 238 | )) |
239 | 239 | |
... | ... |
entries/static/entries/css/entries.css
entries/static/entries/js/forms.js
... | ... | @@ -16,6 +16,9 @@ function show_form_errors(errors) { |
16 | 16 | function bind_add_subforms(selector) { |
17 | 17 | selector.find('.add-button').click(function() { |
18 | 18 | add_subform($(this)); |
19 | + }).closest('.input-group').find('select').change(function() { | |
20 | + // also add subform on selecting subform type | |
21 | + $(this).siblings().find('.add-button').click(); | |
19 | 22 | }); |
20 | 23 | } |
21 | 24 | |
... | ... | @@ -39,6 +42,65 @@ function bind_remove_subforms(selector) { |
39 | 42 | }); |
40 | 43 | } |
41 | 44 | |
45 | +function collapsed_subform_desc(element, negated) { | |
46 | + var tag = element.tagName(); | |
47 | + var ret = ''; | |
48 | + var neg_prefix = (negated ? (gettext('nie') + ' ') : ''); | |
49 | + if (tag === 'label') { | |
50 | + var input_siblings = element.siblings('input'); | |
51 | + if (input_siblings.length === 0) { | |
52 | + var neg = element.hasClass('negated2'); | |
53 | + var values = []; | |
54 | + element.next().children().each(function() { | |
55 | + var val = collapsed_subform_desc($(this), neg); | |
56 | + if (val) { | |
57 | + values.push(val); | |
58 | + } | |
59 | + }); | |
60 | + if (values.length > 0) { | |
61 | + ret += '<b>' + element.html().trim() + ': </b>'; | |
62 | + ret += values.join(' ' + (neg ? gettext('i') : gettext('lub')) + ' '); | |
63 | + } | |
64 | + return ret; | |
65 | + } else { | |
66 | + return ''; | |
67 | + } | |
68 | + } else if (tag === 'input') { | |
69 | + if (element.prop('type') === 'checkbox') { | |
70 | + if (element.prop('checked')) { | |
71 | + return neg_prefix + '<i>' + element.siblings('label').immediateText().trim() + '</i>'; | |
72 | + } else { | |
73 | + return ''; | |
74 | + } | |
75 | + } else if (element.prop('type') === 'text') { | |
76 | + return neg_prefix + '<i>"' + element.val() + '"</i>'; | |
77 | + } else { | |
78 | + return '??? ' + element.attr('class') + ' ???'; | |
79 | + } | |
80 | + } else if (tag === 'select') { | |
81 | + return element.val().map(x => neg_prefix + '<i>' + element.find('option[value="' + x + '"]').html() + '</i>').join(' ' + (negated ? gettext('i') : gettext('lub')) + ' '); | |
82 | + } else if (tag === 'form') { | |
83 | + var attributes = []; | |
84 | + element.children('fieldset').children('.form-group.row').children('label').each(function() { | |
85 | + var attr = collapsed_subform_desc($(this), false); | |
86 | + if (attr) { | |
87 | + attributes.push(attr); | |
88 | + } | |
89 | + }); | |
90 | + if (attributes.length > 0) { | |
91 | + ret += attributes.join(', '); | |
92 | + } | |
93 | + return ret; | |
94 | + } else if (element.siblings('label').length > 0) { | |
95 | + return ''; | |
96 | + } else { | |
97 | + element.children().each(function() { | |
98 | + ret += collapsed_subform_desc($(this), negated); | |
99 | + }); | |
100 | + return ret; | |
101 | + } | |
102 | +} | |
103 | + | |
42 | 104 | function bind_collapse_subforms(selector) { |
43 | 105 | selector.find('.collapse-button').mouseenter(function() { |
44 | 106 | $(this).closest('.subform').addClass('to-collapse').find('.subform').addClass('to-collapse'); |
... | ... | @@ -52,7 +114,7 @@ function bind_collapse_subforms(selector) { |
52 | 114 | $(this).data('collapsed', false); |
53 | 115 | } else { |
54 | 116 | $(this).closest('legend').nextAll().hide(); |
55 | - $(this).closest('legend').find('.collapsed-info').html('[...]'); | |
117 | + $(this).closest('legend').find('.collapsed-info').html(collapsed_subform_desc($(this).closest('.subform'), false)); | |
56 | 118 | $(this).html(gettext('rozwiń')); |
57 | 119 | $(this).data('collapsed', true); |
58 | 120 | } |
... | ... |