admin-config
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -44,32 +44,32 @@ import Entry from "../Entry"; | ||
createEntry(entityName, identifier, fields) { | ||
let entry = new Entry.mapFromRest(entityName, identifier, fields, {}); | ||
fillReferencesValuesFromCollection(collection, referencedValues, fillSimpleReference) { | ||
fillSimpleReference = typeof (fillSimpleReference) === 'undefined' ? false : fillSimpleReference; | ||
fields.forEach(function (field) { | ||
entry.values[field.name()] = field.defaultValue(); | ||
}); | ||
for (let i = 0, l = collection.length; i < l; i++) { | ||
collection[i] = this.fillReferencesValuesFromEntry(collection[i], referencedValues, fillSimpleReference); | ||
} | ||
return entry; | ||
return collection; | ||
} | ||
/** | ||
* Map a JS object from the REST API Response to an Entry | ||
* | ||
* @deprecated use Entry.createFromRest() instead | ||
*/ | ||
mapEntry(entityName, identifier, fields, restEntry) { | ||
let entry = new Entry.mapFromRest(entityName, identifier, fields, restEntry); | ||
return entry; | ||
console.log('DataStore.mapEntry() is deprecated, please use Entry.createFromRest() instead'); | ||
return new Entry.createFromRest(restEntry, fields, entityName, identifier.name()); | ||
} | ||
/** | ||
* Map an array of JS objects from the REST API Response to an array of Entries | ||
* | ||
* @deprecated use Entry.createArrayFromRest() instead | ||
*/ | ||
mapEntries(entityName, identifier, fields, restEntries) { | ||
return restEntries.map(e => this.mapEntry(entityName, identifier, fields, e)); | ||
console.log('DataStore.mapEntries() is deprecated, please use Entry.createArrayFromRest() instead'); | ||
return Entry.createArrayFromRest(restEntries, fields, entityName, identifier.name()); | ||
} | ||
fillReferencesValuesFromCollection(collection, referencedValues, fillSimpleReference) { | ||
fillSimpleReference = typeof (fillSimpleReference) === 'undefined' ? false : fillSimpleReference; | ||
for (let i = 0, l = collection.length; i < l; i++) { | ||
collection[i] = this.fillReferencesValuesFromEntry(collection[i], referencedValues, fillSimpleReference); | ||
} | ||
return collection; | ||
} | ||
fillReferencesValuesFromEntry(entry, referencedValues, fillSimpleReference) { | ||
@@ -76,0 +76,0 @@ for (let referenceField in referencedValues) { |
@@ -29,3 +29,2 @@ import stringUtils from "../Utils/stringUtils"; | ||
this._initViews(); | ||
@@ -32,0 +31,0 @@ } |
@@ -0,1 +1,4 @@ | ||
import {clone, cloneAndFlatten, cloneAndNest} from './Utils/objectProperties'; | ||
class Entry { | ||
@@ -17,26 +20,62 @@ constructor(entityName, values, identifierValue) { | ||
static mapFromRest(entityName, identifier, fields, restEntry) { | ||
if (!restEntry) { | ||
return new Entry(entityName); | ||
static createForFields(fields, entityName) { | ||
let entry = new Entry(entityName); | ||
fields.forEach(field => { | ||
entry.values[field.name()] = field.defaultValue(); | ||
}); | ||
return entry; | ||
} | ||
/** | ||
* Map a JS object from the REST API Response to an Entry | ||
* | ||
* @return {Entry} | ||
*/ | ||
static createFromRest(restEntry, fields, entityName, identifierName) { | ||
if (!restEntry || Object.keys(restEntry).length == 0) { | ||
return Entry.createForFields(fields, entityName); | ||
} | ||
let identifierValue = null; | ||
let values = cloneAndFlatten(restEntry); | ||
fields.forEach(function (field) { | ||
fields.forEach(field => { | ||
let fieldName = field.name(); | ||
if (fieldName in values) { | ||
values[fieldName] = field.getMappedValue(values[fieldName], values); | ||
} | ||
}); | ||
return new Entry(entityName, values, values[identifierName]); | ||
} | ||
/** | ||
* Map an array of JS objects from the REST API Response to an array of Entries | ||
* | ||
* @return {Array[Entry]} | ||
*/ | ||
static createArrayFromRest(restEntries, fields, entityName, identifierName) { | ||
return restEntries.map(e => Entry.createFromRest(e, fields, entityName, identifierName)); | ||
} | ||
/** | ||
* Transform an Entry to a JS object for the REST API Request | ||
* | ||
* @return {Object} | ||
*/ | ||
transformToRest(fields) { | ||
let restEntry = clone(this.values); | ||
fields.forEach(field => { | ||
let fieldName = field.name(); | ||
if (fieldName in restEntry) { | ||
restEntry[fieldName] = field.getMappedValue(restEntry[fieldName], restEntry); | ||
restEntry[fieldName] = field.getTransformedValue(restEntry[fieldName]) | ||
} | ||
}); | ||
// Add identifier value | ||
if (identifier) { | ||
identifierValue = restEntry[identifier.name()]; | ||
} | ||
return cloneAndNest(restEntry); | ||
} | ||
return new Entry(entityName, restEntry, identifierValue); | ||
} | ||
} | ||
export default Entry; |
@@ -31,2 +31,3 @@ import Application from "./Application"; | ||
import Dashboard from './Dashboard'; | ||
import Entry from './Entry'; | ||
@@ -85,2 +86,6 @@ class Factory { | ||
getEntryConstructor() { | ||
return Entry; | ||
} | ||
getDataStore() { | ||
@@ -87,0 +92,0 @@ return new DataStore(); |
@@ -11,2 +11,3 @@ import stringUtils from "../Utils/stringUtils"; | ||
this._maps = []; | ||
this._transforms = []; | ||
this._attributes = {}; | ||
@@ -78,2 +79,5 @@ this._cssClasses = null; | ||
/** | ||
* Add a function to be applied to the response object to turn it into an entry | ||
*/ | ||
map(fn) { | ||
@@ -95,2 +99,37 @@ if (!fn) return this._maps; | ||
getMappedValue(value, entry) { | ||
for (let i in this._maps) { | ||
value = this._maps[i](value, entry); | ||
} | ||
return value; | ||
} | ||
/** | ||
* Add a function to be applied to the entry to turn it into a response object | ||
*/ | ||
transform(fn) { | ||
if (!fn) return this._transforms; | ||
if (typeof(fn) !== "function") { | ||
let type = typeof(fn); | ||
throw new Error(`transform argument should be a function, ${type} given.`); | ||
} | ||
this._transforms.push(fn); | ||
return this; | ||
} | ||
hasTranforms() { | ||
return !!this._transforms.length; | ||
} | ||
getTransformedValue(value, entry) { | ||
for (let i in this._transforms) { | ||
value = this._transforms[i](value, entry); | ||
} | ||
return value; | ||
} | ||
attributes(attributes) { | ||
@@ -128,10 +167,2 @@ if (!arguments.length) { | ||
getMappedValue(value, entry) { | ||
for (let i in this._maps) { | ||
value = this._maps[i](value, entry); | ||
} | ||
return value; | ||
} | ||
validation(validation) { | ||
@@ -138,0 +169,0 @@ if (!arguments.length) { |
@@ -14,3 +14,3 @@ export default { | ||
return text.replace(/[-_](.)/g, function (match, group1) { | ||
return text.replace(/[-_.\s](.)/g, function (match, group1) { | ||
return ' ' + group1.toUpperCase(); | ||
@@ -17,0 +17,0 @@ }); |
@@ -8,6 +8,7 @@ import View from "./View"; | ||
this._icon = null; | ||
this._enabled = true; | ||
} | ||
get enabled() { | ||
return this._enabled || this.entity.views['ListView'].enabled; | ||
return this._enabled && this.entity.views['ListView'].enabled; | ||
} | ||
@@ -14,0 +15,0 @@ |
import Entry from "../Entry"; | ||
import {clone, cloneAndFlatten, cloneAndNest} from '../Utils/objectProperties'; | ||
@@ -67,5 +68,2 @@ class View { | ||
/** | ||
* @deprecated Use getter "entity" instead | ||
*/ | ||
getEntity() { | ||
@@ -75,5 +73,2 @@ return this.entity; | ||
/** | ||
* @deprecated Specify entity at view creation or use "entity" setter instead | ||
*/ | ||
setEntity(entity) { | ||
@@ -266,2 +261,20 @@ this.entity = entity; | ||
/** | ||
* Map a JS object from the REST API Response to an Entry | ||
*/ | ||
mapEntry(restEntry) { | ||
return Entry.createFromRest(restEntry, this._fields, this.entity.name(), this.entity.identifier().name()); | ||
} | ||
mapEntries(restEntries) { | ||
return Entry.createArrayFromRest(restEntries, this._fields, this.entity.name(), this.entity.identifier().name()); | ||
} | ||
/** | ||
* Transform an Entry to a JS object for the REST API Request | ||
*/ | ||
transformEntry(entry) { | ||
return entry.transformToRest(this._fields); | ||
} | ||
/** | ||
* @param {Boolean} optimized | ||
@@ -268,0 +281,0 @@ * @param {Boolean} withRemoteComplete |
{ | ||
"name": "admin-config", | ||
"version": "0.2.4", | ||
"name": "admin-config", | ||
"version": "0.2.5", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -18,41 +18,2 @@ var assert = require('chai').assert; | ||
it('should map some raw entities', function () { | ||
var view = new View(); | ||
view | ||
.addField(new Field('title')) | ||
.setEntity(new Entity().identifier(new Field('post_id'))); | ||
var entries = dataStore.mapEntries(view.entity.name(), view.identifier(), view.getFields(), [ | ||
{ post_id: 1, title: 'Hello', published: true}, | ||
{ post_id: 2, title: 'World', published: false}, | ||
{ post_id: 3, title: 'How to use ng-admin', published: false} | ||
]); | ||
assert.equal(entries.length, 3); | ||
assert.equal(entries[0].identifierValue, 1); | ||
assert.equal(entries[1].values.title, 'World'); | ||
assert.equal(entries[1].values.published, false); | ||
}); | ||
it('should map some one entity when the identifier is not in the view', function () { | ||
var view = new View(), | ||
field = new Field('title'), | ||
entity = new Entity('posts'); | ||
view | ||
.addField(field) | ||
.setEntity(entity); | ||
entity | ||
.identifier(new Field('post_id')); | ||
var entry = dataStore.mapEntry(entity.name(), view.identifier(), view.getFields(), { | ||
post_id: 1, | ||
title: 'Hello', | ||
published: true | ||
}); | ||
assert.equal(entry.identifierValue, 1); | ||
assert.equal(entry.values.title, 'Hello'); | ||
}); | ||
describe('getReferenceChoicesById', function () { | ||
@@ -59,0 +20,0 @@ it('should retrieve choices by id.', function () { |
@@ -9,3 +9,4 @@ var assert = require('chai').assert; | ||
describe('Entry', function() { | ||
describe('mapFromRest', function() { | ||
describe('createFromRest()', function() { | ||
var entity, fields; | ||
@@ -23,3 +24,3 @@ | ||
var view = entity.listView(); | ||
var mappedEntry = Entry.mapFromRest(entity.name(), view.identifier(), view.getFields(), {}); | ||
var mappedEntry = Entry.createFromRest({}, view.getFields(), entity.name(), view.identifier().name()); | ||
assert.deepEqual({}, mappedEntry.values); | ||
@@ -30,3 +31,3 @@ }); | ||
var view = entity.listView(); | ||
var mappedEntry = Entry.mapFromRest(entity.name(), view.identifier(), view.getFields(), { | ||
var mappedEntry = Entry.createFromRest({ | ||
id: 1, | ||
@@ -36,3 +37,3 @@ title: 'ng-admin + ES6 = pure awesomeness!', | ||
tags: [1, 2, 4] | ||
}); | ||
}, view.getFields(), entity.name(), view.identifier()); | ||
@@ -49,3 +50,3 @@ assert.deepEqual({ | ||
var view = entity.listView(); | ||
var mappedEntry = Entry.mapFromRest(entity.name(), view.identifier(), view.getFields(), { id: 1 }); | ||
var mappedEntry = Entry.createFromRest({ id: 1 }, view.getFields(), entity.name(), view.identifier().name()); | ||
assert.equal(1, mappedEntry.identifierValue); | ||
@@ -52,0 +53,0 @@ }); |
@@ -43,8 +43,8 @@ let assert = require('chai').assert, | ||
writeQueries.createOne(view, rawEntity) | ||
.then((rawEntry) => { | ||
.then(rawEntry => { | ||
assert(restWrapper.createOne.calledWith(rawEntity, 'cat', 'http://localhost/cat')); | ||
let dataStore = new DataStore(); | ||
let entry = dataStore.mapEntry(entity.name(), view.identifier(), view.getFields(), rawEntry); | ||
assert.equal(entry.values.data.name, 'Mizu'); | ||
let entry = view.mapEntry(rawEntry); | ||
assert.equal(entry.values['data.name'], 'Mizu'); | ||
}); | ||
@@ -62,8 +62,8 @@ }); | ||
writeQueries.updateOne(view, rawEntity) | ||
.then((rawEntry) => { | ||
.then(rawEntry => { | ||
assert(restWrapper.updateOne.calledWith(rawEntity, 'cat', 'http://localhost/cat/3')); | ||
let dataStore = new DataStore(); | ||
let entry = dataStore.mapEntry(entity.name(), view.identifier(), view.getFields(), rawEntry); | ||
assert.equal(entry.values.data.name, 'Mizute'); | ||
let entry = view.mapEntry(rawEntry); | ||
assert.equal(entry.values['data.name'], 'Mizute'); | ||
}); | ||
@@ -74,8 +74,8 @@ }); | ||
writeQueries.updateOne(view, rawEntity, 3) | ||
.then((rawEntry) => { | ||
.then(rawEntry => { | ||
assert(restWrapper.updateOne.calledWith(rawEntity, 'cat', 'http://localhost/cat/3')); | ||
let dataStore = new DataStore(); | ||
let entry = dataStore.mapEntry(entity.name(), view.identifier(), view.getFields(), rawEntry); | ||
assert.equal(entry.values.data.name, 'Mizute'); | ||
let entry = view.mapEntry(rawEntry); | ||
assert.equal(entry.values['data.name'], 'Mizute'); | ||
}); | ||
@@ -82,0 +82,0 @@ }); |
@@ -7,2 +7,26 @@ var assert = require('chai').assert; | ||
describe('MenuView', function() { | ||
describe('enabled', function() { | ||
var entity, menuView; | ||
beforeEach(function() { | ||
entity = new Entity('post'); | ||
entity.listView().enable(); | ||
menuView = entity.menuView().enable(); | ||
}); | ||
it('should be considered as enable if enabled AND if related list view is enabled', function() { | ||
assert.equal(menuView.enabled, true); | ||
}); | ||
it('should not be enabled if no related list view is enabled', function() { | ||
entity.listView().disable(); | ||
assert.equal(menuView.enabled, false); | ||
}); | ||
it('should not be enabled if disabled', function() { | ||
menuView.disable(); | ||
assert.equal(menuView.enabled, false); | ||
}); | ||
}); | ||
describe('icon', function() { | ||
@@ -9,0 +33,0 @@ it('should default to list glyphicon', function() { |
@@ -217,9 +217,120 @@ var assert = require('chai').assert; | ||
it('should return the identifier.', function () { | ||
var entity = new Entity('post').identifier(new Field('post_id')); | ||
var view = entity.listView(); | ||
view.addField(new Field('name')); | ||
describe('mapEntry()', () => { | ||
assert.equal(view.identifier().name(), 'post_id'); | ||
it('should return an entry', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
assert.instanceOf(view.mapEntry(), Entry); | ||
assert.equal(view.mapEntry().entityName, 'Foo'); | ||
}); | ||
it('should return an empty entry when passed an empty object', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
assert.deepEqual(view.mapEntry({}).values, {}); | ||
}); | ||
it('should use default values when passed an empty object', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
view.fields([ | ||
new Field('foo').defaultValue('bar') | ||
]); | ||
assert.equal(view.mapEntry({}).values.foo, 'bar'); | ||
}); | ||
it('should not use default values when passed a non-empty object', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
view.fields([ | ||
new Field('foo').defaultValue('bar') | ||
]); | ||
assert.notEqual(view.mapEntry({ hello: 1 }).values.foo, 'bar'); | ||
}); | ||
it('should populate the entry based on the values passed as argument', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
let entry = view.mapEntry({ hello: 1, world: 2 }); | ||
assert.equal(entry.values.hello, 1); | ||
assert.equal(entry.values.world, 2); | ||
}); | ||
it('should set the entry identifier value by default', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
assert.equal(view.mapEntry({ id: 1, bar: 2 }).identifierValue, 1); | ||
}); | ||
it('should set the entry identifier value according to the fields', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo').identifier(new Field('bar'))); | ||
assert.equal(view.mapEntry({ id: 1, bar: 2 }).identifierValue, 2); | ||
}); | ||
it('should transform the object values using the fields map functions', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
view.fields([ | ||
new Field('foo').map(v => v - 1) | ||
]); | ||
assert.equal(view.mapEntry({ foo: 2 }).values.foo, 1); | ||
}) | ||
}); | ||
describe('mapEntries()', () => { | ||
it('should return entries based on an array of objects', function () { | ||
let view = new View(); | ||
view | ||
.addField(new Field('title')) | ||
.setEntity(new Entity().identifier(new Field('post_id'))); | ||
let entries = view.mapEntries([ | ||
{ post_id: 1, title: 'Hello', published: true}, | ||
{ post_id: 2, title: 'World', published: false}, | ||
{ post_id: 3, title: 'How to use ng-admin', published: false} | ||
]); | ||
assert.equal(entries.length, 3); | ||
assert.equal(entries[0].identifierValue, 1); | ||
assert.equal(entries[1].values.title, 'World'); | ||
assert.equal(entries[1].values.published, false); | ||
}); | ||
}); | ||
describe('transformEntry()', () => { | ||
it('should return an empty object for empty entries', () => { | ||
let view = new View(); | ||
let entry = new Entry(); | ||
assert.deepEqual(view.transformEntry(entry), {}); | ||
}); | ||
it('should return an object litteral based on the entry values', () => { | ||
let view = new View(); | ||
let entry = new Entry('foo', { id: 1, bar: 2 }, 1); | ||
assert.deepEqual(view.transformEntry(entry), { id: 1, bar: 2 }); | ||
}); | ||
it('should transform the entry values using the fields transform functions', () => { | ||
let view = new View(); | ||
view.setEntity(new Entity().name('Foo')); | ||
view.fields([ | ||
new Field('bar').transform(v => v - 1) | ||
]); | ||
let entry = new Entry('foo', { id: 1, bar: 2 }, 1); | ||
assert.deepEqual(view.transformEntry(entry), { id: 1, bar: 1 }); | ||
}); | ||
}); | ||
describe('identifier()', function() { | ||
it('should return the identifier.', function () { | ||
var entity = new Entity('post').identifier(new Field('post_id')); | ||
var view = entity.listView(); | ||
view.addField(new Field('name')); | ||
assert.equal(view.identifier().name(), 'post_id'); | ||
}); | ||
}); | ||
}); |
149887
71
3892