diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index dfb60a3..2ca92f1 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -35,8 +35,9 @@ class FermentableForm(Form): """ name = StringField('Name', validators=[DataRequired()]) type = SelectField('Type', validators=[DataRequired()], - choices=[(c, c) for c in ['Grain', 'LME', 'DME', 'Sugar', - 'Non-fermentable', 'Other']]) + choices=[(c, c) for c in ['Grain', 'LME', 'DME', + 'Sugar', 'Non-fermentable', + 'Other']]) amount = DecimalField('Amount (lb)', validators=[DataRequired()]) ppg = DecimalField('PPG', validators=[DataRequired()]) color = DecimalField('Color (°L)', validators=[DataRequired()]) @@ -87,7 +88,8 @@ class HopForm(Form): class RecipeForm(FlaskForm): """Form for recipes.""" name = StringField('Name', validators=[DataRequired()]) - efficiency = DecimalField('Batch Efficiency (%)', validators=[DataRequired()]) + efficiency = DecimalField('Batch Efficiency (%)', + validators=[DataRequired()]) volume = DecimalField('Batch Volume (gal)', validators=[DataRequired()]) notes = TextAreaField('Notes') fermentables = FieldList( diff --git a/src/humulus/static/recipes.js b/src/humulus/static/recipes.js new file mode 100644 index 0000000..300072a --- /dev/null +++ b/src/humulus/static/recipes.js @@ -0,0 +1,173 @@ +// Correct all the indices for forms matching item. +function adjustIndices(removedIndex, item) { + var $forms = $(item); + $forms.each(function(i) { + var $form = $(this); + var index = parseInt($form.data('index')); + var newIndex = index - 1; + if (index <= removedIndex) { + // Skip this one + return true; + } + + // Change form index + $form.data('index', newIndex); + + // Change ID in form input, select, and labels + $form.find('input').each(function(j) { + var $item = $(this) + $item.attr('id', $item.attr('id').replace(index, newIndex)); + $item.attr('name', $item.attr('name').replace(index, newIndex)); + }); + $form.find('select').each(function(j) { + var $item = $(this) + $item.attr('id', $item.attr('id').replace(index, newIndex)); + $item.attr('name', $item.attr('name').replace(index, newIndex)); + }); + }); +} + +// Remove a subform from a form matching `formClass` closest to `$remButton`. +// Adjust length on element matching `formsId`. Also adjust indices. +function removeForm($remButton, formClass, formsId) { + var $removedForm = $remButton.closest(formClass); + var removedIndex = parseInt($removedForm.data('index')); + $removedForm.remove(); + var $fermsDiv = $(formsId); + $fermsDiv.data('length', $fermsDiv.data('length') - 1); + adjustIndices(removedIndex, formClass); +} + +// Remove a fermentable +function removeFerm() { + removeForm($(this), '.ferm-form', '#ferms'); +} + +// Remove a hop +function removeHop() { + removeForm($(this), '.hop-form', '#hops'); +} + +// Add a fermentable +function addFerm() { + var $fermsDiv = $('#ferms'); + var fermsLength = $fermsDiv.data('length'); + if (fermsLength == 20) { + window.alert("Can't have more than 20 fermentables."); + return; + } + var newFerm = `