Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 43e6bf1

Browse files
crisbetoThomasBurleson
authored andcommitted
feat(datepicker): allow users to specify the default calendar view
* Allows users to set the default open view of the calendar via the `md-current-view` attribute. * Removes a few unused dependencies from the datepicker. Fixes #9111. Closes #9113
1 parent b6edf55 commit 43e6bf1

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/components/datepicker/js/calendar.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
scope: {
5858
minDate: '=mdMinDate',
5959
maxDate: '=mdMaxDate',
60-
dateFilter: '=mdDateFilter'
60+
dateFilter: '=mdDateFilter',
61+
_currentView: '@mdCurrentView'
6162
},
6263
require: ['ngModel', 'mdCalendar'],
6364
controller: CalendarCtrl,
@@ -116,8 +117,13 @@
116117
/** @type {!angular.NgModelController} */
117118
this.ngModelCtrl = null;
118119

119-
/** @type {String} The currently visible calendar view. */
120-
this.currentView = 'month';
120+
/**
121+
* The currently visible calendar view. Note the prefix on the scope value,
122+
* which is necessary, because the datepicker seems to reset the real one value if the
123+
* calendar is open, but the value on the datepicker's scope is empty.
124+
* @type {String}
125+
*/
126+
this.currentView = this._currentView || 'month';
121127

122128
/** @type {String} Class applied to the selected date cell. */
123129
this.SELECTED_DATE_CLASS = 'md-calendar-selected-date';

src/components/datepicker/js/calendar.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ describe('md-calendar', function() {
9393
}
9494

9595
/** Creates and compiles an md-calendar element. */
96-
function createElement(parentScope) {
96+
function createElement(parentScope, templateOverride) {
9797
var directiveScope = parentScope || $rootScope.$new();
98-
var template = '<md-calendar md-min-date="minDate" md-max-date="maxDate" ' +
98+
var template = templateOverride || '<md-calendar md-min-date="minDate" md-max-date="maxDate" ' +
9999
'ng-model="myDate"></md-calendar>';
100100
var attachedElement = angular.element(template);
101101
document.body.appendChild(attachedElement[0]);
@@ -707,4 +707,12 @@ describe('md-calendar', function() {
707707
}
708708
}
709709
});
710+
711+
it('should have a configurable default view', function() {
712+
ngElement.remove();
713+
var calendar = createElement(null, '<md-calendar ng-model="myDate" md-current-view="year"></md-calendar>')[0];
714+
715+
expect(calendar.querySelector('md-calendar-month')).toBeFalsy();
716+
expect(calendar.querySelector('md-calendar-year')).toBeTruthy();
717+
});
710718
});

src/components/datepicker/js/datepickerDirective.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @param {String=} md-placeholder The date input placeholder value.
2828
* @param {String=} md-open-on-focus When present, the calendar will be opened when the input is focused.
2929
* @param {Boolean=} md-is-open Expression that can be used to open the datepicker's calendar on-demand.
30+
* @param {String=} md-current-view Default open view of the calendar pane. Can be either "month" or "year".
3031
* @param {String=} md-hide-icons Determines which datepicker icons should be hidden. Note that this may cause the
3132
* datepicker to not align properly with other components. **Use at your own risk.** Possible values are:
3233
* * `"all"` - Hides all icons.
@@ -93,7 +94,9 @@
9394
'</div>' +
9495
'<div class="md-datepicker-calendar">' +
9596
'<md-calendar role="dialog" aria-label="{{::ctrl.dateLocale.msgCalendar}}" ' +
96-
'md-min-date="ctrl.minDate" md-max-date="ctrl.maxDate"' +
97+
'md-current-view="{{::ctrl.currentView}}"' +
98+
'md-min-date="ctrl.minDate"' +
99+
'md-max-date="ctrl.maxDate"' +
97100
'md-date-filter="ctrl.dateFilter"' +
98101
'ng-model="ctrl.date" ng-if="ctrl.isCalendarOpen">' +
99102
'</md-calendar>' +
@@ -105,6 +108,7 @@
105108
minDate: '=mdMinDate',
106109
maxDate: '=mdMaxDate',
107110
placeholder: '@mdPlaceholder',
111+
currentView: '@mdCurrentView',
108112
dateFilter: '=mdDateFilter',
109113
isOpen: '=?mdIsOpen',
110114
debounceInterval: '=mdDebounceInterval'
@@ -205,13 +209,8 @@
205209
*
206210
* @ngInject @constructor
207211
*/
208-
function DatePickerCtrl($scope, $element, $attrs, $compile, $timeout, $window,
209-
$mdConstant, $mdTheming, $mdUtil, $mdDateLocale, $$mdDateUtil, $$rAF) {
210-
/** @final */
211-
this.$compile = $compile;
212-
213-
/** @final */
214-
this.$timeout = $timeout;
212+
function DatePickerCtrl($scope, $element, $attrs, $window, $mdConstant,
213+
$mdTheming, $mdUtil, $mdDateLocale, $$mdDateUtil, $$rAF) {
215214

216215
/** @final */
217216
this.$window = $window;

0 commit comments

Comments
 (0)