Add ability to reset filters to BPE

This commit is contained in:
Rob H
2014-08-06 13:27:17 +10:00
parent 37ecadf6fc
commit d27908fd2f
6 changed files with 46 additions and 118 deletions

View File

@@ -32,6 +32,10 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", [
$scope.producers = producers
$scope.taxons = Taxons.taxons
$scope.filterProducers = [{id: "0", name: ""}].concat $scope.producers
$scope.filterTaxons = [{id: "0", name: ""}].concat $scope.taxons
$scope.producerFilter = "0"
$scope.categoryFilter = "0"
$scope.products = []
$scope.filteredProducts = []
$scope.currentFilters = []
@@ -148,29 +152,10 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", [
tab.visible = !tab.visible
$scope.visibleTab = tab
$scope.addFilter = (filter) ->
existingfilterIndex = $scope.indexOfFilter filter
if $scope.filterableColumns.indexOf(filter.property) >= 0 && $scope.filterTypes.indexOf(filter.predicate) >= 0 && filter.value != "" && filter.value != undefined
if (DirtyProducts.count() > 0 and confirm("Unsaved changes will be lost. Continue anyway?")) or (DirtyProducts.count() == 0)
if existingfilterIndex == -1
$scope.currentFilters.push filter
$scope.fetchProducts()
else if confirm("'#{filter.predicate.name}' filter already exists on column '#{filter.property.name}'. Replace it?")
$scope.currentFilters[existingfilterIndex] = filter
$scope.fetchProducts()
else
alert("Please ensure all filter fields are filled in before adding a filter.")
$scope.removeFilter = (filter) ->
index = $scope.currentFilters.indexOf(filter)
if index != -1
$scope.currentFilters.splice index, 1
$scope.fetchProducts()
$scope.indexOfFilter = (filter) ->
for existingFilter, i in $scope.currentFilters
return i if filter.property == existingFilter.property && filter.predicate == existingFilter.predicate
return -1
$scope.resetSelectFilters = ->
$scope.query = ""
$scope.producerFilter = "0"
$scope.categoryFilter = "0"
$scope.editWarn = (product, variant) ->
if (DirtyProducts.count() > 0 and confirm("Unsaved changes will be lost. Continue anyway?")) or (DirtyProducts.count() == 0)

View File

@@ -0,0 +1,4 @@
angular.module("ofn.admin").filter "category", ($filter) ->
return (products, taxonID) ->
return products if taxonID == "0"
return $filter('filter')( products, { category: taxonID }, true )

View File

@@ -0,0 +1,4 @@
angular.module("ofn.admin").filter "producer", ($filter) ->
return (products, producerID) ->
return products if producerID == "0"
$filter('filter')( products, { producer: producerID }, true )

View File

@@ -29,11 +29,11 @@
.filter_select{ :class => "three columns" }
%label{ :for => 'producer_filter' }Producer
%br
%select{ :class => "three columns alpha", :id => 'producer_filter', 'ofn-select2-min-search' => 5, 'ng-model' => 'producerFilter', 'ng-options' => 'producer.id as producer.name for producer in producers' }
%select{ :class => "three columns alpha", :id => 'producer_filter', 'ofn-select2-min-search' => 5, 'ng-model' => 'producerFilter', 'ng-options' => 'producer.id as producer.name for producer in filterProducers' }
.filter_select{ :class => "three columns" }
%label{ :for => 'taxon_filter' }Category
%br
%select{ :class => "three columns alpha", :id => 'category_filter', 'ofn-select2-min-search' => 5, 'ng-model' => 'categoryFilter', 'ng-options' => 'taxon.id as taxon.name for taxon in taxons'}
%select{ :class => "three columns alpha", :id => 'category_filter', 'ofn-select2-min-search' => 5, 'ng-model' => 'categoryFilter', 'ng-options' => 'taxon.id as taxon.name for taxon in filterTaxons'}
.filter_clear{ :class => "two columns omega" }
%label{ :for => 'clear_all_filters' }
%br
@@ -78,7 +78,7 @@
%th.actions
%th.actions
%th.actions
%tbody{ 'ng-repeat' => 'product in filteredProducts = ( products | filter:query | filter:{producer: producerFilter}:true | filter:{category: categoryFilter}:true | limitTo:limit )', 'ng-class-even' => "'even'", 'ng-class-odd' => "'odd'" }
%tbody{ 'ng-repeat' => 'product in filteredProducts = ( products | filter:query | producer: producerFilter | category: categoryFilter | limitTo:limit )', 'ng-class-even' => "'even'", 'ng-class-odd' => "'odd'" }
%tr.product{ :id => "p_{{product.id}}" }
%td.left-actions
%a{ 'ofn-toggle-variants' => 'true', :class => "view-variants icon-chevron-right", 'ng-show' => 'hasVariants(product)' }

View File

