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

Commit e88d617

Browse files
lgalfasojbdeboer
authored andcommitted
feat(ng:switch): Preserve the order of the elements not in the ng-switch
Preserve the order of the elements that are not part of a case nor default in a ng-switch directive BREAKING CHANGE: elements not in the ng-switch were rendered after the ng-switch elements. Now they are rendered in-place. Ng-switch directives should be updated with non ng-switch elements in render-order. e.g. The following was previously rendered with <li>1</li> after "2": <ul ng-switch="select"> <li>1</li> <li ng-switch-when="option">2</li> </ul> To keep the old behaviour, say: <ul ng-switch="select"> <li ng-switch-when="1">2</li> <li>1</li> </ul> Closes #1074
1 parent 90ba9aa commit e88d617

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/ng/directive/ngSwitch.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
* @restrict EA
77
*
88
* @description
9-
* Conditionally change the DOM structure.
9+
* Conditionally change the DOM structure. Elements within ngSwitch but without
10+
* ngSwitchWhen or ngSwitchDefault directives will be preserved at the location
11+
* as specified in the template
1012
*
1113
* @usageContent
1214
* <ANY ng-switch-when="matchValue1">...</ANY>
1315
* <ANY ng-switch-when="matchValue2">...</ANY>
1416
* ...
1517
* <ANY ng-switch-default>...</ANY>
18+
* <ANY>...</ANY>
1619
*
1720
* @scope
1821
* @param {*} ngSwitch|on expression to match against <tt>ng-switch-when</tt>.
@@ -26,6 +29,7 @@
2629
* are multiple default cases, all of them will be displayed when no other
2730
* case match.
2831
*
32+
*
2933
* @example
3034
<doc:example>
3135
<doc:source>
@@ -90,9 +94,9 @@ var ngSwitchDirective = valueFn({
9094
forEach(selectedTranscludes, function(selectedTransclude) {
9195
var selectedScope = scope.$new();
9296
selectedScopes.push(selectedScope);
93-
selectedTransclude(selectedScope, function(caseElement) {
97+
selectedTransclude.transclude(selectedScope, function(caseElement) {
9498
selectedElements.push(caseElement);
95-
element.append(caseElement);
99+
selectedTransclude.element.after(caseElement);
96100
});
97101
});
98102
}
@@ -107,7 +111,7 @@ var ngSwitchWhenDirective = ngDirective({
107111
compile: function(element, attrs, transclude) {
108112
return function(scope, element, attr, ctrl) {
109113
ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
110-
ctrl.cases['!' + attrs.ngSwitchWhen].push(transclude);
114+
ctrl.cases['!' + attrs.ngSwitchWhen].push({ transclude: transclude, element: element });
111115
};
112116
}
113117
});
@@ -119,7 +123,7 @@ var ngSwitchDefaultDirective = ngDirective({
119123
compile: function(element, attrs, transclude) {
120124
return function(scope, element, attr, ctrl) {
121125
ctrl.cases['?'] = (ctrl.cases['?'] || []);
122-
ctrl.cases['?'].push(transclude);
126+
ctrl.cases['?'].push({ transclude: transclude, element: element });
123127
};
124128
}
125129
});

test/ng/directive/ngSwitchSpec.js

+62
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,68 @@ describe('ngSwitch', function() {
9595
}));
9696

9797

98+
it('should always display the elements that do not match a switch',
99+
inject(function($rootScope, $compile) {
100+
element = $compile(
101+
'<ul ng-switch="select">' +
102+
'<li>always </li>' +
103+
'<li ng-switch-when="1">one </li>' +
104+
'<li ng-switch-when="2">two </li>' +
105+
'<li ng-switch-default>other, </li>' +
106+
'<li ng-switch-default>other too </li>' +
107+
'</ul>')($rootScope);
108+
$rootScope.$apply();
109+
expect(element.text()).toEqual('always other, other too ');
110+
$rootScope.select = 1;
111+
$rootScope.$apply();
112+
expect(element.text()).toEqual('always one ');
113+
}));
114+
115+
116+
it('should display the elements that do not have ngSwitchWhen nor ' +
117+
'ngSwitchDefault at the position specified in the template, when the ' +
118+
'first and last elements in the ngSwitch body do not have a ngSwitch* ' +
119+
'directive', inject(function($rootScope, $compile) {
120+
element = $compile(
121+
'<ul ng-switch="select">' +
122+
'<li>1</li>' +
123+
'<li ng-switch-when="1">2</li>' +
124+
'<li>3</li>' +
125+
'<li ng-switch-when="2">4</li>' +
126+
'<li ng-switch-default>5</li>' +
127+
'<li>6</li>' +
128+
'<li ng-switch-default>7</li>' +
129+
'<li>8</li>' +
130+
'</ul>')($rootScope);
131+
$rootScope.$apply();
132+
expect(element.text()).toEqual('135678');
133+
$rootScope.select = 1;
134+
$rootScope.$apply();
135+
expect(element.text()).toEqual('12368');
136+
}));
137+
138+
139+
it('should display the elements that do not have ngSwitchWhen nor ' +
140+
'ngSwitchDefault at the position specified in the template when the ' +
141+
'first and last elements in the ngSwitch have a ngSwitch* directive',
142+
inject(function($rootScope, $compile) {
143+
element = $compile(
144+
'<ul ng-switch="select">' +
145+
'<li ng-switch-when="1">2</li>' +
146+
'<li>3</li>' +
147+
'<li ng-switch-when="2">4</li>' +
148+
'<li ng-switch-default>5</li>' +
149+
'<li>6</li>' +
150+
'<li ng-switch-default>7</li>' +
151+
'</ul>')($rootScope);
152+
$rootScope.$apply();
153+
expect(element.text()).toEqual('3567');
154+
$rootScope.select = 1;
155+
$rootScope.$apply();
156+
expect(element.text()).toEqual('236');
157+
}));
158+
159+
98160
it('should call change on switch', inject(function($rootScope, $compile) {
99161
element = $compile(
100162
'<ng:switch on="url" change="name=\'works\'">' +

0 commit comments

Comments
 (0)