admin-config
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -104,3 +104,4 @@ import Menu from './Menu/Menu'; | ||
return this._menu | ||
}; | ||
} | ||
this._menu = menu; | ||
@@ -107,0 +108,0 @@ return this; |
@@ -37,3 +37,6 @@ import Queries from './Queries' | ||
return this.getRawValues(view.entity, view.name(), view.type, page, view.perPage(), filters, view.filters(), sortField || view.getSortFieldName(), sortDir || view.sortDir(), url) | ||
sortField = sortField || view.getSortFieldName(); | ||
sortDir = sortDir || view.sortDir(); | ||
return this.getRawValues(view.entity, view.name(), view.type, page, view.perPage(), filters, view.filters(), sortField, sortDir, url) | ||
.then((values) => { | ||
@@ -67,2 +70,3 @@ return { | ||
// Compute pagination | ||
if (page !== -1) { | ||
@@ -73,2 +77,3 @@ params._page = (typeof (page) === 'undefined') ? 1 : parseInt(page, 10); | ||
// Compute sorting | ||
if (sortField && sortField.split('.')[0] === viewName) { | ||
@@ -79,2 +84,3 @@ params._sortField = sortField.split('.')[1]; | ||
// Compute filtering | ||
if (filterValues && Object.keys(filterValues).length !== 0) { | ||
@@ -103,43 +109,95 @@ params._filters = {}; | ||
* Returns all References for an entity with associated values [{targetEntity.identifier: targetLabel}, ...] | ||
* by calling the API for each entries | ||
* | ||
* @param {Object} references A hash of Reference and ReferenceMany objects | ||
* @param {ReferenceField} references A hash of Reference and ReferenceMany objects | ||
* @param {Array} rawValues | ||
* | ||
* @returns {promise} | ||
* @returns {Promise} | ||
*/ | ||
getReferencedData(references, rawValues) { | ||
let getRawValues = this.getRawValues.bind(this), | ||
getOne = this.getOne.bind(this), | ||
identifiers, | ||
calls = [], | ||
data; | ||
getFilteredReferenceData(references, rawValues) { | ||
if (!references || !Object.keys(references).length) { | ||
return this._promisesResolver.empty({}); | ||
} | ||
let getOne = this.getOne.bind(this), | ||
calls = []; | ||
for (let i in references) { | ||
let reference = references[i], | ||
targetEntity = reference.targetEntity(); | ||
targetEntity = reference.targetEntity(), | ||
identifiers = reference.getIdentifierValues(rawValues); | ||
if (!rawValues) { | ||
calls.push(getRawValues(targetEntity, targetEntity.name() + '_ListView', 'listView', 1, reference.perPage(), reference.filters(), {}, reference.sortField(), reference.sortDir())); | ||
continue; | ||
for (let k in identifiers) { | ||
calls.push(getOne(targetEntity, 'listView', identifiers[k], reference.name())); | ||
} | ||
} | ||
// get only for identifiers | ||
identifiers = reference.getIdentifierValues(rawValues); | ||
return this.fillFilteredReferencedData(calls, references, rawValues); | ||
}; | ||
/** | ||
* Returns all References for an entity with associated values [{targetEntity.identifier: targetLabel}, ...] | ||
* by calling the API once | ||
* | ||
* @param {[ReferenceField]} references A hash of Reference and ReferenceMany objects | ||
* @param {Array} rawValues | ||
* | ||
* @returns {Promise} | ||
*/ | ||
getOptimizedReferencedData(references, rawValues) { | ||
if (!references || !Object.keys(references).length) { | ||
return this._promisesResolver.empty({}); | ||
} | ||
let getRawValues = this.getRawValues.bind(this), | ||
calls = []; | ||
for (let i in references) { | ||
let reference = references[i], | ||
targetEntity = reference.targetEntity(), | ||
identifiers = reference.getIdentifierValues(rawValues); | ||
// Check if we should retrieve values with 1 or multiple requests | ||
if (reference.hasSingleApiCall()) { | ||
let singleCallFilters = reference.getSingleApiCall(identifiers); | ||
calls.push(getRawValues(targetEntity, targetEntity.name() + '_ListView', 'listView', 1, reference.perPage(), singleCallFilters, {}, reference.sortField(), reference.sortDir())); | ||
let singleCallFilters = reference.getSingleApiCall(identifiers); | ||
calls.push(getRawValues(targetEntity, targetEntity.name() + '_ListView', 'listView', 1, reference.perPage(), singleCallFilters, {}, reference.sortField(), reference.sortDir())); | ||
} | ||
continue; | ||
} | ||
return this.fillOptimizedReferencedData(calls, references); | ||
} | ||
for (let k in identifiers) { | ||
calls.push(getOne(targetEntity, 'listView', identifiers[k], reference.name())); | ||
} | ||
/** | ||
* Returns all References for an entity with associated values [{targetEntity.identifier: targetLabel}, ...] | ||
* without filters on an entity | ||
* | ||
* @param {[ReferenceField]} references A hash of Reference and ReferenceMany objects | ||
* | ||
* @returns {Promise} | ||
*/ | ||
getAllReferencedData(references) { | ||
if (!references || !Object.keys(references).length) { | ||
return this._promisesResolver.empty({}); | ||
} | ||
// Fill all reference entries | ||
return this._promisesResolver.allEvenFailed(calls) | ||
let calls = [], | ||
getRawValues = this.getRawValues.bind(this); | ||
for (let i in references) { | ||
let reference = references[i], | ||
targetEntity = reference.targetEntity(); | ||
calls.push(getRawValues(targetEntity, targetEntity.name() + '_ListView', 'listView', 1, reference.perPage(), reference.filters(), {}, reference.sortField(), reference.sortDir())); | ||
} | ||
return this.fillOptimizedReferencedData(calls, references); | ||
} | ||
/** | ||
* Fill all reference entries to return [{targetEntity.identifier: targetLabel}, ...] | ||
* | ||
* @param {[Promise]} apiCalls | ||
* @param {[Reference]} references | ||
* @returns {Promise} | ||
*/ | ||
fillOptimizedReferencedData(apiCalls, references) { | ||
return this._promisesResolver.allEvenFailed(apiCalls) | ||
.then((responses) => { | ||
@@ -151,3 +209,2 @@ if (responses.length === 0) { | ||
let referencedData = {}, | ||
response, | ||
i = 0; | ||
@@ -157,19 +214,41 @@ | ||
let reference = references[j], | ||
singleCallFilters = reference.getSingleApiCall(identifiers); | ||
response = responses[i++]; | ||
// Retrieve entries depending on 1 or many request was done | ||
if (singleCallFilters || !rawValues) { | ||
response = responses[i++]; | ||
if (response.status == 'error') { | ||
// the response failed | ||
continue; | ||
} | ||
referencedData[reference.name()] = response.result.data; | ||
if (response.status == 'error') { | ||
// the response failed | ||
continue; | ||
} | ||
data = []; | ||
identifiers = reference.getIdentifierValues(rawValues); | ||
referencedData[reference.name()] = response.result.data; | ||
} | ||
return referencedData; | ||
}); | ||
} | ||
/** | ||
* Fill all reference entries to return [{targetEntity.identifier: targetLabel}, ...] | ||
* | ||
* @param {[Promise]} apiCalls | ||
* @param {[Reference]} references | ||
* @param {[Object]} rawValues | ||
* @returns {Promise} | ||
*/ | ||
fillFilteredReferencedData(apiCalls, references, rawValues) { | ||
return this._promisesResolver.allEvenFailed(apiCalls) | ||
.then((responses) => { | ||
if (responses.length === 0) { | ||
return {}; | ||
} | ||
let referencedData = {}, | ||
response, | ||
i = 0; | ||
for (let j in references) { | ||
let data = [], | ||
reference = references[j], | ||
identifiers = reference.getIdentifierValues(rawValues); | ||
for (let k in identifiers) { | ||
@@ -193,3 +272,3 @@ response = responses[i++]; | ||
}); | ||
}; | ||
} | ||
@@ -228,3 +307,3 @@ /** | ||
if (response.status == 'error') { | ||
// one of the responses failed | ||
// If a response fail, skip it | ||
continue; | ||
@@ -231,0 +310,0 @@ } |
class PromisesResolver { | ||
static empty(value) { | ||
return new Promise((resolve) => { | ||
resolve(value); | ||
}); | ||
} | ||
static allEvenFailed(promises) { | ||
@@ -4,0 +10,0 @@ if (!Array.isArray(promises)) { |
@@ -7,2 +7,3 @@ import View from './View'; | ||
this._type = 'DeleteView'; | ||
this._enabled = true; | ||
} | ||
@@ -9,0 +10,0 @@ } |
@@ -10,2 +10,6 @@ import View from "./View"; | ||
get enabled() { | ||
return this._enabled || this.entity.views['ListView'].enabled; | ||
} | ||
icon() { | ||
@@ -12,0 +16,0 @@ if (arguments.length) { |
@@ -11,3 +11,3 @@ import Entry from "../Entry"; | ||
this._enabled = true; | ||
this._enabled = false; | ||
this._fields = []; | ||
@@ -22,3 +22,3 @@ this._type = null; | ||
get enabled() { | ||
return this._enabled; | ||
return this._enabled || !!this._fields.length; | ||
} | ||
@@ -52,2 +52,4 @@ | ||
this._enabled = false; | ||
return this; | ||
} | ||
@@ -57,2 +59,4 @@ | ||
this._enabled = true; | ||
return this; | ||
} | ||
@@ -64,3 +68,3 @@ | ||
isEnabled() { | ||
return this._enabled; | ||
return this.enabled; | ||
} | ||
@@ -144,2 +148,10 @@ | ||
getNonOptimizedReferences() { | ||
return this._getReferencesByOptimizationType(false); | ||
} | ||
getOptimizedReferences() { | ||
return this._getReferencesByOptimizationType(true); | ||
} | ||
getReferencedLists() { | ||
@@ -240,4 +252,25 @@ let result = {}; | ||
} | ||
/** | ||
* | ||
* @param {Boolean} optimized | ||
* @returns {[Reference]} | ||
* @private | ||
*/ | ||
_getReferencesByOptimizationType(optimized=true) { | ||
let result = {}, | ||
references = this.getReferences(); | ||
for (let i in references) { | ||
let reference = references[i]; | ||
if (!!reference.getSingleApiCall() === optimized) { | ||
result[i] = reference; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
export default View; |
{ | ||
"name": "admin-config", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1,4 +0,4 @@ | ||
# admin-config | ||
# admin-config [![Build Status](https://travis-ci.org/marmelab/admin-config.svg?branch=master)](https://travis-ci.org/marmelab/admin-config) | ||
Common files used in both [ng-admin](https://github.com/marmelab/ng-admin) and [reactadmin](https://github.com/marmelab/react-admin). | ||
Common files used in both [ng-admin](https://github.com/marmelab/ng-admin) and [react-admin](https://github.com/marmelab/react-admin). | ||
@@ -5,0 +5,0 @@ ## Installation |
@@ -105,6 +105,12 @@ var assert = require('chai').assert; | ||
it('should return only views of type', () => { | ||
let application = new Application(); | ||
let application = new Application(), | ||
post = new Entity('post'), | ||
comment = new Entity('comment'); | ||
post.views["DashboardView"].enable(); | ||
comment.views["DashboardView"].enable(); | ||
application | ||
.addEntity(new Entity('post')) | ||
.addEntity(new Entity('comment')); | ||
.addEntity(post) | ||
.addEntity(comment); | ||
@@ -125,5 +131,9 @@ let views = application.getViewsOfType('DashboardView'); | ||
let comment = new Entity('comment'); | ||
let post = new Entity('post'); | ||
comment.views.DashboardView.disable(); | ||
post.views.DashboardView.enable(); | ||
application | ||
.addEntity(new Entity('post')) | ||
.addEntity(post) | ||
.addEntity(comment); | ||
@@ -140,2 +150,7 @@ | ||
let [post, comment, tag] = [new Entity('post'), new Entity('comment'), new Entity('tag')]; | ||
post.views["DashboardView"].enable(); | ||
comment.views["DashboardView"].enable(); | ||
tag.views["DashboardView"].enable(); | ||
post.views.DashboardView.order(2); | ||
@@ -171,6 +186,13 @@ comment.views.DashboardView.order(1); | ||
it('should create a menu based on the entity list', () => { | ||
let application = new Application(); | ||
let application = new Application(), | ||
comment = new Entity('comment'), | ||
post = new Entity('post'); | ||
comment.views.ListView.enable(); | ||
post.views.ListView.enable(); | ||
application | ||
.addEntity(new Entity('post')) | ||
.addEntity(new Entity('comment')); | ||
.addEntity(post) | ||
.addEntity(comment); | ||
let menu = application.buildMenuFromEntities(); | ||
@@ -185,5 +207,11 @@ assert.equal(2, menu.children().length); | ||
let [e1, e2, e3] = [new Entity('e1'), new Entity('e2'), new Entity('e3')]; | ||
e1.menuView().order(2); | ||
e2.menuView().order(1); | ||
e3.menuView().order(3); | ||
e1.views.ListView.enable(); | ||
e2.views.ListView.enable(); | ||
e3.views.ListView.enable(); | ||
application | ||
@@ -190,0 +218,0 @@ .addEntity(e1) |
@@ -49,6 +49,9 @@ var assert = require('chai').assert; | ||
assert.equal(true, entity.isReadOnly); | ||
}) ; | ||
}); | ||
it('should disable all edition views', function() { | ||
entity.readOnly(); | ||
entity.views.ListView.enable(); | ||
entity.views.DashboardView.enable(); | ||
assert.equal(true, entity.menuView().enabled); | ||
@@ -55,0 +58,0 @@ assert.equal(true, entity.dashboardView().enabled); |
@@ -134,3 +134,3 @@ let assert = require('chai').assert, | ||
readQueries.getReferencedData(post.views["ListView"].getReferences(), rawPosts) | ||
readQueries.getFilteredReferenceData(post.views["ListView"].getReferences(), rawPosts) | ||
.then((referencedData) => { | ||
@@ -174,3 +174,3 @@ assert.equal(referencedData.author.length, 2); | ||
readQueries.getReferencedData(post.views["ListView"].getReferences(), rawPosts) | ||
readQueries.getOptimizedReferencedData(post.views["ListView"].getReferences(), rawPosts) | ||
.then((referencedData) => { | ||
@@ -177,0 +177,0 @@ assert.equal(referencedData['author'].length, 2); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3093
115411
62