Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

fix(datepicker): enable scrolling when scope destroyed. #7544

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/components/datepicker/datePicker.js
Expand Up @@ -527,6 +527,10 @@
this.calendarPane.classList.remove('md-pane-open');
this.calendarPane.classList.remove('md-datepicker-pos-adjusted');

if (this.isCalendarOpen) {
this.$mdUtil.enableScrolling();
}

if (this.calendarPane.parentNode) {
// Use native DOM removal because we do not want any of the angular state of this element
// to be disposed.
Expand Down Expand Up @@ -570,11 +574,10 @@
/** Close the floating calendar pane. */
DatePickerCtrl.prototype.closeCalendarPane = function() {
if (this.isCalendarOpen) {
this.isCalendarOpen = false;
this.detachCalendarPane();
this.isCalendarOpen = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this because now the person working on this needs to know that isCalendarOpen must be set after detaching; this is a weird coupling of behaviors that makes brittle code. It would be much better to simple call enableScrolling() in the $destroy handler in addition to detachPane, which will no-op if scrolling isn't disabled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand your thoughts. I had the behavior, you desired, before, but I thought calling enableScrolling is to generic, because detachPane will be always called after hidePane, so it made sense to me, to move the enable scroll function completely into the detachPane function.

this.calendarPaneOpenedFrom.focus();
this.calendarPaneOpenedFrom = null;
this.$mdUtil.enableScrolling();

this.ngModelCtrl.$setTouched();

Expand Down
29 changes: 29 additions & 0 deletions src/components/datepicker/datePicker.spec.js
Expand Up @@ -515,6 +515,35 @@ describe('md-date-picker', function() {
expect(controller.calendarPaneOpenedFrom).toBe(null);
expect(controller.isCalendarOpen).toBe(false);
});

it('should re-enable scrolling probably', function() {
var maskLength = getMaskLength();

controller.openCalendarPane({
target: controller.inputElement
});

expect(getMaskLength()).toBe(maskLength + 1);

controller.closeCalendarPane();

expect(getMaskLength()).toBe(maskLength);

controller.openCalendarPane({
target: controller.inputElement
});

expect(getMaskLength()).toBe(maskLength + 1);

// Trigger a scope destruction, like when a route changes.
scope.$destroy();

expect(getMaskLength()).toBe(maskLength);

function getMaskLength() {
return document.body.querySelectorAll('.md-scroll-mask').length;
}
});
});

describe('md-calendar-change', function() {
Expand Down