Skip to content

Commit 7e390a1

Browse files
committed
Merge pull request angular-ui#751 from cmlenz/fix-append-to-body
Fix issues with append-to-body
2 parents ed61077 + a221d11 commit 7e390a1

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

src/uiSelectDirective.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,32 @@ uis.directive('uiSelect',
162162
$document.off('click', onDocumentClick);
163163
});
164164

165+
// Move transcluded elements to their correct position in main template
166+
transcludeFn(scope, function(clone) {
167+
// See Transclude in AngularJS http://blog.omkarpatil.com/2012/11/transclude-in-angularjs.html
168+
169+
// One day jqLite will be replaced by jQuery and we will be able to write:
170+
// var transcludedElement = clone.filter('.my-class')
171+
// instead of creating a hackish DOM element:
172+
var transcluded = angular.element('<div>').append(clone);
173+
174+
var transcludedMatch = transcluded.querySelectorAll('.ui-select-match');
175+
transcludedMatch.removeAttr('ui-select-match'); //To avoid loop in case directive as attr
176+
transcludedMatch.removeAttr('data-ui-select-match'); // Properly handle HTML5 data-attributes
177+
if (transcludedMatch.length !== 1) {
178+
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-match but got '{0}'.", transcludedMatch.length);
179+
}
180+
element.querySelectorAll('.ui-select-match').replaceWith(transcludedMatch);
181+
182+
var transcludedChoices = transcluded.querySelectorAll('.ui-select-choices');
183+
transcludedChoices.removeAttr('ui-select-choices'); //To avoid loop in case directive as attr
184+
transcludedChoices.removeAttr('data-ui-select-choices'); // Properly handle HTML5 data-attributes
185+
if (transcludedChoices.length !== 1) {
186+
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-choices but got '{0}'.", transcludedChoices.length);
187+
}
188+
element.querySelectorAll('.ui-select-choices').replaceWith(transcludedChoices);
189+
});
190+
165191
// Support for appending the select field to the body when its open
166192
var appendToBody = scope.$eval(attrs.appendToBody);
167193
if (appendToBody !== undefined ? appendToBody : uiSelectConfig.appendToBody) {
@@ -181,7 +207,8 @@ uis.directive('uiSelect',
181207
}
182208

183209
// Hold on to a reference to the .ui-select-container element for appendToBody support
184-
var placeholder = null;
210+
var placeholder = null,
211+
originalWidth = '';
185212

186213
function positionDropdown() {
187214
// Remember the absolute position of the element
@@ -193,6 +220,10 @@ uis.directive('uiSelect',
193220
placeholder[0].style.height = offset.height + 'px';
194221
element.after(placeholder);
195222

223+
// Remember the original value of the element width inline style, so it can be restored
224+
// when the dropdown is closed
225+
originalWidth = element[0].style.width;
226+
196227
// Now move the actual dropdown element to the end of the body
197228
$document.find('body').append(element);
198229

@@ -215,34 +246,8 @@ uis.directive('uiSelect',
215246
element[0].style.position = '';
216247
element[0].style.left = '';
217248
element[0].style.top = '';
218-
element[0].style.width = '';
249+
element[0].style.width = originalWidth;
219250
}
220-
221-
// Move transcluded elements to their correct position in main template
222-
transcludeFn(scope, function(clone) {
223-
// See Transclude in AngularJS http://blog.omkarpatil.com/2012/11/transclude-in-angularjs.html
224-
225-
// One day jqLite will be replaced by jQuery and we will be able to write:
226-
// var transcludedElement = clone.filter('.my-class')
227-
// instead of creating a hackish DOM element:
228-
var transcluded = angular.element('<div>').append(clone);
229-
230-
var transcludedMatch = transcluded.querySelectorAll('.ui-select-match');
231-
transcludedMatch.removeAttr('ui-select-match'); //To avoid loop in case directive as attr
232-
transcludedMatch.removeAttr('data-ui-select-match'); // Properly handle HTML5 data-attributes
233-
if (transcludedMatch.length !== 1) {
234-
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-match but got '{0}'.", transcludedMatch.length);
235-
}
236-
element.querySelectorAll('.ui-select-match').replaceWith(transcludedMatch);
237-
238-
var transcludedChoices = transcluded.querySelectorAll('.ui-select-choices');
239-
transcludedChoices.removeAttr('ui-select-choices'); //To avoid loop in case directive as attr
240-
transcludedChoices.removeAttr('data-ui-select-choices'); // Properly handle HTML5 data-attributes
241-
if (transcludedChoices.length !== 1) {
242-
throw uiSelectMinErr('transcluded', "Expected 1 .ui-select-choices but got '{0}'.", transcludedChoices.length);
243-
}
244-
element.querySelectorAll('.ui-select-choices').replaceWith(transcludedChoices);
245-
});
246251
};
247252
}
248253
};

0 commit comments

Comments
 (0)