Tag Rules can be rearranged in UI to set priority

This commit is contained in:
Rob Harrington
2016-05-27 13:53:25 +10:00
parent cb9e3b43f9
commit a1c7a44fa0
12 changed files with 95 additions and 32 deletions

View File

@@ -0,0 +1,36 @@
angular.module("admin.utils").directive "ofnSortable", ($timeout, $parse) ->
restrict: "E"
scope:
items: '@'
position: '@'
afterSort: '&'
handle: "@"
axis: "@"
link: (scope, element, attrs) ->
$timeout ->
scope.axis ||= "y"
scope.handle ||= ".handle"
getScopePos = $parse(scope.position)
setScopePos = getScopePos.assign
element.sortable
handle: scope.handle
helper: 'clone'
axis: scope.axis
items: scope.items
appendTo: element
update: (event, ui) ->
sortableSiblings = ($(ss) for ss in ui.item.siblings(scope.items))
offset = Math.min(ui.item.index(), sortableSiblings[0].index())
newPos = ui.item.index() - offset + 1
oldPos = getScopePos(ui.item.scope())
if newPos < oldPos
for sibScope in sortableSiblings.map((ss) -> ss.scope())
pos = getScopePos(sibScope)
setScopePos(sibScope, pos + 1) if pos >= newPos && pos < oldPos
else if newPos > oldPos
for sibScope in sortableSiblings.map((ss) -> ss.scope())
pos = getScopePos(sibScope)
setScopePos(sibScope, pos - 1) if pos > oldPos && pos <= newPos
setScopePos(ui.item.scope(), newPos)
scope.afterSort()