From f183c831d68664eeb5b2e8ac62454ccfb12e9542 Mon Sep 17 00:00:00 2001 From: Vincent Leonardo Date: Fri, 9 Sep 2016 23:56:20 +0800 Subject: [PATCH 01/13] Fix Indeterminate State not Triggered from 'false' state Replaced default checkbox formatter to ignore comparing 'undefined' value. --- src/directives/bsSwitch.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/directives/bsSwitch.js b/src/directives/bsSwitch.js index 7e1f7f8..f58f2a6 100644 --- a/src/directives/bsSwitch.js +++ b/src/directives/bsSwitch.js @@ -150,6 +150,12 @@ angular.module('frapontillo.bootstrap-switch') controller.$setViewValue(controller.$modelValue); } else { controller.$setViewValue(viewValue); + controller.$formatters[0] = function(value) { + if (value === undefined) { + return value; + } + return angular.equals(value, getTrueValue()); + } } } }; From 240b51e921cc2a84a510c717cde65e9363286ebf Mon Sep 17 00:00:00 2001 From: Vincent Leonardo Date: Sat, 10 Sep 2016 00:11:50 +0800 Subject: [PATCH 02/13] Missing semicolon --- src/directives/bsSwitch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/bsSwitch.js b/src/directives/bsSwitch.js index f58f2a6..8c46b7e 100644 --- a/src/directives/bsSwitch.js +++ b/src/directives/bsSwitch.js @@ -155,7 +155,7 @@ angular.module('frapontillo.bootstrap-switch') return value; } return angular.equals(value, getTrueValue()); - } + }; } } }; From 26c553777745f953b689885d65b4b00cb14909a9 Mon Sep 17 00:00:00 2001 From: Vincent Leonardo Date: Sat, 10 Sep 2016 01:35:44 +0800 Subject: [PATCH 03/13] Added ignore checking 'null' for formatter --- src/directives/bsSwitch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directives/bsSwitch.js b/src/directives/bsSwitch.js index 8c46b7e..93b6e59 100644 --- a/src/directives/bsSwitch.js +++ b/src/directives/bsSwitch.js @@ -151,7 +151,7 @@ angular.module('frapontillo.bootstrap-switch') } else { controller.$setViewValue(viewValue); controller.$formatters[0] = function(value) { - if (value === undefined) { + if (value === undefined || value === null) { return value; } return angular.equals(value, getTrueValue()); From 1d5bc20f53a70a66a3f11946110cc5514f71f487 Mon Sep 17 00:00:00 2001 From: Vincent Leonardo Date: Sat, 10 Sep 2016 01:37:46 +0800 Subject: [PATCH 04/13] Added test cases for toggling between indeterminate value --- test/spec/directives/bsSwitchSpec.js | 136 +++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/test/spec/directives/bsSwitchSpec.js b/test/spec/directives/bsSwitchSpec.js index 41bdcdb..9b7c223 100644 --- a/test/spec/directives/bsSwitchSpec.js +++ b/test/spec/directives/bsSwitchSpec.js @@ -723,4 +723,140 @@ describe('Directive: bsSwitch', function () { } it('should evaluate change expression when model changes', inject(makeTestModelSwitchChange())); it('should evaluate change expression when model changes', inject(makeTestModelSwitchChange(true))); + + // Test the null model from true state + function makeTestToIndeterminateNullFromTrue(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = true; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + scope.model = null; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + }; + } + it('should change from true to the indeterminate state when the model is null', inject(makeTestToIndeterminateNullFromTrue())); + it('should change from true to the indeterminate state when the model is null (input)', inject(makeTestToIndeterminateNullFromTrue(true))); + + // Test the null model from false state + function makeTestToIndeterminateNullFromFalse(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = false; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + scope.model = null; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + }; + } + it('should change from false to the indeterminate state when the model is null', inject(makeTestToIndeterminateNullFromFalse())); + it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestToIndeterminateNullFromFalse(true))); + + // Test the undefined model from true state + function makeTestToIndeterminateUndefinedFromTrue(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = true; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + scope.model = undefined; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + }; + } + it('should change from true to the indeterminate state when the model is null', inject(makeTestToIndeterminateUndefinedFromTrue())); + it('should change from true to the indeterminate state when the model is null (input)', inject(makeTestToIndeterminateUndefinedFromTrue(true))); + + // Test the undefined model from false state + function makeTestToIndeterminateUndefinedFromFalse(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = false; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + scope.model = undefined; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + }; + } + it('should change from false to the indeterminate state when the model is null', inject(makeTestToIndeterminateUndefinedFromFalse())); + it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestToIndeterminateUndefinedFromFalse(true))); + + // Test the changing multiple state + function makeTestMultipleChangeOfStateIndeterminate(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = false; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + scope.model = undefined; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + scope.model = true; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + }; + } + it('should change from false to the indeterminate state when the model is null', inject(makeTestMultipleChangeOfStateIndeterminate())); + it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestMultipleChangeOfStateIndeterminate(true))); + + // Test the changing multiple state other way round + function makeTestMultipleChangeOfStateIndeterminateReverse(input) { + return function () { + var element = compileDirective(undefined, input); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = true; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + scope.model = undefined; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + scope.model = false; + scope.$apply(); + expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + }; + } + it('should change from false to the indeterminate state when the model is null', inject(makeTestMultipleChangeOfStateIndeterminate())); + it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestMultipleChangeOfStateIndeterminate(true))); }); From 97ef133ed6b43e2726a5def4678637ef86d88d77 Mon Sep 17 00:00:00 2001 From: Vincent Leonardo Date: Sat, 10 Sep 2016 01:46:16 +0800 Subject: [PATCH 05/13] Missed out calling some test cases --- test/spec/directives/bsSwitchSpec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/spec/directives/bsSwitchSpec.js b/test/spec/directives/bsSwitchSpec.js index 9b7c223..4e08aba 100644 --- a/test/spec/directives/bsSwitchSpec.js +++ b/test/spec/directives/bsSwitchSpec.js @@ -831,8 +831,8 @@ describe('Directive: bsSwitch', function () { expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); }; } - it('should change from false to the indeterminate state when the model is null', inject(makeTestMultipleChangeOfStateIndeterminate())); - it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestMultipleChangeOfStateIndeterminate(true))); + it('should change from false to the indeterminate state and to true', inject(makeTestMultipleChangeOfStateIndeterminate())); + it('should change from false to the indeterminate state and to true (input)', inject(makeTestMultipleChangeOfStateIndeterminate(true))); // Test the changing multiple state other way round function makeTestMultipleChangeOfStateIndeterminateReverse(input) { @@ -848,8 +848,8 @@ describe('Directive: bsSwitch', function () { scope.model = undefined; scope.$apply(); expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeTruthy(); - expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); - expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); scope.model = false; scope.$apply(); expect(element.hasClass(CONST.SWITCH_INDETERMINATE_CLASS)).toBeFalsy(); @@ -857,6 +857,6 @@ describe('Directive: bsSwitch', function () { expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); }; } - it('should change from false to the indeterminate state when the model is null', inject(makeTestMultipleChangeOfStateIndeterminate())); - it('should change from false to the indeterminate state when the model is null (input)', inject(makeTestMultipleChangeOfStateIndeterminate(true))); + it('should change from false to the indeterminate state and to false', inject(makeTestMultipleChangeOfStateIndeterminateReverse())); + it('should change from false to the indeterminate state and to false (input)', inject(makeTestMultipleChangeOfStateIndeterminateReverse(true))); }); From db1d2c4c5f57480040c5dd93dccf6698ade13d1a Mon Sep 17 00:00:00 2001 From: "joe.prisk" Date: Tue, 8 Nov 2016 10:28:28 +0000 Subject: [PATCH 06/13] fix npm dependencies --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 217ea47..36279d0 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,12 @@ "url": "git@github.com:frapontillo/angular-bootstrap-switch.git" }, "license": "Apache-2.0", - "dependencies": {}, + "dependencies": { + "angular": ">=1.4.0", + "jquery": ">=1.9.0", + "bootstrap": ">=2.3.2", + "bootstrap-switch": "3.3.2" + }, "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-clean": "~0.7.0", From 48334de995f0a609aaae809612248551f6f83687 Mon Sep 17 00:00:00 2001 From: johnz Date: Mon, 20 Mar 2017 11:28:00 +0100 Subject: [PATCH 07/13] Update bootstrap-switch bower dependency to pull in minors and patches and update to 3.3.4 --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 356c9eb..fa09e04 100644 --- a/bower.json +++ b/bower.json @@ -22,7 +22,7 @@ "angular": ">=1.4.0", "jquery": ">=1.9.0", "bootstrap": ">=2.3.2", - "bootstrap-switch": "3.3.2" + "bootstrap-switch": "~3.3.4" }, "devDependencies": { "angular-mocks": ">=1.4.0", From 67b1755122601d4a355211a188c57b737c1eb224 Mon Sep 17 00:00:00 2001 From: johnz Date: Mon, 20 Mar 2017 14:50:14 +0100 Subject: [PATCH 08/13] Fix jquery import in tests --- karma-chrome.conf.js | 2 +- karma.conf.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/karma-chrome.conf.js b/karma-chrome.conf.js index f438fec..48cbb21 100644 --- a/karma-chrome.conf.js +++ b/karma-chrome.conf.js @@ -15,7 +15,7 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - 'bower_components/jquery/dist/jquery.js', + 'bower_components/jquery/jquery.js', 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/bootstrap-switch/dist/js/bootstrap-switch.js', diff --git a/karma.conf.js b/karma.conf.js index d4d4789..04746db 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -15,7 +15,7 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - 'bower_components/jquery/dist/jquery.js', + 'bower_components/jquery/jquery.js', 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/bootstrap-switch/dist/js/bootstrap-switch.js', From 64e39f0dd6bc3b3399bd9a488abb91a6eeeb9bad Mon Sep 17 00:00:00 2001 From: Santiago Castro Date: Tue, 18 Apr 2017 03:15:54 -0300 Subject: [PATCH 09/13] Fix broken Markdown headings --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a10f7d7..7035726 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ angular-bootstrap-switch AngularJS directive for the [bootstrap-switch](https://github.com/nostalgiaz/bootstrap-switch) jQuery plugin. -##Usage +## Usage -###Installation +### Installation ```shell $ bower install angular-bootstrap-switch ``` @@ -23,7 +23,7 @@ $ npm install angular-bootstrap-switch This will install AngularJS, jQuery, and the original bootstrap-switch. -###Registration +### Registration To be able to use the directive, you need to register the `angular-bootstrap-switch` module as a dependency: @@ -33,7 +33,7 @@ angular.module('yourModule', ['frapontillo.bootstrap-switch' ]); ``` -###Directive +### Directive The directive can work on both element and attribute levels. The following example contains all of the supported attributes: ```html @@ -84,21 +84,21 @@ meaning you have to specify the same `ngModel` and a different `value` or `ng-va * `switch-inverse`, inverts the on/off handles * `switch-change`, evaluates an expression whenever the model value changes. Instead, `ng-change` will fire when view value changes (e.g from a click) -###Migrating from bootstrap-switch~2 +### Migrating from bootstrap-switch~2 Read the [CHANGELOG](CHANGELOG.md#030-alpha1-2014-02-22) information to learn what's different in `0.3.0`. -###Examples +### Examples The `example` folder shows a simple working demo of the switch. -###Compatibility +### Compatibility IE8 requires you to attach the directive to an `` or ``. Due to some incompatibilities it is not possible to use a custom tag or `div` instead. -##Development +## Development -###Test and build +### Test and build To build the directive yourself you need to have NodeJS. Then do the following: @@ -110,15 +110,15 @@ $ grunt test-travis $ grunt build ``` -###Contribute +### Contribute To contribute, please follow the generic [AngularJS Contributing Guidelines](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md), with the only exception to send the PR to the `develop` branch instead of `master`. -##Author +## Author Francesco Pontillo () -##License +## License ``` Copyright 2014-2015-2016 Francesco Pontillo From 568b712c09a97a2b07f8b1468209645f1d725a5f Mon Sep 17 00:00:00 2001 From: Francesco Pontillo Date: Wed, 19 Apr 2017 11:12:45 +0200 Subject: [PATCH 10/13] Release v0.5.2 --- CHANGELOG.md | 9 ++++++++- README.md | 6 +++--- bower.json | 2 +- dist/angular-bootstrap-switch.js | 2 +- dist/angular-bootstrap-switch.min.js | 2 +- package.json | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6ab167..f7c67dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ CHANGELOG ========= +## 0.5.2 (2017-04-19) + +- Update `bootstrap-switch` to `~3.3.4` +- Fix `jquery` import in tests +- Fix `npm` dependencies +- Fix `README` headings + ## 0.5.1 (2016-06-04) - Make `switch-change` trigger when model changes @@ -19,7 +26,7 @@ CHANGELOG **BREAKING CHANGES:** - Applications relying on `undefined` as the only indeterminate state - may break if they consider `null` a falsy value. `null` is now an + may break if they consider `null` a falsy value. `null` is now an indeterminate value. ## 0.4.1 (2015-06-15) diff --git a/README.md b/README.md index 7035726..1108f55 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ The directive can work on both element and attribute levels. The following examp Short doc for all of the attributes: * `ng-model`, the value to bind the switch to -* `type`, has to be one of `checkbox` and `radio`. +* `type`, has to be one of `checkbox` and `radio`. This value is mandatory and must be a string, as it cannot be changed once set (see [this answer on StackOverflow](http://stackoverflow.com/a/15155407/801065)). -If you choose `radio`, be sure to follow the [AngularJS radio specs](https://docs.angularjs.org/api/ng/input/input%5Bradio%5D), +If you choose `radio`, be sure to follow the [AngularJS radio specs](https://docs.angularjs.org/api/ng/input/input%5Bradio%5D), meaning you have to specify the same `ngModel` and a different `value` or `ng-value` attribute for each radio * `switch-active`, determines if the switch is enabled or not (changes the inner input's `disabled` attribute) * `switch-readonly`, determines if the switch is read-only or not (changes the inner input's `readonly` attribute) @@ -121,7 +121,7 @@ Francesco Pontillo () ## License ``` - Copyright 2014-2015-2016 Francesco Pontillo + Copyright 2014-2017 Francesco Pontillo Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/bower.json b/bower.json index fa09e04..204ae4f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-bootstrap-switch", - "version": "0.5.1", + "version": "0.5.2", "author": { "name": "Francesco Pontillo", "email": "francescopontillo@gmail.com", diff --git a/dist/angular-bootstrap-switch.js b/dist/angular-bootstrap-switch.js index 1533ff9..70756d8 100644 --- a/dist/angular-bootstrap-switch.js +++ b/dist/angular-bootstrap-switch.js @@ -1,6 +1,6 @@ /** * angular-bootstrap-switch - * @version v0.5.1 - 2016-06-04 + * @version v0.5.2 - 2017-04-19 * @author Francesco Pontillo (francescopontillo@gmail.com) * @link https://github.com/frapontillo/angular-bootstrap-switch * @license Apache License 2.0(http://www.apache.org/licenses/LICENSE-2.0.html) diff --git a/dist/angular-bootstrap-switch.min.js b/dist/angular-bootstrap-switch.min.js index 6d639b8..e1a26ea 100644 --- a/dist/angular-bootstrap-switch.min.js +++ b/dist/angular-bootstrap-switch.min.js @@ -1,6 +1,6 @@ /** * angular-bootstrap-switch - * @version v0.5.1 - 2016-06-04 + * @version v0.5.2 - 2017-04-19 * @author Francesco Pontillo (francescopontillo@gmail.com) * @link https://github.com/frapontillo/angular-bootstrap-switch * @license Apache License 2.0(http://www.apache.org/licenses/LICENSE-2.0.html) diff --git a/package.json b/package.json index 36279d0..86b032b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-bootstrap-switch", - "version": "0.5.1", + "version": "0.5.2", "main": "dist/angular-bootstrap-switch.js", "author": { "name": "Francesco Pontillo", From 5caadacc49ff5945eae8ce1fefc4cb3d73770eb1 Mon Sep 17 00:00:00 2001 From: Olga Karataeva Date: Fri, 1 Dec 2017 13:11:59 +0700 Subject: [PATCH 11/13] Update model value when switch is disabled --- src/directives/bsSwitch.js | 10 ++++++++++ test/spec/directives/bsSwitchSpec.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/directives/bsSwitch.js b/src/directives/bsSwitch.js index 93b6e59..210e2d0 100644 --- a/src/directives/bsSwitch.js +++ b/src/directives/bsSwitch.js @@ -182,6 +182,12 @@ angular.module('frapontillo.bootstrap-switch') // When the model changes controller.$render = function () { initMaybe(); + + // WORKAROUND for https://github.com/Bttstrp/bootstrap-switch/issues/540 + // to update model value when bootstrapSwitch is disabled we should + // re-enable it and only then update 'state' + element.bootstrapSwitch('disabled', ''); + var newValue = controller.$modelValue; if (newValue !== undefined && newValue !== null) { element.bootstrapSwitch('state', newValue === getTrueValue(), true); @@ -189,6 +195,10 @@ angular.module('frapontillo.bootstrap-switch') element.bootstrapSwitch('indeterminate', true, true); controller.$setViewValue(undefined); } + + // return initial value for "disabled" + setActive(); + switchChange(); }; diff --git a/test/spec/directives/bsSwitchSpec.js b/test/spec/directives/bsSwitchSpec.js index 4e08aba..609f414 100644 --- a/test/spec/directives/bsSwitchSpec.js +++ b/test/spec/directives/bsSwitchSpec.js @@ -391,6 +391,34 @@ describe('Directive: bsSwitch', function () { it('should change the model, then deactivate the switch', inject(makeTestChangeModelThenDeactivate())); it('should change the model, deactivate the switch (input)', inject(makeTestChangeModelThenDeactivate(true))); + // Test a model change when switch is deactivated + function makeTestChangeModelWhenSwitchIsDeactivated() { + return function () { + var element = compileDirective('active'); + scope.model = false; + scope.isActive = false; + scope.$apply(); + $timeout.flush(); + // test the active state, should be false + expect(element.hasClass(CONST.SWITCH_DISABLED_CLASS)).toBeTruthy(); + expect(element.find(CONST.INPUT_SELECTOR).attr('disabled')).toBeTruthy(); + // test the model, should be false + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); + + scope.model = true; + scope.$apply(); + + // test the active state, should be false + expect(element.hasClass(CONST.SWITCH_DISABLED_CLASS)).toBeTruthy(); + expect(element.find(CONST.INPUT_SELECTOR).attr('disabled')).toBeTruthy(); + // test the model, should be true + expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); + expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); + }; + } + it('should deactivate the switch, then change the model', inject(makeTestChangeModelWhenSwitchIsDeactivated())); + // Test the activation function makeTestActivate(input) { return function () { From 6203a96d7ca9d55cc31b3ab19954dbd916458852 Mon Sep 17 00:00:00 2001 From: Olga Karataeva Date: Fri, 1 Dec 2017 17:22:19 +0700 Subject: [PATCH 12/13] Code reformatted --- test/spec/directives/bsSwitchSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/spec/directives/bsSwitchSpec.js b/test/spec/directives/bsSwitchSpec.js index 609f414..a44cf0b 100644 --- a/test/spec/directives/bsSwitchSpec.js +++ b/test/spec/directives/bsSwitchSpec.js @@ -406,13 +406,13 @@ describe('Directive: bsSwitch', function () { expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeTruthy(); expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeFalsy(); - scope.model = true; - scope.$apply(); + scope.model = true; + scope.$apply(); - // test the active state, should be false + // test the active state, should be false expect(element.hasClass(CONST.SWITCH_DISABLED_CLASS)).toBeTruthy(); expect(element.find(CONST.INPUT_SELECTOR).attr('disabled')).toBeTruthy(); - // test the model, should be true + // test the model, should be true expect(element.hasClass(CONST.SWITCH_OFF_CLASS)).toBeFalsy(); expect(element.hasClass(CONST.SWITCH_ON_CLASS)).toBeTruthy(); }; From 9c4570b3a36bc88b81decbe78b180421e88e4a4f Mon Sep 17 00:00:00 2001 From: Olga Karataeva Date: Fri, 1 Dec 2017 17:49:26 +0700 Subject: [PATCH 13/13] Code reformatted 2 --- test/spec/directives/bsSwitchSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec/directives/bsSwitchSpec.js b/test/spec/directives/bsSwitchSpec.js index a44cf0b..07cd1f0 100644 --- a/test/spec/directives/bsSwitchSpec.js +++ b/test/spec/directives/bsSwitchSpec.js @@ -391,7 +391,7 @@ describe('Directive: bsSwitch', function () { it('should change the model, then deactivate the switch', inject(makeTestChangeModelThenDeactivate())); it('should change the model, deactivate the switch (input)', inject(makeTestChangeModelThenDeactivate(true))); - // Test a model change when switch is deactivated + // Test a model change when switch is deactivated function makeTestChangeModelWhenSwitchIsDeactivated() { return function () { var element = compileDirective('active');