admin-config
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -10,3 +10,3 @@ import Field from "./Field"; | ||
this._perPage = 30; | ||
this._filters = null; | ||
this._permanentFilters = null; | ||
this._sortField = null; | ||
@@ -48,8 +48,22 @@ this._sortDir = null; | ||
filters(filters) { | ||
/** | ||
* Define permanent filters to be added to the REST API calls | ||
* | ||
* nga.field('post_id', 'reference').permanentFilters({ | ||
* published: true | ||
* }); | ||
* // related API call will be /posts/:id?published=true | ||
* // if the permanent filter depends on the current query string, use a function | ||
* nga.field('post_id', 'reference').permanentFilters(function(search) { | ||
* return { published: true } | ||
* }); | ||
* | ||
* @param {Object|Function} filters list of filters to apply to the call | ||
*/ | ||
permanentFilters(filters) { | ||
if (!arguments.length) { | ||
return this._filters; | ||
return this._permanentFilters; | ||
} | ||
this._filters = filters; | ||
this._permanentFilters = filters; | ||
@@ -59,2 +73,10 @@ return this; | ||
/** | ||
* @deprecated use permanentFilters() unstead | ||
*/ | ||
filters(filters) { | ||
console.warn('ReferenceField.filters() is deprecated, please use ReferenceField.permanentFilters() instead'); | ||
return this.permanentFilters(filters); | ||
} | ||
sortField() { | ||
@@ -61,0 +83,0 @@ if (arguments.length) { |
@@ -27,3 +27,3 @@ import Queries from './Queries' | ||
* @param {Number} page the page number | ||
* @param {Object} filters searchQuery to filter elements | ||
* @param {Object} filterValues searchQuery to filter elements | ||
* @param {String} sortField the field to be sorted ex: entity.fieldName | ||
@@ -34,3 +34,3 @@ * @param {String} sortDir the direction of the sort | ||
*/ | ||
getAll(view, page, filters, sortField, sortDir) { | ||
getAll(view, page, filterValues = {}, sortField, sortDir) { | ||
page = page || 1; | ||
@@ -47,3 +47,5 @@ let url = view.getUrl(); | ||
return this.getRawValues(view.entity, view.name(), view.type, page, view.perPage(), filters, view.filters(), sortField, sortDir, url) | ||
let allFilterValues = Object.assign({}, filterValues, view.permanentFilters()); | ||
return this.getRawValues(view.entity, view.name(), view.type, page, view.perPage(), allFilterValues, view.filters(), sortField, sortDir, url) | ||
.then((values) => { | ||
@@ -190,3 +192,3 @@ return { | ||
let filters = reference.filters(); | ||
let permanentFilters = reference.permanentFilters(); | ||
let filterFields = {}; | ||
@@ -201,3 +203,3 @@ filterFields[reference.name()] = reference; | ||
reference.perPage(), | ||
typeof(filters) === 'function' ? filters(search) : filters, | ||
typeof(permanentFilters) === 'function' ? permanentFilters(search) : permanentFilters, | ||
filterFields, | ||
@@ -204,0 +206,0 @@ reference.sortField(), |
@@ -14,2 +14,3 @@ import View from './View'; | ||
this._filters = []; | ||
this._permanentFilters = {}; | ||
this._exportFields = null; | ||
@@ -94,2 +95,32 @@ | ||
/** | ||
* Define permanent filters to be added to the REST API calls | ||
* | ||
* posts.listView().permanentFilters({ | ||
* published: true | ||
* }); | ||
* // related API call will be /posts?published=true | ||
* | ||
* @param {Object} filters list of filters to apply to the call | ||
*/ | ||
permanentFilters(filters) { | ||
if (!arguments.length) { | ||
return this._permanentFilters; | ||
} | ||
this._permanentFilters = filters; | ||
return this; | ||
} | ||
/** | ||
* Define filters the user can add to the datagrid | ||
* | ||
* posts.listView().filters([ | ||
* nga.field('title'), | ||
* nga.field('age', 'number') | ||
* ]); | ||
* | ||
* @param {Field[]} filters list of filters to add to the GUI | ||
*/ | ||
filters(filters) { | ||
@@ -96,0 +127,0 @@ if (!arguments.length) { |
{ | ||
"name": "admin-config", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -126,9 +126,30 @@ let assert = require('chai').assert, | ||
readQueries.getAll(catView, 1); | ||
readQueries.getAll(catView, 1, [], 'unknow_ListView.name', 'ASC'); | ||
readQueries.getAll(catView, 1, [], viewName + '.id', 'ASC'); | ||
readQueries.getAll(catView, 1, {}, 'unknow_ListView.name', 'ASC'); | ||
readQueries.getAll(catView, 1, {}, viewName + '.id', 'ASC'); | ||
assert(spy.withArgs(catEntity, viewName, catView.type, 1, catView.perPage(), undefined, catView.filters(), viewName + '.name', 'DESC').calledOnce); | ||
assert(spy.withArgs(catEntity, viewName, catView.type, 1, catView.perPage(), [], catView.filters(), viewName + '.name', 'DESC').calledOnce); | ||
assert(spy.withArgs(catEntity, viewName, catView.type, 1, catView.perPage(), [], catView.filters(), viewName + '.id', 'ASC').calledOnce); | ||
assert(spy.withArgs(catEntity, viewName, catView.type, 1, catView.perPage(), {}, catView.filters(), viewName + '.name', 'DESC').callCount == 2); | ||
assert(spy.withArgs(catEntity, viewName, catView.type, 1, catView.perPage(), {}, catView.filters(), viewName + '.id', 'ASC').calledOnce); | ||
}); | ||
it('should send correct filter params to the API call', () => { | ||
let spy = sinon.spy(readQueries, 'getRawValues'); | ||
let entity = new Entity('cat'); | ||
readQueries.getAll(entity.listView(), 1, { name: 'foo'}); | ||
assert(spy.withArgs(entity, 'cat_ListView', 'ListView', 1, 30, { name: 'foo' }, [], 'cat_ListView.id', 'DESC').calledOnce); | ||
}); | ||
it('should include permanent filters', () => { | ||
let spy = sinon.spy(readQueries, 'getRawValues'); | ||
let entity = new Entity('cat'); | ||
entity.listView().permanentFilters({ bar: 1 }); | ||
readQueries.getAll(entity.listView(), 1); | ||
readQueries.getAll(entity.listView(), 1, { name: 'foo'}); | ||
assert(spy.withArgs(entity, 'cat_ListView', 'ListView', 1, 30, { bar: 1 }, [], 'cat_ListView.id', 'DESC').calledOnce); | ||
assert(spy.withArgs(entity, 'cat_ListView', 'ListView', 1, 30, { name: 'foo', bar: 1 }, [], 'cat_ListView.id', 'DESC').calledOnce); | ||
}); | ||
}); | ||
@@ -248,3 +269,3 @@ | ||
it('should execute filters function with current search parameter if filters is a function', () => { | ||
it('should execute permanentFilters function with current search parameter if filters is a function', () => { | ||
var searchParameter = null; | ||
@@ -254,3 +275,3 @@ var field = new ReferenceField('myField') | ||
.targetField(new Field('name')) | ||
.filters((search) => { searchParameter = search; return { filter: search }; }); | ||
.permanentFilters((search) => { searchParameter = search; return { filter: search }; }); | ||
@@ -257,0 +278,0 @@ getRawValuesMock.expects('getRawValues').once(); |
155918
4034