mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-17 00:07:24 +00:00
Tag Rules can be rearranged in UI to set priority
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
angular.module("admin.tagRules").controller "TagRulesCtrl", ($scope, $http, enterprise) ->
|
||||
angular.module("admin.tagRules").controller "TagRulesCtrl", ($scope, $http, $filter, enterprise) ->
|
||||
$scope.tagGroups = enterprise.tag_groups
|
||||
$scope.defaultTagGroup = enterprise.default_tag_group
|
||||
|
||||
$scope.visibilityOptions = [ { id: "visible", name: "VISIBLE" }, { id: "hidden", name: "NOT VISIBLE" } ]
|
||||
|
||||
updateRuleCounts = ->
|
||||
$scope.updateRuleCounts = ->
|
||||
index = $scope.defaultTagGroup.rules.length
|
||||
for tagGroup in $scope.tagGroups
|
||||
for tagGroup in $filter('orderBy')($scope.tagGroups, 'position')
|
||||
tagGroup.startIndex = index
|
||||
index = index + tagGroup.rules.length
|
||||
|
||||
updateRuleCounts()
|
||||
$scope.updateRuleCounts()
|
||||
|
||||
$scope.updateTagsRulesFor = (tagGroup) ->
|
||||
for tagRule in tagGroup.rules
|
||||
@@ -38,17 +38,17 @@ angular.module("admin.tagRules").controller "TagRulesCtrl", ($scope, $http, ente
|
||||
newRule.peferred_exchange_tags = []
|
||||
newRule.preferred_matched_order_cycles_visibility = "visible"
|
||||
tagGroup.rules.push(newRule)
|
||||
updateRuleCounts()
|
||||
$scope.updateRuleCounts()
|
||||
|
||||
$scope.addNewTag = ->
|
||||
$scope.tagGroups.push { tags: [], rules: [] }
|
||||
$scope.tagGroups.push { tags: [], rules: [], position: $scope.tagGroups.length + 1 }
|
||||
|
||||
$scope.deleteTagRule = (tagGroup, tagRule) ->
|
||||
index = tagGroup.rules.indexOf(tagRule)
|
||||
return unless index >= 0
|
||||
if tagRule.id is null
|
||||
tagGroup.rules.splice(index, 1)
|
||||
updateRuleCounts()
|
||||
$scope.updateRuleCounts()
|
||||
else
|
||||
if confirm("Are you sure?")
|
||||
$http
|
||||
@@ -56,4 +56,4 @@ angular.module("admin.tagRules").controller "TagRulesCtrl", ($scope, $http, ente
|
||||
url: "/admin/enterprises/#{enterprise.id}/tag_rules/#{tagRule.id}.json"
|
||||
.success ->
|
||||
tagGroup.rules.splice(index, 1)
|
||||
updateRuleCounts()
|
||||
$scope.updateRuleCounts()
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user