Skip to content

Commit

Permalink
Merge pull request #53 from marmelab/use_same_structure_everywhere
Browse files Browse the repository at this point in the history
[RFR] Use same entity structure everywhere
  • Loading branch information
manuquentin committed Sep 25, 2014
2 parents 7c1d9e5 + ecd5cd9 commit ff6867e
Show file tree
Hide file tree
Showing 21 changed files with 935 additions and 798 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ app.config(function(NgAdminConfigurationProvider, Application, Entity, Field, Re
.addField(Field('actions')
.type('callback')
.list(true)
.label('Actions')
.label('Big value')
// Disable default link on the list view
.isEditLink(false)
// Add a new link to the post thanks to callback
.callback(function(entity) {
// Directive can also be included
return '<a href="#/edit/posts/' + entity.post_id + '">View post</a>';
return '{{ entity.getField("name").value.toUpperCase() }}';
})
);

Expand Down
5 changes: 4 additions & 1 deletion build/ng-admin.min.css

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions build/ng-admin.min.js

Large diffs are not rendered by default.

84 changes: 60 additions & 24 deletions src/javascripts/config-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@

var app = angular.module('myApp', ['ng-admin']);

app.directive('customPostLink', ['$location', function($location) {
return {
restrict: 'E',
template: '<a ng-click="displayPost(entity)">View&nbsp;post</a>',
controller: function($scope, $location) {

},
link: function($scope, element, attributes) {
$scope.displayPost = function(entity) {
var postId = entity.getField('post_id').value;

$location.path('/edit/posts/' + postId);
}
}
}
}]);

app.config(function(NgAdminConfigurationProvider, Application, Entity, Field, Reference, ReferencedList, ReferenceMany) {
function truncate(value) {
if (!value) {
Expand All @@ -19,49 +36,53 @@
}
}

var post = Entity('posts'),
commentBody = Field('body'),
postId = Field('id'),
postCreatedAt = Field('created_at');
var post = new Entity('posts'),
commentBody = new Field('body'),
commentId = new Field('id');

var tag = Entity('tags')
.order(3)
var tag = new Entity('tags')
.label('Tags')
.order(3)
.dashboard(10)
.pagination(pagination)
.infinitePagination(false)
.addField(Field('id')
.addField(new Field('id')
.order(1)
.label('ID')
.type('number')
.identifier(true)
.edition('read-only')
)
.addField(Field('name')
.addField(new Field('name')
.order(2)
.label('Name')
.edition('editable')
.validation({
"required": true,
"max-length" : 150
})
);

var comment = Entity('comments')
).addField(new Field('actions')
.type('callback')
.list(true)
.label('Test')
.isEditLink(false)
);
//
var comment = new Entity('comments')
.order(2)
.label('Comments')
.description('Lists all the blog comments with an infinite pagination')
.dashboard(10)
.pagination(pagination)
.infinitePagination(true)
.addField(postId
.addField(commentId
.order(1)
.label('ID')
.type('number')
.identifier(true)
.edition('read-only')
)
.addField(Reference('post_id')
.addField(new Reference('post_id')
.dashboard(false)
.targetEntity(post)
.targetLabel('title')
Expand All @@ -77,24 +98,39 @@
"max-length" : 150
})
)
.addField(postCreatedAt
.addField(new Field('created_at')
.order(3)
.label('Creation Date')
.type('date')
.edition('editable')
.dashboard(false)
.validation({
"required": true
})
).addField(Field('actions')
).addQuickFilter('Today', function() {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = now.getDate();

month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;

return {
created_at: [year, month, day].join('-')
}
})
.addField(new Field('actions')
.type('callback')
.list(true)
.label('Actions')
.isEditLink(false)
.callback(function(entity) {
return '<a href="#/edit/posts/' + entity.post_id + '">View post</a>';
.callback(function() {
return '<custom-post-link></custom-post-link>';
})
);


post
.label('Posts')
.order(1)
Expand All @@ -104,36 +140,36 @@
.titleCreate('Create a post')
.titleEdit('Edit a post')
.description('Lists all the blog posts with a simple pagination')
.addField(Field('id')
.addField(new Field('id')
.label('ID')
.type('number')
.identifier(true)
.edition('read-only')
)
.addField(Field('title')
.addField(new Field('title')
.label('Title')
.edition('editable')
.truncateList(truncate)
)
.addField(Field('body')
.addField(new Field('body')
.label('Body')
.type('text')
.edition('editable')
.truncateList(truncate)
)
.addField(ReferencedList('comments')
.addField(new ReferencedList('comments')
.label('Comments')
.targetEntity(comment)
.targetField('post_id')
.targetFields([postId, commentBody])
.targetFields([commentId, commentBody])
)
.addField(ReferenceMany('tags')
.addField(new ReferenceMany('tags')
.label('Tags')
.targetEntity(tag)
.targetLabel('name')
);

var app = Application('ng-admin backend demo')
var app = new Application('ng-admin backend demo')
.baseApiUrl('http://localhost:3000/')
.addEntity(post)
.addEntity(comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ define(function() {
var humane = require('humane'),
NProgress = require('nprogress');

var FormController = function($scope, $location, $filter, CrudManager, Validator, data) {
var isNew = typeof(data.entityId) === 'undefined';
var FormController = function($scope, $location, $filter, CrudManager, Validator, entity) {
var isNew = entity.isNew();
this.$scope = $scope;
this.$location = $location;
this.$filter = $filter;
this.CrudManager = CrudManager;
this.Validator = Validator;
this.data = data;
this.entity = entity;
this.openDatepicker = {};
this.title = isNew ? data.entityConfig.getCreateTitle() : data.entityConfig.getEditTitle();
this.description = data.entityConfig.getDescription();
this.title = isNew ? entity.getCreateTitle() : entity.getEditTitle();
this.description = entity.getDescription();

if (isNew) {
this.clear();
Expand All @@ -24,8 +24,9 @@ define(function() {
this.$scope.sortField = 'sortField' in searchParams ? searchParams.sortField : '';
this.$scope.sortDir = 'sortDir' in searchParams ? searchParams.sortDir : '';

this.fields = data.fields;
this.entityLabel = data.entityConfig.label();
this.fields = entity.getFields();
this.entityLabel = entity.label();
this.$scope.entity = this.entity;
this.$scope.itemClass = this.itemClass.bind(this);
this.$scope.edit = this.edit.bind(this);
this.$scope.sort = this.sort.bind(this);
Expand All @@ -35,15 +36,15 @@ define(function() {
};

FormController.prototype.create = function() {
this.$location.path('/create/' + this.data.entityName);
this.$location.path('/create/' + this.entity.name());
};

FormController.prototype.deleteOne = function() {
this.$location.path('/delete/' + this.data.entityName + '/' + this.data.entityId);
this.$location.path('/delete/' + this.entity.name() + '/' + this.entity.getIdentifier().value);
};

FormController.prototype.back = function() {
this.$location.path('/list/' + this.data.entityName);
this.$location.path('/list/' + this.entity.name());
};

FormController.prototype.contains = function(collection, item) {
Expand All @@ -67,20 +68,20 @@ define(function() {
var value,
self = this,
object = {
id: this.data.entityId
id: this.entity.getIdentifier().value
};

angular.forEach(this.data.fields, function(field){
angular.forEach(this.entity.getFields(), function(field){
value = field.value;
if (field.type() === 'date') {
value = self.$filter('date')(value, field.validation().format);
}

object[field.getName()] = value;
object[field.name()] = value;
});

try {
this.Validator.validate(this.data.entityName, object);
this.Validator.validate(this.entity.name(), object);
} catch(e) {
NProgress.done();
humane.log(e, {addnCls: 'humane-flatty-error'});
Expand All @@ -99,11 +100,11 @@ define(function() {
}

this.CrudManager
.createOne(this.data.entityName, object)
.createOne(this.entity.name(), object)
.then(function(response) {
NProgress.done();
humane.log('Changes successfully saved.', {addnCls: 'humane-flatty-success'});
self.$location.path('/edit/' + self.data.entityName + '/' + response.data.id);
self.$location.path('/edit/' + self.entity.name() + '/' + response.data.id);
});
};

Expand All @@ -115,7 +116,7 @@ define(function() {
return;
}

this.CrudManager.updateOne(this.data.entityName, object).then(function() {
this.CrudManager.updateOne(this.entity.name(), object).then(function() {
NProgress.done();
humane.log('Changes successfully saved.', {addnCls: 'humane-flatty-success'});
});
Expand All @@ -142,33 +143,36 @@ define(function() {
return (index % 2 === 0) ? 'even' : 'odd';
};

FormController.prototype.sort = function(entity, field) {
/**
*
* @param {Field} field
*/
FormController.prototype.sort = function(field) {
var dir = 'ASC',
field = entity.getName() + '.' + field;
fieldName = field.getSortName();

if (this.$scope.sortField === field) {
if (this.$scope.sortField === fieldName) {
dir = this.$scope.sortDir === 'ASC' ? 'DESC' : 'ASC';
}

this.changePage(this.$scope.filterQuery, 1, field, dir);
this.changePage(this.$scope.filterQuery, 1, fieldName, dir);
};

FormController.prototype.changePage = function(filter, page, sortField, sortDir) {
this.$location.search('sortField', sortField);
this.$location.search('sortDir', sortDir);
this.$location.path('/edit/' + this.data.entityConfig.getName() + '/' + this.data.entityId);
this.$location.path('/edit/' + this.entity.name() + '/' + this.entity.getIdentifier().value);
};

/**
* Return true if a column is being sorted
*
* @param {Entity} entity
* @param {String} field
* @param {Field} field
*
* @returns {Boolean}
*/
FormController.prototype.isSorting = function(entity, field) {
return this.$scope.sortField === entity.getName() + '.' + field;
FormController.prototype.isSorting = function(field) {
return this.$scope.sortField === field.getSortName();
};

/**
Expand All @@ -178,26 +182,26 @@ define(function() {
* @param {Entity} entity
*/
FormController.prototype.edit = function(item, entity) {
this.$location.path('/edit/' +entity.getName() + '/' + item[entity.getIdentifier().getName()]);
this.$location.path('/edit/' +entity.name() + '/' + item[entity.getIdentifier().name()]);
};

/**
* Clear all fields data
* Clear all fields
*/
FormController.prototype.clear = function() {
angular.forEach(this.data.fields, function(field){
field.value = field.name === 'ReferencedList' ? field.setItems([]) : null;
angular.forEach(this.entity.getFields(), function(field){
field.clear();
});
};

FormController.prototype.destroy = function() {
this.$scope = undefined;
this.$location = undefined;
this.CrudManager = undefined;
this.data = undefined;
this.entity = undefined;
};

FormController.$inject = ['$scope', '$location', '$filter', 'CrudManager', 'Validator', 'data'];
FormController.$inject = ['$scope', '$location', '$filter', 'CrudManager', 'Validator', 'entity'];

return FormController;
});
Loading

0 comments on commit ff6867e

Please sign in to comment.