From 91e4f24fdec1d984bb6f9e96707068c2630e16dd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 17 Jul 2014 16:57:23 +1000 Subject: [PATCH] Extract JS unit options into option value namer --- .../admin/bulk_order_management.js.coffee | 17 +++++--------- .../admin/bulk_product_update.js.coffee | 14 +++-------- .../admin/products/units_controller.js.coffee | 10 +------- .../services/option_value_namer.js.coffee | 23 +++++++++++++++++++ 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/app/assets/javascripts/admin/bulk_order_management.js.coffee b/app/assets/javascripts/admin/bulk_order_management.js.coffee index 55672b6113..6681bea8bf 100644 --- a/app/assets/javascripts/admin/bulk_order_management.js.coffee +++ b/app/assets/javascripts/admin/bulk_order_management.js.coffee @@ -1,6 +1,6 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [ - "$scope", "$http", "dataFetcher", "blankOption", "pendingChanges" - ($scope, $http, dataFetcher, blankOption, pendingChanges) -> + "$scope", "$http", "dataFetcher", "blankOption", "pendingChanges", "optionValueNamer", + ($scope, $http, dataFetcher, blankOption, pendingChanges, optionValueNamer) -> $scope.initialiseVariables = -> start = daysFromToday -7 @@ -137,21 +137,16 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [ $scope.getScale = (value, unitType) -> scaledValue = null validScales = [] - unitScales = - 'weight': [1.0, 1000.0, 1000000.0] - 'volume': [0.001, 1.0, 1000.0] + unitScales = optionValueNamer.unitScales(unitType) - validScales.unshift scale for scale in unitScales[unitType] when value/scale >= 1 + validScales.unshift scale for scale in unitScales when value/scale >= 1 if validScales.length > 0 validScales[0] else - unitScales[unitType][0] + unitScales[0] $scope.getUnitName = (scale, unitType) -> - unitNames = - 'weight': {1.0: 'g', 1000.0: 'kg', 1000000.0: 'T'} - 'volume': {0.001: 'mL', 1.0: 'L', 1000.0: 'kL'} - unitNames[unitType][scale] + optionValueNamer.getUnitName(scale, unitType) $scope.formattedValueWithUnitName = (value, unitsProduct, unitsVariant) -> # A Units Variant is an API object which holds unit properies of a variant diff --git a/app/assets/javascripts/admin/bulk_product_update.js.coffee b/app/assets/javascripts/admin/bulk_product_update.js.coffee index 941e74bb8c..2f24ed8556 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js.coffee +++ b/app/assets/javascripts/admin/bulk_product_update.js.coffee @@ -1,6 +1,6 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", [ - "$scope", "$timeout", "$http", "dataFetcher", "DirtyProducts" - ($scope, $timeout, $http, dataFetcher, DirtyProducts) -> + "$scope", "$timeout", "$http", "dataFetcher", "DirtyProducts", "optionValueNamer", + ($scope, $timeout, $http, dataFetcher, DirtyProducts, optionValueNamer) -> $scope.updateStatusMessage = text: "" style: {} @@ -14,15 +14,7 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", [ taxons: {name: "Taxons", visible: false} available_on: {name: "Available On", visible: false} - $scope.variant_unit_options = [ - ["Weight (g)", "weight_1"], - ["Weight (kg)", "weight_1000"], - ["Weight (T)", "weight_1000000"], - ["Volume (mL)", "volume_0.001"], - ["Volume (L)", "volume_1"], - ["Volume (kL)", "volume_1000"], - ["Items", "items"] - ] + $scope.variant_unit_options = optionValueNamer.variant_unit_options $scope.filterableColumns = [ { name: "Supplier", db_column: "supplier_name" }, diff --git a/app/assets/javascripts/admin/products/units_controller.js.coffee b/app/assets/javascripts/admin/products/units_controller.js.coffee index 3664950772..e2d9c923a0 100644 --- a/app/assets/javascripts/admin/products/units_controller.js.coffee +++ b/app/assets/javascripts/admin/products/units_controller.js.coffee @@ -26,15 +26,7 @@ angular.module("admin.products") $scope.placeholder_text = new optionValueNamer($scope.product.master).name() - $scope.variant_unit_options = [ - ["Weight (g)", "weight_1"], - ["Weight (kg)", "weight_1000"], - ["Weight (T)", "weight_1000000"], - ["Volume (mL)", "volume_0.001"], - ["Volume (L)", "volume_1"], - ["Volume (kL)", "volume_1000"], - ["Items", "items"] - ] + $scope.variant_unit_options = optionValueNamer.variant_unit_options $scope.hasVariants = (product) -> Object.keys(product.variants).length > 0 diff --git a/app/assets/javascripts/admin/services/option_value_namer.js.coffee b/app/assets/javascripts/admin/services/option_value_namer.js.coffee index 6a14dbd324..d05b154433 100644 --- a/app/assets/javascripts/admin/services/option_value_namer.js.coffee +++ b/app/assets/javascripts/admin/services/option_value_namer.js.coffee @@ -1,5 +1,28 @@ angular.module("admin.products").factory "optionValueNamer", -> class OptionValueNamer + @getUnitName: (scale, unitType) -> + unitNames = + 'weight': {1.0: 'g', 1000.0: 'kg', 1000000.0: 'T'} + 'volume': {0.001: 'mL', 1.0: 'L', 1000.0: 'kL'} + unitNames[unitType][scale] + + @unitScales: (unitType) -> + unitScales = + 'weight': [1.0, 1000.0, 1000000.0] + 'volume': [0.001, 1.0, 1000.0] + unitScales[unitType] + + @variant_unit_options: [ + ["Weight (g)", "weight_1"], + ["Weight (kg)", "weight_1000"], + ["Weight (T)", "weight_1000000"], + ["Volume (mL)", "volume_0.001"], + ["Volume (L)", "volume_1"], + ["Volume (kL)", "volume_1000"], + ["Items", "items"] + ] + + constructor: (@variant) -> name: ->