From f15160127bfa9f3031fa1b1acceedd95c9e8004b Mon Sep 17 00:00:00 2001 From: Bob Date: Fri, 4 Jul 2014 21:58:45 +0800 Subject: [PATCH] fixed #9 Besides that, all bootstrap-select options is watched. --- src/nya-bootstrap-select.js | 22 ++++++++++ test/helpers.js | 6 +++ test/spec/nya-bootstrap-select-special.js | 50 +++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test/spec/nya-bootstrap-select-special.js diff --git a/src/nya-bootstrap-select.js b/src/nya-bootstrap-select.js index a0e92a6..e1715d2 100644 --- a/src/nya-bootstrap-select.js +++ b/src/nya-bootstrap-select.js @@ -30,6 +30,24 @@ angular.module('nya.bootstrap.select',[]) // prevent selectDirective render an unknownOption. selectCtrl.renderUnknownOption = angular.noop; var optionArray = []; + + // store data- attribute options of select + var selectorOptions = {}; + var BS_ATTR = ['container', 'countSelectedText', 'dropupAuto', 'header', 'hideDisabled', 'selectedTextFormat', 'size', 'showSubtext', 'showIcon', 'showContent', 'style', 'title', 'width', 'disabled']; + + var checkSelectorOptionsEquality = function() { + var isEqual = true; + angular.forEach(BS_ATTR, function(attr) { + isEqual = isEqual && attrs[attr] === selectorOptions[attr]; + }); + }; + + var updateSelectorOptions = function() { + angular.forEach(BS_ATTR, function(attr) { + selectorOptions[attr] = attrs[attr]; + }); + }; + /** * Check option data attributes, text and value equality. * @param opt the option dom element @@ -88,6 +106,10 @@ angular.module('nya.bootstrap.select',[]) if(hasChanged) { buildSelector(); } + if(!checkSelectorOptionsEquality()) { + updateSelectorOptions(); + $(element).selectpicker('refresh'); + } optionArray = makeOptionArray(optionElements); } diff --git a/test/helpers.js b/test/helpers.js index c453b12..d84d580 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -8,6 +8,12 @@ beforeEach(function() { }; return this.actual.hasClass(cls); + }, + toHaveAttribute: function(cls) { + this.message = function() { + return "Expected '" + angular.mock.dump(this.actual) + "' to have attribute '" + cls + "'."; + }; + return this.actual.is('['+cls+']'); } }); }); diff --git a/test/spec/nya-bootstrap-select-special.js b/test/spec/nya-bootstrap-select-special.js new file mode 100644 index 0000000..c1b6e5b --- /dev/null +++ b/test/spec/nya-bootstrap-select-special.js @@ -0,0 +1,50 @@ +'use strict'; + +/** + * Test for special scenario + */ + +describe('test for some special scenario', function() { + var $scope, $compile, rootElement; + + var options = ['Alpha', 'Bravo', 'Charlie', 'Delta', + 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'Juliet', 'Kilo', 'Lima', + 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', + 'Tango', 'Uniform', 'Victor', 'Whiskey', 'X-ray', 'Yankee', 'Zulu' + ]; + + // load the nyaBootstrapSelect module + beforeEach(module('nya.bootstrap.select')); + + beforeEach(inject(function(_$rootScope_, _$compile_){ + $scope = _$rootScope_.$new(); + $compile = _$compile_; + })); + + it('should refresh selector when some attribute changed', function() { + $scope.options = options; + $scope.enableSelector = true; + $scope.$digest(); + + rootElement = $compile('
' + + '' + + '
')($scope); + + $scope.$digest(); + + var selectElement = rootElement.children('.nya-selectpicker'); + var selectButton = selectElement.next().children('.dropdown-toggle'); + + expect(selectElement).not.toHaveAttribute('disabled'); + expect(selectButton).not.toHaveClass('disabled'); + + $scope.enableSelector = false; + + $scope.$digest(); + + expect(selectElement).toHaveAttribute('disabled'); + expect(selectButton).toHaveClass('disabled'); + }); +}); \ No newline at end of file