Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 027c877

Browse files
committed
Fix support for checkboxes
1 parent 19560f1 commit 027c877

File tree

2 files changed

+113
-22
lines changed

2 files changed

+113
-22
lines changed

angular-confirm.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ angular.module('angular-confirm', ['ui.bootstrap.modal'])
6464
return $uibModal.open(settings).result;
6565
};
6666
})
67-
.directive('confirm', function ($confirm) {
67+
.directive('confirm', function ($confirm, $timeout) {
6868
return {
6969
priority: 1,
7070
restrict: 'A',
@@ -81,9 +81,14 @@ angular.module('angular-confirm', ['ui.bootstrap.modal'])
8181

8282
function onSuccess() {
8383
var rawEl = element[0];
84-
//Event is prevented from default action which causes checkboxes not to check
85-
if (typeof rawEl.checked != "undefined") {
86-
rawEl.checked = !rawEl.checked;
84+
if (["checkbox", "radio"].indexOf(rawEl.type) != -1) {
85+
var model = element.data('$ngModelController');
86+
if (model) {
87+
model.$setViewValue(!rawEl.checked);
88+
model.$render();
89+
} else {
90+
rawEl.checked = !rawEl.checked;
91+
}
8792
}
8893
scope.ngClick();
8994
}
@@ -92,23 +97,26 @@ angular.module('angular-confirm', ['ui.bootstrap.modal'])
9297

9398
$event.preventDefault();
9499

95-
if (angular.isUndefined(scope.confirmIf) || scope.confirmIf) {
100+
$timeout(function() {
96101

97-
var data = {text: scope.confirm};
98-
if (scope.confirmTitle) {
99-
data.title = scope.confirmTitle;
100-
}
101-
if (scope.confirmOk) {
102-
data.ok = scope.confirmOk;
103-
}
104-
if (scope.confirmCancel) {
105-
data.cancel = scope.confirmCancel;
102+
if (angular.isUndefined(scope.confirmIf) || scope.confirmIf) {
103+
var data = {text: scope.confirm};
104+
if (scope.confirmTitle) {
105+
data.title = scope.confirmTitle;
106+
}
107+
if (scope.confirmOk) {
108+
data.ok = scope.confirmOk;
109+
}
110+
if (scope.confirmCancel) {
111+
data.cancel = scope.confirmCancel;
112+
}
113+
$confirm(data, scope.confirmSettings || {}).then(onSuccess);
114+
} else {
115+
scope.$apply(onSuccess);
106116
}
107-
$confirm(data, scope.confirmSettings || {}).then(onSuccess);
108-
} else {
109117

110-
scope.$apply(onSuccess);
111-
}
118+
});
119+
112120
});
113121

114122
}

test/unit/confirmSpec.js

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ describe('angular-confirm', function() {
9999
});
100100

101101
describe('confirm directive', function() {
102-
var $scope, element, $confirm, data;
102+
var $scope, element, $confirm, data, $timeout;
103103

104-
beforeEach(angular.mock.inject(function (_$confirm_, $compile) {
104+
beforeEach(angular.mock.inject(function (_$confirm_, _$timeout_) {
105105
$confirm = _$confirm_;
106+
$timeout = _$timeout_;
106107

107108
$confirm.and.callFake(function(d) {
108109
data = d;
@@ -123,6 +124,7 @@ describe('angular-confirm', function() {
123124

124125
it("should resolve the name to the text property", function() {
125126
element.triggerHandler('click');
127+
$timeout.flush();
126128
expect(data.text).toEqual('Are you sure, Joe?');
127129
});
128130
});
@@ -137,6 +139,7 @@ describe('angular-confirm', function() {
137139

138140
it("should call confirm on click and not call the function", function() {
139141
element.triggerHandler('click');
142+
$timeout.flush();
140143
expect($scope.click).not.toHaveBeenCalled();
141144
expect($confirm).toHaveBeenCalled();
142145
});
@@ -155,6 +158,7 @@ describe('angular-confirm', function() {
155158
$scope.truthy = true;
156159
$scope.$apply();
157160
element.triggerHandler('click');
161+
$timeout.flush();
158162
expect($scope.click).not.toHaveBeenCalled();
159163
expect($confirm).toHaveBeenCalled();
160164
});
@@ -163,6 +167,7 @@ describe('angular-confirm', function() {
163167
$scope.truthy = false;
164168
$scope.$apply();
165169
element.triggerHandler('click');
170+
$timeout.flush();
166171
expect($scope.click).toHaveBeenCalled();
167172
expect($confirm).not.toHaveBeenCalled();
168173
});
@@ -179,6 +184,7 @@ describe('angular-confirm', function() {
179184

180185
it("should resolve the confirmTitle to the title property", function() {
181186
element.triggerHandler('click');
187+
$timeout.flush();
182188
expect(data.title).toEqual('Hello, Joe!');
183189
});
184190

@@ -194,6 +200,7 @@ describe('angular-confirm', function() {
194200

195201
it("should resolve the confirmTitle to the title property", function() {
196202
element.triggerHandler('click');
203+
$timeout.flush();
197204
expect(data.ok).toEqual('Okie Dokie, Joe!');
198205
});
199206
});
@@ -208,6 +215,7 @@ describe('angular-confirm', function() {
208215

209216
it("should resolve the confirmTitle to the title property", function() {
210217
element.triggerHandler('click');
218+
$timeout.flush();
211219
expect(data.cancel).toEqual('No Way, Joe!');
212220
});
213221
});
@@ -222,6 +230,7 @@ describe('angular-confirm', function() {
222230

223231
it("should pass the settings to $confirm", function() {
224232
element.triggerHandler('click');
233+
$timeout.flush();
225234
expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, $scope.settings)
226235
});
227236
});
@@ -235,6 +244,7 @@ describe('angular-confirm', function() {
235244

236245
it("should pass the settings to $confirm", function() {
237246
element.triggerHandler('click');
247+
$timeout.flush();
238248
expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, {name: "Joe"})
239249
});
240250
});
@@ -250,6 +260,7 @@ describe('angular-confirm', function() {
250260

251261
it("should call confirm on click and not call the function", function() {
252262
element.triggerHandler('click');
263+
$timeout.flush();
253264
expect($scope.click).not.toHaveBeenCalled();
254265
expect($confirm).toHaveBeenCalled();
255266
expect(element[0].checked).toBe(false);
@@ -260,24 +271,92 @@ describe('angular-confirm', function() {
260271
describe('with checkbox and confirm if false', function() {
261272

262273
beforeEach(angular.mock.inject(function($compile) {
263-
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" confirm-if="truthy" />');
274+
element = angular.element('<input type="checkbox" ng-click="click()" ng-model="checked" confirm="Are you sure?" confirm-if="truthy" />');
264275
$compile(element)($scope);
265276
$scope.$digest();
266277
}));
267278

268279
it("should set the checkbox to checked", function() {
280+
expect($scope.checked).not.toBeDefined();
269281
expect(element[0].checked).toBe(false);
270282
$scope.truthy = false;
271283
$scope.$apply();
272284
element.triggerHandler('click');
285+
$timeout.flush();
273286
expect($scope.click).toHaveBeenCalled();
274287
expect($confirm).not.toHaveBeenCalled();
275288
expect(element[0].checked).toBe(true);
289+
expect($scope.checked).toBe(true);
276290
});
277291
});
278292

279293
describe('with checkbox already checked and confirm if false', function() {
280294

295+
beforeEach(angular.mock.inject(function($compile) {
296+
$scope.checked = true;
297+
element = angular.element('<input type="checkbox" ng-click="click()" ng-model="checked" confirm="Are you sure?" confirm-if="truthy" checked />');
298+
$compile(element)($scope);
299+
$scope.$digest();
300+
}));
301+
302+
it("should set the checkbox to checked", function() {
303+
expect($scope.checked).toBe(true);
304+
expect(element[0].checked).toBe(true);
305+
$scope.truthy = false;
306+
$scope.$apply();
307+
element.triggerHandler('click');
308+
$timeout.flush();
309+
expect($scope.click).toHaveBeenCalled();
310+
expect($confirm).not.toHaveBeenCalled();
311+
expect(element[0].checked).toBe(false);
312+
expect($scope.checked).toBe(false);
313+
});
314+
});
315+
316+
describe('with checkbox and confirm if false and ng-true-value/ng-false-value', function() {
317+
318+
beforeEach(angular.mock.inject(function($compile) {
319+
element = angular.element('<input type="checkbox" ng-click="click()" ng-model="checked" confirm="Are you sure?" confirm-if="truthy" ng-true-value="\'YES\'" ng-false-value="\'NO\'" />');
320+
$compile(element)($scope);
321+
$scope.$digest();
322+
}));
323+
324+
it("should set the checkbox to checked", function() {
325+
expect($scope.checked).not.toBeDefined();
326+
expect(element[0].checked).toBe(false);
327+
$scope.truthy = false;
328+
$scope.$apply();
329+
element.triggerHandler('click');
330+
$timeout.flush();
331+
expect($scope.click).toHaveBeenCalled();
332+
expect($confirm).not.toHaveBeenCalled();
333+
expect(element[0].checked).toBe(true);
334+
expect($scope.checked).toBe("YES");
335+
});
336+
});
337+
338+
describe('with checkbox without ngModel and confirm if false', function() {
339+
340+
beforeEach(angular.mock.inject(function($compile) {
341+
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" confirm-if="truthy" />');
342+
$compile(element)($scope);
343+
$scope.$digest();
344+
}));
345+
346+
it("should set the checkbox to checked", function() {
347+
expect(element[0].checked).toBe(false);
348+
$scope.truthy = false;
349+
$scope.$apply();
350+
element.triggerHandler('click');
351+
$timeout.flush();
352+
expect($scope.click).toHaveBeenCalled();
353+
expect($confirm).not.toHaveBeenCalled();
354+
expect(element[0].checked).toBe(true);
355+
});
356+
});
357+
358+
describe('with checkbox without ngModel already checked and confirm if false', function() {
359+
281360
beforeEach(angular.mock.inject(function($compile) {
282361
element = angular.element('<input type="checkbox" ng-click="click()" confirm="Are you sure?" confirm-if="truthy" checked />');
283362
$compile(element)($scope);
@@ -289,6 +368,7 @@ describe('angular-confirm', function() {
289368
$scope.truthy = false;
290369
$scope.$apply();
291370
element.triggerHandler('click');
371+
$timeout.flush();
292372
expect($scope.click).toHaveBeenCalled();
293373
expect($confirm).not.toHaveBeenCalled();
294374
expect(element[0].checked).toBe(false);
@@ -297,4 +377,7 @@ describe('angular-confirm', function() {
297377

298378
});
299379

300-
});
380+
381+
382+
});
383+

0 commit comments

Comments
 (0)