diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b6d9ce1a..d7c5e290e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ ### 0.11.0 (Major Release) +#### Adds options of `today` and `yesterday` in the date picker +If you pass in `user_options=True` to the date picker. You will be provided with options to select today or yesterday in the form tag. + +#### Adds `dateHelper` to the rootScope +The dateHelper has the functions `now` and `yesterday` that return javascript Dates for +the current time and the current time - 1 day. + #### Deprecates the _title property In future we will use the standard `verbose_name` property as the display name. The abstract models have been changed to account for this. diff --git a/doc/docs/reference/form_templatetags.md b/doc/docs/reference/form_templatetags.md index a7a24f9d1..e0c4a0e7a 100644 --- a/doc/docs/reference/form_templatetags.md +++ b/doc/docs/reference/form_templatetags.md @@ -44,7 +44,7 @@ Keywords: * `mindate` Angular Javascript expression to return the minimum possible date * `element_name` If this exists this is an Angular expression that is set to the 'name' attribute of the html element * `style` The form style to render this widget with. Possible values are `['horizontal', 'vertical']`. Defaults to 'horizontal' - +* `user_options` If set to `True` this will add the default options of `today`, ie the current date, or `yesterday`, ie t-1. ### {% timepicker ... %} diff --git a/opal/static/css/opal.css b/opal/static/css/opal.css index 2d759272d..58a9c36c4 100644 --- a/opal/static/css/opal.css +++ b/opal/static/css/opal.css @@ -1215,3 +1215,7 @@ hr.bold{ max-height: 150px; overflow-y: auto; } + +.input-group-btn .btn{ + padding: 8.5px 12px; +} diff --git a/opal/static/js/opal/utils.js b/opal/static/js/opal/utils.js index b347105d3..a062ea79b 100644 --- a/opal/static/js/opal/utils.js +++ b/opal/static/js/opal/utils.js @@ -139,6 +139,13 @@ OPAL._run = function($rootScope, ngProgressLite, $modal, $location, $analytics, ngProgressLite.set(0); }); + $rootScope.dateHelper = { + now: function(){ return new Date(); }, + yesterday: function(){ + return moment().subtract(1, 'days').toDate(); + } + } + $rootScope.open_modal = function(controller, template, resolves){ $rootScope.state = 'modal'; diff --git a/opal/static/js/test/utilsTest.js b/opal/static/js/test/utilsTest.js index 49b8bc90e..cb9d23791 100644 --- a/opal/static/js/test/utilsTest.js +++ b/opal/static/js/test/utilsTest.js @@ -46,6 +46,26 @@ describe('Utils.OPAL._run', function (){ passedFunction(); expect(mock_scope.state).toBe('normal'); }); + + it('Should add dateHelper to the root scope.', function () { + var mock_scope = { $on: function(){} }; + OPAL._run(mock_scope, {}, {}) + expect(mock_scope.dateHelper).toBeDefined(); + }); + + it('Should provide the current datetime when dateHelper.now() is called', function () { + var mock_scope = { $on: function(){} }; + OPAL._run(mock_scope, {}, {}); + var now = moment(mock_scope.dateHelper.now()); + expect(now.isSame(moment(), 'minute')).toBe(true); + }); + + it('Should provide the yesterdays datetime when dateHelper.yesterday() is called', function () { + var mock_scope = { $on: function(){} }; + OPAL._run(mock_scope, {}, {}) + var yesterday = moment().subtract(1, "day"); + expect(moment(mock_scope.dateHelper.yesterday()).isSame(yesterday, 'minute')).toBe(true); + }); }); describe('utils.OPAL._track', function(){ diff --git a/opal/templates/_helpers/datepicker.html b/opal/templates/_helpers/datepicker.html index 93bc00c66..f1dfa4071 100644 --- a/opal/templates/_helpers/datepicker.html +++ b/opal/templates/_helpers/datepicker.html @@ -10,21 +10,31 @@ class="col-sm-8" {% endifequal %} > - + {% if options %} +
+ {% endif %} + + {% if user_options %} + +
+ {% endif %} diff --git a/opal/templatetags/forms.py b/opal/templatetags/forms.py index 2cb7c1eb9..eb57f1d92 100644 --- a/opal/templatetags/forms.py +++ b/opal/templatetags/forms.py @@ -216,6 +216,7 @@ def datepicker(*args, **kwargs): context = _input(*args, **kwargs) if 'mindate' in kwargs: context['mindate'] = kwargs['mindate'] + context["user_options"] = kwargs.pop("user_options", False) return context diff --git a/opal/tests/test_templatetags_forms.py b/opal/tests/test_templatetags_forms.py index 2e0b809cd..cd3e90ece 100644 --- a/opal/tests/test_templatetags_forms.py +++ b/opal/tests/test_templatetags_forms.py @@ -295,6 +295,16 @@ def test_datepicker(self): self.assertIn('ng-model="bai"', rendered) self.assertIn('hai', rendered) + def test_datepicker_options_default(self): + template = Template('{% load forms %}{% datepicker label="hai" model="bai" mindate="Date(2013, 12, 22)" %}') + rendered = template.render(Context({})) + self.assertNotIn('bai = now', rendered) + + def test_datepicker_options_on(self): + template = Template('{% load forms %}{% datepicker label="hai" model="bai" mindate="Date(2013, 12, 22)" user_options=True %}') + rendered = template.render(Context({})) + self.assertIn('bai = dateHelper.now()', rendered) + def test_datepicker_min_date(self): template = Template('{% load forms %}{% datepicker label="hai" model="bai" mindate="Date(2013, 12, 22)" %}') rendered = template.render(Context({}))