WIP: Cherry-pick b2d82b6 - Using ofnSelect2 instead of plain select on BOM

This commit is contained in:
Rohan Mitchell
2016-08-31 17:21:06 +10:00
parent 46fcf7b62e
commit d574b8943b
8 changed files with 33 additions and 46 deletions

View File

@@ -4,16 +4,11 @@ angular.module("admin.enterprises").factory 'Enterprises', ($q, EnterpriseResour
pristineByID: {}
index: (params={}, callback=null) ->
includeBlank = !!params['includeBlank']
delete params['includeBlank']
EnterpriseResource.index(params, (data) =>
for enterprise in data
@enterprisesByID[enterprise.id] = enterprise
@pristineByID[enterprise.id] = angular.copy(enterprise)
(callback || angular.noop)(data)
data.unshift(blankOption()) if includeBlank
data
)

View File

@@ -12,8 +12,10 @@ angular.module("admin.indexUtils").directive "ofnSelect2", ($sanitize, $timeout,
$timeout ->
scope.text ||= 'name'
scope.filter ||= -> true
scope.data.unshift(scope.blank) if scope.blank? && typeof scope.blank is "object"
item.name = $sanitize(item.name) for item in scope.data
element.select2
minimumResultsForSearch: scope.minSearch || 0

View File

@@ -3,7 +3,8 @@ angular.module("admin.indexUtils").factory 'Dereferencer', ->
dereference: (array, data)->
if array
for object, i in array
array[i] = data[object.id]
match = data[object.id]
array[i] = match if match?
dereferenceAttr: (array, attr, data)->
if array

View File

@@ -15,13 +15,13 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
LineItems.allSaved() || confirm(t("unsaved_changes_warning"))
$scope.resetSelectFilters = ->
$scope.distributorFilter = blankOption().id
$scope.supplierFilter = blankOption().id
$scope.orderCycleFilter = blankOption().id
$scope.distributorFilter = 0
$scope.supplierFilter = 0
$scope.orderCycleFilter = 0
$scope.quickSearch = ""
$scope.refreshData = ->
unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == "0"
unless !$scope.orderCycleFilter? || $scope.orderCycleFilter == 0
$scope.startDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].first_order
$scope.endDate = OrderCycles.orderCyclesByID[$scope.orderCycleFilter].last_order
@@ -29,9 +29,9 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gt]": "#{parseDate($scope.startDate)}", "q[order][completed_at_lt]": "#{parseDate($scope.endDate)}")
unless $scope.initialized
RequestMonitor.load $scope.distributors = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
RequestMonitor.load $scope.orderCycles = OrderCycles.index(includeBlank: true, ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{daysFromToday(-90)}")
RequestMonitor.load $scope.suppliers = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")
RequestMonitor.load $scope.distributors = Enterprises.index(action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
RequestMonitor.load $scope.orderCycles = OrderCycles.index(ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{daysFromToday(-90)}")
RequestMonitor.load $scope.suppliers = Enterprises.index(action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")
RequestMonitor.load $q.all([$scope.orders.$promise, $scope.distributors.$promise, $scope.orderCycles.$promise]).then ->
Dereferencer.dereferenceAttr $scope.orders, "distributor", Enterprises.enterprisesByID

View File

@@ -2,7 +2,7 @@ angular.module("admin.lineItems").filter "selectFilter", (blankOption, RequestMo
return (lineItems,selectedSupplier,selectedDistributor,selectedOrderCycle) ->
filtered = []
unless RequestMonitor.loading
filtered.push lineItem for lineItem in lineItems when (angular.equals(selectedSupplier,"0") || lineItem.supplier.id == selectedSupplier) &&
(angular.equals(selectedDistributor,"0") || lineItem.order.distributor.id == selectedDistributor) &&
(angular.equals(selectedOrderCycle,"0") || lineItem.order.order_cycle.id == selectedOrderCycle)
filtered.push lineItem for lineItem in lineItems when (angular.equals(selectedSupplier,0) || lineItem.supplier.id == selectedSupplier) &&
(angular.equals(selectedDistributor,0) || lineItem.order.distributor.id == selectedDistributor) &&
(angular.equals(selectedOrderCycle,0) || lineItem.order.order_cycle.id == selectedOrderCycle)
filtered

View File

@@ -31,17 +31,17 @@
%label{ :for => 'supplier_filter' }
= t("admin.producer")
%br
%select{ :class => "three columns alpha", :id => 'supplier_filter', 'select2-min-search' => 5, 'ng-model' => 'supplierFilter', 'ng-options' => 's.id as s.name for s in suppliers' }
%input#supplier_filter.ofn-select2.fullwidth{ type: 'number', 'min-search' => 5, data: 'suppliers', blank: "{ id: 0, name: 'All' }", ng: { model: 'supplierFilter' } }
.filter_select{ :class => "three columns" }
%label{ :for => 'distributor_filter' }
= t("admin.shop")
%br
%select{ :class => "three columns alpha", :id => 'distributor_filter', 'select2-min-search' => 5, 'ng-model' => 'distributorFilter', 'ng-options' => 'd.id as d.name for d in distributors'}
%input#distributor_filter.ofn-select2.fullwidth{ type: 'number', 'min-search' => 5, data: 'distributors', blank: "{ id: 0, name: 'All' }", ng: { model: 'distributorFilter' } }
.filter_select{ :class => "three columns" }
%label{ :for => 'order_cycle_filter' }
= t("admin.order_cycle")
%br
%select{ :class => "three columns alpha", :id => 'order_cycle_filter', 'select2-min-search' => 5, 'ng-model' => 'orderCycleFilter', 'ng-options' => 'oc.id as oc.name for oc in orderCycles', 'confirm-change' => "confirmRefresh()", 'ng-change' => 'refreshData()'}
%input#order_cycle_filter.ofn-select2.fullwidth{ type: 'number', 'min-search' => 5, data: 'orderCycles', blank: "{ id: 0, name: 'All' }", on: { selecting: "confirmRefresh" }, ng: { model: 'orderCycleFilter', change: 'refreshData()' } }
.filter_clear{ :class => "two columns omega" }
%label{ :for => 'clear_all_filters' }
%br

View File

@@ -37,25 +37,14 @@ describe "Enterprises service", ->
expect(result).toDeepEqual response
describe "when params are passed", ->
describe "where includeBlank param is truthy", ->
beforeEach ->
params = {includeBlank: true, someParam: 'someVal'}
$httpBackend.expectGET('/admin/enterprises.json?someParam=someVal').respond 200, response
result = Enterprises.index(params)
$httpBackend.flush()
beforeEach ->
params = { someParam: 'someVal'}
$httpBackend.expectGET('/admin/enterprises.json?someParam=someVal').respond 200, response
result = Enterprises.index(params)
$httpBackend.flush()
it "returns an array of enterprises, with a blank option appended to the beginning", ->
expect(result).toDeepEqual [{id: '0', name: 'All'} ,{ id: 5, name: 'Enterprise 1'}]
describe "where includeBlank param is falsey", ->
beforeEach ->
params = {includeBlank: false, someParam: 'someVal'}
$httpBackend.expectGET('/admin/enterprises.json?someParam=someVal').respond 200, response
result = Enterprises.index(params)
$httpBackend.flush()
it "returns an array of enterprises, with a blank option appended to the beginning", ->
expect(result).toDeepEqual response
it "returns an array of enterprises", ->
expect(result).toDeepEqual response
describe "#save", ->

View File

@@ -63,17 +63,17 @@ describe "LineItemsCtrl", ->
$timeout.flush()
describe "initialisation", ->
it "gets suppliers, adds a blank option as the first in the list", ->
expect(scope.suppliers).toDeepEqual [ { id : '0', name : 'All' }, supplier ]
it "gets suppliers", ->
expect(scope.suppliers).toDeepEqual [supplier ]
it "gets distributors, adds a blank option as the first in the list", ->
expect(scope.distributors).toDeepEqual [ { id : '0', name : 'All' }, distributor ]
it "gets distributors", ->
expect(scope.distributors).toDeepEqual [ distributor ]
it "stores enterprises in an list that is accessible by id", ->
expect(Enterprises.enterprisesByID[1]).toDeepEqual supplier
it "gets order cycles, adds a blank option as the first in the list", ->
expect(scope.orderCycles).toDeepEqual [ { id : '0', name : 'All' }, orderCycle ]
it "gets order cycles", ->
expect(scope.orderCycles).toDeepEqual [ orderCycle ]
it "gets orders, with dereferenced order cycles and distributors", ->
expect(scope.orders).toDeepEqual [ { id: 9, order_cycle: orderCycle, distributor: distributor, number: "R123456" } ]
@@ -85,9 +85,9 @@ describe "LineItemsCtrl", ->
expect(scope.RequestMonitor.loading).toBe false
it "resets the select filters", ->
expect(scope.distributorFilter).toBe '0'
expect(scope.supplierFilter).toBe '0'
expect(scope.orderCycleFilter).toBe '0'
expect(scope.distributorFilter).toBe 0
expect(scope.supplierFilter).toBe 0
expect(scope.orderCycleFilter).toBe 0
expect(scope.quickSearch).toBe = ""
it "resets the form state to pristine", ->