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

feat(ngSwitch): add an optional attribute ngSwitchWhenSeparator #10798

Closed
wants to merge 1 commit into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/ng/directive/ngSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
*
* * `ngSwitchWhen`: the case statement to match against. If match then this
* case will be displayed. If the same match appears multiple times, all the
* elements will be displayed.
* elements will be displayed. It is possible to associate mutiple values to
* the same `ngSwitchWhen` by defining the optional attribute
* `ngSwitchWhenSeparator`. The separator will be used to split the value of
* the `ngSwitchWhen` attribute into multiple tokens, and the element will show
* if any of the `ngSwitch` evaluates to any of these tokens.
* * `ngSwitchDefault`: the default case when no other case match. If there
* are multiple default cases, all of them will be displayed when no other
* case match.
Expand Down Expand Up @@ -189,8 +193,13 @@ var ngSwitchWhenDirective = ngDirective({
require: '^ngSwitch',
multiElement: true,
link: function(scope, element, attrs, ctrl, $transclude) {
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
ctrl.cases['!' + attrs.ngSwitchWhen].push({ transclude: $transclude, element: element });
var cases = attrs.ngSwitchWhen.split(attrs.ngSwitchWhenSeparator).sort().filter(
function(el, ix, ar) { return ar[ix - 1] !== el;}
);
forEach(cases, function(whenCase) {
ctrl.cases['!' + whenCase] = (ctrl.cases['!' + whenCase] || []);
ctrl.cases['!' + whenCase].push({ transclude: $transclude, element: element });
});
}
});

Expand Down
107 changes: 107 additions & 0 deletions test/ng/directive/ngSwitchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,113 @@ describe('ngSwitch', function() {
$rootScope.$apply('mode = "b"');
expect(element.children().length).toBe(1);
}));


describe('ngSwitchWhen separator', function() {
it('should be possible to define a separator', inject(function($rootScope, $compile) {
element = $compile(
'<div ng-switch="mode">' +
'<p ng-switch-when="a|b" ng-switch-when-separator="|">Block1|</p>' +
'<p ng-switch-when="a">Block2|</p>' +
'<p ng-switch-default>Block3|</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('mode = "a"');
expect(element.children().length).toBe(2);
expect(element.text()).toBe('Block1|Block2|');
$rootScope.$apply('mode = "b"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block1|');
$rootScope.$apply('mode = "c"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block3|');
}));


it('should be possible to use a separator at the end of the value', inject(function($rootScope, $compile) {
element = $compile(
'<div ng-switch="mode">' +
'<p ng-switch-when="a|b|" ng-switch-when-separator="|">Block1|</p>' +
'<p ng-switch-when="a">Block2|</p>' +
'<p ng-switch-default>Block3|</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('mode = "a"');
expect(element.children().length).toBe(2);
expect(element.text()).toBe('Block1|Block2|');
$rootScope.$apply('mode = ""');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block1|');
$rootScope.$apply('mode = "c"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block3|');
}));


it('should be possible to use the empty string as a separator', inject(function($rootScope, $compile) {
element = $compile(
'<div ng-switch="mode">' +
'<p ng-switch-when="ab" ng-switch-when-separator="">Block1|</p>' +
'<p ng-switch-when="a">Block2|</p>' +
'<p ng-switch-default>Block3|</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('mode = "a"');
expect(element.children().length).toBe(2);
expect(element.text()).toBe('Block1|Block2|');
$rootScope.$apply('mode = "b"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block1|');
$rootScope.$apply('mode = "c"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block3|');
}));


it('should be possible to use separators that are multiple characters long', inject(function($rootScope, $compile) {
element = $compile(
'<div ng-switch="mode">' +
'<p ng-switch-when="a||b|a" ng-switch-when-separator="||">Block1|</p>' +
'<p ng-switch-when="a">Block2|</p>' +
'<p ng-switch-default>Block3|</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('mode = "a"');
expect(element.children().length).toBe(2);
expect(element.text()).toBe('Block1|Block2|');
$rootScope.$apply('mode = "b|a"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block1|');
$rootScope.$apply('mode = "c"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block3|');
}));


it('should ignore multiple appearances of the same item', inject(function($rootScope, $compile) {
element = $compile(
'<div ng-switch="mode">' +
'<p ng-switch-when="a|b|a" ng-switch-when-separator="|">Block1|</p>' +
'<p ng-switch-when="a">Block2|</p>' +
'<p ng-switch-default>Block3|</div>' +
'</div>'
)($rootScope);

$rootScope.$apply('mode = "a"');
expect(element.children().length).toBe(2);
expect(element.text()).toBe('Block1|Block2|');
$rootScope.$apply('mode = "b"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block1|');
$rootScope.$apply('mode = "c"');
expect(element.children().length).toBe(1);
expect(element.text()).toBe('Block3|');
}));
});
});

describe('ngSwitch animation', function() {
Expand Down