Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Fix filter conflict #663

Merged
merged 2 commits into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
var tag = nga.entity('tags')
.readOnly(); // a readOnly entity has disabled creation, edition, and deletion views

var subCategories = [
{ category: 'tech', label: 'Computers', value: 'computers' },
{ category: 'tech', label: 'Gadgets', value: 'gadgets' },
{ category: 'lifestyle', label: 'Travel', value: 'travel' },
{ category: 'lifestyle', label: 'Fitness', value: 'fitness' }
];

// set the application entities
admin
.addEntity(post)
Expand All @@ -87,6 +94,13 @@
.targetEntity(tag) // the tag entity is defined later in this file
.targetField(nga.field('name')) // the field to be displayed in this list
])
.filters([
nga.field('category', 'choice').choices([
{ label: 'Tech', value: 'tech' },
{ label: 'Lifestyle', value: 'lifestyle' }
]).label('Category'),
nga.field('subcategory', 'choice').choices(subCategories).label('Subcategory')
])
.listActions(['show', 'edit', 'delete']);

post.creationView()
Expand All @@ -99,13 +113,6 @@
nga.field('published_at', 'date') // Date field type translates to a datepicker
]);

var subCategories = [
{ category: 'tech', label: 'Computers', value: 'computers' },
{ category: 'tech', label: 'Gadgets', value: 'gadgets' },
{ category: 'lifestyle', label: 'Travel', value: 'travel' },
{ category: 'lifestyle', label: 'Fitness', value: 'fitness' }
];

post.editionView()
.title('Edit post "{{ entry.values.title }}"') // title() accepts a template string, which has access to the entry
.actions(['list', 'show', 'delete']) // choose which buttons appear in the top action bar. Show is disabled by default
Expand All @@ -119,7 +126,7 @@
nga.field('subcategory', 'choice')
.choices(function(entry) { // choices also accepts a function to return a list of choices based on the current entry
return subCategories.filter(function (c) {
return c.category === entry.values.category
return c.category === entry.values.category;
});
}),
nga.field('tags', 'reference_many') // ReferenceMany translates to a select multiple
Expand Down
14 changes: 14 additions & 0 deletions src/javascripts/ng-admin/Crud/field/maChoiceField.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ function maChoiceField($compile) {
}
});

scope.$watch('field()', function (field) {
if(!field.choices) {
return;
}
var choices;
if (typeof field.choices === 'function') {
choices = field.choices();
}
if(typeof choices === 'function') {
choices = choices(scope.entry);
}
scope.choices = choices;
});

var refreshAttributes = '';
if (field.type().indexOf('reference') === 0 && field.remoteComplete()) {
scope.refreshDelay = field.remoteCompleteOptions().refreshDelay;
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/filter/maFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function maFilterDirective(FieldViewConfiguration) {
var template = `
<div class="row">
<form class="filters col-md-offset-6 col-md-6 form-horizontal" ng-if="shouldFilter()">
<div class="filter form-group input-{{ field.type() }}" ng-repeat="field in filters track by $index">
<div class="filter {{ field.name() }} form-group input-{{ field.type() }}" ng-repeat="field in filters track by $index">
<div class="col-sm-1 col-xs-1 remove_filter">
<a ng-if="!field.pinned()" ng-click="removeFilter(field)"><span class="glyphicon glyphicon-remove"></span></a>
</div>
Expand Down
52 changes: 52 additions & 0 deletions src/javascripts/test/e2e/filterViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,58 @@ describe('List filter', function () {
});
});

describe('filter choice conflict', function () {

beforeEach(function() {
if (hasToLoad) {
browser.get(browser.baseUrl + '#/posts/list');
}
hasToLoad = true;
});

var addFilter = function (name) {
return function () {
return element(by.css('ma-filter-button')).click()
.then(function () {
return element(by.linkText(name)).click();
});
};
};

var removeFilter = function (name) {
return function () {
return element(by.css('.filter.' + name + ' .remove_filter')).click();
};
};

var getChoices = function (name) {
return function () {
return element(by.css('.filter.' + name)).click()
.then(function () {
return element.all(by.css('.filter.' + name + ' .ui-select-choices-row')).getText();
});
};
};

it('should not mix filter choices when adding two filter and then deleting the first', function () {
addFilter('Category')()
.then(getChoices('category'))
.then(function(categoryChoices) {
return expect(categoryChoices).toEqual(['Tech', 'Lifestyle']);
})
.then(addFilter('Subcategory'))
.then(getChoices('subcategory'))
.then(function(subCategoryChoices) {
return expect(subCategoryChoices).toEqual(['Computers', 'Gadgets', 'Travel', 'Fitness']);
})
.then(removeFilter('category'))
.then(getChoices('subcategory'))
.then(function (subCategoryChoices) {
return expect(subCategoryChoices).toEqual(['Computers', 'Gadgets', 'Travel', 'Fitness']);
});
});
});

describe('interaction with pagination', function() {
it('should reset page number', function () {
// Filter globally for 'I'
Expand Down