mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Move dynamic placeholder logic to directive so that it can be used for master variants too
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
angular.module("ofn.admin")
|
||||
.controller "VariantUnitsCtrl", ($scope, optionValueNamer) ->
|
||||
$scope.$watchCollection '[variant.unit_value_with_description, product.variant_unit_name, product.variant_unit_with_scale]', ->
|
||||
[variant_unit, variant_unit_scale] = $scope.productUnitProperties()
|
||||
[unit_value, unit_description] = $scope.variantUnitProperties(variant_unit_scale)
|
||||
variant_object =
|
||||
unit_value: unit_value
|
||||
unit_description: unit_description
|
||||
product:
|
||||
variant_unit_scale: variant_unit_scale
|
||||
variant_unit: variant_unit
|
||||
variant_unit_name: $scope.product.variant_unit_name
|
||||
|
||||
$scope.variant.options_text = new optionValueNamer(variant_object).name()
|
||||
|
||||
$scope.productUnitProperties = ->
|
||||
# get relevant product properties
|
||||
if $scope.product.variant_unit_with_scale
|
||||
match = $scope.product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/)
|
||||
if match
|
||||
variant_unit = match[1]
|
||||
variant_unit_scale = parseFloat(match[2])
|
||||
else
|
||||
variant_unit = $scope.product.variant_unit_with_scale
|
||||
variant_unit_scale = null
|
||||
else
|
||||
variant_unit = variant_unit_scale = null
|
||||
|
||||
[variant_unit, variant_unit_scale]
|
||||
|
||||
$scope.variantUnitProperties = (variant_unit_scale)->
|
||||
# get relevant variant properties
|
||||
if $scope.variant.hasOwnProperty("unit_value_with_description")
|
||||
match = $scope.variant.unit_value_with_description.match(/^([\d\.]+(?= |$)|)( |)(.*)$/)
|
||||
if match
|
||||
unit_value = parseFloat(match[1])
|
||||
unit_value = null if isNaN(unit_value)
|
||||
unit_value *= variant_unit_scale if unit_value && variant_unit_scale
|
||||
unit_description = match[3]
|
||||
[unit_value, unit_description]
|
||||
|
||||
|
||||
48
app/assets/javascripts/admin/directives/display_as.js.coffee
Normal file
48
app/assets/javascripts/admin/directives/display_as.js.coffee
Normal file
@@ -0,0 +1,48 @@
|
||||
angular.module("ofn.admin").directive "ofnDisplayAs", (optionValueNamer) ->
|
||||
link: (scope, element, attrs) ->
|
||||
|
||||
scope.$watchCollection ->
|
||||
return [
|
||||
scope.$eval(attrs.ofnDisplayAs).unit_value_with_description
|
||||
scope.product.variant_unit_name
|
||||
scope.product.variant_unit_with_scale
|
||||
]
|
||||
, ->
|
||||
[variant_unit, variant_unit_scale] = productUnitProperties()
|
||||
[unit_value, unit_description] = variantUnitProperties(variant_unit_scale)
|
||||
variant_object =
|
||||
unit_value: unit_value
|
||||
unit_description: unit_description
|
||||
product:
|
||||
variant_unit_scale: variant_unit_scale
|
||||
variant_unit: variant_unit
|
||||
variant_unit_name: scope.product.variant_unit_name
|
||||
|
||||
scope.placeholder_text = new optionValueNamer(variant_object).name()
|
||||
|
||||
productUnitProperties = ->
|
||||
# get relevant product properties
|
||||
if scope.product.variant_unit_with_scale?
|
||||
match = scope.product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/)
|
||||
if match
|
||||
variant_unit = match[1]
|
||||
variant_unit_scale = parseFloat(match[2])
|
||||
else
|
||||
variant_unit = scope.product.variant_unit_with_scale
|
||||
variant_unit_scale = null
|
||||
else
|
||||
variant_unit = variant_unit_scale = null
|
||||
|
||||
[variant_unit, variant_unit_scale]
|
||||
|
||||
variantUnitProperties = (variant_unit_scale)->
|
||||
# get relevant variant properties
|
||||
variant = scope.$eval(attrs.ofnDisplayAs) # Like this so we can switch between 'master' and 'variant'
|
||||
if variant.unit_value_with_description?
|
||||
match = variant.unit_value_with_description.match(/^([\d\.]+(?= |$)|)( |)(.*)$/)
|
||||
if match
|
||||
unit_value = parseFloat(match[1])
|
||||
unit_value = null if isNaN(unit_value)
|
||||
unit_value *= variant_unit_scale if unit_value && variant_unit_scale
|
||||
unit_description = match[3]
|
||||
[unit_value, unit_description]
|
||||
@@ -0,0 +1,8 @@
|
||||
angular.module("ofn.admin").directive "ofnMaintainUnitScale", ->
|
||||
require: "ngModel"
|
||||
link: (scope, element, attrs, ngModel) ->
|
||||
scope.$watch 'product.variant_unit_with_scale', (newValue, oldValue) ->
|
||||
if not (oldValue == newValue)
|
||||
# Triggers track-variant directive to track the unit_value, so that changes to the unit are passed to the server
|
||||
ngModel.$setViewValue ngModel.$viewValue
|
||||
|
||||
@@ -127,10 +127,10 @@
|
||||
%td.unit{ 'ng-show' => 'columns.unit.visible' }
|
||||
%select.select2{ 'ng-model' => 'product.variant_unit_with_scale', :name => 'variant_unit_with_scale', 'ofn-track-product' => 'variant_unit_with_scale', 'ng-options' => 'unit[1] as unit[0] for unit in variant_unit_options' }
|
||||
%option{'value' => '', 'ng-hide' => "hasVariants(product) && hasUnit(product)"}
|
||||
%input{ 'ng-model' => 'product.master.unit_value_with_description', :name => 'master_unit_value_with_description', 'ofn-track-master' => 'unit_value_with_description', :type => 'text', :placeholder => 'value', 'ng-show' => "!hasVariants(product) && hasUnit(product)" }
|
||||
%input{ 'ng-model' => 'product.master.unit_value_with_description', :name => 'master_unit_value_with_description', 'ofn-track-master' => 'unit_value_with_description', :type => 'text', :placeholder => 'value', 'ng-show' => "!hasVariants(product) && hasUnit(product)", 'ofn-maintain-unit-scale' => true }
|
||||
%input{ 'ng-model' => 'product.variant_unit_name', :name => 'variant_unit_name', 'ofn-track-product' => 'variant_unit_name', :placeholder => 'unit', 'ng-show' => "product.variant_unit_with_scale == 'items'", :type => 'text' }
|
||||
%td.display_as{ 'ng-show' => 'columns.unit.visible' }
|
||||
%input{ name: 'display_as', 'ofn-track-master' => 'display_as', :type => 'text', placeholder: "{{ product.master.options_text }}", ng: { hide: 'hasVariants(product)', model: 'product.master.display_as' } }
|
||||
%input{ 'ofn-display-as' => 'product.master', name: 'display_as', 'ofn-track-master' => 'display_as', type: 'text', placeholder: '{{ placeholder_text }}', ng: { hide: 'hasVariants(product)', model: 'product.master.display_as' } }
|
||||
%td{ 'ng-show' => 'columns.price.visible' }
|
||||
%input{ 'ng-model' => 'product.price', 'ofn-decimal' => :true, :name => 'price', 'ofn-track-product' => 'price', :type => 'text', 'ng-hide' => 'hasVariants(product)' }
|
||||
%td{ 'ng-show' => 'columns.on_hand.visible' }
|
||||
@@ -154,9 +154,9 @@
|
||||
%td{ 'ng-show' => 'columns.name.visible' }
|
||||
%input{ 'ng-model' => 'variant.display_name', :name => 'variant_display_name', 'ofn-track-variant' => 'display_name', :type => 'text', placeholder: "{{ product.name }}" }
|
||||
%td.unit_value{ 'ng-show' => 'columns.unit.visible' }
|
||||
%input{ 'ng-model' => 'variant.unit_value_with_description', :name => 'variant_unit_value_with_description', 'ofn-track-variant' => 'unit_value_with_description', :type => 'text' }
|
||||
%td.display_as{ 'ng-show' => 'columns.unit.visible', 'ng-controller' => "VariantUnitsCtrl" }
|
||||
%input{ 'ng-model' => 'variant.display_as', :name => 'variant_display_as', 'ofn-track-variant' => 'display_as', :type => 'text', placeholder: "{{ variant.options_text }}" }
|
||||
%input{ 'ng-model' => 'variant.unit_value_with_description', :name => 'variant_unit_value_with_description', 'ofn-track-variant' => 'unit_value_with_description', :type => 'text', 'ofn-maintain-unit-scale' => true }
|
||||
%td.display_as{ 'ng-show' => 'columns.unit.visible' }
|
||||
%input{ 'ofn-display-as' => 'variant', 'ng-model' => 'variant.display_as', name: 'variant_display_as', 'ofn-track-variant' => 'display_as', type: 'text', placeholder: '{{ placeholder_text }}' }
|
||||
%td{ 'ng-show' => 'columns.price.visible' }
|
||||
%input{ 'ng-model' => 'variant.price', 'ofn-decimal' => :true, :name => 'variant_price', 'ofn-track-variant' => 'price', :type => 'text' }
|
||||
%td{ 'ng-show' => 'columns.on_hand.visible' }
|
||||
|
||||
@@ -202,6 +202,8 @@ describe "filtering products for submission to database", ->
|
||||
price: 10.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
display_as: "bottle"
|
||||
display_name: "nothing"
|
||||
]
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1
|
||||
@@ -215,15 +217,19 @@ describe "filtering products for submission to database", ->
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_name: 'loaf'
|
||||
unit_value: 250
|
||||
unit_description: "foo"
|
||||
available_on: available_on
|
||||
master_attributes:
|
||||
id: 2
|
||||
unit_value: 250
|
||||
unit_description: "foo"
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
on_hand: 2
|
||||
price: 10.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
display_as: "bottle"
|
||||
display_name: "nothing"
|
||||
]
|
||||
]
|
||||
|
||||
@@ -956,8 +962,8 @@ describe "AdminProductEditCtrl", ->
|
||||
expect(product).toEqual
|
||||
id: 123
|
||||
variants: [
|
||||
{id: -1, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null}
|
||||
{id: -2, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null}
|
||||
{id: -1, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null}
|
||||
{id: -2, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null}
|
||||
]
|
||||
|
||||
it "shows the variant(s)", ->
|
||||
|
||||
Reference in New Issue
Block a user