Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Fix referenced_list loses detailLink when using new entity #654

Merged
merged 1 commit into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
nga.field('average_note', 'float')
.cssClasses('col-sm-4'),
nga.field('comments', 'referenced_list') // display list of related comments
.targetEntity(comment)
.targetEntity(nga.entity('comments'))
.targetReferenceField('post_id')
.targetFields([
nga.field('id').isDetailLink(true),
Expand Down
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/CrudModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ CrudModule.directive('maChoicesColumn', require('./column/maChoicesColumn'));
CrudModule.directive('maDateColumn', require('./column/maDateColumn'));
CrudModule.directive('maJsonColumn', require('./column/maJsonColumn'));
CrudModule.directive('maNumberColumn', require('./column/maNumberColumn'));
CrudModule.directive('maReferencedListColumn', require('./column/maReferencedListColumn'));
CrudModule.directive('maReferenceManyColumn', require('./column/maReferenceManyColumn'));
CrudModule.directive('maReferenceManyLinkColumn', require('./column/maReferenceManyLinkColumn'));
CrudModule.directive('maStringColumn', require('./column/maStringColumn'));
Expand Down
29 changes: 29 additions & 0 deletions src/javascripts/ng-admin/Crud/column/maReferencedListColumn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function maReferencedListColumn(NgAdminConfiguration) {
return {
scope: {
'field': '&',
'datastore': '&'
},
restrict: 'E',
link: {
pre: function(scope) {
scope.field = scope.field();
var targetEntity = scope.field.targetEntity();
scope.entries = scope.datastore().getEntries(targetEntity.uniqueId + '_list');
scope.entity = NgAdminConfiguration().getEntity(targetEntity.name());
}
},
template: `
<ma-datagrid name="{{ field.datagridName() }}"
entries="::entries"
fields="::field.targetFields()"
list-actions="::field.listActions()"
entity="::entity">
</ma-datagrid>`
};
}

maReferencedListColumn.$inject = ['NgAdminConfiguration'];

module.exports = maReferencedListColumn;

138 changes: 66 additions & 72 deletions src/javascripts/ng-admin/Crud/field/maField.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,77 @@
/*global define*/
var _ = require('lodash');

define(function (require) {
'use strict';

var _ = require('lodash');

function maField(FieldViewConfiguration) {
var writeWidgetTypes = _(FieldViewConfiguration)
.map(function(fieldView, field) {
return '<span ng-switch-when="' + field + '">' + fieldView.getWriteWidget() +'</span>';
}).join('');
var template =
function maField(FieldViewConfiguration) {
var writeWidgetTypes = _(FieldViewConfiguration)
.map(function(fieldView, field) {
return '<span ng-switch-when="' + field + '">' + fieldView.getWriteWidget() +'</span>';
}).join('');
var template =
'<div id="row-{{ field.name() }}" class="has-feedback" ng-class="getFieldValidationClass(field)">' +
'<label for="{{ field.name() }}" class="col-sm-2 control-label">' +
'{{ field.label() }}<span ng-if="field.validation().required">&nbsp;*</span>&nbsp;' +
'</label>' +
'<div ng-if="field.editable()" ng-class="getClassesForField(field, entry)" ng-switch="field.type()">' +
writeWidgetTypes +
'<span ng-show="fieldHasValidation(field)" class="glyphicon form-control-feedback" ng-class="fieldIsValid(field) ? \'glyphicon-ok\' : \'glyphicon-remove\'"></span>' +
'</div>' +
'<div ng-if="!field.editable()" ng-class="field.getCssClasses(entry)||\'col-sm-10\'">' +
'<p class="form-control-static">' +
'<ma-column field="::field" entry="::entry" entity="::entity" datastore="::datastore"></ma-column>' +
'</p>' +
'</div>' +
'<label for="{{ field.name() }}" class="col-sm-2 control-label">' +
'{{ field.label() }}<span ng-if="field.validation().required">&nbsp;*</span>&nbsp;' +
'</label>' +
'<div ng-if="field.editable()" ng-class="getClassesForField(field, entry)" ng-switch="field.type()">' +
writeWidgetTypes +
'<span ng-show="fieldHasValidation(field)" class="glyphicon form-control-feedback" ng-class="fieldIsValid(field) ? \'glyphicon-ok\' : \'glyphicon-remove\'"></span>' +
'</div>' +
'<div ng-if="!field.editable()" ng-class="field.getCssClasses(entry)||\'col-sm-10\'">' +
'<p class="form-control-static">' +
'<ma-column field="::field" entry="::entry" entity="::entity" datastore="::datastore"></ma-column>' +
'</p>' +
'</div>' +
'</div>';
return {
restrict: 'E',
scope: {
field: '&',
entry: '=',
entity: '&',
form: '&',
'datastore': '&'
},
link: function(scope) {
scope.field = scope.field();
scope.type = scope.field.type();
scope.entity = scope.entity();
scope.form = scope.form();
scope.datastore = scope.datastore();
return {
restrict: 'E',
scope: {
field: '&',
entry: '=',
entity: '&',
form: '&',
datastore: '&'
},
link: function(scope) {
scope.field = scope.field();
scope.type = scope.field.type();
scope.entity = scope.entity();
scope.form = scope.form();
scope.datastore = scope.datastore();

scope.getClassesForField = function(field, entry) {
return 'ng-admin-field-' + field.name().replace('.', '_') + ' ' + (field.getCssClasses(entry) || 'col-sm-10 col-md-8 col-lg-7');
};
scope.getClassesForField = function(field, entry) {
return 'ng-admin-field-' + field.name().replace('.', '_') + ' ' + (field.getCssClasses(entry) || 'col-sm-10 col-md-8 col-lg-7');
};

scope.getInputForField = function(field) {
return scope.form[field.name()];
};
scope.getInputForField = function(field) {
return scope.form[field.name()];
};

/**
* Should validation status be displayed for a given field?
*
* - No for non-editable fields, or template fields which not have a corresponding input
* - No for non-altered input
* - Yes otherwise
*/
scope.fieldHasValidation = function(field) {
var input = this.getInputForField(field);
return input && input.$dirty;
};
/**
* Should validation status be displayed for a given field?
*
* - No for non-editable fields, or template fields which not have a corresponding input
* - No for non-altered input
* - Yes otherwise
*/
scope.fieldHasValidation = function(field) {
var input = this.getInputForField(field);
return input && input.$dirty;
};

scope.fieldIsValid = function(field) {
var input = this.getInputForField(field);
return input && input.$valid;
};
scope.fieldIsValid = function(field) {
var input = this.getInputForField(field);
return input && input.$valid;
};

scope.getFieldValidationClass = function(field) {
if (this.fieldHasValidation(field)) {
return this.fieldIsValid(field) ? 'has-success' : 'has-error';
}
};
scope.getFieldValidationClass = function(field) {
if (this.fieldHasValidation(field)) {
return this.fieldIsValid(field) ? 'has-success' : 'has-error';
}
};

},
template: template
};
}
},
template: template
};
}

maField.$inject = ['FieldViewConfiguration'];
maField.$inject = ['FieldViewConfiguration'];

return maField;
});
module.exports = maField;
16 changes: 2 additions & 14 deletions src/javascripts/ng-admin/Crud/fieldView/ReferencedListFieldView.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
module.exports = {
getReadWidget: () =>
'<ma-datagrid name="{{ field.datagridName() }}" ' +
'entries="::datastore.getEntries(field.targetEntity().uniqueId + \'_list\')" ' +
'fields="::field.targetFields()" ' +
'list-actions="::field.listActions()" ' +
'entity="::field.targetEntity()">' +
'</ma-datagrid>',
getReadWidget: () => '<ma-referenced-list-column field="::field" datastore="::datastore"></ma-referenced-list-column>',
getLinkWidget: () => 'error: cannot display referenced_list field as linkable',
getFilterWidget: () => 'error: cannot display referenced_list field as filter',
getWriteWidget: () =>
'<ma-datagrid name="{{ field.datagridName() }}"' +
'entries="::datastore.getEntries(field.targetEntity().uniqueId + \'_list\')" ' +
'fields="::field.targetFields()" ' +
'list-actions="::field.listActions()" ' +
'entity="::field.targetEntity()">' +
'</ma-datagrid>'
getWriteWidget: () => '<ma-referenced-list-column field="::field" datastore="::datastore"></ma-referenced-list-column>'
};
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/show/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 compile="::showController.title">

<div class="show-value" ng-class="::'ng-admin-field-' + field.name() + ' ' + (field.getCssClasses(entry) || 'col-sm-10 col-md-8 col-lg-7')">

<ma-column field="::field" entry="::entry" entity="::entity" datastore="::showController.dataStore"></ma-column>
<ma-column field="::field" entry="::entry" entity="::showController.entity" datastore="::showController.dataStore"></ma-column>

</div>
</div>
Expand Down