From 9451f1b66d590a5f90aec7ab46a9c6b83ded4999 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 22 Nov 2019 17:58:08 +0000 Subject: [PATCH] Remove funky panel open listener code and initialize the panel data with a ng-init in the panel template --- .../directives/panel_ctrl.js.coffee | 2 +- .../order_cycles/controllers/edit.js.coffee | 4 +--- .../controllers/incoming_controller.js.coffee | 2 +- .../order_cycle_exchanges_controller.js.coffee | 18 +++++------------- .../controllers/outgoing_controller.js.coffee | 2 +- .../exchange_distributed_products.html.haml | 2 +- .../exchange_supplied_products.html.haml | 2 +- .../order_cycles/_exchange_form.html.haml | 4 ++-- .../controllers/edit_spec.js.coffee | 1 - .../incoming_controller_spec.js.coffee | 3 +-- .../outgoing_controller_spec.js.coffee | 3 +-- 11 files changed, 15 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/admin/index_utils/directives/panel_ctrl.js.coffee b/app/assets/javascripts/admin/index_utils/directives/panel_ctrl.js.coffee index d2e541992c..4259ce63e2 100644 --- a/app/assets/javascripts/admin/index_utils/directives/panel_ctrl.js.coffee +++ b/app/assets/javascripts/admin/index_utils/directives/panel_ctrl.js.coffee @@ -13,7 +13,7 @@ angular.module("admin.indexUtils").directive "panelCtrl", (Panels) -> this.registerSelectionListener = (callback) -> $scope.$on "selection:changed", (event, selection) -> - callback(selection, $element) + callback(selection) this diff --git a/app/assets/javascripts/admin/order_cycles/controllers/edit.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/edit.js.coffee index 0730f1e0a3..73a6b3fcba 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/edit.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/edit.js.coffee @@ -3,9 +3,7 @@ angular.module('admin.orderCycles') $controller('AdminOrderCycleBasicCtrl', {$scope: $scope, ocInstance: ocInstance}) order_cycle_id = $location.absUrl().match(/\/admin\/order_cycles\/(\d+)/)[1] - $scope.order_cycle = OrderCycle.load order_cycle_id, -> - $scope.exchangeListChanged() if $scope.exchangeListChanged? - + $scope.order_cycle = OrderCycle.load(order_cycle_id) $scope.enterprises = Enterprise.index(order_cycle_id: order_cycle_id) $scope.enterprise_fees = EnterpriseFee.index(order_cycle_id: order_cycle_id) diff --git a/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee index e3bb5055f2..5e34e42c72 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/incoming_controller.js.coffee @@ -24,4 +24,4 @@ angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($ $scope.addSupplier = ($event) -> $event.preventDefault() - OrderCycle.addSupplier $scope.new_supplier_id, $scope.exchangeListChanged + OrderCycle.addSupplier $scope.new_supplier_id diff --git a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee index d4046f381a..066bcaf25c 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee @@ -1,5 +1,5 @@ angular.module('admin.orderCycles') - .controller 'AdminOrderCycleExchangesCtrl', ($scope, $controller, $filter, $window, $location, OrderCycle, ExchangeProduct, Enterprise, EnterpriseFee, Schedules, RequestMonitor, ocInstance, StatusMessage) -> + .controller 'AdminOrderCycleExchangesCtrl', ($scope, $controller, $filter, $window, $location, $timeout, OrderCycle, ExchangeProduct, Enterprise, EnterpriseFee, Schedules, RequestMonitor, ocInstance, StatusMessage) -> $controller('AdminEditOrderCycleCtrl', {$scope: $scope, ocInstance: ocInstance, $location: $location}) $scope.supplier_enterprises = Enterprise.producer_enterprises @@ -45,17 +45,9 @@ angular.module('admin.orderCycles') ExchangeProduct.index params, (products) -> $scope.enterprises[exchange.enterprise_id].supplied_products = products - # Register listeners to capture first toggle open of the products panel of the exchange - $scope.exchangeListChanged = -> - panelRows = angular.element(".panel-row") - for panelRow in panelRows - panelCtrl = angular.element(panelRow).controller('panelCtrl') - panelCtrl.registerSelectionListener($scope.initializeExchangeProductsPanel) - # initialize exchange products panel if not yet done $scope.exchangeProdutsPanelInitialized = [] - $scope.initializeExchangeProductsPanel = (selection, $element) -> - scope = $element.scope() - return if $scope.exchangeProdutsPanelInitialized[scope.exchange.enterprise_id] - $scope.loadExchangeProducts(scope.exchange) - $scope.exchangeProdutsPanelInitialized[scope.exchange.enterprise_id] = true + $scope.initializeExchangeProductsPanel = (exchange) -> + return if $scope.exchangeProdutsPanelInitialized[exchange.enterprise_id] + $scope.loadExchangeProducts(exchange) + $scope.exchangeProdutsPanelInitialized[exchange.enterprise_id] = true diff --git a/app/assets/javascripts/admin/order_cycles/controllers/outgoing_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/outgoing_controller.js.coffee index e7f193de47..157a723672 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/outgoing_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/outgoing_controller.js.coffee @@ -11,7 +11,7 @@ angular.module('admin.orderCycles').controller 'AdminOrderCycleOutgoingCtrl', ($ $scope.addDistributor = ($event) -> $event.preventDefault() - OrderCycle.addDistributor $scope.new_distributor_id, $scope.exchangeListChanged + OrderCycle.addDistributor $scope.new_distributor_id $scope.submit = ($event, destination) -> $event.preventDefault() diff --git a/app/assets/javascripts/templates/admin/panels/exchange_distributed_products.html.haml b/app/assets/javascripts/templates/admin/panels/exchange_distributed_products.html.haml index 5fe23e6826..4dad8310ce 100644 --- a/app/assets/javascripts/templates/admin/panels/exchange_distributed_products.html.haml +++ b/app/assets/javascripts/templates/admin/panels/exchange_distributed_products.html.haml @@ -1,4 +1,4 @@ -.row.exchange-distributed-products +.row.exchange-distributed-products{'ng-init' => 'initializeExchangeProductsPanel(exchange)'} .sixteen.columns.alpha.omega .exchange-select-all-variants %label diff --git a/app/assets/javascripts/templates/admin/panels/exchange_supplied_products.html.haml b/app/assets/javascripts/templates/admin/panels/exchange_supplied_products.html.haml index 10950258f9..e6c80e973a 100644 --- a/app/assets/javascripts/templates/admin/panels/exchange_supplied_products.html.haml +++ b/app/assets/javascripts/templates/admin/panels/exchange_supplied_products.html.haml @@ -1,4 +1,4 @@ -.row.exchange-supplied-products +.row.exchange-supplied-products{'ng-init' => 'initializeExchangeProductsPanel(exchange)'} .sixteen.columns.alpha.omega .exchange-select-all-variants %label diff --git a/app/views/admin/order_cycles/_exchange_form.html.haml b/app/views/admin/order_cycles/_exchange_form.html.haml index e844ffc6aa..63a44e9a3b 100644 --- a/app/views/admin/order_cycles/_exchange_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_form.html.haml @@ -35,10 +35,10 @@ - if type == 'supplier' %tr.panel-row{ object: "exchange", panels: "{products: 'exchange_supplied_products'}", - locals: "$index,order_cycle,exchange,enterprises,setExchangeVariants,suppliedVariants,removeDistributionOfVariant", + locals: "$index,order_cycle,exchange,enterprises,setExchangeVariants,suppliedVariants,removeDistributionOfVariant,initializeExchangeProductsPanel", colspan: 4 } - if type == 'distributor' %tr.panel-row{ object: "exchange", panels: "{products: 'exchange_distributed_products', tags: 'exchange_tags'}", - locals: "$index,order_cycle,exchange,enterprises,setExchangeVariants,incomingExchangeVariantsFor,variantSuppliedToOrderCycle", + locals: "$index,order_cycle,exchange,enterprises,setExchangeVariants,incomingExchangeVariantsFor,variantSuppliedToOrderCycle,initializeExchangeProductsPanel", colspan: 5 } diff --git a/spec/javascripts/unit/admin/order_cycles/controllers/edit_spec.js.coffee b/spec/javascripts/unit/admin/order_cycles/controllers/edit_spec.js.coffee index 8258dd248a..bd60efb299 100644 --- a/spec/javascripts/unit/admin/order_cycles/controllers/edit_spec.js.coffee +++ b/spec/javascripts/unit/admin/order_cycles/controllers/edit_spec.js.coffee @@ -11,7 +11,6 @@ describe 'AdminEditOrderCycleCtrl', -> scope = order_cycle_form: jasmine.createSpyObj('order_cycle_form', ['$dirty', '$setPristine']) $watch: jasmine.createSpy('$watch') - exchangeListChanged: jasmine.createSpy('exchangeListChanged') event = preventDefault: jasmine.createSpy('preventDefault') location = diff --git a/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee b/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee index 0543dfd2ac..e650317e44 100644 --- a/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/order_cycles/controllers/incoming_controller_spec.js.coffee @@ -10,7 +10,6 @@ describe 'AdminOrderCycleIncomingCtrl', -> beforeEach -> scope = $watch: jasmine.createSpy('$watch') - exchangeListChanged: jasmine.createSpy('exchangeListChanged') location = absUrl: -> 'example.com/admin/order_cycles/27/edit' @@ -45,4 +44,4 @@ describe 'AdminOrderCycleIncomingCtrl', -> scope.addSupplier(event) expect(event.preventDefault).toHaveBeenCalled() - expect(OrderCycle.addSupplier).toHaveBeenCalledWith('new supplier id', scope.exchangeListChanged) + expect(OrderCycle.addSupplier).toHaveBeenCalledWith('new supplier id') diff --git a/spec/javascripts/unit/admin/order_cycles/controllers/outgoing_controller_spec.js.coffee b/spec/javascripts/unit/admin/order_cycles/controllers/outgoing_controller_spec.js.coffee index 880ec2d928..5268a9e8f4 100644 --- a/spec/javascripts/unit/admin/order_cycles/controllers/outgoing_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/order_cycles/controllers/outgoing_controller_spec.js.coffee @@ -10,7 +10,6 @@ describe 'AdminOrderCycleOutgoingCtrl', -> beforeEach -> scope = $watch: jasmine.createSpy('$watch') - exchangeListChanged: jasmine.createSpy('exchangeListChanged') location = absUrl: -> 'example.com/admin/order_cycles/27/edit' @@ -35,4 +34,4 @@ describe 'AdminOrderCycleOutgoingCtrl', -> scope.addDistributor(event) expect(event.preventDefault).toHaveBeenCalled() - expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id', scope.exchangeListChanged) + expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id')