-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #761: Attributes that depend on party type don't show up on party…
… creation
- Loading branch information
bohare
committed
Jan 24, 2017
1 parent
2c0f545
commit f0cc4ad
Showing
30 changed files
with
1,158 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-env jquery */ | ||
|
||
function enableGroup() { | ||
$('.party-gr').removeClass('hidden'); | ||
$('.party-in').addClass('hidden'); | ||
$('.party-co').addClass('hidden'); | ||
$('.party-gr .form-control').prop('disabled', ''); | ||
$('.party-in .form-control').prop('disabled', 'disabled'); | ||
$('.party-co .form-control').prop('disabled', 'disabled'); | ||
} | ||
|
||
function enableIndividual() { | ||
$('.party-in').removeClass('hidden'); | ||
$('.party-gr').addClass('hidden'); | ||
$('.party-co').addClass('hidden'); | ||
$('.party-in .form-control').prop('disabled', ''); | ||
$('.party-gr .form-control').prop('disabled', 'disabled'); | ||
$('.party-co .form-control').prop('disabled', 'disabled'); | ||
} | ||
|
||
function enableCorporation() { | ||
$('.party-co').removeClass('hidden'); | ||
$('.party-gr').addClass('hidden'); | ||
$('.party-in').addClass('hidden'); | ||
$('.party-co .form-control').prop('disabled', ''); | ||
$('.party-gr .form-control').prop('disabled', 'disabled'); | ||
$('.party-in .form-control').prop('disabled', 'disabled'); | ||
} | ||
|
||
function disableConditionals() { | ||
$('.party-co').addClass('hidden'); | ||
$('.party-gr').addClass('hidden'); | ||
$('.party-in').addClass('hidden'); | ||
$('.party-co .form-control').prop('disabled', 'disabled'); | ||
$('.party-gr .form-control').prop('disabled', 'disabled'); | ||
$('.party-in .form-control').prop('disabled', 'disabled'); | ||
} | ||
|
||
function toggleParsleyRequired(val) { | ||
const typeChoices = ['in', 'gr', 'co']; | ||
$.each(typeChoices, function(idx, choice) { | ||
if (val === choice) { | ||
$.each($(`.party-${val} .form-control`), function(idx, value) { | ||
if (value.hasAttribute('data-parsley-required')) { | ||
$(value).attr('data-parsley-required', true); | ||
$(value).prop('required', 'required'); | ||
} | ||
}); | ||
} else { | ||
$.each($(`.party-${choice} .form-control`), function(idx, value) { | ||
if (value.hasAttribute('data-parsley-required')) { | ||
$(value).attr('data-parsley-required', false); | ||
$(value).prop('required', ''); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
function toggleStates(val) { | ||
if (val === 'in') { | ||
enableIndividual(); | ||
toggleParsleyRequired(val); | ||
} | ||
if (val === 'gr') { | ||
enableGroup(); | ||
toggleParsleyRequired(val); | ||
} | ||
if (val === 'co') { | ||
enableCorporation(); | ||
toggleParsleyRequired(val); | ||
} | ||
if (val === '') { | ||
disableConditionals(); | ||
} | ||
} | ||
|
||
$().ready(function() { | ||
const val = $('.party-type').val().toLowerCase(); | ||
toggleStates(val); | ||
}); | ||
|
||
|
||
$('select.party-type').on('change', function(e) { | ||
const val = e.target.value.toLowerCase(); | ||
toggleStates(val); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django import template | ||
from django.forms import ChoiceField, FileField | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter(name='field_value') | ||
def field_value(field): | ||
"""Return the value for this BoundField.""" | ||
if field.form.is_bound: | ||
if isinstance(field.field, FileField) and field.data is None: | ||
val = field.form.initial.get(field.name, field.field.initial) | ||
else: | ||
val = field.data | ||
else: | ||
val = field.form.initial.get(field.name, field.field.initial) | ||
if callable(val): | ||
val = val() | ||
if val is None: | ||
val = '' | ||
return val | ||
|
||
|
||
@register.filter(name='display_choice_verbose') | ||
def display_choice_verbose(field): | ||
"""Return the displayed value for this BoundField.""" | ||
if isinstance(field.field, ChoiceField): | ||
value = field_value(field) | ||
for (val, desc) in field.field.choices: | ||
if val == value: | ||
return desc | ||
|
||
|
||
@register.filter(name='set_parsley_required') | ||
def set_parsley_required(field): | ||
if field.field.required: | ||
field.field.widget.attrs = { | ||
'data-parsley-required': 'true' | ||
} | ||
return field |
Oops, something went wrong.