mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-13 04:00:21 +00:00
Adding tags to variant overrides
This commit is contained in:
@@ -6,11 +6,20 @@ angular.module("admin.utils").directive "tagsWithTranslation", ($timeout) ->
|
||||
tagsAttr: "@?"
|
||||
tagListAttr: "@?"
|
||||
findTags: "&"
|
||||
form: '=?'
|
||||
link: (scope, element, attrs) ->
|
||||
$timeout ->
|
||||
scope.tagsAttr ||= "tags"
|
||||
scope.tagListAttr ||= "tag_list"
|
||||
|
||||
watchString = "object.#{scope.tagsAttr}"
|
||||
scope.$watchCollection watchString, ->
|
||||
compileTagList = ->
|
||||
scope.object[scope.tagListAttr] = (tag.text for tag in scope.object[scope.tagsAttr]).join(",")
|
||||
|
||||
scope.tagAdded = ->
|
||||
compileTagList()
|
||||
|
||||
scope.tagRemoved = ->
|
||||
# For some reason the tags input doesn't mark the form
|
||||
# as dirty when a tag is removed, which breaks the save bar
|
||||
scope.form.$setDirty(true) if typeof scope.form isnt 'undefined'
|
||||
compileTagList()
|
||||
|
||||
@@ -6,7 +6,6 @@ angular.module("admin.variantOverrides").directive "trackInheritance", (VariantO
|
||||
|
||||
ngModel.$parsers.push (viewValue) ->
|
||||
if ngModel.$dirty && viewValue
|
||||
variantOverride = VariantOverrides.inherit(scope.hub_id, scope.variant.id)
|
||||
DirtyVariantOverrides.add variantOverride
|
||||
DirtyVariantOverrides.inherit scope.hub_id, scope.variant.id, scope.variantOverrides[scope.hub_id][scope.variant.id].id
|
||||
scope.displayDirty()
|
||||
viewValue
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
angular.module("admin.variantOverrides").directive "trackTagList", (VariantOverrides, DirtyVariantOverrides) ->
|
||||
link: (scope, element, attrs) ->
|
||||
watchString = "variantOverrides[#{scope.hub_id}][#{scope.variant.id}].tag_list"
|
||||
scope.$watch watchString, (newValue, oldValue) ->
|
||||
if typeof newValue isnt 'undefined' && newValue != oldValue
|
||||
scope.inherit = false
|
||||
vo_id = scope.variantOverrides[scope.hub_id][scope.variant.id].id
|
||||
DirtyVariantOverrides.set scope.hub_id, scope.variant.id, vo_id, 'tag_list', newValue
|
||||
scope.displayDirty()
|
||||
@@ -3,8 +3,8 @@ angular.module("admin.variantOverrides").directive "ofnTrackVariantOverride", (D
|
||||
link: (scope, element, attrs, ngModel) ->
|
||||
ngModel.$parsers.push (viewValue) ->
|
||||
if ngModel.$dirty
|
||||
variantOverride = scope.variantOverrides[scope.hub_id][scope.variant.id]
|
||||
scope.inherit = false
|
||||
DirtyVariantOverrides.add variantOverride
|
||||
vo_id = scope.variantOverrides[scope.hub_id][scope.variant.id].id
|
||||
DirtyVariantOverrides.set scope.hub_id, scope.variant.id, vo_id, attrs.ofnTrackVariantOverride, viewValue
|
||||
scope.displayDirty()
|
||||
viewValue
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http) ->
|
||||
angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http, VariantOverrides) ->
|
||||
new class DirtyVariantOverrides
|
||||
dirtyVariantOverrides: {}
|
||||
|
||||
add: (vo) ->
|
||||
@dirtyVariantOverrides[vo.hub_id] ||= {}
|
||||
@dirtyVariantOverrides[vo.hub_id][vo.variant_id] = vo
|
||||
add: (hub_id, variant_id, vo_id) ->
|
||||
@dirtyVariantOverrides[hub_id] ||= {}
|
||||
@dirtyVariantOverrides[hub_id][variant_id] ||=
|
||||
{ id: vo_id, variant_id: variant_id, hub_id: hub_id }
|
||||
|
||||
set: (hub_id, variant_id, vo_id, attr, value) ->
|
||||
if attr in @requiredAttrs()
|
||||
@add(hub_id, variant_id, vo_id)
|
||||
@dirtyVariantOverrides[hub_id][variant_id][attr] = value
|
||||
|
||||
inherit: (hub_id, variant_id, vo_id) ->
|
||||
@add(hub_id, variant_id, vo_id)
|
||||
blankVo = angular.copy(VariantOverrides.inherit(hub_id, variant_id))
|
||||
delete blankVo[attr] for attr, value of blankVo when attr not in @requiredAttrs()
|
||||
@dirtyVariantOverrides[hub_id][variant_id] = blankVo
|
||||
|
||||
count: ->
|
||||
count = 0
|
||||
@@ -27,3 +39,6 @@ angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http
|
||||
url: "/admin/variant_overrides/bulk_update"
|
||||
data:
|
||||
variant_overrides: @all()
|
||||
|
||||
requiredAttrs: ->
|
||||
['id','hub_id','variant_id','sku','price','count_on_hand','on_demand','default_stock','resettable','tag_list']
|
||||
|
||||
@@ -30,6 +30,8 @@ angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOve
|
||||
on_demand: null
|
||||
default_stock: null
|
||||
resettable: false
|
||||
tag_list: ''
|
||||
tags: []
|
||||
|
||||
updateIds: (updatedVos) ->
|
||||
for vo in updatedVos
|
||||
|
||||
@@ -1 +1 @@
|
||||
angular.module("admin.variantOverrides", ["admin.indexUtils", "admin.utils", "admin.dropdown", "admin.inventoryItems"])
|
||||
angular.module("admin.variantOverrides", ["admin.indexUtils", "admin.utils", "admin.dropdown", "admin.inventoryItems", 'ngTagsInput'])
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
%tags-input{ template: 'admin/tag.html', ng: { model: 'object[tagsAttr]' } }
|
||||
%tags-input{ template: 'admin/tag.html', ng: { model: 'object[tagsAttr]' }, on: { tag: { added: 'tagAdded()', removed:'tagRemoved()' } } }
|
||||
%auto-complete{source: "findTags({query: $query})",
|
||||
template: "admin/tag_autocomplete.html",
|
||||
"min-length" => "0",
|
||||
|
||||
Reference in New Issue
Block a user