Skip to content

Commit db5eff7

Browse files
Allow custom placeholder elements, fixes marceljuenemann#77
1 parent e0b232e commit db5eff7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

angular-drag-and-drop-lists.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,19 @@ angular.module('dndLists', [])
219219
* CSS classes:
220220
* - dndPlaceholder When an element is dragged over the list, a new placeholder child
221221
* element will be added. This element is of type li and has the class
222-
* dndPlaceholder set.
222+
* dndPlaceholder set. Alternatively, you can define your own placeholder
223+
* by creating a child element with dndPlaceholder class.
223224
* - dndDragover Will be added to the list while an element is dragged over the list.
224225
*/
225226
.directive('dndList', ['$parse', '$timeout', 'dndDropEffectWorkaround', 'dndDragTypeWorkaround',
226227
function($parse, $timeout, dndDropEffectWorkaround, dndDragTypeWorkaround) {
227228
return function(scope, element, attr) {
228229
// While an element is dragged over the list, this placeholder element is inserted
229230
// at the location where the element would be inserted after dropping
230-
var placeholder = angular.element("<li class='dndPlaceholder'></li>");
231+
var placeholder = getPlaceholderElement();
231232
var placeholderNode = placeholder[0];
232233
var listNode = element[0];
234+
placeholder.remove();
233235

234236
var horizontal = attr.dndHorizontalList && scope.$eval(attr.dndHorizontalList);
235237
var externalSources = attr.dndExternalSources && scope.$eval(attr.dndExternalSources);
@@ -396,6 +398,21 @@ angular.module('dndLists', [])
396398
return mousePointer < targetPosition + targetSize / 2;
397399
}
398400

401+
/**
402+
* Tries to find a child element that has the dndPlaceholder class set. If none was found, a
403+
* new li element is created.
404+
*/
405+
function getPlaceholderElement() {
406+
var placeholder;
407+
angular.forEach(element.children(), function(childNode) {
408+
var child = angular.element(childNode);
409+
if (child.hasClass('dndPlaceholder')) {
410+
placeholder = child;
411+
}
412+
});
413+
return placeholder || angular.element("<li class='dndPlaceholder'></li>");
414+
}
415+
399416
/**
400417
* We use the position of the placeholder node to determine at which position of the array the
401418
* object needs to be inserted

demo/types/types.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
</div>
1717
</li>
1818

19+
<li class="dndPlaceholder">
20+
Drop any <strong>{{list.allowedTypes.join(' or ')}}</strong> here
21+
</li>
22+
1923
</ul>

0 commit comments

Comments
 (0)