Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 7d3a750

Browse files
Umer Farooqicfantv
Umer Farooq
authored andcommitted
feat(dropdown): make dropdown-append-to-body configurable (#6356)
* feat(dropdown): make dropdown-append-to-body configurable Make dropdown-append-to-body accept a value which will be evaluated to determine if the menu should be appended to body. If no value is specified, or a non-false value is specified then the menu will be appended to body. If the value is `false` then the menu will not be appended to body. * feat(dropdown): append and remove menu when menu is opened or closed Only append and remove append-to and append-to-body menus when the menu is opened or closed. This allows for the values of append-to and append-to-body to be evaluated when the menu is toggled open, and also prevents littering of the DOM. * fix(dropdown): don't remove the dropdown-menu on close Instead of removing the dropdown-menu on close, append it back to the original element.
1 parent 71dc691 commit 7d3a750

File tree

3 files changed

+271
-73
lines changed

3 files changed

+271
-73
lines changed

src/dropdown/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Each of these parts need to be used as attribute directives.
2525
* `dropdown-append-to-body`
2626
<small class="badge">B</small>
2727
_(Default: `false`)_ -
28-
Appends the inner dropdown-menu to the body element.
28+
Appends the inner dropdown-menu to the body element if the attribute is present without a value, or with a non `false` value.
2929

3030
* `is-open`
3131
<small class="badge">$</small>

src/dropdown/dropdown.js

+35-21
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.multiMap', 'ui.bootstrap.
144144
getIsOpen,
145145
setIsOpen = angular.noop,
146146
toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop,
147-
appendToBody = false,
148-
appendTo = null,
149147
keynavEnabled = false,
150148
selectedOption = null,
151149
body = $document.find('body');
@@ -162,26 +160,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.multiMap', 'ui.bootstrap.
162160
});
163161
}
164162

165-
if (angular.isDefined($attrs.dropdownAppendTo)) {
166-
var appendToEl = $parse($attrs.dropdownAppendTo)(scope);
167-
if (appendToEl) {
168-
appendTo = angular.element(appendToEl);
169-
}
170-
}
171-
172-
appendToBody = angular.isDefined($attrs.dropdownAppendToBody);
173163
keynavEnabled = angular.isDefined($attrs.keyboardNav);
174-
175-
if (appendToBody && !appendTo) {
176-
appendTo = body;
177-
}
178-
179-
if (appendTo && self.dropdownMenu) {
180-
appendTo.append(self.dropdownMenu);
181-
$element.on('$destroy', function handleDestroyEvent() {
182-
self.dropdownMenu.remove();
183-
});
184-
}
185164
};
186165

187166
this.toggle = function(open) {
@@ -253,7 +232,42 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.multiMap', 'ui.bootstrap.
253232
}
254233
};
255234

235+
function removeDropdownMenu() {
236+
$element.append(self.dropdownMenu);
237+
}
238+
256239
scope.$watch('isOpen', function(isOpen, wasOpen) {
240+
var appendTo = null,
241+
appendToBody = false;
242+
243+
if (angular.isDefined($attrs.dropdownAppendTo)) {
244+
var appendToEl = $parse($attrs.dropdownAppendTo)(scope);
245+
if (appendToEl) {
246+
appendTo = angular.element(appendToEl);
247+
}
248+
}
249+
250+
if (angular.isDefined($attrs.dropdownAppendToBody)) {
251+
var appendToBodyValue = $parse($attrs.dropdownAppendToBody)(scope);
252+
if (appendToBodyValue !== false) {
253+
appendToBody = true;
254+
}
255+
}
256+
257+
if (appendToBody && !appendTo) {
258+
appendTo = body;
259+
}
260+
261+
if (appendTo && self.dropdownMenu) {
262+
if (isOpen) {
263+
appendTo.append(self.dropdownMenu);
264+
$element.on('$destroy', removeDropdownMenu);
265+
} else {
266+
$element.off('$destroy', removeDropdownMenu);
267+
removeDropdownMenu();
268+
}
269+
}
270+
257271
if (appendTo && self.dropdownMenu) {
258272
var pos = $position.positionElements($element, self.dropdownMenu, 'bottom-left', true),
259273
css,

0 commit comments

Comments
 (0)