Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 758a1e2

Browse files
committed
refactor(sortable): abstract placeholder & helper element retrieval
1 parent a20ee2f commit 758a1e2

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

src/sortable.js

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,48 @@ angular.module('ui.sortable', [])
3737
return null;
3838
}
3939

40+
function getPlaceholderElement (element) {
41+
var placeholder = element.sortable('option','placeholder');
42+
43+
// placeholder.element will be a function if the placeholder, has
44+
// been created (placeholder will be an object). If it hasn't
45+
// been created, either placeholder will be false if no
46+
// placeholder class was given or placeholder.element will be
47+
// undefined if a class was given (placeholder will be a string)
48+
if (placeholder && placeholder.element && typeof placeholder.element === 'function') {
49+
var result = placeholder.element();
50+
// workaround for jquery ui 1.9.x,
51+
// not returning jquery collection
52+
result = angular.element(result);
53+
return result;
54+
}
55+
return null;
56+
}
57+
58+
function getPlaceholderExcludesludes (element, placeholder) {
59+
// exact match with the placeholder's class attribute to handle
60+
// the case that multiple connected sortables exist and
61+
// the placehoilder option equals the class of sortable items
62+
var excludes = element.find('[class="' + placeholder.attr('class') + '"]:not([ng-repeat], [data-ng-repeat])');
63+
return excludes;
64+
}
65+
4066
function hasSortingHelper (element, ui) {
4167
var helperOption = element.sortable('option','helper');
4268
return helperOption === 'clone' || (typeof helperOption === 'function' && ui.item.sortable.isCustomHelperUsed());
4369
}
4470

71+
function getSortingHelper (element, ui, savedNodes) {
72+
var result = null;
73+
if (hasSortingHelper(element, ui) &&
74+
element.sortable( 'option', 'appendTo' ) === 'parent') {
75+
// The .ui-sortable-helper element (that's the default class name)
76+
// is placed last.
77+
result = savedNodes.last();
78+
}
79+
return result;
80+
}
81+
4582
// thanks jquery-ui
4683
function isFloating (item) {
4784
return (/left|right/).test(item.css('float')) || (/inline|table-cell/).test(item.css('display'));
@@ -161,24 +198,9 @@ angular.module('ui.sortable', [])
161198

162199
// If this list has a placeholder (the connected lists won't),
163200
// don't inlcude it in saved nodes.
164-
var placeholder = element.sortable('option','placeholder');
165-
166-
// placeholder.element will be a function if the placeholder, has
167-
// been created (placeholder will be an object). If it hasn't
168-
// been created, either placeholder will be false if no
169-
// placeholder class was given or placeholder.element will be
170-
// undefined if a class was given (placeholder will be a string)
171-
if (placeholder && placeholder.element && typeof placeholder.element === 'function') {
172-
var phElement = placeholder.element();
173-
// workaround for jquery ui 1.9.x,
174-
// not returning jquery collection
175-
phElement = angular.element(phElement);
176-
177-
// exact match with the placeholder's class attribute to handle
178-
// the case that multiple connected sortables exist and
179-
// the placehoilder option equals the class of sortable items
180-
var excludes = element.find('[class="' + phElement.attr('class') + '"]:not([ng-repeat], [data-ng-repeat])');
181-
201+
var placeholder = getPlaceholderElement(element);
202+
if (placeholder && placeholder.length) {
203+
var excludes = getPlaceholderExcludesludes(element, placeholder);
182204
savedNodes = savedNodes.not(excludes);
183205
}
184206

@@ -217,11 +239,11 @@ angular.module('ui.sortable', [])
217239
// the start and stop of repeat sections and sortable doesn't
218240
// respect their order (even if we cancel, the order of the
219241
// comments are still messed up).
220-
if (hasSortingHelper(element, ui) && !ui.item.sortable.received &&
221-
element.sortable( 'option', 'appendTo' ) === 'parent') {
222-
// restore all the savedNodes except .ui-sortable-helper element
223-
// (which is placed last). That way it will be garbage collected.
224-
savedNodes = savedNodes.not(savedNodes.last());
242+
var sortingHelper = !ui.item.sortable.received && getSortingHelper(element, ui, savedNodes);
243+
if (sortingHelper && sortingHelper.length) {
244+
// Restore all the savedNodes except from the sorting helper element.
245+
// That way it will be garbage collected.
246+
savedNodes = savedNodes.not(sortingHelper);
225247
}
226248
savedNodes.appendTo(element);
227249

@@ -264,12 +286,14 @@ angular.module('ui.sortable', [])
264286
if ((!('dropindex' in ui.item.sortable) || ui.item.sortable.isCanceled()) &&
265287
!angular.equals(element.contents(), savedNodes)) {
266288

267-
if (hasSortingHelper(element, ui) && element.sortable( 'option', 'appendTo' ) === 'parent') {
268-
// restore all the savedNodes except .ui-sortable-helper element
269-
// (which is placed last). That way it will be garbage collected.
270-
savedNodes = savedNodes.not(savedNodes.last());
289+
var sortingHelper = getSortingHelper(element, ui, savedNodes);
290+
if (sortingHelper && sortingHelper.length) {
291+
// Restore all the savedNodes except from the sorting helper element.
292+
// That way it will be garbage collected.
293+
savedNodes = savedNodes.not(sortingHelper);
271294
}
272295
savedNodes.appendTo(element);
296+
savedNodes.appendTo(element);
273297
}
274298
}
275299

0 commit comments

Comments
 (0)