Skip to content

Commit

Permalink
Fixes #4702: Only include selected repositories in UI when filtering …
Browse files Browse the repository at this point in the history
…for content view.
  • Loading branch information
ehelms committed Jun 23, 2014
1 parent bcf1dd7 commit 75f4ee5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ angular.module('Bastion.content-views').controller('ContentViewAvailableReposito
$scope.repositoriesTable = nutupane.table;

$scope.addRepositories = function (contentView) {
var selected = nutupane.getAllSelectedResults().included.ids;
var selected = $scope.getSelected(nutupane);

contentView['repository_ids'] = contentView['repository_ids'].concat(selected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ angular.module('Bastion.content-views').controller('ContentViewRepositoriesListC
$scope.repositoriesTable = nutupane.table;

$scope.removeRepositories = function () {
var selected = nutupane.getAllSelectedResults().included.ids,
ids = [];
var ids = [],
selected = $scope.getSelected(nutupane);

angular.forEach($scope.contentView['repository_ids'], function (id) {
if (selected.indexOf(id) === -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ angular.module('Bastion.content-views').service('ContentViewRepositoriesUtil',

scope.product = {id: 'all'};
scope.products = {};
scope.filteredItems = [];

scope.$watch('repositoriesTable.rows', function (repositories) {
scope.products = extractProducts(repositories);
Expand All @@ -46,6 +47,17 @@ angular.module('Bastion.content-views').service('ContentViewRepositoriesUtil',
return include;
};

scope.getSelected = function (nutupane) {
var selected = nutupane.getAllSelectedResults().included.ids,
filtered = _.pluck(scope.filteredItems, 'id');

selected = _.reject(selected, function (id) {
return !_.contains(filtered, id);
});

return selected;
};

function extractProducts(repositories) {
var products = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3 translate>
<tbody>
<tr alch-table-row
row-select="repository"
ng-repeat="repository in repositoriesTable.rows | filter:repositorySearch | filter:repositoryFilter">
ng-repeat="repository in repositoriesTable.rows | filter:repositorySearch | filter:repositoryFilter | as:'filteredItems'">
<td alch-table-cell>
<a ui-sref="products.details.repositories.info({productId: repository.product.id, repositoryId: repository.id})">
{{ repository.name }}
Expand Down
33 changes: 33 additions & 0 deletions engines/bastion/app/assets/javascripts/bastion/utils/as.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2014 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public
* License as published by the Free Software Foundation; either version
* 2 of the License (GPLv2) or (at your option) any later version.
* There is NO WARRANTY for this software, express or implied,
* including the implied warranties of MERCHANTABILITY,
* NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
* have received a copy of GPLv2 along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
**/

/**
* @ngdoc filter
* @name Bastion.utils.filter:as
*
* @requires $parse
*
* @description
* Adds variable to scope with the value passed in. This allows adding to the
* scope a variable that contains the result of multiple applied filters.
*
* @example
* <ul>
<li ng-repeat="item in items | filter:customFilter | as:filteredItems"></li>
</ul>
*/
angular.module('Bastion.utils').filter('as', ['$parse', function ($parse) {
return function (value, path) {
return $parse(path).assign(this, value);
};
}]);
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ describe('Controller: ContentViewAvailableRepositoriesController', function() {
});

it('provides a method to add repositories to a content view', function() {
$scope.filteredItems = [{id: 1}];
$scope.addRepositories($scope.contentView);

expect($scope.save).toHaveBeenCalled();
expect($scope.contentView['repository_ids'].length).toBe(2);
expect($scope.contentView['repository_ids'].length).toBe(1);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ describe('Controller: ContentViewRepositoriesListController', function() {
});

it('provides a method to add repositories to a content view', function() {
$scope.filteredItems = [{id: 1}];
$scope.removeRepositories($scope.contentView);

expect($scope.save).toHaveBeenCalled();
expect($scope.contentView['repository_ids'].length).toBe(1);
expect($scope.contentView['repository_ids'].length).toBe(2);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ describe('Service: ContentViewRepositoriesService', function() {
expect(Object.keys($scope.products).length).toBe(2);
});

it('should provide a method to get all selected repositories', function () {
var Nutupane = function () {
this.getAllSelectedResults = function () {
return {included: {ids: [1, 2]}};
};

this.table = {};
},
nutupane = new Nutupane();

$scope.filteredItems = [{id: 1}];

expect($scope.getSelected(nutupane).length).toBe(1);
});

});
33 changes: 33 additions & 0 deletions engines/bastion/test/utils/as.filter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2014 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public
* License as published by the Free Software Foundation; either version
* 2 of the License (GPLv2) or (at your option) any later version.
* There is NO WARRANTY for this software, express or implied,
* including the implied warranties of MERCHANTABILITY,
* NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
* have received a copy of GPLv2 along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*/

describe('Filter:as', function() {
var array, scope = {};

beforeEach(module('Bastion.utils'));

beforeEach(inject(function($filter) {
array = [
{id: 1, name: 'one'},
{id: 2, name: 'two'},
{id: 3, name: 'three'}
];
scope.asFilter = $filter('as')
}));

it("should set items to the value of array", function() {
expect(scope.asFilter(array, 'items')).toEqual(array);
expect(scope.items).toEqual(array);
});

});

0 comments on commit 75f4ee5

Please sign in to comment.