Skip to content

Commit

Permalink
Merge pull request #449 from marmelab/list_buttons_without_listview
Browse files Browse the repository at this point in the history
[RFR] Fix button display when a view is disabled
  • Loading branch information
jeromemacias committed May 20, 2015
2 parents a312bdc + 7ba34e9 commit a2ea0ec
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/column/maColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ define(function (require) {
var referenceEntity = field.targetEntity().name();
var relatedEntity = Configuration().getEntity(referenceEntity);
if (!relatedEntity) return false;
return relatedEntity.isReadOnly ? relatedEntity.showView().isEnabled() : relatedEntity.editionView().isEnabled();
return relatedEntity.isReadOnly ? relatedEntity.showView().enabled : relatedEntity.editionView().enabled;
};

return {
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/delete/batchDelete.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row list-header">
<div class="col-lg-12">
<ma-view-actions override="::batchDeleteController.actions" selection="batchDeleteController.selection" entity="::batchDeleteController.entity">
<ma-list-button entity="::entity"></ma-list-button>
<ma-list-button ng-if="::entity.listView().enabled" entity="::entity"></ma-list-button>
</ma-view-actions>

<div class="page-header">
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/delete/delete.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-lg-12">
<ma-view-actions override="::deleteController.actions" entry="::entry" entity="::deleteController.entity">
<ma-list-button entity="::entity"></ma-list-button>
<ma-list-button ng-if="::entity.listView().enabled" entity="::entity"></ma-list-button>
</ma-view-actions>

<div class="page-header">
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/form/create.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-lg-12">
<ma-view-actions override="::formController.actions" entry="entry" entity="::formController.entity">
<ma-list-button entity="::entity"></ma-list-button>
<ma-list-button ng-if="::entity.listView().enabled" entity="::entity"></ma-list-button>
</ma-view-actions>

<div class="page-header">
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/form/edit.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-lg-12">
<ma-view-actions override="::formController.actions" entry="entry" entity="::formController.entity">
<ma-list-button entity="::entity"></ma-list-button>
<ma-list-button ng-if="::entity.listView().enabled" entity="::entity"></ma-list-button>
<ma-delete-button ng-if="::entity.deletionView().enabled" entry="entry" entity="::entity"></ma-delete-button>
</ma-view-actions>

Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ define(function (require) {
error404.status = 404; // trigger the 404 error
throw error404;
}
if (!view.isEnabled()) {
if (!view.enabled) {
throw new Error('The ' + viewName + ' is disabled for this entity');
}
return view;
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/show/show.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-lg-12">
<ma-view-actions override="::showController.actions" entry="entry" entity="::showController.entity">
<ma-list-button entity="::entity"></ma-list-button>
<ma-list-button ng-if="::entity.listView().enabled" entity="::entity"></ma-list-button>
<ma-edit-button ng-if="::entity.editionView().enabled" entry="entry" entity="::entity"></ma-edit-button>
<ma-delete-button ng-if="::entity.deletionView().enabled" entry="entry" entity="::entity"></ma-delete-button>
</ma-view-actions>
Expand Down
3 changes: 2 additions & 1 deletion src/javascripts/ng-admin/es6/lib/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class Application {
this._menu = this.buildMenuFromEntities();
}
return this._menu
};
}

this._menu = menu;
return this;
}
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/es6/lib/View/DeleteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class DeleteView extends View {
constructor(name) {
super(name);
this._type = 'DeleteView';
this._enabled = true;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/javascripts/ng-admin/es6/lib/View/MenuView.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class MenuView extends View {
this._icon = null;
}

get enabled() {
return this._enabled || this.entity.views['ListView'].enabled;
}

icon() {
if (arguments.length) {
console.warn('entity.menuView() is deprecated. Please use the Menu class instead');
Expand Down
10 changes: 7 additions & 3 deletions src/javascripts/ng-admin/es6/lib/View/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class View {
this._description = '';
this._template = null;

this._enabled = true;
this._enabled = false;
this._fields = [];
this._type = null;
this._name = name;
Expand All @@ -18,7 +18,7 @@ class View {
}

get enabled() {
return this._enabled;
return this._enabled || !!this._fields.length;
}

title(title) {
Expand Down Expand Up @@ -47,17 +47,21 @@ class View {

disable() {
this._enabled = false;

return this;
}

enable() {
this._enabled = true;

return this;
}

/**
* @deprecated Use getter "enabled" instead
*/
isEnabled() {
return this._enabled;
return this.enabled;
}

/**
Expand Down
42 changes: 35 additions & 7 deletions src/javascripts/ng-admin/es6/tests/lib/ApplicationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ describe('Application', function() {
});

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);

let views = application.getViewsOfType('DashboardView');

Expand All @@ -121,9 +127,13 @@ describe('Application', function() {
it('should return only enabled views of type', () => {
let application = new Application();
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);

let views = application.getViewsOfType('DashboardView');
Expand All @@ -135,6 +145,11 @@ describe('Application', function() {
it('should return ordered views of type', function() {
let application = new Application();
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);
comment.views.DashboardView.order(1);
tag.views.DashboardView.order(3);
Expand Down Expand Up @@ -165,10 +180,17 @@ describe('Application', function() {

describe('buildMenuFromEntities', () => {
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();
assert.equal(2, menu.children().length);
let [menu1, menu2] = menu.children();
Expand All @@ -178,9 +200,15 @@ describe('Application', function() {
it('should use the menuView order when provided', () => {
let application = new Application();
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
.addEntity(e1)
.addEntity(e2)
Expand Down
5 changes: 4 additions & 1 deletion src/javascripts/ng-admin/es6/tests/lib/Entity/EntityTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ describe('Entity', function() {
it('should set read-only attribute', function() {
entity.readOnly();
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);
assert.equal(true, entity.dashboardView().enabled);
assert.equal(true, entity.listView().enabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ define(function (require) {
var app = new Application(),
entity1 = new Entity('myEntity1'),
entity2 = new Entity('myEntity2'),
dashboard = entity1.dashboardView(),
dashboard2 = entity2.dashboardView(),
createView = entity2.creationView();
dashboard = entity1.dashboardView().enable(),
dashboard2 = entity2.dashboardView().enable(),
createView = entity2.creationView().enable();

app.addEntity(entity1);
app.addEntity(entity2);
Expand All @@ -47,8 +47,8 @@ define(function (require) {
lists = app.getViewsOfType('ListView');

expect(dashboards.length).toBe(2);
expect(forms.length).toBe(2);
expect(lists.length).toBe(2);
expect(forms.length).toBe(1);
expect(lists.length).toBe(0);

expect(dashboards[0].getEntity().name()).toBe('myEntity1');
expect(dashboards[1].getEntity().name()).toBe('myEntity2');
Expand Down

0 comments on commit a2ea0ec

Please sign in to comment.