Skip to content

Commit

Permalink
Merge branch 'v0.11.0' into as_menuitem
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmiller committed Jun 15, 2018
2 parents ebb943a + d5ece47 commit e3a2e37
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
7 changes: 7 additions & 0 deletions 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.

Expand Down
2 changes: 1 addition & 1 deletion doc/docs/reference/form_templatetags.md
Expand Up @@ -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 ... %}

Expand Down
4 changes: 4 additions & 0 deletions opal/static/css/opal.css
Expand Up @@ -1215,3 +1215,7 @@ hr.bold{
max-height: 150px;
overflow-y: auto;
}

.input-group-btn .btn{
padding: 8.5px 12px;
}
7 changes: 7 additions & 0 deletions opal/static/js/opal/utils.js
Expand Up @@ -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';

Expand Down
20 changes: 20 additions & 0 deletions opal/static/js/test/utilsTest.js
Expand Up @@ -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(){
Expand Down
42 changes: 26 additions & 16 deletions opal/templates/_helpers/datepicker.html
Expand Up @@ -10,21 +10,31 @@
class="col-sm-8"
{% endifequal %}
>
<input type="text" class="form-control"
uib-datepicker-popup="dd/MM/yyyy"
ng-model="{{ model }}"
is-open="{{ formname }}[{{ element_name }} + '_open']"
min-date="{{ mindate}}"
max-date="maxDate"
show-button-bar="false"
show-weeks="false"
name="[[ {{ element_name}} ]]"
datepicker-options="{startingDay: 1}"
ng-focus="{{ formname }}[{{ element_name }} + '_open']=true"
close-text="Close"
{% if change %}
ng-change="{{ change }}"
{% endif %}
/>
{% if options %}
<div class="input-group">
{% endif %}
<input type="text" class="form-control"
uib-datepicker-popup="dd/MM/yyyy"
ng-model="{{ model }}"
is-open="{{ formname }}[{{ element_name }} + '_open']"
min-date="{{ mindate}}"
max-date="maxDate"
show-button-bar="false"
show-weeks="false"
name="[[ {{ element_name}} ]]"
datepicker-options="{startingDay: 1}"
ng-focus="{{ formname }}[{{ element_name }} + '_open']=true"
close-text="Close"
{% if change %}
ng-change="{{ change }}"
{% endif %}
/>
{% if user_options %}
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="{{ model }} = dateHelper.now()" href="">Today</a>
<button type="button" class="btn btn-default" ng-click="{{ model }} = dateHelper.yesterday()" href="">Yesterday</a>
</span>
</div>
{% endif %}
</div>
</div>
1 change: 1 addition & 0 deletions opal/templatetags/forms.py
Expand Up @@ -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


Expand Down
10 changes: 10 additions & 0 deletions opal/tests/test_templatetags_forms.py
Expand Up @@ -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({}))
Expand Down

0 comments on commit e3a2e37

Please sign in to comment.