@@ -555,7 +555,9 @@ feature %q{
expect(page).to have_selector "a.delete-product", :count => 3
first("a.delete-product").click
within "tr#p_#{p1.id}" do
first("a.delete-product").click
end
expect(page).to have_selector "a.delete-product", :count => 2
@@ -576,7 +578,9 @@ feature %q{
expect(page).to have_selector "a.delete-variant", :count => 3
first("a.delete-variant").click
within "tr#v_#{v1.id}" do
first("a.delete-variant").click
end
expect(page).to have_selector "a.delete-variant", :count => 2
@@ -599,7 +603,9 @@ feature %q{
expect(page).to have_selector "a.edit-product", :count => 3
first("a.edit-product").click
within "tr#p_#{p1.id}" do
first("a.edit-product").click
end
URI.parse(current_url).path.should == "/admin/products/#{p1.permalink}/edit"
end
@@ -668,7 +674,7 @@ feature %q{
expect(page).to have_selector "th", :text => "ON HAND"
expect(page).to have_selector "th", :text => "AV. ON"
first("div#columns_dropdown div.menu div.menu_item", text: /.{0,1}Producer/).click
first("div#columns_dropdown div.menu div.menu_item", text: /^.{0,1}Producer$/).click
expect(page).to have_no_selector "th", :text => "PRODUCER"
expect(page).to have_selector "th", :text => "NAME"
@@ -696,11 +702,19 @@ feature %q{
expect(page).to have_field "product_name", with: p1.name
expect(page).to have_field "product_name", with: p2.name
# Set a filter
select2_select s1.name, from: "producer_filter"
# Products are hidden when filtered out
expect(page).to have_field "product_name", with: p1.name
expect(page).to have_no_field "product_name", with: p2.name
# Clearing filters
click_button "Clear All"
# All products are shown again
expect(page).to have_field "product_name", with: p1.name
expect(page).to have_field "product_name", with: p2.name
end
end
end

View File

@@ -1094,94 +1094,15 @@ describe "AdminProductEditCtrl", ->
describe "filtering products", ->
describe "adding a filter to the filter list", ->
filterObject1 = filterObject2 = null
beforeEach ->
spyOn($scope, "fetchProducts").andReturn "nothing"
spyOn(DirtyProducts, "count").andReturn 0
filterObject1 = {property: $scope.filterableColumns[0], predicate: $scope.filterTypes[0], value: "value1"}
filterObject2 = {property: $scope.filterableColumns[1], predicate: $scope.filterTypes[1], value: "value2"}
$scope.addFilter filterObject1
$scope.addFilter filterObject2
it "adds objects sent to addFilter() to $scope.currentFilters", ->
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
it "ignores objects sent to addFilter() which do not contain a 'property' with a corresponding key in filterableColumns", ->
filterObject3 = {property: "some_random_property", predicate: $scope.filterTypes[0], value: "value3"}
$scope.addFilter filterObject3
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
it "ignores objects sent to addFilter() which do not contain a query with a corresponding key in filterTypes", ->
filterObject3 = {property: $scope.filterableColumns[0], predicate: "something", value: "value3"}
$scope.addFilter filterObject3
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
it "ignores objects sent to addFilter() which have a blank 'value' property", ->
filterObject3 = {property: $scope.filterableColumns[0], predicate: $scope.filterTypes[1], value: ""}
$scope.addFilter filterObject3
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
it "calls fetchProducts when adding a new filter", ->
expect($scope.fetchProducts.calls.length).toEqual(2)
describe "when unsaved products exist", ->
beforeEach ->
filterObject3 = {property: $scope.filterableColumns[0], predicate: $scope.filterTypes[1], value: "value3"}
spyOn(window, "confirm").andReturn false
DirtyProducts.count.andReturn 1
$scope.addFilter filterObject3
it "it does not call fetchProducts", ->
expect($scope.fetchProducts.calls.length).toEqual(2)
it "does not add the filter to $scope.currentFilters", ->
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
it "asks the user to save changes before proceeding", ->
expect(window.confirm).toHaveBeenCalledWith "Unsaved changes will be lost. Continue anyway?"
describe "when a filter on the same property and predicate already exists", ->
filterObject3 = null
beforeEach ->
filterObject3 = { property: filterObject2.property, predicate: filterObject2.predicate, value: "new value" }
it "asks the user for permission before proceeding", ->
spyOn(window, "confirm").andReturn true
$scope.addFilter filterObject3
expect(window.confirm).toHaveBeenCalledWith "'#{filterObject3.predicate.name}' filter already exists on column '#{filterObject3.property.name}'. Replace it?"
it "replaces the filter in $scope.currentFilters when user clicks OK", ->
spyOn(window, "confirm").andReturn true
$scope.addFilter filterObject3
expect($scope.currentFilters).toEqual [filterObject1, filterObject3]
it "does not add the filter to $scope.currentFilters when user clicks cancel", ->
spyOn(window, "confirm").andReturn false
$scope.addFilter filterObject3
expect($scope.currentFilters).toEqual [filterObject1, filterObject2]
describe "removing a filter from the filter list", ->
filterObject1 = filterObject2 = null
beforeEach ->
spyOn($scope, "fetchProducts").andReturn "nothing"
filterObject1 = {property: $scope.filterableColumns[0], predicate: $scope.filterTypes[0], value: "Product1"}
filterObject2 = {property: $scope.filterableColumns[0], predicate: $scope.filterTypes[0], value: "Product2"}
$scope.currentFilters = [ filterObject1, filterObject2 ]
it "removes the specified filter from $scope.currentFilters and calls fetchProducts", ->
$scope.removeFilter filterObject1
expect($scope.currentFilters).toEqual [ filterObject2 ]
expect($scope.fetchProducts.calls.length).toEqual 1
it "ignores filters which do not exist in currentFilters", ->
filterObject3 = {property: $scope.filterableColumns[1], predicate: $scope.filterTypes[1], value: "SomethingElse"}
$scope.removeFilter filterObject3
expect($scope.currentFilters).toEqual [ filterObject1, filterObject2 ]
expect($scope.fetchProducts.calls.length).toEqual 0
describe "clearing filters", ->
it "resets filter variables", ->
$scope.query = "lala"
$scope.producerFilter = "5"
$scope.categoryFilter = "6"
$scope.resetSelectFilters()
expect($scope.query).toBe ""
expect($scope.producerFilter).toBe "0"
expect($scope.categoryFilter).toBe "0"
describe "converting arrays of objects with ids to an object with ids as keys", ->