Skip to content

Instantly share code, notes, and snippets.

@martin-wintz
Last active December 10, 2015 09:39
Show Gist options
  • Save martin-wintz/4416122 to your computer and use it in GitHub Desktop.
Save martin-wintz/4416122 to your computer and use it in GitHub Desktop.
Changes to ui-sortable for compatibility with ConnectWith option.
onStart = (e, ui) ->
# Save position of dragged item, original model, and original element parent
ui.item.data('ui-sortable-start', ui.item.index())
ui.item.data('ui-sortable-start-model', ngModel)
ui.item.data('ui-sortable-start-parent', ui.item.context.parentElement)
onUpdate = (e, ui) ->
# Fetch saved and current position of dropped element
start = ui.item.data('ui-sortable-start')
end = ui.item.index()
# Only sort if the original and target parent elements are the same
startParent = ui.item.data('ui-sortable-start-parent')
if startParent is ui.item.context.parentElement
# Reorder array and apply change to scope
ngModel.$modelValue.splice(end, 0, ngModel.$modelValue.splice(start, 1)[0])
onReceive = (e, ui) ->
start = ui.item.data('ui-sortable-start')
end = ui.item.index()
startModel = ui.item.data('ui-sortable-start-model')
# Remove the element from the start array and insert it in this one in the correct order
ngModel.$modelValue.splice(end, 0, startModel.$modelValue.splice(start, 1)[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment