mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Simplify new product form
This commit is contained in:
@@ -15,5 +15,6 @@
|
||||
//= require admin/spree_auth
|
||||
//= require admin/spree_promo
|
||||
//= require ./admin
|
||||
//= require ./products/products
|
||||
|
||||
//= require_tree .
|
||||
|
||||
1
app/assets/javascripts/admin/products/products.js.coffee
Normal file
1
app/assets/javascripts/admin/products/products.js.coffee
Normal file
@@ -0,0 +1 @@
|
||||
angular.module("admin.products", [])
|
||||
@@ -0,0 +1,46 @@
|
||||
angular.module("admin.products")
|
||||
.controller "unitsCtrl", ($scope) ->
|
||||
$scope.product = { master: {} }
|
||||
|
||||
$scope.$watch ->
|
||||
$scope.product.variant_unit_with_scale
|
||||
, ->
|
||||
if $scope.product.variant_unit_with_scale
|
||||
match = $scope.product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/)
|
||||
if match
|
||||
$scope.product.variant_unit = match[1]
|
||||
$scope.product.variant_unit_scale = parseFloat(match[2])
|
||||
else
|
||||
$scope.product.variant_unit = $scope.product.variant_unit_with_scale
|
||||
$scope.product.variant_unit_scale = null
|
||||
else
|
||||
$scope.product.variant_unit = $scope.product.variant_unit_scale = null
|
||||
|
||||
$scope.$watch ->
|
||||
$scope.product.master.unit_value_with_description
|
||||
, ->
|
||||
if $scope.product.master.hasOwnProperty("unit_value_with_description")
|
||||
match = $scope.product.master.unit_value_with_description.match(/^([\d\.]+(?= |$)|)( |)(.*)$/)
|
||||
if match
|
||||
$scope.product.master.unit_value = parseFloat(match[1])
|
||||
$scope.product.master.unit_value = null if isNaN($scope.product.master.unit_value)
|
||||
$scope.product.master.unit_value *= $scope.product.variant_unit_scale if $scope.product.master.unit_value && $scope.product.variant_unit_scale
|
||||
$scope.product.master.unit_description = match[3]
|
||||
|
||||
$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 (ML)", "volume_1000000"],
|
||||
["Items", "items"]
|
||||
]
|
||||
|
||||
$scope.hasVariants = (product) ->
|
||||
Object.keys(product.variants).length > 0
|
||||
|
||||
$scope.hasUnit = (product) ->
|
||||
product.variant_unit_with_scale?
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/ replace "fieldset[data-hook='new_product']"
|
||||
|
||||
%fieldset{ id: "new_product" }
|
||||
%legend{align: "center"}= t(:new_product)
|
||||
.row
|
||||
= f.field_container :name do
|
||||
= f.label :name, t(:name)
|
||||
%span.required *
|
||||
%br/
|
||||
= f.text_field :name, :class => 'fullwidth title'
|
||||
= f.error_message_on :name
|
||||
.row
|
||||
.alpha.six.columns
|
||||
= f.field_container :supplier do
|
||||
= f.label :supplier
|
||||
%span.required *
|
||||
= f.collection_select(:supplier_id, Enterprise.is_primary_producer.managed_by(spree_current_user).by_name, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"})
|
||||
= f.error_message_on :supplier
|
||||
.five.columns
|
||||
= f.field_container :price do
|
||||
= f.label :price, t(:price)
|
||||
%span.required *
|
||||
%br/
|
||||
= f.text_field :price, :class => 'fullwidth'
|
||||
= f.error_message_on :price
|
||||
.five.columns.omega
|
||||
= f.field_container :on_hand do
|
||||
= f.label :on_hand, t(:on_hand)
|
||||
%span.required *
|
||||
%br/
|
||||
= f.text_field :on_hand, :class => 'fullwidth'
|
||||
= f.error_message_on :on_hand
|
||||
.row{ 'ng-controller' => 'unitsCtrl' }
|
||||
.six.columns.alpha
|
||||
= f.label :variant_unit_with_scale, :units
|
||||
%select.select2.fullwidth{ id: 'product_variant_unit_with_scale', 'ng-model' => 'product.variant_unit_with_scale', 'ng-options' => 'unit[1] as unit[0] for unit in variant_unit_options' }
|
||||
%option{'value' => '', 'ng-hide' => "hasUnit(product)"}
|
||||
%input{ type: 'hidden', 'ng-value' => 'product.variant_unit', name: 'product[variant_unit]' }
|
||||
%input{ type: 'hidden', 'ng-value' => 'product.variant_unit_scale', name: 'product[variant_unit_scale]' }
|
||||
.five.columns
|
||||
= f.label :product_unit_value_with_description, :value
|
||||
%input.fullwidth{ id: 'product_unit_value_with_description', 'ng-model' => 'product.master.unit_value_with_description', :type => 'text', :placeholder => 'eg. 2', 'ng-disabled' => "!hasUnit(product)" }
|
||||
%input{ type: 'hidden', 'ng-value' => 'product.master.unit_value', name: 'product[unit_value]' }
|
||||
%input{ type: 'hidden', 'ng-value' => 'product.master.unit_description', name: 'product[unit_description]' }
|
||||
.five.columns.omega{ 'ng-show' => "product.variant_unit_with_scale == 'items'" }
|
||||
= f.label :product_variant_unit_name, :unit_name
|
||||
%input.fullwidth{ id: 'product_variant_unit_name','ng-model' => 'product.variant_unit_name', :name => 'product[variant_unit_name]', :placeholder => 'eg. bunches',bu :type => 'text' }
|
||||
#product-from-prototype.clearfix{"data-hook" => "product-from-prototype"}
|
||||
= render :file => 'spree/admin/prototypes/show' if @prototype
|
||||
= render :partial => 'spree/admin/shared/new_resource_links'
|
||||
|
||||
|
||||
:javascript
|
||||
angular.element(document.getElementById("new_product")).ready(function() {
|
||||
angular.bootstrap(document.getElementById("new_product"), ['admin.products']);
|
||||
});
|
||||
Reference in New Issue
Block a user