diff --git a/modules/directives/sortable/sortable.coffee b/modules/directives/sortable/sortable.coffee index cb1c870..1abe2a1 100644 --- a/modules/directives/sortable/sortable.coffee +++ b/modules/directives/sortable/sortable.coffee @@ -21,9 +21,30 @@ angular.module('ui.directives').directive 'uiSortable', ['ui.config', (uiConfig) # Fetch saved and current position of dropped element start = ui.item.data('ui-sortable-start') end = ui.item.index() + ui.item.indexs =ui.item.index() + + # if item is received not sort it again as it is in correct position + if ui.item.received == true + ui.item.received = false + else + # Reorder array and apply change to scope + ngModel.$modelValue.splice(end, 0, ngModel.$modelValue.splice(start, 1)[0]) + + scope.$apply() + + onReceive = (e, ui) -> + # added item to array into correct position and set up flag + ngModel.$modelValue.splice(ui.item.indexs,0,ui.item.connectedData) + ui.item.received = true; + scope.$apply() - # Reorder array and apply change to scope - ngModel.$modelValue.splice(end, 0, ngModel.$modelValue.splice(start, 1)[0]) + onRemove = (e, ui) -> + # copy data into item + if ngModel.$modelValue.length == 1 + ui.item.connectedData = ngModel.$modelValue.splice(0,1)[0] + else + ui.item.connectedData = ngModel.$modelValue.splice(ui.item.index(),1)[0] + scope.$apply() # If user provided 'start' callback compose it with onStart function @@ -40,6 +61,20 @@ angular.module('ui.directives').directive 'uiSortable', ['ui.config', (uiConfig) _update?(e, ui) scope.$apply() + # If user provided 'receive' callback compose it with onReceive function + _receive = opts.receive + opts.receive = (e, ui) -> + onReceive(e, ui) + _receive?(e, ui) + scope.$apply(); + + # If user provided 'remove' callback compose it with onRemove function + _remove = opts.remove + opts.remove = (e, ui) -> + onRemove(e, ui) + _remove?(e, ui) + scope.$apply(); + # Create sortable element.sortable(opts)