mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Adding dynamic text to display as placeholder
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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]
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
angular.module("ofn.admin").factory "optionValueNamer", ($resource) ->
|
||||
class OptionValueNamer
|
||||
constructor: (@variant) ->
|
||||
|
||||
name: ->
|
||||
[value, unit] = @option_value_value_unit()
|
||||
separator = if @value_scaled() then '' else ' '
|
||||
|
||||
name_fields = []
|
||||
name_fields.push "#{value}#{separator}#{unit}" if value? && unit?
|
||||
name_fields.push @variant.unit_description if @variant.unit_description?
|
||||
name_fields.join ' '
|
||||
|
||||
value_scaled: ->
|
||||
@variant.product.variant_unit_scale?
|
||||
|
||||
option_value_value_unit: ->
|
||||
if @variant.unit_value?
|
||||
if @variant.product.variant_unit in ["weight", "volume"]
|
||||
[value, unit_name] = @option_value_value_unit_scaled()
|
||||
|
||||
else
|
||||
value = @variant.unit_value
|
||||
unit_name = @variant.product.variant_unit_name
|
||||
# TODO needs to add pluralize to line below
|
||||
# unit_name = unit_name if value > 1
|
||||
|
||||
value = parseInt(value, 10) if value == parseInt(value, 10)
|
||||
|
||||
else
|
||||
value = unit_name = null
|
||||
|
||||
[value, unit_name]
|
||||
|
||||
option_value_value_unit_scaled: ->
|
||||
[unit_scale, unit_name] = @scale_for_unit_value()
|
||||
|
||||
value = @variant.unit_value / unit_scale
|
||||
|
||||
[value, unit_name]
|
||||
|
||||
scale_for_unit_value: ->
|
||||
units =
|
||||
'weight':
|
||||
1.0: 'g'
|
||||
1000.0: 'kg'
|
||||
1000000.0: 'T'
|
||||
'volume':
|
||||
0.001: 'mL'
|
||||
1.0: 'L'
|
||||
1000000.0: 'ML'
|
||||
|
||||
# Find the largest available unit where unit_value comes to >= 1 when expressed in it.
|
||||
# If there is none available where this is true, use the smallest available unit.
|
||||
unit = ([scale, unit_name] for scale, unit_name of units[@variant.product.variant_unit] when @variant.unit_value / scale >= 1).reduce (unit, [scale, unit_name]) ->
|
||||
if (unit && scale > unit[0]) || !unit?
|
||||
[scale, unit_name]
|
||||
else
|
||||
unit
|
||||
, null
|
||||
if !unit?
|
||||
unit = ([scale, unit_name] for scale, unit_name of units[@variant.product.variant_unit]).reduce (unit, [scale, unit_name]) ->
|
||||
if scale < unit[0] then [scale, unit_name] else unit
|
||||
, [Infinity,""]
|
||||
|
||||
unit
|
||||
@@ -155,7 +155,7 @@
|
||||
%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' }
|
||||
%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 }}" }
|
||||
%td{ 'ng-show' => 'columns.price.visible' }
|
||||
%input{ 'ng-model' => 'variant.price', 'ofn-decimal' => :true, :name => 'variant_price', 'ofn-track-variant' => 'price', :type => 'text' }
|
||||
|
||||
Reference in New Issue
Block a user