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

Commit 911ec72

Browse files
devversionThomasBurleson
authored andcommitted
fix(switch): fix switch drag functionality.
This fixes the complete switch drag functionality. Fix ported from my previous work at material2 (https://github.com/angular/material2/pull/7/files) Fixes #4719 Fixes #2338 Closes #6715
1 parent a0ca139 commit 911ec72

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/components/checkbox/checkbox.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ function MdCheckboxDirective(inputDirective, $mdAria, $mdConstant, $mdTheming, $
172172
}
173173

174174
function listener(ev) {
175-
if (element[0].hasAttribute('disabled')) {
175+
// skipToggle boolean is used by the switch directive to prevent the click event
176+
// when releasing the drag. There will be always a click if releasing the drag over the checkbox
177+
if (element[0].hasAttribute('disabled') || scope.skipToggle) {
176178
return;
177179
}
178180

src/components/switch/switch.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ angular.module('material.components.switch', [
4646
*
4747
* </hljs>
4848
*/
49-
function MdSwitch(mdCheckboxDirective, $mdUtil, $mdConstant, $parse, $$rAF, $mdGesture) {
49+
function MdSwitch(mdCheckboxDirective, $mdUtil, $mdConstant, $parse, $$rAF, $mdGesture, $timeout) {
5050
var checkboxDirective = mdCheckboxDirective[0];
5151

5252
return {
@@ -138,11 +138,17 @@ function MdSwitch(mdCheckboxDirective, $mdUtil, $mdConstant, $parse, $$rAF, $mdG
138138

139139
// We changed if there is no distance (this is a click a click),
140140
// or if the drag distance is >50% of the total.
141-
var isChanged = ngModel.$viewValue ? drag.translate > 0.5 : drag.translate < 0.5;
141+
var isChanged = ngModel.$viewValue ? drag.translate < 0.5 : drag.translate > 0.5;
142142
if (isChanged) {
143143
applyModelValue(!ngModel.$viewValue);
144144
}
145145
drag = null;
146+
147+
// Wait for incoming mouse click
148+
scope.skipToggle = true;
149+
$timeout(function() {
150+
scope.skipToggle = false;
151+
}, 1);
146152
}
147153

148154
function applyModelValue(newValue) {

src/components/switch/switch.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ describe('<md-switch>', function() {
6565
parentScope.$apply();
6666
expect(element.attr('tabindex')).toEqual('-1');
6767
});
68+
69+
it('should skip click event if releasing drag over element', function() {
70+
var checkbox = $compile('<md-switch></md-switch>')(parentScope);
71+
var scope = checkbox.scope();
72+
73+
// skipToggle is used here to imitate an ending drag, same behavior as in the component.
74+
scope.skipToggle = true;
75+
scope.$apply();
76+
77+
checkbox.triggerHandler('click');
78+
79+
expect(checkbox[0]).not.toHaveClass('md-checked');
80+
81+
});
6882
});

0 commit comments

Comments
 (0)