From da9ba28ab18f9c78f354fa07851388732eb05421 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 14 Oct 2014 12:48:07 +0200 Subject: [PATCH] Add support for min, max and step in the NumberPicker Also use them in the plan edit form. Change-Id: I8220a5562597d036d924dc49404f8bf56f51212f --- tuskar_ui/infrastructure/overview/forms.py | 5 ++++- .../infrastructure/js/tuskar.number_picker.js | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/tuskar_ui/infrastructure/overview/forms.py b/tuskar_ui/infrastructure/overview/forms.py index d42e8d586..15ba235bd 100644 --- a/tuskar_ui/infrastructure/overview/forms.py +++ b/tuskar_ui/infrastructure/overview/forms.py @@ -135,7 +135,10 @@ class EditPlan(horizon.forms.SelfHandlingForm): for role in plan.role_list: field = django.forms.IntegerField( label=role.name, - widget=tuskar_ui.forms.NumberPickerInput, + widget=tuskar_ui.forms.NumberPickerInput(attrs={ + 'min': 1 if role.name in ('controller', 'compute') else 0, + 'step': 2 if role.name == 'controller' else 1, + }), initial=plan.get_role_node_count(role), required=False ) diff --git a/tuskar_ui/infrastructure/static/infrastructure/js/tuskar.number_picker.js b/tuskar_ui/infrastructure/static/infrastructure/js/tuskar.number_picker.js index 498081567..dee7bc4a0 100644 --- a/tuskar_ui/infrastructure/static/infrastructure/js/tuskar.number_picker.js +++ b/tuskar_ui/infrastructure/static/infrastructure/js/tuskar.number_picker.js @@ -16,13 +16,23 @@ tuskar.number_picker = (function () { if ($this.attr('readonly')) { $this.parent().addClass('readonly'); } - $this.next('a.arrow-right').click(function () { - $this.val((+$this.val()) + 1); + function change(step) { + var value = +$this.val(); + var maximum = +$this.attr('max'); + var minimum = +$this.attr('min'); + value += step; + if (!isNaN(maximum)) { value = Math.min(maximum, value); } + if (!isNaN(minimum)) { value = Math.max(minimum, value); } + $this.val(value); $this.trigger('change'); + } + $this.next('a.arrow-right').click(function () { + var step = +($this.attr('step') || 1); + change(step); }); $this.prev('a.arrow-left').click(function () { - $this.val(Math.max(0, (+$this.val()) - 1)); - $this.trigger('change'); + var step = -($this.attr('step') || 1); + change(step); }); }); };